From 3b03512eee4559ecb3c3a6266826b433428e6e33 Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Mon, 2 Jan 2023 15:41:44 +0800 Subject: [PATCH] Improve code coverage (#47) --- .../cosec/api/context/SecurityContextTest.kt | 52 ++++++++++++++++ .../cosec/api/principal/CoSecPrincipalTest.kt | 62 +++++++++++++++++++ .../AuthenticatedConditionMatcherTest.kt | 2 - .../InDefaultTenantConditionMatcherTest.kt | 2 - .../InPlatformTenantConditionMatcherTest.kt | 2 - .../InUserTenantConditionMatcherTest.kt | 2 - .../part/DefaultPartExtractorTest.kt | 3 +- .../condition/part/EqConditionMatcherTest.kt | 2 +- .../serialization/CoSecJsonSerializerTest.kt | 10 +-- .../me/ahoo/cosec/webflux/ReactiveRequest.kt | 2 +- 10 files changed, 122 insertions(+), 17 deletions(-) create mode 100644 cosec-api/src/test/kotlin/me/ahoo/cosec/api/context/SecurityContextTest.kt create mode 100644 cosec-api/src/test/kotlin/me/ahoo/cosec/api/principal/CoSecPrincipalTest.kt diff --git a/cosec-api/src/test/kotlin/me/ahoo/cosec/api/context/SecurityContextTest.kt b/cosec-api/src/test/kotlin/me/ahoo/cosec/api/context/SecurityContextTest.kt new file mode 100644 index 00000000..5e5bc5e9 --- /dev/null +++ b/cosec-api/src/test/kotlin/me/ahoo/cosec/api/context/SecurityContextTest.kt @@ -0,0 +1,52 @@ +/* + * Copyright [2021-present] [ahoo wang (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 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") + } + } +} diff --git a/cosec-api/src/test/kotlin/me/ahoo/cosec/api/principal/CoSecPrincipalTest.kt b/cosec-api/src/test/kotlin/me/ahoo/cosec/api/principal/CoSecPrincipalTest.kt new file mode 100644 index 00000000..01c1484b --- /dev/null +++ b/cosec-api/src/test/kotlin/me/ahoo/cosec/api/principal/CoSecPrincipalTest.kt @@ -0,0 +1,62 @@ +/* + * Copyright [2021-present] [ahoo wang (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 + get() = TODO("Not yet implemented") + override val policies: Set + get() = TODO("Not yet implemented") + override val roles: Set + 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 + get() = TODO("Not yet implemented") + override val policies: Set + get() = TODO("Not yet implemented") + override val roles: Set + get() = TODO("Not yet implemented") + } + assertThat(authenticated.authenticated(), equalTo(true)) + } +} diff --git a/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/AuthenticatedConditionMatcherTest.kt b/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/AuthenticatedConditionMatcherTest.kt index 26a653a2..e65cd828 100644 --- a/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/AuthenticatedConditionMatcherTest.kt +++ b/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/AuthenticatedConditionMatcherTest.kt @@ -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 diff --git a/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/InDefaultTenantConditionMatcherTest.kt b/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/InDefaultTenantConditionMatcherTest.kt index cd4719e0..83a5bd43 100644 --- a/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/InDefaultTenantConditionMatcherTest.kt +++ b/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/InDefaultTenantConditionMatcherTest.kt @@ -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 diff --git a/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/InPlatformTenantConditionMatcherTest.kt b/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/InPlatformTenantConditionMatcherTest.kt index 65058e17..9441c8ad 100644 --- a/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/InPlatformTenantConditionMatcherTest.kt +++ b/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/InPlatformTenantConditionMatcherTest.kt @@ -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 diff --git a/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/InUserTenantConditionMatcherTest.kt b/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/InUserTenantConditionMatcherTest.kt index d66810d0..25c82fc1 100644 --- a/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/InUserTenantConditionMatcherTest.kt +++ b/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/context/InUserTenantConditionMatcherTest.kt @@ -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 diff --git a/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/part/DefaultPartExtractorTest.kt b/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/part/DefaultPartExtractorTest.kt index 81851d07..92655081 100644 --- a/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/part/DefaultPartExtractorTest.kt +++ b/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/part/DefaultPartExtractorTest.kt @@ -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 { @@ -101,4 +100,4 @@ class DefaultPartExtractorTest { equalTo("principal.name") ) } -} \ No newline at end of file +} diff --git a/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/part/EqConditionMatcherTest.kt b/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/part/EqConditionMatcherTest.kt index 2008dc52..6ca4c7b9 100644 --- a/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/part/EqConditionMatcherTest.kt +++ b/cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/part/EqConditionMatcherTest.kt @@ -46,4 +46,4 @@ class EqConditionMatcherTest { } assertThat(conditionMatcher.match(requestNotMatch, mockk()), `is`(false)) } -} \ No newline at end of file +} diff --git a/cosec-core/src/test/kotlin/me/ahoo/cosec/serialization/CoSecJsonSerializerTest.kt b/cosec-core/src/test/kotlin/me/ahoo/cosec/serialization/CoSecJsonSerializerTest.kt index 8791592e..0d2a887e 100644 --- a/cosec-core/src/test/kotlin/me/ahoo/cosec/serialization/CoSecJsonSerializerTest.kt +++ b/cosec-core/src/test/kotlin/me/ahoo/cosec/serialization/CoSecJsonSerializerTest.kt @@ -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.* diff --git a/cosec-webflux/src/main/kotlin/me/ahoo/cosec/webflux/ReactiveRequest.kt b/cosec-webflux/src/main/kotlin/me/ahoo/cosec/webflux/ReactiveRequest.kt index 90cd47d1..36538e93 100644 --- a/cosec-webflux/src/main/kotlin/me/ahoo/cosec/webflux/ReactiveRequest.kt +++ b/cosec-webflux/src/main/kotlin/me/ahoo/cosec/webflux/ReactiveRequest.kt @@ -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