Skip to content

Commit

Permalink
Added test to shared snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardolopezb committed Jul 14, 2023
1 parent ced86ce commit 75f97c7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand All @@ -33,8 +25,8 @@ class TestController {

@PostMapping("/test")
@ResponseBody
fun createTest(principal: Principal, @RequestBody testDto: CreateTestDTO): ResponseEntity<TestDTO> {
return ResponseEntity(testService.createTest(testDto, principal.name), HttpStatus.CREATED)
fun createTest(@RequestHeader("Authorization") token: String, principal: Principal, @RequestBody testDto: CreateTestDTO): ResponseEntity<TestDTO> {
return ResponseEntity(testService.createTest(token, testDto, principal.name), HttpStatus.CREATED)
}

@PutMapping("/test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestDTO>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 75f97c7

Please sign in to comment.