Skip to content

Commit

Permalink
#7 - added approve/reject handlers for rank name/level replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
hohonuuli committed Feb 7, 2025
1 parent 468c79b commit fc13221
Show file tree
Hide file tree
Showing 60 changed files with 617 additions and 418 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ trait AuthorizationEndpointsSuite extends DatabaseFunSuite with EndpointsSuite:
.send(backendStub)
.join


response.body match
case Left(e) => fail(e)
case Right(body) =>
Expand Down Expand Up @@ -112,6 +111,6 @@ trait AuthorizationEndpointsSuite extends DatabaseFunSuite with EndpointsSuite:

response.body match
case Left(e) =>
// this is expected. READONLY users cannot login
// this is expected. READONLY users cannot login
case Right(body) =>
fail("READONLY user should not be able to login")
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ trait ConceptEndpointsSuite extends EndpointsSuite with DataInitializer with Use
runGet(
endpoints.findParentEndpointImpl,
s"http://test.com/v1/concept/parent/${name}",
response =>
assertEquals(response.code, StatusCode.NotFound)
response => assertEquals(response.code, StatusCode.NotFound)
// val obtained = checkResponse[ConceptMetadata](response.body)
// assertEquals(obtained.name, root.getPrimaryConceptName.getName)
)
Expand Down Expand Up @@ -126,12 +125,10 @@ trait ConceptEndpointsSuite extends EndpointsSuite with DataInitializer with Use
runGet(
endpoints.findByNameImpl,
s"http://test.com/v1/concept/${name}",
response =>
assertEquals(response.code, StatusCode.NotFound)
response => assertEquals(response.code, StatusCode.NotFound)
)
}


