Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functional tests for prevent external #142

Open
wants to merge 5 commits into
base: authorization/post-cache
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/test/kotlin/org/prebid/cache/functional/AerospikeCacheSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AerospikeCacheSpec : ShouldSpec({

should("throw an exception when cache record is absent in Aerospike repository") {
// given: Prebid cache config
val config = prebidCacheConfig.getBaseAerospikeConfig("true")
val config = prebidCacheConfig.getBaseAerospikeConfig(true)
val cachePrefix = config["cache.prefix"]

// when: GET cache endpoint with random UUID is called
Expand All @@ -42,7 +42,7 @@ class AerospikeCacheSpec : ShouldSpec({
should("rethrow an exception from Aerospike cache server when such happens") {
// given: Prebid Cache with not matched to Aerospike server namespace
val unmatchedNamespace = getRandomString()
val config = prebidCacheConfig.getBaseAerospikeConfig("true", unmatchedNamespace)
val config = prebidCacheConfig.getBaseAerospikeConfig(true, unmatchedNamespace)
val prebidCacheApi = BaseSpec.getPrebidCacheApi(config)

// and: Default request object
Expand All @@ -64,19 +64,19 @@ class AerospikeCacheSpec : ShouldSpec({
should("throw an exception when aerospike.prevent_UUID_duplication=true and request with already existing UUID is send") {
// given: Prebid Cache with aerospike.prevent_UUID_duplication=true property
val prebidCacheApi = BaseSpec.getPrebidCacheApi(
prebidCacheConfig.getBaseAerospikeConfig("true") +
prebidCacheConfig.getAerospikePreventUuidDuplicationConfig("true")
prebidCacheConfig.getBaseAerospikeConfig(true) +
prebidCacheConfig.getAerospikePreventUuidDuplicationConfig(true)
)

// and: First request object with set UUID
val uuid = getRandomUuid()
val xmlPayloadTransfer = PayloadTransfer.getDefaultXmlPayloadTransfer().apply { key = uuid }
val requestObject = RequestObject.of(xmlPayloadTransfer)

// and: First request object is saved to Aerospike cache
// and: The first request object is saved to Aerospike cache
prebidCacheApi.postCache(requestObject)

// and: Second request object with already existing UUID is prepared
// and: A second request object with already existing UUID is prepared
val jsonPayloadTransfer = PayloadTransfer.getDefaultJsonPayloadTransfer().apply { key = uuid }
val secondRequestObject = RequestObject.of(jsonPayloadTransfer)

Expand All @@ -93,8 +93,8 @@ class AerospikeCacheSpec : ShouldSpec({
should("return back two request UUIDs when allow_external_UUID=true and 2 payload transfers were successfully cached in Aerospike") {
// given: Prebid Cache with allow_external_UUID=true property
val prebidCacheApi = BaseSpec.getPrebidCacheApi(
prebidCacheConfig.getBaseAerospikeConfig("true") +
prebidCacheConfig.getAerospikePreventUuidDuplicationConfig("false")
prebidCacheConfig.getBaseAerospikeConfig(true) +
prebidCacheConfig.getAerospikePreventUuidDuplicationConfig(false)
)

// and: Request object with 2 payload transfers and set UUIDs is prepared
Expand All @@ -105,7 +105,7 @@ class AerospikeCacheSpec : ShouldSpec({
// when: POST cache endpoint is called
val responseObject: ResponseObject = prebidCacheApi.postCache(requestObject)

// then: UUIDs from request object are returned
// then: UUIDs from a request object are returned
responseObject.responses.isEmpty() shouldBe false
responseObject.responses.size shouldBe 2

Expand All @@ -117,19 +117,19 @@ class AerospikeCacheSpec : ShouldSpec({
should("update existing cache record when aerospike.prevent_UUID_duplication=false and request with already existing UUID is send") {
// given: Prebid Cache with aerospike.prevent_UUID_duplication=false
val prebidCacheApi = BaseSpec.getPrebidCacheApi(
prebidCacheConfig.getBaseAerospikeConfig("true") +
prebidCacheConfig.getAerospikePreventUuidDuplicationConfig("false")
prebidCacheConfig.getBaseAerospikeConfig(true) +
prebidCacheConfig.getAerospikePreventUuidDuplicationConfig(false)
)

// and: First request object
val uuid = getRandomUuid()
val xmlPayloadTransfer = PayloadTransfer.getDefaultXmlPayloadTransfer().apply { key = uuid }
val requestObject = RequestObject.of(xmlPayloadTransfer)

// and: First request object is saved to Aerospike cache
// and: The first request object is saved to Aerospike cache
prebidCacheApi.postCache(requestObject)

// and: Second request object with already existing UUID is prepared
// and: A second request object with already existing UUID is prepared
val jsonPayloadTransfer = PayloadTransfer.getDefaultJsonPayloadTransfer().apply { key = uuid }
val secondRequestObject = RequestObject.of(jsonPayloadTransfer)
val requestTransferValue = objectMapper.readValue(secondRequestObject.puts[0].value, TransferValue::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ApacheIgniteCacheSpec : ShouldSpec({

should("throw an exception when cache record is absent in Apache ignite repository") {
// given: Prebid cache config
val config = prebidCacheConfig.getBaseApacheIgniteConfig("true")
val config = prebidCacheConfig.getBaseApacheIgniteConfig(true)
val cachePrefix = config["cache.prefix"]

// when: GET cache endpoint with random UUID is called
Expand All @@ -42,7 +42,7 @@ class ApacheIgniteCacheSpec : ShouldSpec({

should("throw an exception when cache is absent in Apache ignite repository due to expiration") {
// given: Prebid Cache with set min and max expiry as 0
val config = prebidCacheConfig.getBaseApacheIgniteConfig("false") +
val config = prebidCacheConfig.getBaseApacheIgniteConfig(false) +
prebidCacheConfig.getCacheExpiryConfig("0", "0")
val prebidCacheApi = BaseSpec.getPrebidCacheApi(config)

Expand All @@ -65,7 +65,7 @@ class ApacheIgniteCacheSpec : ShouldSpec({

should("rethrow an exception from Apache ignite cache server when such happens") {
// given: Prebid Cache with not matched to Apache ignite server namespace
val config = prebidCacheConfig.getBaseApacheIgniteConfig("true", getRandomString())
val config = prebidCacheConfig.getBaseApacheIgniteConfig(true, getRandomString())
val prebidCacheApi = BaseSpec.getPrebidCacheApi(config)

// and: Default request object
Expand All @@ -86,7 +86,7 @@ class ApacheIgniteCacheSpec : ShouldSpec({

should("return back PBC UUID when allow_external_UUID=false and request object was successfully cached in Apache ignite") {
// given: Prebid Cache with disabled allow_external_UUID property
val prebidCacheApi = BaseSpec.getPrebidCacheApi(prebidCacheConfig.getBaseApacheIgniteConfig("false"))
val prebidCacheApi = BaseSpec.getPrebidCacheApi(prebidCacheConfig.getBaseApacheIgniteConfig(false))

// and: Request object
val requestObject = RequestObject.getDefaultJsonRequestObject()
Expand All @@ -102,7 +102,7 @@ class ApacheIgniteCacheSpec : ShouldSpec({

should("return back two random UUIDs when allow_external_UUID=false and 2 payload transfers were successfully cached") {
// given: Prebid Cache with disabled allow_external_UUID property
val prebidCacheApi = BaseSpec.getPrebidCacheApi(prebidCacheConfig.getBaseApacheIgniteConfig("false"))
val prebidCacheApi = BaseSpec.getPrebidCacheApi(prebidCacheConfig.getBaseApacheIgniteConfig(false))

// given: Request object
val xmlPayloadTransfer = PayloadTransfer.getDefaultXmlPayloadTransfer().apply { key = null }
Expand All @@ -121,7 +121,7 @@ class ApacheIgniteCacheSpec : ShouldSpec({

should("return back a request UUID when allow_external_UUID=true and request object was successfully cached") {
// given: Prebid Cache with enabled allow_external_UUID property
val prebidCacheApi = BaseSpec.getPrebidCacheApi(prebidCacheConfig.getBaseApacheIgniteConfig("true"))
val prebidCacheApi = BaseSpec.getPrebidCacheApi(prebidCacheConfig.getBaseApacheIgniteConfig(true))

// and: Request object with set payload transfer UUID key
val requestObject = RequestObject.getDefaultJsonRequestObject().apply { puts[0].key = getRandomUuid() }
Expand All @@ -137,7 +137,7 @@ class ApacheIgniteCacheSpec : ShouldSpec({

should("return back two request UUIDs when allow_external_UUID=true and 2 payload transfers were successfully cached in Apache ignite") {
// given: Prebid Cache with allow_external_UUID=true property
val prebidCacheApi = BaseSpec.getPrebidCacheApi(prebidCacheConfig.getBaseApacheIgniteConfig("true"))
val prebidCacheApi = BaseSpec.getPrebidCacheApi(prebidCacheConfig.getBaseApacheIgniteConfig(true))

// and: Request object with 2 payload transfers and set UUIDs is prepared
val xmlPayloadTransfer = PayloadTransfer.getDefaultXmlPayloadTransfer().apply { key = getRandomUuid() }
Expand All @@ -158,18 +158,18 @@ class ApacheIgniteCacheSpec : ShouldSpec({

should("shouldn't update existing cache record when request with already existing UUID is send") {
// given: Prebid Cache with allow_external_UUID=true property
val prebidCacheApi = BaseSpec.getPrebidCacheApi(prebidCacheConfig.getBaseApacheIgniteConfig("true"))
val prebidCacheApi = BaseSpec.getPrebidCacheApi(prebidCacheConfig.getBaseApacheIgniteConfig(true))

// and: First request object
val uuid = getRandomUuid()
val xmlPayloadTransfer = PayloadTransfer.getDefaultXmlPayloadTransfer().apply { key = uuid }
val requestObject = RequestObject.of(xmlPayloadTransfer)
val requestTransferValue = objectMapper.readValue(requestObject.puts[0].value, TransferValue::class.java)

// and: First request object is saved to Aerospike cache
// and: The first request object is saved to Apache ignite cache
prebidCacheApi.postCache(requestObject)

// and: Second request object with already existing UUID is prepared
// and: A second request object with already existing UUID is prepared
val jsonPayloadTransfer = PayloadTransfer.getDefaultJsonPayloadTransfer().apply { key = uuid }
val secondRequestObject = RequestObject.of(jsonPayloadTransfer)

Expand All @@ -181,7 +181,7 @@ class ApacheIgniteCacheSpec : ShouldSpec({
responseObject.responses.size shouldBe 1
responseObject.responses[0].uuid shouldBe secondRequestObject.puts[0].key

// and: Cache record was updated in Aerospike with a second request object payload
// and: Cache record was updated in Apache ignite with a second request object payload
val getCacheResponse = prebidCacheApi.getCache(responseObject.responses[0].uuid)
val responseTransferValue = objectMapper.readValue(getCacheResponse.bodyAsText(), TransferValue::class.java)

Expand Down
184 changes: 184 additions & 0 deletions src/test/kotlin/org/prebid/cache/functional/AuthenticationCacheSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package org.prebid.cache.functional

import io.kotest.assertions.assertSoftly
import io.kotest.assertions.throwables.shouldThrowExactly
import io.kotest.core.spec.style.ShouldSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.beEmpty
import io.ktor.client.statement.bodyAsText
import io.ktor.http.contentType
import org.prebid.cache.functional.BaseSpec.Companion.prebidCacheConfig
import org.prebid.cache.functional.mapper.objectMapper
import org.prebid.cache.functional.model.request.RequestObject
import org.prebid.cache.functional.model.request.TransferValue
import org.prebid.cache.functional.service.ApiException
import org.prebid.cache.functional.util.getRandomString
import org.springframework.http.HttpStatus.UNAUTHORIZED

class AuthenticationCacheSpec : ShouldSpec({

should("should save JSON transfer value without api-key in header when cache-write-secured is disabled") {
// given: Prebid Cache with api.cache-write-secured=false property
val prebidCacheApi = BaseSpec.getPrebidCacheApi(
prebidCacheConfig.getBaseRedisConfig(
allowExternalUuid = true,
cacheWriteSecured = false,
apiKey = getRandomString()
)
)

// and: Request object with JSON transfer value
val requestObject = RequestObject.getDefaultJsonRequestObject()
val requestTransferValue = objectMapper.readValue(requestObject.puts[0].value, TransferValue::class.java)

// when: POST cache endpoint is called
val postResponse = prebidCacheApi.postCache(requestObject, apiKey = null)

// when: GET cache endpoint is called
val getCacheResponse = BaseSpec.getPrebidCacheApi().getCache(postResponse.responses[0].uuid)

// then: response content type is the same as request object type
getCacheResponse.contentType()?.contentType shouldBe "application"
getCacheResponse.contentType()?.contentSubtype shouldBe requestObject.puts[0].type.getValue()

// and: transfer value is returned
val responseTransferValue = objectMapper.readValue(getCacheResponse.bodyAsText(), TransferValue::class.java)

assertSoftly {
responseTransferValue.adm shouldBe requestTransferValue.adm
responseTransferValue.width shouldBe requestTransferValue.width
responseTransferValue.height shouldBe requestTransferValue.height
}
}

should("should save JSON transfer value without api-key in header when cache-write-secured is enabled") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In test case you have apiKey

// given: Prebid Cache with api.cache-write-secured=true property
val prebidApiKey = getRandomString()
val prebidCacheApi = BaseSpec.getPrebidCacheApi(
prebidCacheConfig.getBaseRedisConfig(
allowExternalUuid = true,
cacheWriteSecured = true,
apiKey = prebidApiKey
)
)

// and: Request object with JSON transfer value
val requestObject = RequestObject.getDefaultJsonRequestObject()
val requestTransferValue = objectMapper.readValue(requestObject.puts[0].value, TransferValue::class.java)

// when: POST cache endpoint is called
val postResponse = prebidCacheApi.postCache(requestObject, apiKey = prebidApiKey)

// when: GET cache endpoint is called
val getCacheResponse = BaseSpec.getPrebidCacheApi().getCache(postResponse.responses[0].uuid)

// then: response content type is the same as request object type
getCacheResponse.contentType()?.contentType shouldBe "application"
getCacheResponse.contentType()?.contentSubtype shouldBe requestObject.puts[0].type.getValue()

// and: transfer value is returned
val responseTransferValue = objectMapper.readValue(getCacheResponse.bodyAsText(), TransferValue::class.java)

assertSoftly {
responseTransferValue.adm shouldBe requestTransferValue.adm
responseTransferValue.width shouldBe requestTransferValue.width
responseTransferValue.height shouldBe requestTransferValue.height
}
}

should("should throw exception when cache-write-secured is enabled and trying to save payload transfer without api-key") {
// given: Prebid Cache with api.cache-write-secured=true property
val prebidCacheApi = BaseSpec.getPrebidCacheApi(
prebidCacheConfig.getBaseRedisConfig(
allowExternalUuid = true,
cacheWriteSecured = true,
apiKey = getRandomString()
)
)

// and: Request object with JSON transfer value
val requestObject = RequestObject.getDefaultJsonRequestObject()

// when: POST cache endpoint is called
val exception = shouldThrowExactly<ApiException> { prebidCacheApi.postCache(requestObject, apiKey = null) }

// then: Bad Request exception is thrown
assertSoftly {
exception.statusCode shouldBe UNAUTHORIZED.value()
exception.responseBody should beEmpty()
}
}

should("should throw exception when cache-write-secured is enabled and trying to save payload transfer with empty api-key") {
// given: Prebid Cache with api.cache-write-secured=true property
val prebidCacheApi = BaseSpec.getPrebidCacheApi(
prebidCacheConfig.getBaseRedisConfig(
allowExternalUuid = true,
cacheWriteSecured = true,
apiKey = getRandomString()
)
)

// and: Request object with JSON transfer value
val requestObject = RequestObject.getDefaultJsonRequestObject()

// when: POST cache endpoint is called
val exception =
shouldThrowExactly<ApiException> { prebidCacheApi.postCache(requestObject, apiKey = "") }

// then: Bad Request exception is thrown
assertSoftly {
exception.statusCode shouldBe UNAUTHORIZED.value()
exception.responseBody should beEmpty()
}
}

should("should throw exception when cache-write-secured is enabled and trying to save payload transfer with invalid api-key") {
// given: Prebid Cache with api.cache-write-secured=true property
val prebidCacheApi = BaseSpec.getPrebidCacheApi(
prebidCacheConfig.getBaseRedisConfig(
allowExternalUuid = true,
cacheWriteSecured = true,
apiKey = getRandomString()
)
)

// and: Request object with JSON transfer value
val requestObject = RequestObject.getDefaultJsonRequestObject()

// when: POST cache endpoint is called
val exception =
shouldThrowExactly<ApiException> { prebidCacheApi.postCache(requestObject, apiKey = getRandomString()) }

// then: Bad Request exception is thrown
assertSoftly {
exception.statusCode shouldBe UNAUTHORIZED.value()
exception.responseBody should beEmpty()
}
}

should("should throw exception when cache-write-secured is enabled and trying to save payload transfer with different case strategy api-key") {
// given: Prebid Cache with api.cache-write-secured=true property
val prebidApiKey = getRandomString()
val prebidCacheApi = BaseSpec.getPrebidCacheApi(
prebidCacheConfig.getBaseRedisConfig(
allowExternalUuid = true,
cacheWriteSecured = true,
apiKey = prebidApiKey
)
)

// and: Request object with JSON transfer value
val requestObject = RequestObject.getDefaultJsonRequestObject()

// when: POST cache endpoint is called
val exception =
shouldThrowExactly<ApiException> { prebidCacheApi.postCache(requestObject, apiKey = prebidApiKey.uppercase()) }

// then: Bad Request exception is thrown
assertSoftly {
exception.statusCode shouldBe UNAUTHORIZED.value()
exception.responseBody should beEmpty()
}
}
})
2 changes: 1 addition & 1 deletion src/test/kotlin/org/prebid/cache/functional/BaseSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class BaseSpec {
ContainerDependencies.apacheIgniteContainer.getContainerHost(),
)

fun getPrebidCacheApi(config: Map<String, String> = prebidCacheConfig.getBaseRedisConfig("false")): PrebidCacheApi {
fun getPrebidCacheApi(config: Map<String, String> = prebidCacheConfig.getBaseRedisConfig(false)): PrebidCacheApi {
return ContainerDependencies.prebidCacheContainerPool.getPrebidCacheContainer(config)
.let { container -> PrebidCacheApi(container.host, container.getHostPort()) }
}
Expand Down
Loading