Skip to content

Commit

Permalink
Merge pull request #20 from Cognifide/feature/add-resource-unit-tests
Browse files Browse the repository at this point in the history
Fixed resource tests
  • Loading branch information
arkadiusz-rosiak authored Oct 13, 2020
2 parents 90ec22a + b4eb529 commit 72f5fcd
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ abstract class EditResourceTest(
@ParameterizedTest
@Allowed
fun userCanEditResources(user: User, path: String) {
addUndoAction(CreateResource(authorInstance, user, path, createdResource))
val createResource = CreateResource(authorInstance, ConfigurationProvider.apmtUser, path, createdResource)
addUndoAction(createResource)
createResource.execute()

EditResource(authorInstance, user, path, editedResource)
.execute()
Expand All @@ -40,7 +42,9 @@ abstract class EditResourceTest(
@ParameterizedTest
@Denied
fun userCannotEditResources(user: User, path: String) {
addUndoAction(CreateResource(authorInstance, user, path, createdResource))
val createResource = CreateResource(authorInstance, ConfigurationProvider.apmtUser, path, createdResource)
addUndoAction(createResource)
createResource.execute()

EditResource(authorInstance, user, path, editedResource)
.execute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class RemoveResourceTest(
.execute()
.then()
.assertThat()
.statusCode(200)
.statusCode(204)
}

@DisplayName("User cannot remove resources")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.cognifide.apmt.tests.resource

import com.cognifide.apmt.actions.CSRF_TOKEN
import com.cognifide.apmt.tests.APMT_TEXT
import com.cognifide.apmt.tests.ApmtUsers
import com.cognifide.apmt.tests.testCase
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 ApmtCreateResourceTest : CreateResourceTest(
testCase {
paths(
"/content/my-site/de_de/home/text"
)
allowedUsers(
ApmtUsers.AUTHOR,
ApmtUsers.SUPER_AUTHOR
)
},
resource = {
jcrPrimaryType = "nt:unstructured"
slingResourceType = APMT_TEXT
"value" set "Hello World!"
}
) {

@BeforeEach
fun beforeEach() {
registerUser("admin", "admin")
registerUsers(*ApmtUsers.values())

stubFor(
post(urlPathEqualTo("/content/my-site/de_de/home/text"))
.withHeader(CSRF_TOKEN, matching("author|super-author"))
.willReturn(aResponse().withStatus(201))
)

stubFor(
post(urlPathEqualTo("/content/my-site/de_de/home/text"))
.withHeader(CSRF_TOKEN, equalTo("user"))
.willReturn(aResponse().withStatus(500))
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.cognifide.apmt.tests.resource

import com.cognifide.apmt.actions.CSRF_TOKEN
import com.cognifide.apmt.tests.APMT_TEXT
import com.cognifide.apmt.tests.ApmtUsers
import com.cognifide.apmt.tests.testCase
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 ApmtEditResourceTest : EditResourceTest(
testCase {
paths(
"/content/my-site/de_de/home/text"
)
allowedUsers(
ApmtUsers.AUTHOR,
ApmtUsers.SUPER_AUTHOR
)
},
createdResource = {
jcrPrimaryType = "nt:unstructured"
slingResourceType = APMT_TEXT
"value" set "Hello World!"
},
editedResource = {
jcrPrimaryType = "nt:unstructured"
slingResourceType = APMT_TEXT
"value" set "Hello edited World!"
}
) {

@BeforeEach
fun beforeEach() {
registerUser("admin", "admin")
registerUsers(*ApmtUsers.values())

// apmt user creates resource first
stubFor(
post(urlPathEqualTo("/content/my-site/de_de/home/text"))
.withHeader(CSRF_TOKEN, equalTo("admin"))
.willReturn(aResponse().withStatus(201))
)

stubFor(
post(urlPathEqualTo("/content/my-site/de_de/home/text"))
.withHeader(CSRF_TOKEN, matching("author|super-author"))
.willReturn(aResponse().withStatus(200))
)

stubFor(
post(urlPathEqualTo("/content/my-site/de_de/home/text"))
.withHeader(CSRF_TOKEN, equalTo("user"))
.willReturn(aResponse().withStatus(500))
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.cognifide.apmt.tests.resource

import com.cognifide.apmt.actions.CSRF_TOKEN
import com.cognifide.apmt.tests.APMT_TEXT
import com.cognifide.apmt.tests.ApmtUsers
import com.cognifide.apmt.tests.testCase
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 ApmtRemoveResourceTest : RemoveResourceTest(
testCase {
paths(
"/content/my-site/de_de/home/text"
)
allowedUsers(
ApmtUsers.AUTHOR,
ApmtUsers.SUPER_AUTHOR
)
},
resource = {
jcrPrimaryType = "nt:unstructured"
slingResourceType = APMT_TEXT
"value" set "Hello World!"
}
) {
@BeforeEach
fun beforeEach() {
registerUser("admin", "admin")
registerUsers(*ApmtUsers.values())

// apmt user creates resource first
stubFor(
post(urlPathEqualTo("/content/my-site/de_de/home/text"))
.withHeader(CSRF_TOKEN, equalTo("admin"))
.willReturn(aResponse().withStatus(201))
)

stubFor(
delete(urlPathEqualTo("/content/my-site/de_de/home/text"))
.withHeader(CSRF_TOKEN, matching("author|super-author"))
.willReturn(aResponse().withStatus(204))
)

stubFor(
delete(urlPathEqualTo("/content/my-site/de_de/home/text"))
.withHeader(CSRF_TOKEN, equalTo("user"))
.willReturn(aResponse().withStatus(500))
)
}
}

0 comments on commit 72f5fcd

Please sign in to comment.