Skip to content

Commit

Permalink
Merge pull request #107 from RADAR-base/test-basic-auth
Browse files Browse the repository at this point in the history
Test basic auth
  • Loading branch information
Bdegraaf1234 authored Jun 12, 2024
2 parents 7a3de4e + 22220d9 commit 11053fa
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
5 changes: 3 additions & 2 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ object Versions {

const val ktor = "2.3.10"
const val radarJersey = "0.11.1"
const val radarCommons = "1.1.3-SNAPSHOT"
const val radarSchemas = "0.8.8"
const val radarCommons = "1.1.2"
const val radarSchemas = "0.8.9"
const val jackson = "2.15.3"
const val slf4j = "2.0.13"
const val log4j2 = "2.23.1"
Expand All @@ -22,4 +22,5 @@ object Versions {
const val mockitoKotlin = "5.3.1"
const val grizzly = "4.0.2"
const val hamcrest = "2.2"
const val okHttp = "4.12.0"
}
2 changes: 1 addition & 1 deletion radar-gateway/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ dependencies {

testImplementation("org.mockito.kotlin:mockito-kotlin:${Versions.mockitoKotlin}")
testImplementation("org.hamcrest:hamcrest:${Versions.hamcrest}")
testImplementation("com.squareup.okhttp3:mockwebserver:${Versions.okHttp}")

integrationTestImplementation(platform("io.ktor:ktor-bom:${Versions.ktor}"))
integrationTestImplementation("io.ktor:ktor-client-content-negotiation")
integrationTestImplementation("io.ktor:ktor-client-content-negotiation")
integrationTestImplementation("io.ktor:ktor-serialization-kotlinx-json")

testImplementation("org.radarbase:radar-schemas-commons:${Versions.radarSchemas}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG
import io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_USER_INFO_CONFIG
import io.ktor.http.HttpHeaders
import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.radarbase.gateway.config.GatewayConfig
import org.radarbase.gateway.config.KafkaConfig
import org.radarbase.gateway.inject.SchemaRetrieverFactory
import org.radarbase.topic.AvroTopic
import org.radarcns.kafka.ObservationKey
import org.radarcns.passive.phone.PhoneAcceleration

class SchemaRetrieverFactoryTest {
@Test
fun `test basic auth credentials are correctly passed`() {
val mockConfig: GatewayConfig = mock()
// Create a new MockWebServer
val server = MockWebServer()

val topic = AvroTopic(
"test",
ObservationKey.getClassSchema(),
PhoneAcceleration.getClassSchema(),
ObservationKey::class.java,
PhoneAcceleration::class.java,
)

// Schedule a response

server.enqueue(
MockResponse()
.setHeader(HttpHeaders.ContentType, "application/json")
.setBody(
"""
{
"id": 1
}
""".trimIndent(),
),
)

// Start the server
server.start()

// Update the SCHEMA_REGISTRY_URL_CONFIG to use the mock server's URL
val serializationConfig = mapOf(
SCHEMA_REGISTRY_URL_CONFIG to server.url("/").toString(),
SCHEMA_REGISTRY_USER_INFO_CONFIG to "username:password",
)

val mockKafkaConfig: KafkaConfig = mock()
whenever(mockConfig.kafka).doReturn(mockKafkaConfig)
whenever(mockKafkaConfig.serialization).doReturn(serializationConfig)

val factory = SchemaRetrieverFactory(mockConfig)
val retriever = factory.get()
runBlocking {
retriever.addSchema(topic.name, false, topic.keySchema)
}

// Get the request that was received by the MockWebServer
val request = server.takeRequest()

// Verify the Basic Auth credentials
val authHeader = request.getHeader("Authorization")
assertEquals("Basic dXNlcm5hbWU6cGFzc3dvcmQ=", authHeader)

// Shut down the server. Instances cannot be reused.
server.shutdown()
}
}

0 comments on commit 11053fa

Please sign in to comment.