From 75f97c76fcaa3a97a6e56f92569643f7161501e2 Mon Sep 17 00:00:00 2001 From: ricardolopezb Date: Fri, 14 Jul 2023 18:11:05 -0300 Subject: [PATCH] Added test to shared snippets --- .../domains/test/controller/TestController.kt | 14 +++----------- .../domains/test/service/TestService.kt | 2 +- .../domains/test/service/TestServiceImpl.kt | 7 ++++--- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/ingsis/snippetmanager/domains/test/controller/TestController.kt b/src/main/kotlin/ingsis/snippetmanager/domains/test/controller/TestController.kt index 8631ff4..a2289ce 100644 --- a/src/main/kotlin/ingsis/snippetmanager/domains/test/controller/TestController.kt +++ b/src/main/kotlin/ingsis/snippetmanager/domains/test/controller/TestController.kt @@ -7,15 +7,7 @@ import ingsis.snippetmanager.domains.test.service.TestService import org.springframework.beans.factory.annotation.Autowired import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity -import org.springframework.web.bind.annotation.CrossOrigin -import org.springframework.web.bind.annotation.DeleteMapping -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PathVariable -import org.springframework.web.bind.annotation.PostMapping -import org.springframework.web.bind.annotation.PutMapping -import org.springframework.web.bind.annotation.RequestBody -import org.springframework.web.bind.annotation.ResponseBody -import org.springframework.web.bind.annotation.RestController +import org.springframework.web.bind.annotation.* import java.security.Principal import java.util.* @@ -33,8 +25,8 @@ class TestController { @PostMapping("/test") @ResponseBody - fun createTest(principal: Principal, @RequestBody testDto: CreateTestDTO): ResponseEntity { - return ResponseEntity(testService.createTest(testDto, principal.name), HttpStatus.CREATED) + fun createTest(@RequestHeader("Authorization") token: String, principal: Principal, @RequestBody testDto: CreateTestDTO): ResponseEntity { + return ResponseEntity(testService.createTest(token, testDto, principal.name), HttpStatus.CREATED) } @PutMapping("/test") diff --git a/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestService.kt b/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestService.kt index fc87e11..ec5c346 100644 --- a/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestService.kt +++ b/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestService.kt @@ -5,7 +5,7 @@ import ingsis.snippetmanager.domains.test.dto.TestDTO import java.util.* interface TestService { - fun createTest(testDTO: CreateTestDTO, userId: String): TestDTO + fun createTest(token: String, testDTO: CreateTestDTO, userId: String): TestDTO fun updateTest(test: TestDTO, userId: String): TestDTO fun deleteTest(id: UUID, userId: String) fun getTestsByUser(id: String): List diff --git a/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestServiceImpl.kt b/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestServiceImpl.kt index 1ec8b04..fa20bfa 100644 --- a/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestServiceImpl.kt +++ b/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestServiceImpl.kt @@ -6,6 +6,7 @@ import ingsis.snippetmanager.domains.test.dto.TestDTO import ingsis.snippetmanager.domains.test.model.Test import ingsis.snippetmanager.domains.test.repository.TestRepository import ingsis.snippetmanager.error.HTTPError +import ingsis.snippetmanager.service.ShareSnippetService import org.springframework.beans.factory.annotation.Autowired import org.springframework.http.HttpStatus import org.springframework.stereotype.Service @@ -25,11 +26,11 @@ class TestServiceImpl : TestService{ this.snippetRepository = snippetRepository } - override fun createTest(testDTO: CreateTestDTO, userId: String): TestDTO { + override fun createTest(token: String, testDTO: CreateTestDTO, userId: String): TestDTO { val snippet = this.snippetRepository.findById(testDTO.snippetId!!) - - if (snippet.get().ownerId != userId) throw HTTPError("User must own snippet to create a test", HttpStatus.FORBIDDEN) + val sharedIds = ShareSnippetService.getSharedWithMeSnippetsIds(token) + if (snippet.get().ownerId != userId && !sharedIds.contains(testDTO.snippetId)) throw HTTPError("User must have access to snippet to create a test", HttpStatus.FORBIDDEN) val test = Test( description = testDTO.description,