Skip to content

Commit

Permalink
runs
Browse files Browse the repository at this point in the history
  • Loading branch information
jnatten committed Oct 28, 2024
1 parent fe47fd6 commit 493746e
Show file tree
Hide file tree
Showing 18 changed files with 262 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ trait TestEnvironment

val s3Client: NdlaS3Client = mock[NdlaS3Client]

val ndlaClient: NdlaClient = mock[NdlaClient]
val myndlaApiClient: MyNDLAApiClient = mock[MyNDLAApiClient]
val ndlaClient: NdlaClient = mock[NdlaClient]
val myndlaApiClient: MyNDLAApiClient = mock[MyNDLAApiClient]

val readService: ReadService = mock[ReadService]
val writeService: WriteService = mock[WriteService]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

package no.ndla.common.model.domain.myndla.auth

import no.ndla.common.model.domain.myndla.{ArenaGroup, MyNDLAUser}
import sttp.model.headers.{AuthenticationScheme, WWWAuthenticateChallenge}
import sttp.monad.MonadError
import sttp.tapir.*
import sttp.tapir.CodecFormat.TextPlain
import sttp.tapir.EndpointInput.{AuthInfo, AuthType}
import sttp.tapir.server.PartialServerEndpoint

import scala.collection.immutable.ListMap

Expand Down Expand Up @@ -45,5 +42,4 @@ object AuthUtility {
)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ComponentRegistry(properties: FrontpageApiProperties)
val healthController = new TapirHealthController

override val myndlaApiClient: MyNDLAApiClient = new MyNDLAApiClient
override val ndlaClient: NdlaClient = new NdlaClient
override val ndlaClient: NdlaClient = new NdlaClient

private val swagger = new SwaggerController(
List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,21 @@ package no.ndla.learningpathapi.controller
import cats.implicits.catsSyntaxEitherId
import no.ndla.common.model.api.CommaSeparatedList.*
import no.ndla.common.model.api.{Author, License}
import no.ndla.common.model.domain.myndla.auth.AuthUtility
import no.ndla.language.Language
import no.ndla.language.Language.AllLanguages
import no.ndla.learningpathapi.Props
import no.ndla.learningpathapi.integration.TaxonomyApiClient
import no.ndla.learningpathapi.model.api.*
import no.ndla.learningpathapi.model.domain
import no.ndla.learningpathapi.model.domain.UserInfo.{LearningpathCombinedUser, LearningpathTokenUser}
import no.ndla.learningpathapi.model.domain.UserInfo.{LearningpathCombinedUser}
import no.ndla.learningpathapi.model.domain.{LearningPathStatus as _, License as _, *}
import no.ndla.learningpathapi.service.search.{SearchConverterServiceComponent, SearchService}
import no.ndla.learningpathapi.service.{ConverterService, ReadService, UpdateService}
import no.ndla.learningpathapi.validation.LanguageValidator
import no.ndla.mapping
import no.ndla.mapping.LicenseDefinition
import no.ndla.network.model.CombinedUser
import no.ndla.network.tapir.NoNullJsonPrinter.jsonBody
import no.ndla.network.tapir.TapirUtil.errorOutputsFor
import no.ndla.network.tapir.auth.TokenUser
import no.ndla.network.tapir.{DynamicHeaders, TapirController}
import sttp.model.StatusCode
import sttp.tapir.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import no.ndla.common.model.NDLADate
import no.ndla.common.model.domain.learningpath.LearningpathCopyright
import no.ndla.common.model.domain.{Tag, Title}
import no.ndla.language.Language.getSupportedLanguages
import no.ndla.learningpathapi.model.domain.UserInfo.{LearningpathCombinedUser, LearningpathTokenUser}
import no.ndla.learningpathapi.model.domain.UserInfo.LearningpathCombinedUser
import no.ndla.learningpathapi.validation.DurationValidator
import no.ndla.network.model.CombinedUser
import no.ndla.network.tapir.auth.TokenUser
import scalikejdbc.*

import scala.util.{Failure, Success, Try}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ package no.ndla.learningpathapi.service

import cats.implicits.*
import io.lemonlabs.uri.typesafe.dsl.*
import no.ndla.common.errors.NotFoundException
import no.ndla.common.errors.{AccessDeniedException, NotFoundException}
import no.ndla.common.implicits.OptionImplicit
import no.ndla.common.model.domain.learningpath
import no.ndla.common.model.domain.learningpath.{EmbedType, EmbedUrl}
import no.ndla.common.model.{api as commonApi, domain as common}
Expand All @@ -19,15 +20,14 @@ import no.ndla.language.Language.*
import no.ndla.learningpathapi.Props
import no.ndla.learningpathapi.integration.*
import no.ndla.learningpathapi.model.api.{LearningPathStatus as _, *}
import no.ndla.learningpathapi.model.domain.UserInfo.{LearningpathCombinedUser, LearningpathTokenUser}
import no.ndla.learningpathapi.model.domain.UserInfo.LearningpathCombinedUser
import no.ndla.learningpathapi.model.domain.*
import no.ndla.learningpathapi.model.{api, domain}
import no.ndla.learningpathapi.repository.LearningPathRepositoryComponent
import no.ndla.learningpathapi.validation.{LanguageValidator, LearningPathValidator}
import no.ndla.mapping.License.getLicense
import no.ndla.network.ApplicationUrl
import no.ndla.network.model.CombinedUser
import no.ndla.network.tapir.auth.TokenUser

import scala.util.{Failure, Success, Try}

Expand Down Expand Up @@ -312,7 +312,7 @@ trait ConverterService {
)
}

