Лабораторная 2 на 5 готово

This commit is contained in:
Dmitry
2026-10-21 20:50:08 +03:00
parent 65003b6119
commit 5eb4db24a8
63 changed files with 2752 additions and 31 deletions
@@ -0,0 +1,123 @@
package org.example;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
public class ApiIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Test
@DisplayName("400 1. Неверный / битый JSON должен возвращать 400 Bad Request")
void shouldReturn400ForMalformedJson() throws Exception {
String malformedJson = "{\"username\": \"bad_json\", \"display_name\":}";
mockMvc.perform(post("/api/v1/users")
.contentType(MediaType.APPLICATION_JSON)
.content(malformedJson))
.andExpect(status().isBadRequest());
}
@Test
@DisplayName("400 2. Валидация: пустой username должен возвращать 400 Bad Request")
void shouldReturn400ForEmptyUsername() throws Exception {
String invalidJson = "{\"username\": \"\", \"display_name\": \"User\"}";
mockMvc.perform(post("/api/v1/users")
.contentType(MediaType.APPLICATION_JSON)
.content(invalidJson))
.andExpect(status().isBadRequest());
}
@Test
@DisplayName("404 3. Неучастник чата получает 404 (чат скрыт)")
void shouldReturn404WhenUserIsNotChatMember() throws Exception {
String aliceJson = mockMvc.perform(post("/api/v1/users")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"username\": \"alice_test\", \"display_name\": \"Alice\"}"))
.andExpect(status().isCreated())
.andReturn().getResponse().getContentAsString();
String aliceId = extractId(aliceJson);
String bobJson = mockMvc.perform(post("/api/v1/users")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"username\": \"bob_test\", \"display_name\": \"Bob\"}"))
.andExpect(status().isCreated())
.andReturn().getResponse().getContentAsString();
String bobId = extractId(bobJson);
String spyJson = mockMvc.perform(post("/api/v1/users")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"username\": \"spy_test\", \"display_name\": \"Spy\"}"))
.andExpect(status().isCreated())
.andReturn().getResponse().getContentAsString();
String spyId = extractId(spyJson);
String chatJson = String.format(
"{\"title\":\"Private\", \"member_ids\":[\"%s\",\"%s\"]}", aliceId, bobId);
String chat = mockMvc.perform(post("/api/v1/chats")
.header("X-User-Id", aliceId) // ← обязательно!
.contentType(MediaType.APPLICATION_JSON)
.content(chatJson))
.andExpect(status().isCreated())
.andReturn().getResponse().getContentAsString();
String chatId = extractId(chat);
mockMvc.perform(post("/api/v1/chats/" + chatId + "/messages")
.header("X-User-Id", spyId)
.contentType(MediaType.APPLICATION_JSON)
.content("{\"text\": \"Secret message\"}"))
.andExpect(status().isNotFound());
}
@Test
@DisplayName("200 4. Пагинация: получение ограниченного числа сообщений по limit")
void shouldPaginateMessagesWithLimit() throws Exception {
String userJson = mockMvc.perform(post("/api/v1/users")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"username\": \"paginator\", \"display_name\": \"Paginator\"}"))
.andExpect(status().isCreated())
.andReturn().getResponse().getContentAsString();
String userId = extractId(userJson);
String chat = mockMvc.perform(post("/api/v1/chats")
.header("X-User-Id", userId)
.contentType(MediaType.APPLICATION_JSON)
.content(String.format(
"{\"title\":\"Paginated Chat\", \"member_ids\":[\"%s\"]}", userId)))
.andExpect(status().isCreated())
.andReturn().getResponse().getContentAsString();
String chatId = extractId(chat);
for (int i = 1; i <= 3; i++) {
mockMvc.perform(post("/api/v1/chats/" + chatId + "/messages")
.header("X-User-Id", userId)
.contentType(MediaType.APPLICATION_JSON)
.content("{\"text\": \"Msg " + i + "\"}"))
.andExpect(status().isCreated());
}
mockMvc.perform(get("/api/v1/chats/" + chatId + "/messages")
.param("limit", "2")
.header("X-User-Id", userId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.items.length()").value(2))
.andExpect(jsonPath("$.next_cursor").isNotEmpty());
}
private String extractId(String json) {
return json.split("\"id\":\"")[1].split("\"")[0];
}
}