-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get public services by catalogId
- Loading branch information
1 parent
abf9f49
commit 2c5b7e8
Showing
8 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/no/digdir/service_catalog/controller/PublicServiceController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/no/digdir/service_catalog/model/LocalizedStrings.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/main/kotlin/no/digdir/service_catalog/model/PublicService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
) |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/no/digdir/service_catalog/mongodb/PublicServiceRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/no/digdir/service_catalog/service/PublicServiceService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/kotlin/no/digdir/service_catalog/integration/PublicServices.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters