Add tests for Note retrieval
This commit is contained in:
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user