-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update functional tests for prevent external POST
- Loading branch information
1 parent
fd7c185
commit 274ca21
Showing
4 changed files
with
197 additions
and
61 deletions.
There are no files selected for viewing
184 changes: 184 additions & 0 deletions
184
src/test/kotlin/org/prebid/cache/functional/AuthenticationCacheSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") { | ||
// 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() | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters