Skip to content
Snippets Groups Projects
Commit 9960281c authored by Maciej Żuralski's avatar Maciej Żuralski
Browse files

refactor: authorization in wedding controller tests

parent 0c8adc7e
Branches
......@@ -58,7 +58,6 @@ public class WeddingControllerTests {
}
@Test
@WithMockUser(username = "user", roles = {"CLIENT_USER"})
void getWedding_ShouldReturnWedding_WhenExists() throws Exception {
weddingRepository.save(WeddingEntity.builder()
.id(UUID.nameUUIDFromBytes("wedding_1".getBytes()))
......@@ -75,7 +74,7 @@ public class WeddingControllerTests {
}
""".formatted(UUID.nameUUIDFromBytes("wedding_1".getBytes()));
mockMvc.perform(get("/api/wedding/{id}", UUID.nameUUIDFromBytes("wedding_1".getBytes()))
mockMvc.perform(get("/api/weddings/{id}", UUID.nameUUIDFromBytes("wedding_1".getBytes()))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
......@@ -83,24 +82,38 @@ public class WeddingControllerTests {
}
@Test
void getWedding_ShouldReturnNotAuthorized_WhenNotAuthorized() throws Exception {
mockMvc.perform(get("/api/wedding/{id}", UUID.nameUUIDFromBytes("wedding_1".getBytes()))
void getWedding_ShouldReturnWedding_WhenNotAuthorized() throws Exception {
weddingRepository.save(WeddingEntity.builder()
.id(UUID.nameUUIDFromBytes("wedding_1".getBytes()))
.name("Wedding 1")
.eventStartDateTime(LocalDateTime.parse("2024-11-21T14:41"))
.build());
@Language("JSON")
String expected = """
{
"id": "%s",
"name": "Wedding 1",
"eventStartDateTime": "2024-11-21T14:41:00"
}
""".formatted(UUID.nameUUIDFromBytes("wedding_1".getBytes()));
mockMvc.perform(get("/api/weddings/{id}", UUID.nameUUIDFromBytes("wedding_1".getBytes()))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isUnauthorized())
.andExpect(content().string(""));
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().json(expected, true));
}
@Test
@WithMockUser(username = "user", roles = {"CLIENT_USER"})
void getWedding_ShouldReturnNotFound_WhenDoesNotExistAndAuthorized() throws Exception {
mockMvc.perform(get("/api/wedding/{id}", UUID.nameUUIDFromBytes("wedding_1".getBytes()))
void getWedding_ShouldReturnNotFound_WhenDoesNotExist() throws Exception {
mockMvc.perform(get("/api/weddings/{id}", UUID.nameUUIDFromBytes("wedding_1".getBytes()))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
.andExpect(content().string(""));
}
@Test
@WithMockUser(username = "user", roles = {"CLIENT_USER"})
void getWeddingImage_ShouldReturnWeddingBackgroundImage_WhenExists() throws Exception {
byte[] image = new byte[]{1, 2, 3};
......@@ -118,6 +131,14 @@ public class WeddingControllerTests {
.andExpect(content().bytes(image));
}
@Test
void getWeddingImage_ShouldReturnNotFound_WhenDoesNotExist() throws Exception {
mockMvc.perform(get("/api/weddings/{id}/background", UUID.nameUUIDFromBytes("wedding_1".getBytes()))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
.andExpect(content().string(""));
}
@Test
@WithMockUser(username = "user", roles = {"CLIENT_USER"})
......@@ -130,7 +151,7 @@ public class WeddingControllerTests {
MockMultipartFile mockFile = new MockMultipartFile("image", "background.jpg", "image/jpeg", new byte[]{1, 2, 3});
mockMvc.perform(multipart("/api/wedding/{weddingId}/background", UUID.nameUUIDFromBytes("wedding_1".getBytes()))
mockMvc.perform(multipart("/api/weddings/{weddingId}/background", UUID.nameUUIDFromBytes("wedding_1".getBytes()))
.file(mockFile)
.contentType(MediaType.MULTIPART_FORM_DATA))
.andExpect(status().isCreated())
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment