Łukasz Bednarczyk
08/24/2021, 7:07 PMsam
08/25/2021, 1:57 AMŁukasz Bednarczyk
08/25/2021, 6:21 AMfun `when device auth headers are missing`(apiTestEnv: ApplicationEngineEnvironment, path: String) = describeSpec {
describe("when device auth headers are missing") {
it("should returns status 401") {
withApplication(apiTestEnv) {
val call = handleRequest(uri = path, method = HttpMethod.Get)
call.response.status() shouldBe HttpStatusCode.Unauthorized
}
}
}
}
Test:
class AudioFilesHandlerTest : ApiModuleTest() {
private val deviceAuthenticationService = mockk<DeviceAuthenticationService>()
private val saveAudioFileService = mockk<SaveAudioFileService>()
override val koinModules: Module
get() = module {
single { deviceAuthenticationService }
single { saveAudioFileService }
}
init {
describe("AudioFilesHandler") {
val audioFilesUri = "/api/audio-files"
describe("GET $audioFilesUri") {
include(`when device auth headers are missing`(apiTestEnv, audioFilesUri))
describe("when device is registered") {
val product = createProduct()
val device = createDevice(product)
val verifiedDevice = createVerifiedDevice(device)
every {
deviceAuthenticationService.validate(product.productId, device.deviceId)
} returns verifiedDevice
it("should returns 200") {
withApplication(apiTestEnv) {
val call = handleRequest(uri = audioFilesUri, method = HttpMethod.Get) {
val boundary = "WebAppBoundary"
addHeader(ContentType, FormData.withParameter("boundary", boundary).toString())
addHeader("productId", product.productId)
addHeader("deviceId", device.deviceId)
}
call.response.status() shouldBe HttpStatusCode.OK
}
}
}
}
}
}
}
abstract class ApiModuleTest : DescribeSpec(), KoinTest {
protected lateinit var apiTestEnv: ApplicationEngineEnvironment
abstract val koinModules: Module
override fun beforeSpec(spec: Spec) {
super.beforeSpec(spec)
startKoin {
modules(koinModules)
}
}
override fun beforeTest(testCase: TestCase) {
super.beforeTest(testCase)
apiTestEnv = createTestEnvironment {
config = HoconApplicationConfig(ConfigFactory.load("api.conf"))
}
}
override fun afterSpec(spec: Spec) {
super.afterSpec(spec)
stopKoin()
}
}
sam
08/25/2021, 11:47 AMŁukasz Bednarczyk
08/25/2021, 11:50 AM