Skip to content

Commit

Permalink
refactor: Simplify Value create code and use ProjectIri instead of St…
Browse files Browse the repository at this point in the history
…ring (#3378)
  • Loading branch information
seakayone authored Oct 2, 2024
1 parent 49b8259 commit 382826f
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ class PermissionsResponderSpec extends CoreSpec with ImplicitSender {
"return the default object access permissions 'string' for the 'knora-base:LinkObj' resource class (system resource class)" in {
val actual = UnsafeZioRun.runOrThrow(
permissionsResponder(
_.getDefaultResourcePermissions(
_.newResourceDefaultObjectAccessPermissions(
ProjectIri.unsafeFrom(SharedTestDataADM.incunabulaProjectIri),
OntologyConstants.KnoraBase.LinkObj.toSmartIri,
SharedTestDataADM.incunabulaProjectAdminUser,
Expand All @@ -541,26 +541,29 @@ class PermissionsResponderSpec extends CoreSpec with ImplicitSender {
}

"return the default object access permissions 'string' for the 'knora-base:hasStillImageFileValue' property (system property)" in {
UnsafeZioRun
val actual = UnsafeZioRun
.runOrThrow(
permissionsResponder(
_.getDefaultValuePermissions(
projectIri = SharedTestDataADM.incunabulaProjectIri,
resourceClassIri = stringFormatter.toSmartIri(OntologyConstants.KnoraBase.StillImageRepresentation),
propertyIri = stringFormatter.toSmartIri(OntologyConstants.KnoraBase.HasStillImageFileValue),
targetUser = SharedTestDataADM.incunabulaProjectAdminUser,
_.newValueDefaultObjectAccessPermissions(
ProjectIri.unsafeFrom(SharedTestDataADM.incunabulaProjectIri),
stringFormatter.toSmartIri(OntologyConstants.KnoraBase.StillImageRepresentation),
stringFormatter.toSmartIri(OntologyConstants.KnoraBase.HasStillImageFileValue),
SharedTestDataADM.incunabulaProjectAdminUser,
),
),
)
.shouldEqual(

assert(
actual == DefaultObjectAccessPermissionsStringResponseADM(
"M knora-admin:Creator,knora-admin:ProjectMember|V knora-admin:KnownUser,knora-admin:UnknownUser",
)
),
)
}

"return the default object access permissions 'string' for the 'incunabula:book' resource class (project resource class)" in {
val actual = UnsafeZioRun.runOrThrow(
permissionsResponder(
_.getDefaultResourcePermissions(
_.newResourceDefaultObjectAccessPermissions(
ProjectIri.unsafeFrom(SharedTestDataADM.incunabulaProjectIri),
SharedOntologyTestDataADM.INCUNABULA_BOOK_RESOURCE_CLASS.toSmartIri,
SharedTestDataADM.incunabulaProjectAdminUser,
Expand All @@ -577,7 +580,7 @@ class PermissionsResponderSpec extends CoreSpec with ImplicitSender {
"return the default object access permissions 'string' for the 'incunabula:page' resource class (project resource class)" in {
val actual = UnsafeZioRun.runOrThrow(
permissionsResponder(
_.getDefaultResourcePermissions(
_.newResourceDefaultObjectAccessPermissions(
ProjectIri.unsafeFrom(SharedTestDataADM.incunabulaProjectIri),
SharedOntologyTestDataADM.INCUNABULA_PAGE_RESOURCE_CLASS.toSmartIri,
SharedTestDataADM.incunabulaProjectAdminUser,
Expand All @@ -592,26 +595,28 @@ class PermissionsResponderSpec extends CoreSpec with ImplicitSender {
}

"return the default object access permissions 'string' for the 'anything:hasInterval' property" in {
UnsafeZioRun
.runOrThrow(
permissionsResponder(
_.getDefaultValuePermissions(
projectIri = SharedTestDataADM.anythingProjectIri,
resourceClassIri = stringFormatter.toSmartIri("http://www.knora.org/ontology/0001/anything#Thing"),
propertyIri = stringFormatter.toSmartIri("http://www.knora.org/ontology/0001/anything#hasInterval"),
targetUser = SharedTestDataADM.anythingUser2,
),
val actual = UnsafeZioRun.runOrThrow(
permissionsResponder(
_.newValueDefaultObjectAccessPermissions(
ProjectIri.unsafeFrom(SharedTestDataADM.anythingProjectIri),
stringFormatter.toSmartIri("http://www.knora.org/ontology/0001/anything#Thing"),
stringFormatter.toSmartIri("http://www.knora.org/ontology/0001/anything#hasInterval"),
SharedTestDataADM.anythingUser2,
),
)
.shouldEqual(
),
)

assert(
actual == DefaultObjectAccessPermissionsStringResponseADM(
"CR knora-admin:Creator|M knora-admin:ProjectMember|V knora-admin:KnownUser|RV knora-admin:UnknownUser",
)
),
)
}

"return the default object access permissions 'string' for the 'anything:Thing' class" in {
val actual = UnsafeZioRun.runOrThrow(
permissionsResponder(
_.getDefaultResourcePermissions(
_.newResourceDefaultObjectAccessPermissions(
ProjectIri.unsafeFrom(SharedTestDataADM.anythingProjectIri),
"http://www.knora.org/ontology/0001/anything#Thing".toSmartIri,
SharedTestDataADM.anythingUser2,
Expand All @@ -626,102 +631,90 @@ class PermissionsResponderSpec extends CoreSpec with ImplicitSender {
}

"return the default object access permissions 'string' for the 'anything:Thing' class and 'anything:hasText' property" in {
UnsafeZioRun
val actual = UnsafeZioRun
.runOrThrow(
permissionsResponder(
_.getDefaultValuePermissions(
projectIri = SharedTestDataADM.anythingProjectIri,
_.newValueDefaultObjectAccessPermissions(
projectIri = ProjectIri.unsafeFrom(SharedTestDataADM.anythingProjectIri),
resourceClassIri = stringFormatter.toSmartIri("http://www.knora.org/ontology/0001/anything#Thing"),
propertyIri = stringFormatter.toSmartIri("http://www.knora.org/ontology/0001/anything#hasText"),
targetUser = SharedTestDataADM.anythingUser1,
),
),
)
.shouldEqual("CR knora-admin:Creator")
actual.permissionLiteral shouldEqual "CR knora-admin:Creator"
}

"return the default object access permissions 'string' for the 'images:Bild' class and 'anything:hasText' property" in {
UnsafeZioRun
val actual = UnsafeZioRun
.runOrThrow(
permissionsResponder(
_.getDefaultValuePermissions(
projectIri = SharedTestDataADM.anythingProjectIri,
resourceClassIri = stringFormatter.toSmartIri(s"${SharedOntologyTestDataADM.IMAGES_ONTOLOGY_IRI}#bild"),
propertyIri = stringFormatter.toSmartIri("http://www.knora.org/ontology/0001/anything#hasText"),
targetUser = SharedTestDataADM.anythingUser2,
_.newValueDefaultObjectAccessPermissions(
ProjectIri.unsafeFrom(SharedTestDataADM.anythingProjectIri),
stringFormatter.toSmartIri(s"${SharedOntologyTestDataADM.IMAGES_ONTOLOGY_IRI}#bild"),
stringFormatter.toSmartIri("http://www.knora.org/ontology/0001/anything#hasText"),
SharedTestDataADM.anythingUser2,
),
),
)
.shouldEqual(
"CR knora-admin:Creator|M knora-admin:ProjectMember|V knora-admin:KnownUser|RV knora-admin:UnknownUser",
)
}

"return 'BadRequest' if the supplied project IRI DefaultObjectAccessPermissionsStringForPropertyGetADM is not valid" in {
val projectIri = ""
UnsafeZioRun
.runOrThrow(
permissionsResponder(
_.getDefaultValuePermissions(
projectIri = projectIri,
resourceClassIri = stringFormatter.toSmartIri(SharedOntologyTestDataADM.IMAGES_BILD_RESOURCE_CLASS),
propertyIri = stringFormatter.toSmartIri(SharedOntologyTestDataADM.IMAGES_TITEL_PROPERTY),
targetUser = SharedTestDataADM.imagesUser02,
),
).flip.map(_.getMessage),
)
.shouldEqual(s"Invalid project IRI $projectIri")
assert(
actual == DefaultObjectAccessPermissionsStringResponseADM(
"CR knora-admin:Creator|M knora-admin:ProjectMember|V knora-admin:KnownUser|RV knora-admin:UnknownUser",
),
)
}

"return 'BadRequest' if the supplied resourceClass IRI for DefaultObjectAccessPermissionsStringForPropertyGetADM is not valid" in {
UnsafeZioRun
.runOrThrow(
val exit = UnsafeZioRun
.run(
permissionsResponder(
_.getDefaultValuePermissions(
projectIri = SharedTestDataADM.imagesProjectIri,
resourceClassIri = stringFormatter.toSmartIri(SharedTestDataADM.customResourceIRI),
propertyIri = stringFormatter.toSmartIri(SharedOntologyTestDataADM.IMAGES_TITEL_PROPERTY),
targetUser = SharedTestDataADM.imagesReviewerUser,
_.newValueDefaultObjectAccessPermissions(
ProjectIri.unsafeFrom(SharedTestDataADM.imagesProjectIri),
stringFormatter.toSmartIri(SharedTestDataADM.customResourceIRI),
stringFormatter.toSmartIri(SharedOntologyTestDataADM.IMAGES_TITEL_PROPERTY),
SharedTestDataADM.imagesReviewerUser,
),
).flip.map(_.getMessage),
),
)
.shouldEqual(s"Invalid resource class IRI: ${SharedTestDataADM.customResourceIRI}")
assertFailsWithA[BadRequestException](
exit,
s"Invalid resource class IRI: ${SharedTestDataADM.customResourceIRI}",
)
}

"return 'BadRequest' if the supplied property IRI for DefaultObjectAccessPermissionsStringForPropertyGetADM is not valid" in {
UnsafeZioRun
.runOrThrow(
permissionsResponder(
_.getDefaultValuePermissions(
projectIri = SharedTestDataADM.imagesProjectIri,
resourceClassIri = stringFormatter.toSmartIri(SharedOntologyTestDataADM.IMAGES_BILD_RESOURCE_CLASS),
propertyIri = stringFormatter.toSmartIri(SharedTestDataADM.customValueIRI),
targetUser = SharedTestDataADM.imagesReviewerUser,
),
).flip.map(_.getMessage),
)
.shouldEqual(s"Invalid property IRI: ${SharedTestDataADM.customValueIRI}")
val exit = UnsafeZioRun.run(
permissionsResponder(
_.newValueDefaultObjectAccessPermissions(
ProjectIri.unsafeFrom(SharedTestDataADM.imagesProjectIri),
stringFormatter.toSmartIri(SharedOntologyTestDataADM.IMAGES_BILD_RESOURCE_CLASS),
stringFormatter.toSmartIri(SharedTestDataADM.customValueIRI),
SharedTestDataADM.imagesReviewerUser,
),
),
)
assertFailsWithA[BadRequestException](exit, s"Invalid property IRI: ${SharedTestDataADM.customValueIRI}")
}

"return 'BadRequest' if the target user of DefaultObjectAccessPermissionsStringForPropertyGetADM is an Anonymous user" in {
UnsafeZioRun
.runOrThrow(
permissionsResponder(
_.getDefaultValuePermissions(
projectIri = SharedTestDataADM.imagesProjectIri,
resourceClassIri = stringFormatter.toSmartIri(SharedOntologyTestDataADM.IMAGES_BILD_RESOURCE_CLASS),
propertyIri = stringFormatter.toSmartIri(SharedOntologyTestDataADM.IMAGES_TITEL_PROPERTY),
targetUser = SharedTestDataADM.anonymousUser,
),
).flip.map(_.getMessage),
)
.shouldEqual("Anonymous Users are not allowed.")
val exit = UnsafeZioRun.run(
permissionsResponder(
_.newValueDefaultObjectAccessPermissions(
ProjectIri.unsafeFrom(SharedTestDataADM.imagesProjectIri),
stringFormatter.toSmartIri(SharedOntologyTestDataADM.IMAGES_BILD_RESOURCE_CLASS),
stringFormatter.toSmartIri(SharedOntologyTestDataADM.IMAGES_TITEL_PROPERTY),
SharedTestDataADM.anonymousUser,
),
),
)
assertFailsWithA[BadRequestException](exit, "Anonymous Users are not allowed.")
}

"return the default object access permissions 'string' for the 'anything:Thing' resource class for the root user (system admin and not member of project)" in {
val actual = UnsafeZioRun.runOrThrow(
permissionsResponder(
_.getDefaultResourcePermissions(
_.newResourceDefaultObjectAccessPermissions(
ProjectIri.unsafeFrom(SharedTestDataADM.anythingProjectIri),
"http://www.knora.org/ontology/0001/anything#Thing".toSmartIri,
SharedTestDataADM.rootUser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package org.knora.webapi.responders.v2
import org.apache.pekko.actor.Status.Failure
import org.apache.pekko.pattern.ask
import org.apache.pekko.testkit.ImplicitSender
import org.scalatest.Assertion
import zio.ZIO

import java.time.Instant
Expand Down
Loading

0 comments on commit 382826f

Please sign in to comment.