-
Notifications
You must be signed in to change notification settings - Fork 15
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
osulzhenko
wants to merge
5
commits into
authorization/post-cache
Choose a base branch
from
functional-tests/sepate-post-endpoint
base: authorization/post-cache
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3aef20a
Add draft functional tests for prevent external POST
osulzhenko ff6bf33
wip
osulzhenko fd7c185
Merge branch 'refs/heads/authorization/post-cache' into functional-te…
osulzhenko 1cd03d2
Update functional tests for prevent external POST
osulzhenko 825189a
update after code review
osulzhenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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