diff --git a/.travis.yml b/.travis.yml index 74392a6..8caeb0c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ branches: - /^release.*$/ jdk: - - oraclejdk8 + - openjdk8 before_install: - chmod +x gradlew \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 10705bb..b7a2dce 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -32,8 +32,9 @@ subprojects { dependencies { "implementation"(kotlin("stdlib-jdk8")) - "implementation"("org.junit.jupiter:junit-jupiter-api:5.4.2") - "testRuntime"("org.junit.jupiter:junit-jupiter-engine:5.4.2") + "implementation"("org.junit.jupiter:junit-jupiter-api:5.5.1") + "implementation"("org.junit.jupiter:junit-jupiter-params:5.5.1") + "testRuntime"("org.junit.jupiter:junit-jupiter-engine:5.5.1") "implementation"("org.apache.commons:commons-lang3:3.8.1") } diff --git a/core/build.gradle.kts b/core/build.gradle.kts index 8a6bb44..96f5670 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -4,5 +4,11 @@ plugins { dependencies { implementation("io.rest-assured:rest-assured:4.0.0") + implementation("org.yaml:snakeyaml:1.19") + implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.8") + implementation("com.fasterxml.jackson.core:jackson-databind:2.9.8") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.8") implementation("com.github.tomakehurst:wiremock-jre8:2.23.2") + implementation("org.junit.jupiter:junit-jupiter-api:5.5.1") + implementation("org.junit.jupiter:junit-jupiter-params:5.5.1") } diff --git a/core/src/main/kotlin/com/cognifide/apmt/ArgumentsFactory.kt b/core/src/main/kotlin/com/cognifide/apmt/ArgumentsFactory.kt new file mode 100644 index 0000000..b761e68 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/ArgumentsFactory.kt @@ -0,0 +1,90 @@ +package com.cognifide.apmt + +import org.junit.jupiter.api.Assumptions +import org.junit.jupiter.params.provider.Arguments + +fun createAllowed(testCases: List): List { + return toArguments(testCases.flatMap { createAllowed(it) }.distinct().sorted()) +} + +fun createDenied(testCases: List): List { + return toArguments(testCases.flatMap { createDenied(it) }.distinct().sorted()) +} + +private fun createAllowed(testCase: TestCaseConfiguration): List { + return createArguments( + testCase.paths, + testCase.allowedUsers, + testCase.deniedUsers, + testCase.allUsers, + testCase.allowedPairsPredicate, + testCase.deniedPairsPredicate + ) +} + +private fun createDenied(testCase: TestCaseConfiguration): List { + return createArguments( + testCase.paths, + testCase.deniedUsers, + testCase.allowedUsers, + testCase.allUsers, + testCase.deniedPairsPredicate, + testCase.allowedPairsPredicate + ) +} + +private fun createArguments( + paths: List, + users: List, + opposedUsers: List, + allUsers: List, + predicate: ((user: User, path: String) -> Boolean)?, + opposedPredicate: ((user: User, path: String) -> Boolean)? +): List { + return when { + users.isNotEmpty() -> pairs(users, paths, predicate) + opposedUsers.isNotEmpty() && allUsers.isNotEmpty() -> { + val allPairs = pairs(allUsers, paths) + allPairs - pairs(opposedUsers, paths, opposedPredicate) + } + else -> listOf() + } +} + +private fun toArguments(usersAndPaths: Collection): List { + val arguments = mutableListOf() + usersAndPaths.forEach { arguments.add(Arguments.of(it.user, it.path)) } + + Assumptions.assumeFalse(arguments.isEmpty(), "No arguments") + + return arguments.toList() +} + +private fun pairs( + users: List, + paths: List, + predicate: ((user: User, path: String) -> Boolean)? +): List { + val pairs = pairs(users, paths) + if (predicate != null) { + return pairs.filter { predicate(it.user, it.path) } + } + return pairs +} + +private fun pairs(users: List, paths: List): List { + val results = mutableListOf() + for (user in users) { + for (path in paths) { + results.add(UserAndPath(user, path)) + } + } + return results.toList() +} + +data class UserAndPath(val user: User, val path: String) : Comparable { + + override fun compareTo(other: UserAndPath): Int { + return this.user.username.compareTo(other.user.username) + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/cognifide/apmt/Checks.kt b/core/src/main/kotlin/com/cognifide/apmt/Checks.kt deleted file mode 100644 index a3eb451..0000000 --- a/core/src/main/kotlin/com/cognifide/apmt/Checks.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.cognifide.apmt - -import org.junit.jupiter.api.Assertions - -object Checks { - - fun pathContainsUser(path: String, user: String) { - Assertions.assertTrue(path.contains(user)) - } - - fun alwaysSuccess(path: String, user: String) { - Assertions.assertTrue(true) - } -} diff --git a/core/src/main/kotlin/com/cognifide/apmt/Group.kt b/core/src/main/kotlin/com/cognifide/apmt/Group.kt deleted file mode 100644 index b4439dc..0000000 --- a/core/src/main/kotlin/com/cognifide/apmt/Group.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.cognifide.apmt - -interface Group { - - val groupName: String -} diff --git a/core/src/main/kotlin/com/cognifide/apmt/PermissionTest.kt b/core/src/main/kotlin/com/cognifide/apmt/PermissionTest.kt deleted file mode 100644 index f45ae68..0000000 --- a/core/src/main/kotlin/com/cognifide/apmt/PermissionTest.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.cognifide.apmt - -import org.junit.jupiter.api.TestFactory - -abstract class PermissionTest( - private val configuration: PermissionTestsConfiguration.() -> Unit -) { - @TestFactory - fun runTests() = createPermissionTests(configuration) -} diff --git a/core/src/main/kotlin/com/cognifide/apmt/PermissionTestsConfiguration.kt b/core/src/main/kotlin/com/cognifide/apmt/PermissionTestsConfiguration.kt deleted file mode 100644 index bad8d16..0000000 --- a/core/src/main/kotlin/com/cognifide/apmt/PermissionTestsConfiguration.kt +++ /dev/null @@ -1,20 +0,0 @@ -package com.cognifide.apmt - -class PermissionTestsConfiguration( - internal val users: MutableList = mutableListOf(), - internal val groups: MutableList = mutableListOf(), - internal val testCaseConfigurations: MutableList = mutableListOf() -) { - - fun registerUsers(users: Array) = this.users.addAll(users) - fun registerUsers(users: List) = this.users.addAll(users) - - fun registerGroups(groups: Array) = this.groups.addAll(groups) - fun registerGroups(groups: List) = this.groups.addAll(groups) - - operator fun String.invoke(initConfiguration: TestCaseConfiguration.() -> Unit) { - val configuration = TestCaseConfiguration().apply(initConfiguration) - configuration.name = this - testCaseConfigurations.add(configuration) - } -} diff --git a/core/src/main/kotlin/com/cognifide/apmt/PermissionTestsFactory.kt b/core/src/main/kotlin/com/cognifide/apmt/PermissionTestsFactory.kt deleted file mode 100644 index 4356d90..0000000 --- a/core/src/main/kotlin/com/cognifide/apmt/PermissionTestsFactory.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.cognifide.apmt - -import org.junit.jupiter.api.DynamicTest - -fun createPermissionTests(initConfiguration: PermissionTestsConfiguration.() -> Unit): List { - val configuration = PermissionTestsConfiguration().apply(initConfiguration) - val dynamicTests = mutableListOf() - configuration.testCaseConfigurations.forEach { testCase -> - for (user in testCase.users) { - for (path in testCase.paths) { - dynamicTests.add(DynamicTest.dynamicTest(testCase.name) { - testCase.test?.invoke(path, user.username) - }) - } - } - } - return dynamicTests -} diff --git a/core/src/main/kotlin/com/cognifide/apmt/TestCase.kt b/core/src/main/kotlin/com/cognifide/apmt/TestCase.kt new file mode 100644 index 0000000..ce88361 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/TestCase.kt @@ -0,0 +1,6 @@ +package com.cognifide.apmt + +interface TestCase { + + fun toTestCaseConfiguration(): TestCaseConfiguration +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/cognifide/apmt/TestCaseConfiguration.kt b/core/src/main/kotlin/com/cognifide/apmt/TestCaseConfiguration.kt index ecc74e4..2ecc551 100644 --- a/core/src/main/kotlin/com/cognifide/apmt/TestCaseConfiguration.kt +++ b/core/src/main/kotlin/com/cognifide/apmt/TestCaseConfiguration.kt @@ -1,19 +1,40 @@ package com.cognifide.apmt -import kotlin.reflect.KFunction2 -class TestCaseConfiguration( - var name: String = "", - var test: KFunction2? = null, - internal val paths: MutableList = mutableListOf(), - internal val users: MutableList = mutableListOf() +class TestCaseConfiguration @JvmOverloads constructor( + internal var allowedUsers: List = listOf(), + internal var deniedUsers: List = listOf(), + internal var paths: List = listOf(), + internal var allUsers: List = listOf(), + internal var allowedPairsPredicate: ((user: User, path: String) -> Boolean)? = null, + internal var deniedPairsPredicate: ((user: User, path: String) -> Boolean)? = null ) { - fun addPath(path: String) { - paths.add(path) + fun allUsers(allUsers: Array) { + this.allUsers = allUsers.toList() } - fun addUser(user: User) { - users.add(user) + fun allUsers(allUsers: List) { + this.allUsers = allUsers + } + + fun paths(vararg paths: String) { + this.paths = paths.toList() + } + + fun allowedUsers(vararg users: User) { + this.allowedUsers = users.toList() + } + + fun deniedUsers(vararg users: User) { + this.deniedUsers = users.toList() + } + + fun allowedPairsPredicate(predicate: (user: User, path: String) -> Boolean) { + this.allowedPairsPredicate = predicate + } + + fun deniedPairsPredicate(predicate: (user: User, path: String) -> Boolean) { + this.deniedPairsPredicate = predicate } } diff --git a/core/src/main/kotlin/com/cognifide/apmt/User.kt b/core/src/main/kotlin/com/cognifide/apmt/User.kt index 381f007..e46b226 100644 --- a/core/src/main/kotlin/com/cognifide/apmt/User.kt +++ b/core/src/main/kotlin/com/cognifide/apmt/User.kt @@ -4,5 +4,5 @@ interface User { val username: String val password: String - val groups: List + } diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/Action.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/Action.kt index 706989e..4596289 100644 --- a/core/src/main/kotlin/com/cognifide/apmt/actions/Action.kt +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/Action.kt @@ -3,9 +3,9 @@ package com.cognifide.apmt.actions import io.restassured.response.Response interface Action { - fun prepare(): Response? - fun execute(): Response? - fun undo(): Response? + fun prepare(): Response? = null + fun execute(): Response + fun undo(): Response? = null fun successCode(): Int fun failureCode(): Int diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/ActionContext.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/ActionContext.kt index 673cb23..59f71e9 100644 --- a/core/src/main/kotlin/com/cognifide/apmt/actions/ActionContext.kt +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/ActionContext.kt @@ -11,6 +11,7 @@ object ActionContext { fun basicRequestSpec(user: User, instance: Instance): RequestSpecification { return RestAssured .given() + .headers(instance.headers) .header(CSRF_TOKEN, obtainCsrfToken(user, instance)) .auth().preemptive().basic(user.username, user.password) } @@ -18,6 +19,7 @@ object ActionContext { private fun obtainCsrfToken(user: User, instance: Instance): String { return RestAssured .given() + .headers(instance.headers) .auth().preemptive().basic(user.username, user.password) .`when`() .get(instance.url + CSRF_ENDPOINT) diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/asset/CreateAsset.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/asset/CreateAsset.kt new file mode 100644 index 0000000..ffc203b --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/asset/CreateAsset.kt @@ -0,0 +1,31 @@ +package com.cognifide.apmt.actions.asset + +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.ActionContext +import com.cognifide.apmt.config.Instance +import io.restassured.response.Response + +class CreateAsset( + private val instance: Instance, + private val user: User, + private val path: String +) : Action { + + override fun execute(): Response { + return ActionContext.basicRequestSpec(user, instance) + .formParam("jcr:primaryType", "sling:OrderedFolder") + .formParam("jcr:content/jcr:primaryType", "nt:unstructured") + .formParam("jcr:content/jcr:title", "Test Title") + .`when`() + .post(instance.url + path) + } + + override fun undo(): Response? { + return ActionContext.basicRequestSpec(user, instance) + .delete(instance.url + path) + } + + override fun successCode(): Int = 200 + override fun failureCode(): Int = 404 +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/asset/EditAsset.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/asset/EditAsset.kt new file mode 100644 index 0000000..23f4860 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/asset/EditAsset.kt @@ -0,0 +1,24 @@ +package com.cognifide.apmt.actions.asset + +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.ActionContext +import com.cognifide.apmt.config.Instance +import io.restassured.response.Response + +class EditAsset( + private val instance: Instance, + private val user: User, + private val path: String +) : Action { + + override fun execute(): Response { + return ActionContext.basicRequestSpec(user, instance) + .formParam("jcr:title", "Some Title") + .`when`() + .post(instance.url + path) + } + + override fun successCode(): Int = 200 + override fun failureCode(): Int = 500 +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/asset/RemoveAsset.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/asset/RemoveAsset.kt new file mode 100644 index 0000000..7c05cb0 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/asset/RemoveAsset.kt @@ -0,0 +1,22 @@ +package com.cognifide.apmt.actions.asset + +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.ActionContext +import com.cognifide.apmt.config.Instance +import io.restassured.response.Response + +class RemoveAsset( + private val instance: Instance, + private val user: User, + private val path: String +) : Action { + + override fun execute(): Response { + return ActionContext.basicRequestSpec(user, instance) + .delete(instance.url + path) + } + + override fun successCode(): Int = 204 + override fun failureCode(): Int = 403 +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/page/CreatePage.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/page/CreatePage.kt new file mode 100644 index 0000000..efe7a44 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/page/CreatePage.kt @@ -0,0 +1,55 @@ +package com.cognifide.apmt.actions.page + +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.ActionContext +import com.cognifide.apmt.common.PageContent +import com.cognifide.apmt.config.Instance +import io.restassured.response.Response + +class CreatePage( + private val instance: Instance, + private val user: User, + private val path: String, + pageContent: (PageContent.() -> Unit)? = null +) : Action { + + private val pageParams: MutableMap = mutableMapOf() + + init { + val newPageContent = PageContent() + if (pageContent != null) { + newPageContent.apply(pageContent) + } + this.pageParams.putAll(DEFAULT_PARAMS) + this.pageParams.putAll(newPageContent.toMap()) + } + + override fun execute(): Response { + val url = instance.url + path + return ActionContext.basicRequestSpec(user, instance) + .given() + .contentType("application/x-www-form-urlencoded; charset=UTF-8") + .formParams(pageParams) + .`when`() + .post(url) + } + + override fun undo(): Response { + val url = instance.url + path + return ActionContext.basicRequestSpec(user, instance) + .given() + .`when`() + .delete(url) + } + + override fun successCode(): Int = 200 + override fun failureCode(): Int = 500 + + companion object { + private val DEFAULT_PARAMS = mapOf( + "jcr:primaryType" to "cq:Page", + "jcr:content/jcr:primaryType" to "cq:PageContent" + ) + } +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/page/EditPage.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/page/EditPage.kt new file mode 100644 index 0000000..811f335 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/page/EditPage.kt @@ -0,0 +1,38 @@ +package com.cognifide.apmt.actions.page + +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.ActionContext +import com.cognifide.apmt.config.Instance +import io.restassured.response.Response + +class EditPage( + private val instance: Instance, + private val user: User, + private val path: String, + pageParams: Map = mapOf() +) : Action { + + private val pageParams: MutableMap = mutableMapOf() + + init { + this.pageParams.putAll(DEFAULT_PARAMS) + this.pageParams.putAll(pageParams) + } + + override fun execute(): Response { + val url = instance.url + path + "/jcr:content" + return ActionContext.basicRequestSpec(user, instance) + .given() + .formParams(pageParams) + .`when`() + .post(url) + } + + override fun successCode(): Int = 200 + override fun failureCode(): Int = 500 + + companion object { + private val DEFAULT_PARAMS = mapOf("copy-block/text" to "

Some text

") + } +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/page/EditPageProperties.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/page/EditPageProperties.kt new file mode 100644 index 0000000..7e291c2 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/page/EditPageProperties.kt @@ -0,0 +1,26 @@ +package com.cognifide.apmt.actions.page + +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.ActionContext +import com.cognifide.apmt.config.Instance +import io.restassured.response.Response + +class EditPageProperties( + private val instance: Instance, + private val user: User, + private val path: String +) : Action { + + override fun execute(): Response { + val url = instance.url + path + return ActionContext.basicRequestSpec(user, instance) + .given() + .formParam("jcr:title", "Updated Title") + .`when`() + .post(url) + } + + override fun successCode(): Int = 200 + override fun failureCode(): Int = 500 +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/page/OpenPage.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/page/OpenPage.kt new file mode 100644 index 0000000..7e6fbf7 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/page/OpenPage.kt @@ -0,0 +1,33 @@ +package com.cognifide.apmt.actions.page + +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.ActionContext +import com.cognifide.apmt.config.Instance +import io.restassured.RestAssured +import io.restassured.response.Response + +class OpenPage( + private val instance: Instance, + private val user: User? = null, + private val path: String, + private val queryParams: Map = mapOf() +) : Action { + + override fun execute(): Response { + val url = instance.url + path + val specification = RestAssured.given() + .headers(instance.headers) + .relaxedHTTPSValidation() + if (user != null) { + specification.spec(ActionContext.basicRequestSpec(user, instance)) + } + return specification + .params(queryParams) + .`when`() + .get(url) + } + + override fun successCode(): Int = 200 + override fun failureCode(): Int = 404 +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/page/RemovePage.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/page/RemovePage.kt new file mode 100644 index 0000000..a187381 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/page/RemovePage.kt @@ -0,0 +1,25 @@ +package com.cognifide.apmt.actions.page + +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.ActionContext +import com.cognifide.apmt.config.Instance +import io.restassured.response.Response + +class RemovePage( + private val instance: Instance, + private val user: User, + private val path: String +) : Action { + + override fun execute(): Response { + val url = instance.url + path + return ActionContext.basicRequestSpec(user, instance) + .given() + .`when`() + .delete(url) + } + + override fun successCode(): Int = 204 + override fun failureCode(): Int = 403 +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/page/ReplicatePage.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/page/ReplicatePage.kt new file mode 100644 index 0000000..149cd6d --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/page/ReplicatePage.kt @@ -0,0 +1,37 @@ +package com.cognifide.apmt.actions.page + +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.ActionContext +import com.cognifide.apmt.config.Instance +import io.restassured.response.Response + +class ReplicatePage( + private val instance: Instance, + private val user: User, + private val path: String +) : Action { + + override fun execute(): Response { + val url = instance.url + "/bin/replicate.json" + return ActionContext.basicRequestSpec(user, instance) + .given() + .formParam("cmd", "activate") + .formParam("path", path) + .`when`() + .post(url) + } + + override fun undo(): Response { + val url = instance.url + "/bin/replicate.json" + return ActionContext.basicRequestSpec(user, instance) + .given() + .formParam("cmd", "deactivate") + .formParam("path", path) + .`when`() + .post(url) + } + + override fun successCode(): Int = 200 + override fun failureCode(): Int = 403 +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/resource/CreateResource.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/resource/CreateResource.kt new file mode 100644 index 0000000..41d016f --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/resource/CreateResource.kt @@ -0,0 +1,37 @@ +package com.cognifide.apmt.actions.resource + +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.ActionContext +import com.cognifide.apmt.common.Resource +import com.cognifide.apmt.config.Instance +import io.restassured.response.Response + +class CreateResource( + private val instance: Instance, + private val user: User, + private val path: String, + private val resource: (Resource.() -> Unit)? = null +) : Action { + + override fun execute(): Response { + val newResource = Resource() + if (resource != null) { + newResource.apply(resource) + } + return ActionContext.basicRequestSpec(user, instance) + .formParams(newResource.toMap()) + .`when`() + .post(instance.url + path) + } + + override fun undo(): Response { + return ActionContext.basicRequestSpec(user, instance) + .`when`() + .delete(instance.url + path) + } + + + override fun successCode(): Int = 201 + override fun failureCode(): Int = 403 +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/resource/EditResource.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/resource/EditResource.kt new file mode 100644 index 0000000..46e38f1 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/resource/EditResource.kt @@ -0,0 +1,30 @@ +package com.cognifide.apmt.actions.resource + +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.ActionContext +import com.cognifide.apmt.common.Resource +import com.cognifide.apmt.config.Instance +import io.restassured.response.Response + +class EditResource( + private val instance: Instance, + private val user: User, + private val path: String, + private val resource: (Resource.() -> Unit)? = null +) : Action { + + override fun execute(): Response { + val newResource = Resource() + if (resource != null) { + newResource.apply(resource) + } + return ActionContext.basicRequestSpec(user, instance) + .formParams(newResource.toMap()) + .`when`() + .post(instance.url + path) + } + + override fun successCode(): Int = 200 + override fun failureCode(): Int = 500 +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/common/ReadResource.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/resource/ReadResource.kt similarity index 65% rename from core/src/main/kotlin/com/cognifide/apmt/actions/common/ReadResource.kt rename to core/src/main/kotlin/com/cognifide/apmt/actions/resource/ReadResource.kt index be8391e..685ab84 100644 --- a/core/src/main/kotlin/com/cognifide/apmt/actions/common/ReadResource.kt +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/resource/ReadResource.kt @@ -1,4 +1,4 @@ -package com.cognifide.apmt.actions.common +package com.cognifide.apmt.actions.resource import com.cognifide.apmt.User import com.cognifide.apmt.actions.Action @@ -7,20 +7,17 @@ import com.cognifide.apmt.config.Instance import io.restassured.response.Response class ReadResource( - val instance: Instance, - val user: User, - val path: String + private val instance: Instance, + private val user: User, + private val path: String ) : Action { - override fun prepare(): Response? = null - override fun execute(): Response? { + override fun execute(): Response { return ActionContext.basicRequestSpec(user, instance) .`when`() .get(instance.url + path + ".json") } - override fun undo(): Response? = null - override fun successCode(): Int = 200 override fun failureCode(): Int = 404 } diff --git a/core/src/main/kotlin/com/cognifide/apmt/actions/resource/RemoveResource.kt b/core/src/main/kotlin/com/cognifide/apmt/actions/resource/RemoveResource.kt new file mode 100644 index 0000000..7e212a0 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/actions/resource/RemoveResource.kt @@ -0,0 +1,23 @@ +package com.cognifide.apmt.actions.resource + +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.ActionContext +import com.cognifide.apmt.config.Instance +import io.restassured.response.Response + +class RemoveResource( + private val instance: Instance, + private val user: User, + private val path: String +) : Action { + + override fun execute(): Response { + return ActionContext.basicRequestSpec(user, instance) + .`when`() + .delete(instance.url + path) + } + + override fun successCode(): Int = 200 + override fun failureCode(): Int = 500 +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/common/PageContent.kt b/core/src/main/kotlin/com/cognifide/apmt/common/PageContent.kt new file mode 100644 index 0000000..94cec4b --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/common/PageContent.kt @@ -0,0 +1,28 @@ +package com.cognifide.apmt.common + +class PageContent( + var jcrTitle: String = "[APMT] Test Page", + var cqTemplate: String = "", + var slingResourceType: String = "", + private val properties: MutableMap = mutableMapOf() +) { + + fun properties(vararg pairs: Pair) { + properties.putAll(pairs) + } + + infix fun String.set(value: String) { + properties[this] = value + } + + internal fun toMap(): Map { + val pageProperties = mutableMapOf() + pageProperties["jcr:content/jcr:title"] = jcrTitle + pageProperties["jcr:content/sling:resourceType"] = slingResourceType + pageProperties["jcr:content/cq:template"] = cqTemplate + + properties.forEach { (key, value) -> pageProperties["jcr:content/$key"] = value } + + return pageProperties.toMap() + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/cognifide/apmt/common/Resource.kt b/core/src/main/kotlin/com/cognifide/apmt/common/Resource.kt new file mode 100644 index 0000000..d641b49 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/common/Resource.kt @@ -0,0 +1,28 @@ +package com.cognifide.apmt.common + +class Resource( + var jcrPrimaryType: String? = null, + var slingResourceType: String? = null, + private val properties: MutableMap = mutableMapOf() +) { + + fun properties(vararg pairs: Pair) { + properties.putAll(pairs) + } + + infix fun String.set(value: String) { + properties[this] = value + } + + internal fun toMap(): Map { + val map = mutableMapOf() + if (jcrPrimaryType != null) { + map["jcr:primaryType"] = jcrPrimaryType!! + } + if (slingResourceType != null) { + map["sling:resourceType"] = slingResourceType!! + } + map.putAll(properties) + return map.toMap() + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/cognifide/apmt/config/Admin.kt b/core/src/main/kotlin/com/cognifide/apmt/config/Admin.kt new file mode 100644 index 0000000..19ff1ad --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/config/Admin.kt @@ -0,0 +1,8 @@ +package com.cognifide.apmt.config + +import com.fasterxml.jackson.annotation.JsonProperty + +data class Admin( + @field:JsonProperty val username: String, + @field:JsonProperty val password: String +) \ No newline at end of file diff --git a/core/src/main/kotlin/com/cognifide/apmt/config/Configuration.kt b/core/src/main/kotlin/com/cognifide/apmt/config/Configuration.kt new file mode 100644 index 0000000..eda8737 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/config/Configuration.kt @@ -0,0 +1,8 @@ +package com.cognifide.apmt.config + +import com.fasterxml.jackson.annotation.JsonProperty + +data class Configuration( + @field:JsonProperty val admin: Admin, + @field:JsonProperty val instances: Instances +) \ No newline at end of file diff --git a/core/src/main/kotlin/com/cognifide/apmt/config/ConfigurationProvider.kt b/core/src/main/kotlin/com/cognifide/apmt/config/ConfigurationProvider.kt new file mode 100644 index 0000000..3fdcda6 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/config/ConfigurationProvider.kt @@ -0,0 +1,35 @@ +package com.cognifide.apmt.config + +import com.cognifide.apmt.User +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory +import com.fasterxml.jackson.module.kotlin.KotlinModule + +object ConfigurationProvider { + + private val configuration: Configuration + + val authorInstance: Instance + get() = configuration.instances.author + + val publishInstance: Instance + get() = configuration.instances.publish + + val adminUser: User + get() = AdminUser(configuration.admin.username, configuration.admin.password) + + init { + configuration = fetchFromFile("/apmt.yaml", Configuration::class.java) + } + + private fun fetchFromFile(fileName: String, clazz: Class): T { + javaClass.getResourceAsStream(fileName).use { + return ObjectMapper(YAMLFactory()).registerModule(KotlinModule()).readValue(it, clazz) + } + } + + private class AdminUser( + override val username: String, + override val password: String + ) : User +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/config/Instance.kt b/core/src/main/kotlin/com/cognifide/apmt/config/Instance.kt index e9e3be1..d294f18 100644 --- a/core/src/main/kotlin/com/cognifide/apmt/config/Instance.kt +++ b/core/src/main/kotlin/com/cognifide/apmt/config/Instance.kt @@ -1,3 +1,7 @@ package com.cognifide.apmt.config -data class Instance(val name: String, val url: String) +data class Instance( + val name: String, + val url: String, + val headers: Map = mapOf() +) diff --git a/core/src/main/kotlin/com/cognifide/apmt/config/Instances.kt b/core/src/main/kotlin/com/cognifide/apmt/config/Instances.kt new file mode 100644 index 0000000..abd42d8 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/config/Instances.kt @@ -0,0 +1,8 @@ +package com.cognifide.apmt.config + +import com.fasterxml.jackson.annotation.JsonProperty + +data class Instances( + @field:JsonProperty val author: Instance, + @field:JsonProperty val publish: Instance +) diff --git a/core/src/main/kotlin/com/cognifide/apmt/tests/Allowed.kt b/core/src/main/kotlin/com/cognifide/apmt/tests/Allowed.kt new file mode 100644 index 0000000..f266a87 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/tests/Allowed.kt @@ -0,0 +1,11 @@ +package com.cognifide.apmt.tests + +import com.cognifide.apmt.tests.ApmtBaseTest.Companion.ALLOWED +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.MethodSource + +@Target(AnnotationTarget.FUNCTION) +@Retention(AnnotationRetention.RUNTIME) +@ParameterizedTest +@MethodSource(ALLOWED) +annotation class Allowed \ No newline at end of file diff --git a/core/src/main/kotlin/com/cognifide/apmt/tests/ApmtBaseTest.kt b/core/src/main/kotlin/com/cognifide/apmt/tests/ApmtBaseTest.kt new file mode 100644 index 0000000..1b64821 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/tests/ApmtBaseTest.kt @@ -0,0 +1,17 @@ +package com.cognifide.apmt.tests + +import com.cognifide.apmt.TestCase +import com.cognifide.apmt.createAllowed +import com.cognifide.apmt.createDenied + +abstract class ApmtBaseTest(private vararg val testCases: TestCase) { + + fun allowed() = createAllowed(testCases.map { it.toTestCaseConfiguration() }) + + fun denied() = createDenied(testCases.map { it.toTestCaseConfiguration() }) + + companion object { + const val ALLOWED = "allowed" + const val DENIED = "denied" + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/cognifide/apmt/tests/Denied.kt b/core/src/main/kotlin/com/cognifide/apmt/tests/Denied.kt new file mode 100644 index 0000000..8db6353 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/tests/Denied.kt @@ -0,0 +1,11 @@ +package com.cognifide.apmt.tests + +import com.cognifide.apmt.tests.ApmtBaseTest.Companion.DENIED +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.MethodSource + +@Target(AnnotationTarget.FUNCTION) +@Retention(AnnotationRetention.RUNTIME) +@ParameterizedTest +@MethodSource(DENIED) +annotation class Denied \ No newline at end of file diff --git a/core/src/main/kotlin/com/cognifide/apmt/tests/asset/CreateAssetTest.kt b/core/src/main/kotlin/com/cognifide/apmt/tests/asset/CreateAssetTest.kt new file mode 100644 index 0000000..0b58e03 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/tests/asset/CreateAssetTest.kt @@ -0,0 +1,59 @@ +package com.cognifide.apmt.tests.asset + +import com.cognifide.apmt.TestCase +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.asset.CreateAsset +import com.cognifide.apmt.config.ConfigurationProvider +import com.cognifide.apmt.tests.Allowed +import com.cognifide.apmt.tests.ApmtBaseTest +import com.cognifide.apmt.tests.Denied +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.params.ParameterizedTest + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@DisplayName("Check user permissions to create asset") +abstract class CreateAssetTest(vararg testCases: TestCase) : ApmtBaseTest(*testCases) { + + private val authorInstance = ConfigurationProvider.authorInstance + private var undoAction: Action? = null + + @DisplayName("User can create assets") + @ParameterizedTest + @Allowed + fun userCanCreateAssets(user: User, path: String) { + undoAction = CreateAsset(authorInstance, ConfigurationProvider.adminUser, path) + + CreateAsset(authorInstance, user, path) + .execute() + .then() + .assertThat() + .statusCode(201) + } + + @DisplayName("User cannot create assets") + @ParameterizedTest + @Denied + fun userCannotCreateAssets(user: User, path: String) { + undoAction = CreateAsset(authorInstance, ConfigurationProvider.adminUser, path) + + CreateAsset(authorInstance, user, path) + .execute() + .then() + .assertThat() + .statusCode(500) + } + + @BeforeEach + fun init() { + undoAction = null + } + + @AfterEach + fun cleanUp() { + undoAction?.undo() + } +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/tests/asset/EditAssetTest.kt b/core/src/main/kotlin/com/cognifide/apmt/tests/asset/EditAssetTest.kt new file mode 100644 index 0000000..c3fd99d --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/tests/asset/EditAssetTest.kt @@ -0,0 +1,62 @@ +package com.cognifide.apmt.tests.asset + +import com.cognifide.apmt.TestCase +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.asset.CreateAsset +import com.cognifide.apmt.actions.asset.EditAsset +import com.cognifide.apmt.config.ConfigurationProvider +import com.cognifide.apmt.tests.Allowed +import com.cognifide.apmt.tests.ApmtBaseTest +import com.cognifide.apmt.tests.Denied +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.params.ParameterizedTest + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@DisplayName("Check user permissions to edit asset") +abstract class EditAssetTest(vararg testCases: TestCase) : ApmtBaseTest(*testCases) { + + private val authorInstance = ConfigurationProvider.authorInstance + private var undoAction: Action? = null + + @DisplayName("User can edit assets") + @ParameterizedTest + @Allowed + fun userCanEditAssets(user: User, path: String) { + undoAction = CreateAsset(authorInstance, ConfigurationProvider.adminUser, path) + undoAction?.prepare() + + EditAsset(authorInstance, user, path) + .execute() + .then() + .assertThat() + .statusCode(200) + } + + @DisplayName("User cannot edit assets") + @ParameterizedTest + @Denied + fun userCannotEditAssets(user: User, path: String) { + undoAction = CreateAsset(authorInstance, ConfigurationProvider.adminUser, path) + undoAction?.prepare() + + EditAsset(authorInstance, user, path) + .execute() + .then() + .assertThat() + .statusCode(500) + } + + @BeforeEach + fun init() { + undoAction = null + } + + @AfterEach + fun cleanup() { + undoAction!!.undo() + } +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/tests/asset/RemoveAssetTest.kt b/core/src/main/kotlin/com/cognifide/apmt/tests/asset/RemoveAssetTest.kt new file mode 100644 index 0000000..07a2ec9 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/tests/asset/RemoveAssetTest.kt @@ -0,0 +1,62 @@ +package com.cognifide.apmt.tests.asset + +import com.cognifide.apmt.TestCase +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.asset.CreateAsset +import com.cognifide.apmt.actions.asset.RemoveAsset +import com.cognifide.apmt.config.ConfigurationProvider +import com.cognifide.apmt.tests.Allowed +import com.cognifide.apmt.tests.ApmtBaseTest +import com.cognifide.apmt.tests.Denied +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.params.ParameterizedTest + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@DisplayName("Check user permissions to remove asset") +abstract class RemoveAssetTest(vararg testCases: TestCase) : ApmtBaseTest(*testCases) { + + private val authorInstance = ConfigurationProvider.authorInstance + private var undoAction: Action? = null + + @DisplayName("User can remove asset") + @ParameterizedTest + @Allowed + fun userCanDeleteAssets(user: User, path: String) { + undoAction = CreateAsset(authorInstance, ConfigurationProvider.adminUser, path) + undoAction?.execute() + + RemoveAsset(authorInstance, user, path) + .execute() + .then() + .assertThat() + .statusCode(204) + } + + @DisplayName("User can not remove asset") + @ParameterizedTest + @Denied + fun userCannotDeleteAssets(user: User, path: String) { + undoAction = CreateAsset(authorInstance, ConfigurationProvider.adminUser, path) + undoAction?.execute() + + RemoveAsset(authorInstance, user, path) + .execute() + .then() + .assertThat() + .statusCode(403) + } + + @BeforeEach + fun init() { + undoAction = null + } + + @AfterEach + fun cleanup() { + undoAction?.undo() + } +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/tests/page/CreatePageTest.kt b/core/src/main/kotlin/com/cognifide/apmt/tests/page/CreatePageTest.kt new file mode 100644 index 0000000..30cd8a2 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/tests/page/CreatePageTest.kt @@ -0,0 +1,66 @@ +package com.cognifide.apmt.tests.page + +import com.cognifide.apmt.TestCase +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.page.CreatePage +import com.cognifide.apmt.common.PageContent +import com.cognifide.apmt.config.ConfigurationProvider +import com.cognifide.apmt.tests.Allowed +import com.cognifide.apmt.tests.ApmtBaseTest +import com.cognifide.apmt.tests.Denied +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.params.ParameterizedTest + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@DisplayName("Check user permissions to create pages") +abstract class CreatePageTest( + vararg testCases: TestCase, + private val pageContent: (PageContent.() -> Unit)? = null +) : ApmtBaseTest(*testCases) { + + private val authorInstance = ConfigurationProvider.authorInstance + private var undoAction: Action? = null + + @DisplayName("User can create pages") + @ParameterizedTest + @Allowed + fun userCanCreatePages(user: User, path: String) { + undoAction = CreatePage(authorInstance, ConfigurationProvider.adminUser, path) + undoAction?.undo() + + CreatePage(authorInstance, user, path, pageContent) + .execute() + .then() + .assertThat() + .statusCode(201) + } + + @DisplayName("User cannot create pages") + @ParameterizedTest + @Denied + fun userCannotCreatePages(user: User, path: String) { + undoAction = CreatePage(authorInstance, ConfigurationProvider.adminUser, path) + undoAction?.undo() + + CreatePage(authorInstance, user, path, pageContent) + .execute() + .then() + .assertThat() + .statusCode(500) + } + + @BeforeEach + fun init() { + undoAction = null + } + + @AfterEach + fun cleanup() { + undoAction?.undo() + } +} + diff --git a/core/src/main/kotlin/com/cognifide/apmt/tests/page/EditPagePropertiesTest.kt b/core/src/main/kotlin/com/cognifide/apmt/tests/page/EditPagePropertiesTest.kt new file mode 100644 index 0000000..273fc4e --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/tests/page/EditPagePropertiesTest.kt @@ -0,0 +1,62 @@ +package com.cognifide.apmt.tests.page + +import com.cognifide.apmt.TestCase +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.page.CreatePage +import com.cognifide.apmt.actions.page.EditPageProperties +import com.cognifide.apmt.config.ConfigurationProvider +import com.cognifide.apmt.tests.Allowed +import com.cognifide.apmt.tests.ApmtBaseTest +import com.cognifide.apmt.tests.Denied +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.params.ParameterizedTest + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@DisplayName("Check user permissions to edit page properties") +abstract class EditPagePropertiesTest(vararg testCases: TestCase) : ApmtBaseTest(*testCases) { + + private var authorInstance = ConfigurationProvider.authorInstance + private var undoAction: Action? = null + + @DisplayName("User can edit page properties") + @ParameterizedTest + @Allowed + fun userCanEditPageProperties(user: User, path: String) { + undoAction = CreatePage(authorInstance, ConfigurationProvider.adminUser, path) + undoAction?.execute() + + EditPageProperties(authorInstance, user, "$path/jcr:content") + .execute() + .then() + .assertThat() + .statusCode(200) + } + + @DisplayName("User cannot edit page properties") + @ParameterizedTest + @Denied + fun userCannotEditPageProperties(user: User, path: String) { + undoAction = CreatePage(authorInstance, ConfigurationProvider.adminUser, path) + undoAction?.execute() + + EditPageProperties(authorInstance, user, "$path/jcr:content") + .execute() + .then() + .assertThat() + .statusCode(500) + } + + @BeforeEach + fun init() { + undoAction = null + } + + @AfterEach + fun cleanup() { + undoAction?.undo() + } +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/tests/page/EditPageTest.kt b/core/src/main/kotlin/com/cognifide/apmt/tests/page/EditPageTest.kt new file mode 100644 index 0000000..850fed3 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/tests/page/EditPageTest.kt @@ -0,0 +1,62 @@ +package com.cognifide.apmt.tests.page + +import com.cognifide.apmt.TestCase +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.page.CreatePage +import com.cognifide.apmt.actions.page.EditPage +import com.cognifide.apmt.config.ConfigurationProvider +import com.cognifide.apmt.tests.Allowed +import com.cognifide.apmt.tests.ApmtBaseTest +import com.cognifide.apmt.tests.Denied +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.params.ParameterizedTest + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@DisplayName("Check user permissions to edit pages") +abstract class EditPageTest(vararg testCases: TestCase) : ApmtBaseTest(*testCases) { + + private var authorInstance = ConfigurationProvider.authorInstance + private var undoAction: Action? = null + + @DisplayName("User can edit pages") + @ParameterizedTest + @Allowed + fun userCanEditPages(user: User, path: String) { + undoAction = CreatePage(authorInstance, ConfigurationProvider.adminUser, path) + undoAction?.execute() + + EditPage(authorInstance, user, path) + .execute() + .then() + .assertThat() + .statusCode(200) + } + + @DisplayName("User cannot edit pages") + @ParameterizedTest + @Denied + fun userCannotEditPages(user: User, path: String) { + undoAction = CreatePage(authorInstance, ConfigurationProvider.adminUser, path) + undoAction?.execute() + + EditPage(authorInstance, user, path) + .execute() + .then() + .assertThat() + .statusCode(500) + } + + @BeforeEach + fun init() { + undoAction = null + } + + @AfterEach + fun cleanup() { + undoAction?.undo() + } +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/tests/page/OpenPageTest.kt b/core/src/main/kotlin/com/cognifide/apmt/tests/page/OpenPageTest.kt new file mode 100644 index 0000000..ea87c73 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/tests/page/OpenPageTest.kt @@ -0,0 +1,69 @@ +package com.cognifide.apmt.tests.page + +import com.cognifide.apmt.TestCase +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.page.CreatePage +import com.cognifide.apmt.actions.page.OpenPage +import com.cognifide.apmt.config.ConfigurationProvider +import com.cognifide.apmt.config.Instance +import com.cognifide.apmt.tests.Allowed +import com.cognifide.apmt.tests.ApmtBaseTest +import com.cognifide.apmt.tests.Denied +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.params.ParameterizedTest + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@DisplayName("Check user permissions to open pages") +abstract class OpenPageTest( + vararg testCases: TestCase, + private var instance: Instance = ConfigurationProvider.authorInstance +) : ApmtBaseTest(*testCases) { + + private var undoAction: Action? = null + + @DisplayName("User can open pages") + @ParameterizedTest + @Allowed + fun userCanOpenPages(user: User, path: String) { + if (instance == ConfigurationProvider.authorInstance) { + undoAction = CreatePage(instance, ConfigurationProvider.adminUser, path) + undoAction?.execute() + } + + OpenPage(instance, user, path) + .execute() + .then() + .assertThat() + .statusCode(200) + } + + @DisplayName("User cannot open pages") + @ParameterizedTest + @Denied + fun userCannotOpenPages(user: User, path: String) { + if (instance == ConfigurationProvider.authorInstance) { + undoAction = CreatePage(instance, ConfigurationProvider.adminUser, path) + undoAction?.execute() + } + + OpenPage(instance, user, path) + .execute() + .then() + .assertThat() + .statusCode(404) + } + + @BeforeEach + fun init() { + undoAction = null + } + + @AfterEach + fun cleanup() { + undoAction?.undo() + } +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/tests/page/RemovePageTest.kt b/core/src/main/kotlin/com/cognifide/apmt/tests/page/RemovePageTest.kt new file mode 100644 index 0000000..ce7e706 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/tests/page/RemovePageTest.kt @@ -0,0 +1,62 @@ +package com.cognifide.apmt.tests.page + +import com.cognifide.apmt.TestCase +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.page.CreatePage +import com.cognifide.apmt.actions.page.RemovePage +import com.cognifide.apmt.config.ConfigurationProvider +import com.cognifide.apmt.tests.Allowed +import com.cognifide.apmt.tests.ApmtBaseTest +import com.cognifide.apmt.tests.Denied +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.params.ParameterizedTest + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@DisplayName("Check user permissions to remove pages") +abstract class RemovePageTest(vararg testCases: TestCase) : ApmtBaseTest(*testCases) { + + private var authorInstance = ConfigurationProvider.authorInstance + private var undoAction: Action? = null + + @DisplayName("User can remove pages") + @ParameterizedTest + @Allowed + fun userCanRemovePages(user: User, path: String) { + undoAction = CreatePage(authorInstance, ConfigurationProvider.adminUser, path) + undoAction?.execute() + + RemovePage(authorInstance, user, path) + .execute() + .then() + .assertThat() + .statusCode(204) + } + + @DisplayName("User cannot remove pages") + @ParameterizedTest + @Denied + fun userCannotRemovePages(user: User, path: String) { + undoAction = CreatePage(authorInstance, ConfigurationProvider.adminUser, path) + undoAction?.execute() + + RemovePage(authorInstance, user, path) + .execute() + .then() + .assertThat() + .statusCode(403) + } + + @BeforeEach + fun init() { + undoAction = null + } + + @AfterEach + fun cleanup() { + undoAction?.undo() + } +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/tests/page/ReplicatePageTest.kt b/core/src/main/kotlin/com/cognifide/apmt/tests/page/ReplicatePageTest.kt new file mode 100644 index 0000000..fda33e6 --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/tests/page/ReplicatePageTest.kt @@ -0,0 +1,80 @@ +package com.cognifide.apmt.tests.page + +import com.cognifide.apmt.TestCase +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.page.CreatePage +import com.cognifide.apmt.actions.page.ReplicatePage +import com.cognifide.apmt.config.ConfigurationProvider +import com.cognifide.apmt.tests.Allowed +import com.cognifide.apmt.tests.ApmtBaseTest +import com.cognifide.apmt.tests.Denied +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.params.ParameterizedTest + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@DisplayName("Check user permissions to replicate pages") +abstract class ReplicatePageTest(vararg testCases: TestCase) : ApmtBaseTest(*testCases) { + + private var authorInstance = ConfigurationProvider.authorInstance + private var undoActions: MutableList = mutableListOf() + + @DisplayName("User can replicate pages") + @ParameterizedTest + @Allowed + fun userCanReplicatePage(user: User, path: String) { + val createPageAction = CreatePage(authorInstance, ConfigurationProvider.adminUser, path) + createPageAction.execute() + undoActions.add(createPageAction) + + val pageReplicationAction = ReplicatePage(authorInstance, user, path) + pageReplicationAction + .execute() + .then() + .assertThat() + .statusCode(200) + + pageReplicationAction + .undo() + .then() + .assertThat() + .statusCode(200) + } + + @DisplayName("User cannot replicate pages") + @ParameterizedTest + @Denied + fun userCannotReplicatePage(user: User, path: String) { + val createPageAction = CreatePage(authorInstance, ConfigurationProvider.adminUser, path) + createPageAction.execute() + undoActions.add(createPageAction) + + val pageReplicationAction = ReplicatePage(authorInstance, user, path) + pageReplicationAction + .execute() + .then() + .assertThat().statusCode(403) + + val adminPageReplicationAction = ReplicatePage(authorInstance, ConfigurationProvider.adminUser, path) + adminPageReplicationAction.execute() + undoActions.add(adminPageReplicationAction) + + pageReplicationAction + .execute() + .then() + .assertThat().statusCode(403) + } + + @BeforeEach + fun init() { + undoActions.clear() + } + + @AfterEach + fun cleanup() { + undoActions.asReversed().forEach { it.undo() } + } +} diff --git a/core/src/main/kotlin/com/cognifide/apmt/tests/resource/CreateResourceTest.kt b/core/src/main/kotlin/com/cognifide/apmt/tests/resource/CreateResourceTest.kt new file mode 100644 index 0000000..2fa5fce --- /dev/null +++ b/core/src/main/kotlin/com/cognifide/apmt/tests/resource/CreateResourceTest.kt @@ -0,0 +1,56 @@ +package com.cognifide.apmt.tests.resource + +import com.cognifide.apmt.TestCase +import com.cognifide.apmt.User +import com.cognifide.apmt.actions.Action +import com.cognifide.apmt.actions.resource.CreateResource +import com.cognifide.apmt.common.Resource +import com.cognifide.apmt.config.ConfigurationProvider +import com.cognifide.apmt.tests.Allowed +import com.cognifide.apmt.tests.ApmtBaseTest +import com.cognifide.apmt.tests.Denied +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.params.ParameterizedTest + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@DisplayName("Check user permissions to create resources") +abstract class CreateResourceTest( + vararg testCases: TestCase, + private val resource: (Resource.() -> Unit)? = null +) : ApmtBaseTest(*testCases) { + + private val authorInstance = ConfigurationProvider.authorInstance + private var undoAction: Action? = null + + @DisplayName("User can create resources") + @ParameterizedTest + @Allowed + fun userCanCreateResources(user: User, path: String) { + undoAction = CreateResource(authorInstance, user, path, resource) + undoAction + ?.execute() + ?.then() + ?.assertThat() + ?.statusCode(201) + } + + @DisplayName("User cannot create resources") + @ParameterizedTest + @Denied + fun userCannotCreateResources(user: User, path: String) { + undoAction = CreateResource(authorInstance, user, path, resource) + undoAction + ?.execute() + ?.then() + ?.assertThat() + ?.statusCode(500) + } + + @AfterEach + fun cleanUp() { + undoAction?.undo() + } +} + diff --git a/core/src/test/kotlin/ExampleTest.kt b/core/src/test/kotlin/ExampleTest.kt deleted file mode 100644 index ee28092..0000000 --- a/core/src/test/kotlin/ExampleTest.kt +++ /dev/null @@ -1,20 +0,0 @@ -import com.cognifide.apmt.Checks -import com.cognifide.apmt.PermissionTest - -class ExampleTest : PermissionTest({ - - registerUsers(Users.values()) - registerGroups(Groups.values()) - - "add assets" { - test = Checks::pathContainsUser - addPath("/content/sites/author") - addUser(Users.GLOBAL_AUTHOR) - } - - "modify assets" { - test = Checks::alwaysSuccess - addPath("/content/sites/site") - addUser(Users.GLOBAL_AUTHOR) - } -}) diff --git a/core/src/test/kotlin/Groups.kt b/core/src/test/kotlin/Groups.kt deleted file mode 100644 index 37b2f58..0000000 --- a/core/src/test/kotlin/Groups.kt +++ /dev/null @@ -1,5 +0,0 @@ -import com.cognifide.apmt.Group - -enum class Groups(override val groupName: String) : Group { - AUTHOR("author") -} diff --git a/core/src/test/kotlin/Users.kt b/core/src/test/kotlin/Users.kt deleted file mode 100644 index 331a75a..0000000 --- a/core/src/test/kotlin/Users.kt +++ /dev/null @@ -1,11 +0,0 @@ -import com.cognifide.apmt.Group -import com.cognifide.apmt.User - -enum class Users( - override val password: String, - override val username: String, - override val groups: List -) : User { - GLOBAL_AUTHOR("xxx", "author", listOf(Groups.AUTHOR)), - PL_AUTHOR("xxx", "pl_author", listOf(Groups.AUTHOR)) -} diff --git a/core/src/test/kotlin/com/cognifide/apmt/TestData.kt b/core/src/test/kotlin/com/cognifide/apmt/TestData.kt index 7df5bc7..ce5d470 100644 --- a/core/src/test/kotlin/com/cognifide/apmt/TestData.kt +++ b/core/src/test/kotlin/com/cognifide/apmt/TestData.kt @@ -5,7 +5,6 @@ import com.cognifide.apmt.config.Instance val TEST_USER = object : User { override val username: String = "testuser" override val password: String = "testpassword" - override val groups: List = emptyList() } val MOCK_SERVER = Instance("Mock Server", "http://localhost:8080") diff --git a/core/src/test/kotlin/com/cognifide/apmt/actions/ActionContextTest.kt b/core/src/test/kotlin/com/cognifide/apmt/actions/ActionContextTest.kt index 312ea0f..3951d33 100644 --- a/core/src/test/kotlin/com/cognifide/apmt/actions/ActionContextTest.kt +++ b/core/src/test/kotlin/com/cognifide/apmt/actions/ActionContextTest.kt @@ -3,6 +3,7 @@ package com.cognifide.apmt.actions import com.cognifide.apmt.MOCK_SERVER import com.cognifide.apmt.TEST_USER import com.cognifide.apmt.util.AemStub +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUsers import com.github.tomakehurst.wiremock.client.BasicCredentials import com.github.tomakehurst.wiremock.client.WireMock.* import org.junit.jupiter.api.Test @@ -12,6 +13,7 @@ class ActionContextTest { @Test @AemStub fun basicRequestSpecHasCsrfToken() { + registerUsers(TEST_USER) stubFor( get(urlPathEqualTo("/")) .withBasicAuth(TEST_USER.username, TEST_USER.password) @@ -28,7 +30,7 @@ class ActionContextTest { verify( getRequestedFor(urlPathEqualTo("/")) - .withHeader(CSRF_TOKEN, equalTo("CORRECT_TOKEN")) + .withHeader(CSRF_TOKEN, equalTo(TEST_USER.username)) .withBasicAuth(BasicCredentials(TEST_USER.username, TEST_USER.password)) ) } diff --git a/core/src/test/kotlin/com/cognifide/apmt/actions/common/ReadResourceTest.kt b/core/src/test/kotlin/com/cognifide/apmt/actions/resource/ReadResourceTest.kt similarity index 89% rename from core/src/test/kotlin/com/cognifide/apmt/actions/common/ReadResourceTest.kt rename to core/src/test/kotlin/com/cognifide/apmt/actions/resource/ReadResourceTest.kt index e65b1f7..6a74670 100644 --- a/core/src/test/kotlin/com/cognifide/apmt/actions/common/ReadResourceTest.kt +++ b/core/src/test/kotlin/com/cognifide/apmt/actions/resource/ReadResourceTest.kt @@ -1,8 +1,9 @@ -package com.cognifide.apmt.actions.common +package com.cognifide.apmt.actions.resource import com.cognifide.apmt.MOCK_SERVER import com.cognifide.apmt.TEST_USER import com.cognifide.apmt.util.AemStub +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUsers import com.cognifide.apmt.util.HumanReadableCamelCase import com.github.tomakehurst.wiremock.client.WireMock.* import org.junit.jupiter.api.Assertions.assertEquals @@ -25,6 +26,8 @@ class ReadResourceTest { @Test @AemStub fun callingExecutePerformsGet() { + registerUsers(TEST_USER) + stubFor( get(urlPathEqualTo("/readResource")) .willReturn(ok()) diff --git a/core/src/test/kotlin/com/cognifide/apmt/tests/ExampleTestCases.kt b/core/src/test/kotlin/com/cognifide/apmt/tests/ExampleTestCases.kt new file mode 100644 index 0000000..d8dddcd --- /dev/null +++ b/core/src/test/kotlin/com/cognifide/apmt/tests/ExampleTestCases.kt @@ -0,0 +1,81 @@ +package com.cognifide.apmt.tests + +import com.cognifide.apmt.TestCase +import com.cognifide.apmt.TestCaseConfiguration + +enum class ExampleTestCases(private val initConfig: TestCaseConfiguration.() -> Unit) : TestCase { + + ADD_ASSET({ + paths( + "/content/dam/my-product/screens", + "/content/dam/my-product/images" + ) + allowedUsers( + ExampleUsers.AUTHOR, + ExampleUsers.SUPER_AUTHOR + ) + }), + EDIT_ASSET({ + paths( + "/content/dam/my-product/screens", + "/content/dam/my-product/images" + ) + deniedUsers( + ExampleUsers.USER, + ExampleUsers.AUTHOR + ) + }), + REMOVE_ASSET({ + paths( + "/content/dam/my-product/screens", + "/content/dam/my-product/images" + ) + deniedUsers( + ExampleUsers.USER, + ExampleUsers.AUTHOR + ) + }), + ADD_PAGE({ + paths( + "/content/my-site/en_gl/home" + ) + allowedUsers( + ExampleUsers.AUTHOR, + ExampleUsers.SUPER_AUTHOR + ) + }), + EDIT_PAGE({ + paths( + "/content/my-site/en_gl/home" + ) + allowedUsers( + ExampleUsers.USER, + ExampleUsers.AUTHOR, + ExampleUsers.SUPER_AUTHOR + ) + }), + EDIT_PAGE_PROPERTIES({ + paths( + "/content/my-site/en_gl/home" + ) + allowedUsers( + ExampleUsers.USER, + ExampleUsers.AUTHOR, + ExampleUsers.SUPER_AUTHOR + ) + }), + OPEN_PAGE({ + paths( + "/content/my-site/en_gl/home" + ) + allowedUsers( + *ExampleUsers.values() + ) + }), ; + + override fun toTestCaseConfiguration(): TestCaseConfiguration { + val testCaseConfiguration = TestCaseConfiguration().apply(initConfig) + testCaseConfiguration.allUsers(ExampleUsers.values()) + return testCaseConfiguration + } +} \ No newline at end of file diff --git a/core/src/test/kotlin/com/cognifide/apmt/tests/ExampleUsers.kt b/core/src/test/kotlin/com/cognifide/apmt/tests/ExampleUsers.kt new file mode 100644 index 0000000..80bfa05 --- /dev/null +++ b/core/src/test/kotlin/com/cognifide/apmt/tests/ExampleUsers.kt @@ -0,0 +1,12 @@ +package com.cognifide.apmt.tests + +import com.cognifide.apmt.User + +enum class ExampleUsers( + override val username: String, + override val password: String +) : User { + USER("user", "password"), + AUTHOR("author", "password"), + SUPER_AUTHOR("super-author", "password") +} diff --git a/core/src/test/kotlin/com/cognifide/apmt/tests/asset/ExampleCreateAssetTest.kt b/core/src/test/kotlin/com/cognifide/apmt/tests/asset/ExampleCreateAssetTest.kt new file mode 100644 index 0000000..0c26831 --- /dev/null +++ b/core/src/test/kotlin/com/cognifide/apmt/tests/asset/ExampleCreateAssetTest.kt @@ -0,0 +1,33 @@ +package com.cognifide.apmt.tests.asset + +import com.cognifide.apmt.actions.CSRF_TOKEN +import com.cognifide.apmt.tests.ExampleTestCases +import com.cognifide.apmt.tests.ExampleUsers +import com.cognifide.apmt.util.AemStub +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUser +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUsers +import com.github.tomakehurst.wiremock.client.WireMock.* +import org.junit.jupiter.api.BeforeEach + +@AemStub +class ExampleCreateAssetTest : CreateAssetTest( + ExampleTestCases.ADD_ASSET +) { + + @BeforeEach + fun beforeEach() { + registerUser("admin", "admin") + registerUsers(*ExampleUsers.values()) + + stubFor( + post(urlPathMatching("/content/dam/my-product/(images|screens)")) + .willReturn(aResponse().withStatus(201)) + ) + stubFor( + post(urlPathMatching("/content/dam/my-product/(images|screens)")) + .withHeader(CSRF_TOKEN, equalTo(ExampleUsers.USER.username)) + .willReturn(aResponse().withStatus(500)) + ) + } +} + diff --git a/core/src/test/kotlin/com/cognifide/apmt/tests/asset/ExampleEditAssetTest.kt b/core/src/test/kotlin/com/cognifide/apmt/tests/asset/ExampleEditAssetTest.kt new file mode 100644 index 0000000..a5d4886 --- /dev/null +++ b/core/src/test/kotlin/com/cognifide/apmt/tests/asset/ExampleEditAssetTest.kt @@ -0,0 +1,32 @@ +package com.cognifide.apmt.tests.asset + +import com.cognifide.apmt.actions.CSRF_TOKEN +import com.cognifide.apmt.tests.ExampleTestCases +import com.cognifide.apmt.tests.ExampleUsers +import com.cognifide.apmt.util.AemStub +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUser +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUsers +import com.github.tomakehurst.wiremock.client.WireMock.* +import org.junit.jupiter.api.BeforeEach + +@AemStub +class ExampleEditAssetTest : EditAssetTest( + ExampleTestCases.EDIT_ASSET +) { + + @BeforeEach + fun beforeEach() { + registerUser("admin", "admin") + registerUsers(*ExampleUsers.values()) + + stubFor( + post(urlPathMatching("/content/dam/my-product/(images|screens)")) + .willReturn(aResponse().withStatus(500)) + ) + stubFor( + post(urlPathMatching("/content/dam/my-product/(images|screens)")) + .withHeader(CSRF_TOKEN, equalTo(ExampleUsers.SUPER_AUTHOR.username)) + .willReturn(aResponse().withStatus(200)) + ) + } +} \ No newline at end of file diff --git a/core/src/test/kotlin/com/cognifide/apmt/tests/asset/ExampleRemoveAssetTest.kt b/core/src/test/kotlin/com/cognifide/apmt/tests/asset/ExampleRemoveAssetTest.kt new file mode 100644 index 0000000..edc7d49 --- /dev/null +++ b/core/src/test/kotlin/com/cognifide/apmt/tests/asset/ExampleRemoveAssetTest.kt @@ -0,0 +1,37 @@ +package com.cognifide.apmt.tests.asset + +import com.cognifide.apmt.actions.CSRF_TOKEN +import com.cognifide.apmt.tests.ExampleTestCases +import com.cognifide.apmt.tests.ExampleUsers +import com.cognifide.apmt.util.AemStub +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUser +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUsers +import com.github.tomakehurst.wiremock.client.WireMock.* +import org.junit.jupiter.api.BeforeEach + +@AemStub +class ExampleRemoveAssetTest : RemoveAssetTest( + ExampleTestCases.REMOVE_ASSET +) { + + @BeforeEach + fun beforeEach() { + registerUser("admin", "admin") + registerUsers(*ExampleUsers.values()) + + stubFor( + delete(urlPathMatching("/content/dam/my-product/(images|screens)")) + .willReturn(aResponse().withStatus(403)) + ) + stubFor( + post(urlPathMatching("/content/dam/my-product/(images|screens)")) + .withHeader(CSRF_TOKEN, equalTo("admin")) + .willReturn(aResponse().withStatus(201)) + ) + stubFor( + delete(urlPathMatching("/content/dam/my-product/(images|screens)")) + .withHeader(CSRF_TOKEN, equalTo(ExampleUsers.SUPER_AUTHOR.username)) + .willReturn(aResponse().withStatus(204)) + ) + } +} \ No newline at end of file diff --git a/core/src/test/kotlin/com/cognifide/apmt/tests/page/ExampleCreatePageTest.kt b/core/src/test/kotlin/com/cognifide/apmt/tests/page/ExampleCreatePageTest.kt new file mode 100644 index 0000000..4b1342c --- /dev/null +++ b/core/src/test/kotlin/com/cognifide/apmt/tests/page/ExampleCreatePageTest.kt @@ -0,0 +1,40 @@ +package com.cognifide.apmt.tests.page + +import com.cognifide.apmt.actions.CSRF_TOKEN +import com.cognifide.apmt.tests.ExampleTestCases +import com.cognifide.apmt.tests.ExampleUsers +import com.cognifide.apmt.util.AemStub +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUser +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUsers +import com.github.tomakehurst.wiremock.client.WireMock.* +import org.junit.jupiter.api.BeforeEach + +@AemStub +class ExampleCreatePageTest : CreatePageTest( + ExampleTestCases.ADD_PAGE, + pageContent = { + jcrTitle = "Example Page" + slingResourceType = "apmt/components/testPage" + cqTemplate = "apmt/templates/testPage" + + "apmtType" set "apmtTestPage" + } +) { + + @BeforeEach + fun beforeEach() { + registerUser("admin", "admin") + registerUsers(*ExampleUsers.values()) + + stubFor( + post(urlPathMatching("/content/my-site/en_gl/home")) + .willReturn(aResponse().withStatus(201)) + ) + stubFor( + post(urlPathMatching("/content/my-site/en_gl/home")) + .withHeader(CSRF_TOKEN, equalTo(ExampleUsers.USER.username)) + .willReturn(aResponse().withStatus(500)) + ) + } +} + diff --git a/core/src/test/kotlin/com/cognifide/apmt/tests/page/ExampleEditPagePropertiesTest.kt b/core/src/test/kotlin/com/cognifide/apmt/tests/page/ExampleEditPagePropertiesTest.kt new file mode 100644 index 0000000..196cde1 --- /dev/null +++ b/core/src/test/kotlin/com/cognifide/apmt/tests/page/ExampleEditPagePropertiesTest.kt @@ -0,0 +1,32 @@ +package com.cognifide.apmt.tests.page + +import com.cognifide.apmt.actions.CSRF_TOKEN +import com.cognifide.apmt.tests.ExampleTestCases +import com.cognifide.apmt.tests.ExampleUsers +import com.cognifide.apmt.util.AemStub +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUser +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUsers +import com.github.tomakehurst.wiremock.client.WireMock.* +import org.junit.jupiter.api.BeforeEach + +@AemStub +class ExampleEditPagePropertiesTest : EditPagePropertiesTest( + ExampleTestCases.EDIT_PAGE_PROPERTIES +) { + + @BeforeEach + fun beforeEach() { + registerUser("admin", "admin") + registerUsers(*ExampleUsers.values()) + + stubFor( + post(urlPathEqualTo("/content/my-site/en_gl/home/jcr%3Acontent")) + .willReturn(aResponse().withStatus(200)) + ) + stubFor( + post(urlPathEqualTo("/content/my-site/en_gl/home")) + .withHeader(CSRF_TOKEN, equalTo("admin")) + .willReturn(aResponse().withStatus(201)) + ) + } +} \ No newline at end of file diff --git a/core/src/test/kotlin/com/cognifide/apmt/tests/page/ExampleEditPageTest.kt b/core/src/test/kotlin/com/cognifide/apmt/tests/page/ExampleEditPageTest.kt new file mode 100644 index 0000000..3ee49bb --- /dev/null +++ b/core/src/test/kotlin/com/cognifide/apmt/tests/page/ExampleEditPageTest.kt @@ -0,0 +1,34 @@ +package com.cognifide.apmt.tests.page + +import com.cognifide.apmt.tests.ExampleTestCases +import com.cognifide.apmt.tests.ExampleUsers +import com.cognifide.apmt.util.AemStub +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUser +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUsers +import com.github.tomakehurst.wiremock.client.WireMock.* +import org.junit.jupiter.api.BeforeEach + +@AemStub +class ExampleEditPageTest : EditPageTest( + ExampleTestCases.EDIT_PAGE +) { + + @BeforeEach + fun beforeEach() { + registerUser("admin", "admin") + registerUsers(*ExampleUsers.values()) + + stubFor( + post(urlPathEqualTo("/content/my-site/en_gl/home")) + .withHeader("apmt-header1", equalTo("apmt-value1")) + .withHeader("apmt-header2", equalTo("apmt-value2")) + .willReturn(aResponse().withStatus(200)) + ) + stubFor( + post(urlPathEqualTo("/content/my-site/en_gl/home/jcr%3Acontent")) + .withHeader("apmt-header1", equalTo("apmt-value1")) + .withHeader("apmt-header2", equalTo("apmt-value2")) + .willReturn(aResponse().withStatus(200)) + ) + } +} \ No newline at end of file diff --git a/core/src/test/kotlin/com/cognifide/apmt/tests/page/ExampleOpenPageTest.kt b/core/src/test/kotlin/com/cognifide/apmt/tests/page/ExampleOpenPageTest.kt new file mode 100644 index 0000000..de5ec39 --- /dev/null +++ b/core/src/test/kotlin/com/cognifide/apmt/tests/page/ExampleOpenPageTest.kt @@ -0,0 +1,30 @@ +package com.cognifide.apmt.tests.page + +import com.cognifide.apmt.config.ConfigurationProvider +import com.cognifide.apmt.tests.ExampleTestCases +import com.cognifide.apmt.tests.ExampleUsers +import com.cognifide.apmt.util.AemStub +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUser +import com.cognifide.apmt.util.AemStubExtension.Companion.registerUsers +import com.github.tomakehurst.wiremock.client.WireMock.* +import org.junit.jupiter.api.BeforeEach + +@AemStub +class ExampleOpenPageTest : OpenPageTest( + ExampleTestCases.OPEN_PAGE, + instance = ConfigurationProvider.publishInstance +) { + + @BeforeEach + fun beforeEach() { + registerUser("admin", "admin") + registerUsers(*ExampleUsers.values()) + + stubFor( + get(urlPathEqualTo("/content/my-site/en_gl/home")) + .withHeader("apmt-header1", equalTo("apmt-value1")) + .withHeader("apmt-header2", equalTo("apmt-value2")) + .willReturn(aResponse().withStatus(200)) + ) + } +} \ No newline at end of file diff --git a/core/src/test/kotlin/com/cognifide/apmt/util/AemStubExtension.kt b/core/src/test/kotlin/com/cognifide/apmt/util/AemStubExtension.kt index df4f65e..188ff5c 100644 --- a/core/src/test/kotlin/com/cognifide/apmt/util/AemStubExtension.kt +++ b/core/src/test/kotlin/com/cognifide/apmt/util/AemStubExtension.kt @@ -1,24 +1,33 @@ package com.cognifide.apmt.util -import com.cognifide.apmt.TEST_USER +import com.cognifide.apmt.User import com.cognifide.apmt.actions.CSRF_ENDPOINT -import com.github.tomakehurst.wiremock.client.WireMock +import com.github.tomakehurst.wiremock.client.WireMock.* import org.junit.jupiter.api.extension.AfterEachCallback import org.junit.jupiter.api.extension.BeforeEachCallback import org.junit.jupiter.api.extension.ExtensionContext class AemStubExtension : BeforeEachCallback, AfterEachCallback { + override fun beforeEach(context: ExtensionContext?) { getWireMockServer(context).start() - - WireMock.stubFor( - WireMock.get(WireMock.urlPathEqualTo(CSRF_ENDPOINT)) - .withBasicAuth(TEST_USER.username, TEST_USER.password) - .willReturn(WireMock.okJson("{ \"token\": \"CORRECT_TOKEN\" }")) - ) } override fun afterEach(context: ExtensionContext?) { getWireMockServer(context).stop() } + + companion object { + fun registerUser(username: String, password: String) { + stubFor( + get(urlPathEqualTo(CSRF_ENDPOINT)) + .withBasicAuth(username, password) + .willReturn(okJson("{ \"token\": \"$username\" }")) + ) + } + + fun registerUsers(vararg users: User) { + users.forEach { registerUser(it.username, it.password) } + } + } } diff --git a/core/src/test/resources/apmt.yaml b/core/src/test/resources/apmt.yaml new file mode 100644 index 0000000..fc30a52 --- /dev/null +++ b/core/src/test/resources/apmt.yaml @@ -0,0 +1,18 @@ +# Instance configuration +--- +admin: + username: admin + password: admin +instances: + author: + name: author@local + url: http://localhost:8080 + headers: + apmt-header1: apmt-value1 + apmt-header2: apmt-value2 + publish: + name: publish@local + url: http://localhost:8080 + headers: + apmt-header1: apmt-value1 + apmt-header2: apmt-value2