Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/pm/get public services #19

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.3</version>
<version>3.1.5</version>
<relativePath/>
</parent>

Expand All @@ -27,7 +27,7 @@
<maven.exec.skip>false</maven.exec.skip>

<kotlin.version>1.9.10</kotlin.version>
<testcontainers.version>1.19.0</testcontainers.version>
<testcontainers.version>1.19.1</testcontainers.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -176,7 +176,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<version>0.8.11</version>
<executions>
<execution>
<id>before-unit-test-execution</id>
Expand Down Expand Up @@ -234,7 +234,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<version>3.2.1</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
<argLine>${surefire.jacoco.args}</argLine>
Expand All @@ -248,7 +248,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.1.2</version>
<version>3.2.1</version>
<configuration>
<argLine>${failsafe.jacoco.args}</argLine>
<groups>integration</groups>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package no.digdir.service_catalog.controller

import no.digdir.service_catalog.model.PublicService
import no.digdir.service_catalog.security.EndpointPermissions
import no.digdir.service_catalog.service.PublicServiceService
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.oauth2.jwt.Jwt
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.CrossOrigin
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping

@Controller
@CrossOrigin
@RequestMapping(value = ["/catalogs/{catalogId}/public-services"])
class PublicServiceController(private val publicServiceService: PublicServiceService, private val endpointPermissions: EndpointPermissions) {

@GetMapping
fun getAllPublicServices(@AuthenticationPrincipal jwt: Jwt, @PathVariable catalogId: String): ResponseEntity<List<PublicService>> =
if (endpointPermissions.hasOrgReadPermission(jwt, catalogId)) {
ResponseEntity(publicServiceService.findPublicServicesByCatalogId(catalogId), HttpStatus.OK)
} else {
ResponseEntity(HttpStatus.FORBIDDEN)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package no.digdir.service_catalog.model

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonInclude

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
data class LocalizedStrings(
val nb: String?,
val nn: String?,
val en: String?
)
18 changes: 18 additions & 0 deletions src/main/kotlin/no/digdir/service_catalog/model/PublicService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package no.digdir.service_catalog.model

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonInclude
import org.springframework.data.mongodb.core.index.CompoundIndex
import org.springframework.data.mongodb.core.index.CompoundIndexes
import org.springframework.data.mongodb.core.mapping.Document

@Document(collection = "publicServices")
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@CompoundIndexes(value = [CompoundIndex(name = "catalog_id", def = "{'catalogId' : 1}")])
data class PublicService (
val id: String,
val catalogId: String,
val title: LocalizedStrings?,
val description: LocalizedStrings?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package no.digdir.service_catalog.mongodb

import no.digdir.service_catalog.model.PublicService
import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.stereotype.Repository

@Repository
interface PublicServiceRepository : MongoRepository<PublicService, String> {
fun getByCatalogId(catalogId: String): List<PublicService>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package no.digdir.service_catalog.service

import no.digdir.service_catalog.mongodb.PublicServiceRepository
import org.springframework.stereotype.Service

@Service
class PublicServiceService(private val publicServiceRepository: PublicServiceRepository) {
fun findPublicServicesByCatalogId(catalogId: String) =
publicServiceRepository.getByCatalogId(catalogId)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package no.digdir.service_catalog.integration

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import no.digdir.service_catalog.model.PublicService
import no.digdir.service_catalog.utils.ApiTestContext
import no.digdir.service_catalog.utils.PUBLIC_SERVICES
import no.digdir.service_catalog.utils.apiAuthorizedRequest
import no.digdir.service_catalog.utils.jwt.Access
import no.digdir.service_catalog.utils.jwt.JwtToken
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpStatus
import org.springframework.test.context.ContextConfiguration

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = ["spring.profiles.active=test"])
@ContextConfiguration(initializers = [ApiTestContext.Initializer::class])
@Tag("integration")
class PublicServices: ApiTestContext() {
private val mapper = jacksonObjectMapper()

@Test
fun `able to get all public services`() {
val response = apiAuthorizedRequest("/catalogs/910244132/public-services", port, null, JwtToken(Access.ORG_READ).toString(), "GET")
Assertions.assertEquals(HttpStatus.OK.value(), response["status"])

val result: List<PublicService> = mapper.readValue(response["body"] as String)
Assertions.assertEquals(PUBLIC_SERVICES, result)
}

@Test
fun `unauthorized when missing token`() {
val response = apiAuthorizedRequest("/catalogs/910244132/public-services", port, null, null, "GET")
Assertions.assertEquals(HttpStatus.UNAUTHORIZED.value(), response["status"])
}

@Test
fun `forbidden when authorized for other catalog`() {
val response = apiAuthorizedRequest("/catalogs/910244132/public-services", port, null, JwtToken(Access.WRONG_ORG_READ).toString(), "GET")
Assertions.assertEquals(HttpStatus.FORBIDDEN.value(), response["status"])
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.digdir.service_catalog.utils

import no.digdir.service_catalog.mongodb.PublicServiceRepository
import no.digdir.service_catalog.mongodb.ServiceRepository
import org.junit.jupiter.api.BeforeEach
import org.springframework.beans.factory.annotation.Autowired
Expand All @@ -18,10 +19,17 @@ abstract class ApiTestContext {

@Autowired
private lateinit var serviceRepository: ServiceRepository

@Autowired
private lateinit var publicServiceRepository: PublicServiceRepository

@BeforeEach
fun resetDatabase() {
serviceRepository.deleteAll()
serviceRepository.saveAll(SERVICES)

publicServiceRepository.deleteAll()
publicServiceRepository.saveAll(PUBLIC_SERVICES)
}

internal class Initializer : ApplicationContextInitializer<ConfigurableApplicationContext> {
Expand Down
13 changes: 13 additions & 0 deletions src/test/kotlin/no/digdir/service_catalog/utils/TestData.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package no.digdir.service_catalog.utils

import no.digdir.service_catalog.model.LocalizedStrings
import no.digdir.service_catalog.model.PublicService
import no.digdir.service_catalog.model.Service
import org.testcontainers.shaded.com.google.common.collect.ImmutableMap

Expand All @@ -17,3 +19,14 @@ val SERVICE_0 = Service("0", "title 0")
val SERVICE_1 = Service("1", "title 1")

val SERVICES = listOf(SERVICE_0, SERVICE_1)

val PUBLIC_SERVICE_0 =
PublicService("0", "910244132",
title = LocalizedStrings("Tittel 0", "Tittel 0", "Tittel 0"),
description = LocalizedStrings("Beskrivelse 0", "Beskriving 0", "Description 0"))
val PUBLIC_SERVICE_1 =
PublicService("1", "910244132",
title = LocalizedStrings("Tittel 1", "Tittel 1", "Tittel 1"),
description = LocalizedStrings("Beskrivelse 1", "Beskriving 1", "Description 1"))

val PUBLIC_SERVICES = listOf(PUBLIC_SERVICE_0, PUBLIC_SERVICE_1)