Skip to content

Commit

Permalink
Added test for login service
Browse files Browse the repository at this point in the history
  • Loading branch information
hohonuuli committed May 18, 2024
1 parent 01733c3 commit 590808d
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.mbari.oni.endpoints

import org.mbari.oni.SqlServerMixin

class SqlServerAuthorizationEndpointsSuite extends AuthorizationEndpointsSuite with SqlServerMixin {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) Monterey Bay Aquarium Research Institute 2024
*
* oni code is non-public software. Unauthorized copying of this file,
* via any medium is strictly prohibited. Proprietary and confidential.
*/

package org.mbari.oni.endpoints

import io.circe.parser.decode
import org.mbari.oni.domain.{Authorization, UserAccount, UserAccountRoles}
import org.mbari.oni.etc.circe.CirceCodecs.given
import org.mbari.oni.etc.jwt.JwtService
import org.mbari.oni.jpa.DatabaseFunSuite
import org.mbari.oni.jpa.entities.TestEntityFactory
import org.mbari.oni.services.UserAccountService
import sttp.client3.*
import sttp.client3.circe.*
import sttp.client3.testing.SttpBackendStub
import sttp.model.StatusCode
import sttp.tapir.server.interceptor.CustomiseInterceptors
import sttp.tapir.server.interceptor.exception.ExceptionHandler
import sttp.tapir.server.model.ValuedEndpointOutput
import sttp.tapir.server.nima.{Id, NimaServerOptions}
import sttp.tapir.server.stub.TapirStubInterpreter

import java.util.Base64


trait AuthorizationEndpointsSuite extends DatabaseFunSuite with EndpointsSuite:

given jwtService: JwtService = JwtService("mbari", "foo", "bar")
lazy val authorizationEndpoints = new AuthorizationEndpoints(entityManagerFactory)

test("auth"):

val backendStub = newBackendStub(authorizationEndpoints.authEndpointImpl)

val response = basicRequest
.post(uri"http://test.com/v1/auth")
.header("Authorization", "APIKEY foo")
.send(backendStub)

response.body match
case Left(e) => fail(e)
case Right(body) =>

assertEquals(response.code, StatusCode.Ok)
assert(response.body.isRight)

// println(body)
val d = decode[Authorization](body)
assert(d.isRight)
val bearerAuth = d.getOrElse(throw new Exception("No bearer auth"))
assert(jwtService.verify(bearerAuth.accessToken))

test("login"):
val userService = UserAccountService(entityManagerFactory)
val userAccount = UserAccount(
"test1234",
"SuperSecretPassword",
UserAccountRoles.ADMINISTRATOR.getRoleName,
isEncrypted = Some(false)
)
userService.create(userAccount) match
case Left(e) => fail(e.getMessage)
case Right(ua) =>

val backendStub = newBackendStub(authorizationEndpoints.loginEndpointImpl)

val credentials = Base64.getEncoder.encodeToString(s"${ua.username}:${userAccount.password}".getBytes)
val response = basicRequest
.post(uri"http://test.com/v1/auth/login")
.header("Authorization", s"BASIC $credentials")
.send(backendStub)

response.body match
case Left(e) =>
fail(e)
case Right(body) =>

assertEquals(response.code, StatusCode.Ok)
assert(response.body.isRight)

// println(body)
val d = decode[Authorization](body)
assert(d.isRight)
val bearerAuth = d.getOrElse(throw new Exception("No bearer auth"))
assert(jwtService.verify(bearerAuth.accessToken))


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

package org.mbari.oni.jpa.entities

import org.mbari.oni.domain.{ConceptNameTypes, MediaTypes}
import org.mbari.oni.domain.{ConceptNameTypes, MediaTypes, UserAccountRoles}
import org.mbari.oni.etc.jdk.Strings

import java.time.Instant
Expand Down Expand Up @@ -151,3 +151,14 @@ object TestEntityFactory:
// DON'T DO THIS. The ID should be assigned by the database. Otherwise inserts will fail.
// entity.setId(nextConceptId.incrementAndGet())
entity

def createUserAccount(role: String = UserAccountRoles.ADMINISTRATOR.getRoleName): UserAccountEntity =
val entity = new UserAccountEntity()
entity.setUserName(Strings.random(20))
entity.setPassword(Strings.random(20))
entity.setEmail(s"{Strings.random(10)}@mbari.org")
entity.setFirstName(Strings.random(10))
entity.setLastName(Strings.random(10))
entity.setAffiliation(Strings.random(20))
entity.setRole(role)
entity
1 change: 1 addition & 0 deletions oni/src/main/scala/org/mbari/oni/etc/jwt/JwtService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ case class JwtService(issuer: String, apiKey: String, signingSecret: String):
.withIssuer(issuer)
.withIssuedAt(iat)
.withExpiresAt(exp)
.withSubject(Option(entity.getId).getOrElse(-1).toString)
.withClaim("name", name)
.withClaim("role", entity.getRole)
.sign(algorithm)
Expand Down

This file was deleted.

0 comments on commit 590808d

Please sign in to comment.