Skip to content

Commit

Permalink
Improve code coverage (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Jan 2, 2023
1 parent 43038cc commit 3b03512
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.cosec.api.context

import me.ahoo.cosec.api.principal.CoSecPrincipal
import me.ahoo.cosec.api.tenant.Tenant
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.*
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class SecurityContextTest {
@Test
fun getRequiredAttribute() {
val securityContext: SecurityContext = object : SecurityContext {
override val principal: CoSecPrincipal
get() = TODO("Not yet implemented")

override fun setAttribute(key: String, value: Any): SecurityContext {
TODO("Not yet implemented")
}

override fun <T> getAttribute(key: String): T? {
if (key == "no") {
return null
}
return key as T
}

override val tenant: Tenant
get() = TODO("Not yet implemented")
}
assertThat(securityContext.getAttribute("key"), equalTo("key"))
assertThat(securityContext.getRequiredAttribute("key"), equalTo("key"))

assertThat(securityContext.getAttribute("no"), nullValue())
Assertions.assertThrows(IllegalArgumentException::class.java) {
securityContext.getRequiredAttribute("no")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.cosec.api.principal

import me.ahoo.cosec.api.principal.CoSecPrincipal.Companion.ANONYMOUS_ID
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.Test

class CoSecPrincipalTest {

@Test
fun anonymous() {
val anonymous = object : CoSecPrincipal {
override val id: String
get() = ANONYMOUS_ID

override fun getName(): String {
TODO("Not yet implemented")
}

override val attrs: Map<String, Any>
get() = TODO("Not yet implemented")
override val policies: Set<String>
get() = TODO("Not yet implemented")
override val roles: Set<String>
get() = TODO("Not yet implemented")
}
assertThat(anonymous.anonymous(), equalTo(true))
}

@Test
fun authenticated() {
val authenticated = object : CoSecPrincipal {
override val id: String
get() = "id"

override fun getName(): String {
TODO("Not yet implemented")
}

override val attrs: Map<String, Any>
get() = TODO("Not yet implemented")
override val policies: Set<String>
get() = TODO("Not yet implemented")
override val roles: Set<String>
get() = TODO("Not yet implemented")
}
assertThat(authenticated.authenticated(), equalTo(true))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import io.mockk.mockk
import me.ahoo.cosec.api.context.SecurityContext
import me.ahoo.cosec.api.context.request.Request
import me.ahoo.cosec.configuration.JsonConfiguration
import me.ahoo.cosec.policy.condition.context.AuthenticatedConditionMatcher
import me.ahoo.cosec.policy.condition.context.AuthenticatedConditionMatcherFactory
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.`is`
import org.junit.jupiter.api.Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import io.mockk.mockk
import me.ahoo.cosec.api.context.SecurityContext
import me.ahoo.cosec.api.context.request.Request
import me.ahoo.cosec.configuration.JsonConfiguration
import me.ahoo.cosec.policy.condition.context.InDefaultTenantConditionMatcher
import me.ahoo.cosec.policy.condition.context.InDefaultTenantConditionMatcherFactory
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.*
import org.junit.jupiter.api.Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import io.mockk.mockk
import me.ahoo.cosec.api.context.SecurityContext
import me.ahoo.cosec.api.context.request.Request
import me.ahoo.cosec.configuration.JsonConfiguration
import me.ahoo.cosec.policy.condition.context.InPlatformTenantConditionMatcher
import me.ahoo.cosec.policy.condition.context.InPlatformTenantConditionMatcherFactory
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.*
import org.junit.jupiter.api.Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import io.mockk.mockk
import me.ahoo.cosec.api.context.SecurityContext
import me.ahoo.cosec.api.context.request.Request
import me.ahoo.cosec.configuration.JsonConfiguration
import me.ahoo.cosec.policy.condition.context.InUserTenantConditionMatcher
import me.ahoo.cosec.policy.condition.context.InUserTenantConditionMatcherFactory
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.*
import org.junit.jupiter.api.Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import me.ahoo.cosec.api.context.SecurityContext
import me.ahoo.cosec.api.context.request.Request
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.*

import org.junit.jupiter.api.Test

class DefaultPartExtractorTest {
Expand Down Expand Up @@ -101,4 +100,4 @@ class DefaultPartExtractorTest {
equalTo("principal.name")
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ class EqConditionMatcherTest {
}
assertThat(conditionMatcher.match(requestNotMatch, mockk()), `is`(false))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ import me.ahoo.cosec.policy.action.NoneActionMatcherFactory
import me.ahoo.cosec.policy.action.PathActionMatcherFactory
import me.ahoo.cosec.policy.action.RegularActionMatcherFactory
import me.ahoo.cosec.policy.condition.AllConditionMatcherFactory
import me.ahoo.cosec.policy.condition.NoneConditionMatcherFactory
import me.ahoo.cosec.policy.condition.OgnlConditionMatcherFactory
import me.ahoo.cosec.policy.condition.SpelConditionMatcherFactory
import me.ahoo.cosec.policy.condition.context.AuthenticatedConditionMatcherFactory
import me.ahoo.cosec.policy.condition.context.InDefaultTenantConditionMatcherFactory
import me.ahoo.cosec.policy.condition.part.InConditionMatcherFactory
import me.ahoo.cosec.policy.condition.context.InPlatformTenantConditionMatcherFactory
import me.ahoo.cosec.policy.condition.context.InUserTenantConditionMatcherFactory
import me.ahoo.cosec.policy.condition.NoneConditionMatcherFactory
import me.ahoo.cosec.policy.condition.OgnlConditionMatcherFactory
import me.ahoo.cosec.policy.condition.part.RegularConditionMatcherFactory
import me.ahoo.cosec.policy.condition.SpelConditionMatcherFactory
import me.ahoo.cosec.policy.condition.part.CONDITION_MATCHER_PART_KEY
import me.ahoo.cosec.policy.condition.part.InConditionMatcherFactory
import me.ahoo.cosec.policy.condition.part.RegularConditionMatcherFactory
import me.ahoo.cosec.policy.condition.part.RequestParts
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ data class ReactiveRequest(
override val tenantId: String,
override val remoteIp: String,
override val origin: String,
override val referer: String,
override val referer: String
) : Request,
Delegated<ServerWebExchange>

0 comments on commit 3b03512

Please sign in to comment.