From ac7b526539cff680d649cf358654f72defb3ec49 Mon Sep 17 00:00:00 2001 From: Sean Starkey Date: Mon, 1 Jun 2026 19:32:02 -0600 Subject: [PATCH] Add tests for Note retrieval --- .../NoteRetrievalControllerTest.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/test/java/com/seanstarkey/notesvault/controller/NoteRetrievalControllerTest.java diff --git a/src/test/java/com/seanstarkey/notesvault/controller/NoteRetrievalControllerTest.java b/src/test/java/com/seanstarkey/notesvault/controller/NoteRetrievalControllerTest.java new file mode 100644 index 0000000..3ebf746 --- /dev/null +++ b/src/test/java/com/seanstarkey/notesvault/controller/NoteRetrievalControllerTest.java @@ -0,0 +1,92 @@ +/** + * NoteRetrievalControllerTest.java + */ +package com.seanstarkey.notesvault.controller; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpHeaders; +import org.springframework.transaction.annotation.Transactional; + +import java.util.UUID; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +@Transactional +class NoteRetrievalControllerTest extends ControllerApiTestSupport { + + @Test + void getNoteReturnsOwnedNote() throws Exception { + register("alice", "secret123"); + String token = jwtUtil.generateToken("alice"); + UUID noteId = createNote(token, "Alice Private", "Only Alice owns this"); + + mockMvc.perform(get("/notes/{id}", noteId) + .header(HttpHeaders.AUTHORIZATION, "Bearer " + token)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(noteId.toString())) + .andExpect(jsonPath("$.title").value("Alice Private")) + .andExpect(jsonPath("$.content").value("Only Alice owns this")) + .andExpect(jsonPath("$.ownerUsername").value("alice")) + .andExpect(jsonPath("$.access").value("OWNER")) + .andExpect(jsonPath("$.createdAt").isNotEmpty()) + .andExpect(jsonPath("$.updatedAt").isNotEmpty()); + } + + @Test + void getNoteReturnsSharedNoteAsReadOnly() throws Exception { + register("alice", "secret123"); + register("bob", "secret456"); + String aliceToken = jwtUtil.generateToken("alice"); + String bobToken = jwtUtil.generateToken("bob"); + UUID noteId = createNote(aliceToken, "Shared Note", "Bob may read this"); + + shareWith(noteId, "bob"); + + mockMvc.perform(get("/notes/{id}", noteId) + .header(HttpHeaders.AUTHORIZATION, "Bearer " + bobToken)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(noteId.toString())) + .andExpect(jsonPath("$.title").value("Shared Note")) + .andExpect(jsonPath("$.content").value("Bob may read this")) + .andExpect(jsonPath("$.ownerUsername").value("alice")) + .andExpect(jsonPath("$.access").value("SHARED_READ")); + } + + @Test + void getNoteReturnsNotFoundForUnsharedNoteOwnedByAnotherUser() throws Exception { + register("alice", "secret123"); + register("bob", "secret456"); + String aliceToken = jwtUtil.generateToken("alice"); + String bobToken = jwtUtil.generateToken("bob"); + UUID noteId = createNote(aliceToken, "Alice Private", "Bob cannot read this"); + + mockMvc.perform(get("/notes/{id}", noteId) + .header(HttpHeaders.AUTHORIZATION, "Bearer " + bobToken)) + .andExpect(status().isNotFound()) + .andExpect(jsonPath("$.error").value("Note not found")); + } + + @Test + void getNoteRequiresBearerToken() throws Exception { + mockMvc.perform(get("/notes/{id}", UUID.randomUUID())) + .andExpect(status().isUnauthorized()) + .andExpect(jsonPath("$.error").value("Unauthorized")); + } + + @Test + void getNoteRejectsInvalidUuidPathVariable() throws Exception { + register("alice", "secret123"); + String token = jwtUtil.generateToken("alice"); + + mockMvc.perform(get("/notes/{id}", "not-a-uuid") + .header(HttpHeaders.AUTHORIZATION, "Bearer " + token)) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.error").value("Invalid request parameter")); + } +}