private def getVerificationStatus(user: TokenUser): LearningPathVerificationStatus.Value =
private def getVerificationStatus(user: CombinedUser): LearningPathVerificationStatus.Value =
if (user.isNdla)
LearningPathVerificationStatus.CREATED_BY_NDLA
else LearningPathVerificationStatus.EXTERNAL
Expand All @@ -321,7 +321,7 @@ trait ConverterService {
existing: LearningPath,
newLearningPath: NewCopyLearningPathV2,
user: CombinedUser
): LearningPath = {
): Try[LearningPath] = {
val oldTitle = Seq(common.Title(newLearningPath.title, newLearningPath.language))

val oldDescription = newLearningPath.description match {
Expand All @@ -336,63 +336,66 @@ trait ConverterService {
Seq(common.Tag(value, newLearningPath.language))
}

val title = mergeLanguageFields(existing.title, oldTitle)
val description = mergeLanguageFields(existing.description, oldDescription)
val tags = converterService.mergeLearningPathTags(existing.tags, oldTags)
val coverPhotoId = newLearningPath.coverPhotoMetaUrl
.map(converterService.extractImageId)
.getOrElse(existing.coverPhotoId)
val duration =
if (newLearningPath.duration.nonEmpty) newLearningPath.duration
else existing.duration
val copyright = newLearningPath.copyright
.map(converterService.asCopyright)
.getOrElse(existing.copyright)
user.id.toTry(AccessDeniedException("User id not found")).map { ownerId =>
val title = mergeLanguageFields(existing.title, oldTitle)
val description = mergeLanguageFields(existing.description, oldDescription)
val tags = converterService.mergeLearningPathTags(existing.tags, oldTags)
val coverPhotoId = newLearningPath.coverPhotoMetaUrl
.map(converterService.extractImageId)
.getOrElse(existing.coverPhotoId)
val duration =
if (newLearningPath.duration.nonEmpty) newLearningPath.duration
else existing.duration
val copyright = newLearningPath.copyright
.map(converterService.asCopyright)
.getOrElse(existing.copyright)

existing.copy(
id = None,
revision = None,
externalId = None,
isBasedOn = if (existing.isPrivate) None else existing.id,
title = title,
description = description,
status = LearningPathStatus.PRIVATE,
verificationStatus = getVerificationStatus(user),
lastUpdated = clock.now(),
owner = user.id,
copyright = copyright,
learningsteps = existing.learningsteps.map(ls =>
ls.map(_.copy(id = None, revision = None, externalId = None, learningPathId = None))
),
tags = tags,
coverPhotoId = coverPhotoId,
duration = duration
)
existing.copy(
id = None,
revision = None,
externalId = None,
isBasedOn = if (existing.isPrivate) None else existing.id,
title = title,
description = description,
status = LearningPathStatus.PRIVATE,
verificationStatus = getVerificationStatus(user),
lastUpdated = clock.now(),
owner = ownerId,
copyright = copyright,
learningsteps = existing.learningsteps
.map(ls => ls.map(_.copy(id = None, revision = None, externalId = None, learningPathId = None))),
tags = tags,
coverPhotoId = coverPhotoId,
duration = duration
)
}
}

def newLearningPath(newLearningPath: NewLearningPathV2, user: CombinedUser): LearningPath = {
def newLearningPath(newLearningPath: NewLearningPathV2, user: CombinedUser): Try[LearningPath] = {
val domainTags =
if (newLearningPath.tags.isEmpty) Seq.empty
else
Seq(common.Tag(newLearningPath.tags, newLearningPath.language))

domain.LearningPath(
None,
None,
None,
None,
Seq(common.Title(newLearningPath.title, newLearningPath.language)),
Seq(domain.Description(newLearningPath.description, newLearningPath.language)),
newLearningPath.coverPhotoMetaUrl.flatMap(converterService.extractImageId),
newLearningPath.duration,
domain.LearningPathStatus.PRIVATE,
getVerificationStatus(user),
clock.now(),
domainTags,
user.id,
converterService.asCopyright(newLearningPath.copyright),
Some(Seq.empty)
)
user.id.toTry(AccessDeniedException("User id not found")).map { ownerId =>
domain.LearningPath(
None,
None,
None,
None,
Seq(common.Title(newLearningPath.title, newLearningPath.language)),
Seq(domain.Description(newLearningPath.description, newLearningPath.language)),
newLearningPath.coverPhotoMetaUrl.flatMap(converterService.extractImageId),
newLearningPath.duration,
domain.LearningPathStatus.PRIVATE,
getVerificationStatus(user),
clock.now(),
domainTags,
ownerId,
converterService.asCopyright(newLearningPath.copyright),
Some(Seq.empty)
)
}
}

def getApiIntroduction(learningSteps: Seq[domain.LearningStep]): Seq[api.Introduction] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import no.ndla.common.errors.{AccessDeniedException, NotFoundException, Validati
import no.ndla.common.model.api as commonApi
import no.ndla.learningpathapi.model.api.*
import no.ndla.learningpathapi.model.domain
import no.ndla.learningpathapi.model.domain.UserInfo.{LearningpathCombinedUser, LearningpathTokenUser}
import no.ndla.learningpathapi.model.domain.UserInfo.LearningpathCombinedUser
import no.ndla.learningpathapi.model.domain.{InvalidLpStatusException, StepStatus, LearningPathStatus as _}
import no.ndla.learningpathapi.repository.LearningPathRepositoryComponent
import no.ndla.network.clients.{FeideApiClient, MyNDLAApiClient, RedisClient}
import no.ndla.network.model.CombinedUser
import no.ndla.network.tapir.auth.TokenUser

import scala.math.max
import scala.util.{Failure, Success, Try}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@

package no.ndla.learningpathapi.service

import cats.implicits.*
import no.ndla.common.Clock
import no.ndla.common.errors.{AccessDeniedException, NotFoundException}
import no.ndla.common.implicits.*
import no.ndla.learningpathapi.Props
import no.ndla.learningpathapi.integration.{SearchApiClient, TaxonomyApiClient}
import no.ndla.learningpathapi.model.api.*
import no.ndla.learningpathapi.model.domain
import no.ndla.learningpathapi.model.domain.UserInfo.{LearningpathCombinedUser, LearningpathTokenUser}
import no.ndla.learningpathapi.model.domain.UserInfo.LearningpathCombinedUser
import no.ndla.learningpathapi.model.domain.{LearningPathStatus, LearningPath as _, LearningStep as _, *}
import no.ndla.learningpathapi.repository.LearningPathRepositoryComponent
import no.ndla.learningpathapi.service.search.SearchIndexService
Expand Down Expand Up @@ -83,28 +82,27 @@ trait UpdateService {
case None => Failure(NotFoundException("Could not find learningpath to copy."))
case Some(Failure(ex)) => Failure(ex)
case Some(Success(existing)) =>
val toInsert = converterService.newFromExistingLearningPath(existing, newLearningPath, owner)
learningPathValidator.validate(toInsert, allowUnknownLanguage = true)
converterService.asApiLearningpathV2(
learningPathRepository.insert(toInsert),
newLearningPath.language,
fallback = true,
owner
)
for {
toInsert <- converterService.newFromExistingLearningPath(existing, newLearningPath, owner)
_ = learningPathValidator.validate(toInsert, allowUnknownLanguage = true)
converted <- converterService.asApiLearningpathV2(
learningPathRepository.insert(toInsert),
newLearningPath.language,
fallback = true,
owner
)
} yield converted
}
}

def addLearningPathV2(newLearningPath: NewLearningPathV2, owner: CombinedUser): Try[LearningPathV2] =
writeDuringWriteRestrictionOrAccessDenied(owner) {
val learningPath = converterService.newLearningPath(newLearningPath, owner)
learningPathValidator.validate(learningPath)

converterService.asApiLearningpathV2(
learningPathRepository.insert(learningPath),
newLearningPath.language,
fallback = true,
owner
)
for {
learningPath <- converterService.newLearningPath(newLearningPath, owner)
_ = learningPathValidator.validate(learningPath)
inserted = learningPathRepository.insert(learningPath)
converted <- converterService.asApiLearningpathV2(inserted, newLearningPath.language, fallback = true, owner)
} yield converted
}

def updateLearningPathV2(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import no.ndla.learningpathapi.model.domain.*
import no.ndla.learningpathapi.model.{api, domain}
import no.ndla.learningpathapi.{TestData, TestEnvironment, UnitSuite}
import no.ndla.mapping.License.getLicenses
import no.ndla.network.tapir.auth.TokenUser
import no.ndla.network.model.CombinedUser
import no.ndla.tapirtesting.TapirControllerTest
import org.mockito.ArgumentMatchers.{eq as eqTo, *}
import org.mockito.Mockito.{reset, times, verify, when}
Expand Down Expand Up @@ -193,7 +193,7 @@ class LearningpathControllerV2Test extends UnitSuite with TestEnvironment with T
}

test("That /with-status returns 400 if invalid status is specified") {
when(readService.learningPathWithStatus(any[String], any[TokenUser]))
when(readService.learningPathWithStatus(any[String], any[CombinedUser]))
.thenReturn(Failure(InvalidLpStatusException("Bad status")))

val res = simpleHttpClient.send(
Expand All @@ -202,7 +202,7 @@ class LearningpathControllerV2Test extends UnitSuite with TestEnvironment with T
)
res.code.code should be(400)

when(readService.learningPathWithStatus(any[String], any[TokenUser]))
when(readService.learningPathWithStatus(any[String], any[CombinedUser]))
.thenReturn(Success(List.empty))

val res2 = simpleHttpClient.send(
Expand Down
Loading

0 comments on commit 493746e

Please sign in to comment.