test("findByNameContaining") {
val root = init(2, 0)
val child = root.getChildConcepts.iterator().next()
Expand Down Expand Up @@ -168,10 +165,10 @@ trait ConceptEndpointsSuite extends EndpointsSuite with DataInitializer with Use

val attempt = testWithUserAuth(
user =>
val root = init(2, 0)
val name = root.getPrimaryConceptName.getName
val root = init(2, 0)
val name = root.getPrimaryConceptName.getName
val (rankLevel, rankName) = TestEntityFactory.randomRankLevelAndName()
val expectedRank = Some(s"${{rankLevel.getOrElse("")}}${{rankName.getOrElse("")}}")
val expectedRank = Some(s"${{ rankLevel.getOrElse("") }}${{ rankName.getOrElse("") }}")

val conceptCreate = ConceptCreate(
"SomeChildConcept",
Expand Down Expand Up @@ -207,11 +204,11 @@ trait ConceptEndpointsSuite extends EndpointsSuite with DataInitializer with Use

val attempt = testWithUserAuth(
user =>
val root = init(3, 0)
val grandChild = root.getChildConcepts.iterator().next().getChildConcepts.iterator().next()
val root = init(3, 0)
val grandChild = root.getChildConcepts.iterator().next().getChildConcepts.iterator().next()
val (rankLevel, rankName) = TestEntityFactory.randomRankLevelAndName()
val expectedRank = Some(s"${{rankLevel.getOrElse("")}}${{rankName.getOrElse("")}}")
val conceptUpdate = ConceptUpdate(
val expectedRank = Some(s"${{ rankLevel.getOrElse("") }}${{ rankName.getOrElse("") }}")
val conceptUpdate = ConceptUpdate(
Some(root.getPrimaryConceptName.getName),
rankLevel = rankLevel,
rankName = rankName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import scala.concurrent.{ExecutionContext, Future}

trait EndpointsSuite extends munit.FunSuite:

given ExecutionContext = ExecutionContext.global
given ExecutionContext = ExecutionContext.global
private val log: System.Logger = System.getLogger(getClass.getName)

def runDelete(
Expand Down Expand Up @@ -97,7 +97,6 @@ trait EndpointsSuite extends munit.FunSuite:
.auth
.bearer(bearer)


val response = request.send(backendStub).join
assertions(response)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@

package org.mbari.oni.endpoints

import org.mbari.oni.domain.{ExtendedLink, ILink, LinkCreate, LinkRenameToConceptRequest, LinkRenameToConceptResponse, LinkUpdate}
import org.mbari.oni.domain.{
ExtendedLink,
ILink,
LinkCreate,
LinkRenameToConceptRequest,
LinkRenameToConceptResponse,
LinkUpdate
}
import org.mbari.oni.etc.jwt.JwtService
import org.mbari.oni.jpa.DataInitializer
import org.mbari.oni.services.UserAuthMixin
Expand Down Expand Up @@ -104,13 +111,13 @@ trait LinkTemplateEndpointsSuite extends EndpointsSuite with DataInitializer wit
}

test("renameToConcept") {
val root = init(3, 10)
val descendants = root.getDescendants.asScala
val root = init(3, 10)
val descendants = root.getDescendants.asScala
val allLinkTemplates = descendants.flatMap(_.getConceptMetadata.getLinkTemplates.asScala).toSeq
val link = allLinkTemplates.head
val request = LinkRenameToConceptRequest(link.getToConcept, Strings.random(10))
val link = allLinkTemplates.head
val request = LinkRenameToConceptRequest(link.getToConcept, Strings.random(10))
// log.atError.log(request.stringify)
val attempt = testWithUserAuth(
val attempt = testWithUserAuth(
user =>
runPut(
endpoints.renameToConceptImpl,
Expand Down
51 changes: 21 additions & 30 deletions it/src/main/scala/org/mbari/oni/endpoints/MediaEndpointsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,27 @@ import org.mbari.oni.etc.circe.CirceCodecs.{*, given}
import java.net.URI
import scala.jdk.CollectionConverters.*

trait MediaEndpointsSuite extends EndpointsSuite with DataInitializer with UserAuthMixin {
trait MediaEndpointsSuite extends EndpointsSuite with DataInitializer with UserAuthMixin:

given jwtService: JwtService = JwtService("mbari", "foo", "bar")
lazy val fastPhylogenyService = FastPhylogenyService(entityManagerFactory)
given jwtService: JwtService = JwtService("mbari", "foo", "bar")
lazy val fastPhylogenyService = FastPhylogenyService(entityManagerFactory)
lazy val endpoints: MediaEndpoints = MediaEndpoints(entityManagerFactory, fastPhylogenyService)
private val password = Strings.random(10)
private val password = Strings.random(10)

def createMedia(): Seq[Media] = {
def createMedia(): Seq[Media] =
val root = init(1, 6)
root.getDescendants
.asScala
.flatMap(_.getConceptMetadata.getMedias.asScala)
.toSeq
.map(Media.from)
.sortBy(_.url.toExternalForm)
}

test("mediaForConcept") {
val expected = createMedia()
val opt = expected.head.conceptName
val opt = expected.head.conceptName
assert(opt.isDefined)
val name = opt.get
val name = opt.get
runGet(
endpoints.mediaForConceptEndpointImpl,
s"http://test.com/v1/media/${name}",
Expand All @@ -62,7 +61,7 @@ trait MediaEndpointsSuite extends EndpointsSuite with DataInitializer with UserA
}

test("createMedia") {
val root = init(2, 0)
val root = init(2, 0)
assert(root != null)
val mediaCreate = MediaCreate(
conceptName = root.getName,
Expand All @@ -72,7 +71,7 @@ trait MediaEndpointsSuite extends EndpointsSuite with DataInitializer with UserA
mediaType = Some(MediaTypes.IMAGE.name),
isPrimary = Some(true)
)
val attempt = testWithUserAuth(
val attempt = testWithUserAuth(
user =>
runPost(
endpoints.createMediaEndpointImpl,
Expand All @@ -85,29 +84,29 @@ trait MediaEndpointsSuite extends EndpointsSuite with DataInitializer with UserA
assertEquals(mediaCreate.url, obtained.url)
assertEquals(mediaCreate.caption, obtained.caption)
assertEquals(mediaCreate.credit, obtained.credit)
val t = Media.resolveMimeType(mediaCreate.mediaType.getOrElse(""), obtained.url.toExternalForm)
val t = Media.resolveMimeType(mediaCreate.mediaType.getOrElse(""), obtained.url.toExternalForm)
assertEquals(t, obtained.mimeType)
assertEquals(mediaCreate.isPrimary.getOrElse(false), obtained.isPrimary)
,
jwt = jwtService.login(user.username, password, user.toEntity)
,
jwt = jwtService.login(user.username, password, user.toEntity)
),
password
)
attempt match
case Left(value) => fail(value.toString)
case Left(value) => fail(value.toString)
case Right(value) => assert(true)
}

test("updateMedia") {
val media = createMedia().head
val media = createMedia().head
val mediaUpdate = MediaUpdate(
url = Some(URI.create(s"http://www.mbari.org/${Strings.random(10)}.png").toURL),
caption = Some(Strings.random(1000)),
credit = Some(Strings.random(255)),
mediaType = Some(MediaTypes.IMAGE.name),
isPrimary = Some(true)
)
val attempt = testWithUserAuth(
val attempt = testWithUserAuth(
user =>
runPut(
endpoints.updateMediaEndpointImpl,
Expand All @@ -119,21 +118,21 @@ trait MediaEndpointsSuite extends EndpointsSuite with DataInitializer with UserA
assertEquals(mediaUpdate.url.orNull, obtained.url)
assertEquals(mediaUpdate.caption, obtained.caption)
assertEquals(mediaUpdate.credit, obtained.credit)
val t = Media.resolveMimeType(mediaUpdate.mediaType.getOrElse(""), obtained.url.toExternalForm)
val t = Media.resolveMimeType(mediaUpdate.mediaType.getOrElse(""), obtained.url.toExternalForm)
assertEquals(t, obtained.mimeType)
assertEquals(mediaUpdate.isPrimary.getOrElse(false), obtained.isPrimary)
,
jwt = jwtService.login(user.username, password, user.toEntity)
,
jwt = jwtService.login(user.username, password, user.toEntity)
),
password
)
attempt match
case Left(value) => fail(value.toString)
case Left(value) => fail(value.toString)
case Right(value) => assert(true)
}

test("deleteMedia") {
val media = createMedia().head
val media = createMedia().head
val attempt = testWithUserAuth(
user =>
runDelete(
Expand All @@ -145,14 +144,6 @@ trait MediaEndpointsSuite extends EndpointsSuite with DataInitializer with UserA
password
)
attempt match
case Left(value) => fail(value.toString)
case Left(value) => fail(value.toString)
case Right(value) => assert(true)
}







}
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ trait PrefNodeEndpointsSuite extends EndpointsSuite with DataInitializer with Us
}

test("updateEndpoint (query params with form body)") {
val nodes = createNodes(1)
val node = nodes.head
val newValue = "new_value_" + Strings.random(5)
val nodes = createNodes(1)
val node = nodes.head
val newValue = "new_value_" + Strings.random(5)
val updatedNode = PrefNodeUpdate(None, None, newValue)
testWithUserAuth(
user =>
Expand All @@ -224,9 +224,9 @@ trait PrefNodeEndpointsSuite extends EndpointsSuite with DataInitializer with Us
}

test("updateEndpoint (query params with json body)") {
val nodes = createNodes(1)
val node = nodes.head
val newValue = "new_value_" + Strings.random(5)
val nodes = createNodes(1)
val node = nodes.head
val newValue = "new_value_" + Strings.random(5)
val updatedNode = PrefNodeUpdate(None, None, newValue)
testWithUserAuth(
user =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ trait UserAccountsEndpointsSuite extends EndpointsSuite with DataInitializer:
val entity = TestEntityFactory.createUserAccount(UserAccountRoles.ADMINISTRATOR.getRoleName)
val userAccount = UserAccount.from(entity).copy(password = Strings.random(10))


runPost(
endpoints.createEndpointImpl,
"http://test.com/v1/users",
Expand All @@ -128,7 +127,6 @@ trait UserAccountsEndpointsSuite extends EndpointsSuite with DataInitializer:
val entity = TestEntityFactory.createUserAccount(UserAccountRoles.READONLY.getRoleName)
val userAccount = UserAccount.from(entity).copy(password = Strings.random(10))


runPost(
endpoints.createEndpointImpl,
"http://test.com/v1/users",
Expand All @@ -144,9 +142,9 @@ trait UserAccountsEndpointsSuite extends EndpointsSuite with DataInitializer:
}

test("createEndpoint (form camelCase)") {
val entity = TestEntityFactory.createUserAccount(UserAccountRoles.ADMINISTRATOR.getRoleName)
val entity = TestEntityFactory.createUserAccount(UserAccountRoles.ADMINISTRATOR.getRoleName)
val userAccount = UserAccount.from(entity).copy(password = Strings.random(10))
val formBody = userAccount.toFormBody
val formBody = userAccount.toFormBody

runPost(
endpoints.createEndpointImpl,
Expand All @@ -163,9 +161,9 @@ trait UserAccountsEndpointsSuite extends EndpointsSuite with DataInitializer:
}

test("createEndpoint (form snake_case)") {
val entity = TestEntityFactory.createUserAccount(UserAccountRoles.ADMINISTRATOR.getRoleName)
val entity = TestEntityFactory.createUserAccount(UserAccountRoles.ADMINISTRATOR.getRoleName)
val userAccount = UserAccount.from(entity).copy(password = Strings.random(10))
val formBody = userAccount.toFormBody.replace("firstName", "first_name").replace("lastName", "last_name")
val formBody = userAccount.toFormBody.replace("firstName", "first_name").replace("lastName", "last_name")

runPost(
endpoints.createEndpointImpl,
Expand Down
52 changes: 52 additions & 0 deletions it/src/main/scala/org/mbari/oni/issues/Oni7.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2024 Monterey Bay Aquarium Research Institute
*
* 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 org.mbari.oni.issues

import org.mbari.oni.endpoints.{EndpointsSuite, HistoryEndpoints}
import org.mbari.oni.etc.jwt.JwtService
import org.mbari.oni.jdbc.FastPhylogenyService
import org.mbari.oni.jpa.DataInitializer
import org.mbari.oni.services.{HistoryService, LinkTemplateService, UserAuthMixin}

trait Oni7 extends EndpointsSuite with DataInitializer with UserAuthMixin:

given jwtService: JwtService = JwtService("mbari", "foo", "bar")

lazy val fastPhylogenyService = new FastPhylogenyService(entityManagerFactory)
lazy val historyService = new HistoryService(entityManagerFactory)
lazy val historyEndpoints: HistoryEndpoints = HistoryEndpoints(entityManagerFactory, fastPhylogenyService)
private val password = "foofoofoo"

test("mbari-org/oni#7 - accept rank level replace") {
val root = init(2, 0)

}

test("mbari-org/oni#7 - reject rank level replace") {
val root = init(2, 0)

}

test("mbari-org/oni#7 - accept rank name replace") {
val root = init(2, 0)

}

test("mbari-org/oni#7 - reject rank name replace") {
val root = init(2, 0)

}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ object TestEntityFactory:
entity.setCitation(s)
entity


def randomRankLevelAndName(): (Option[String], Option[String]) =
val idx = random.nextInt(RankValidator.ValidRanks.size)
RankValidator.ValidRankLevelsAndNames(idx)
Loading

0 comments on commit fc13221

Please sign in to comment.