重构底层框架
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package com.patbond.patbond.user;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class UserApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
// Verifies the user service starts with the committed application.yml
|
||||
// and no external infrastructure (post ADR-002 Nacos removal).
|
||||
}
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
package com.patbond.patbond.user.controller;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* MockMvc tests against the current in-memory UserService implementation.
|
||||
* The service is a stateful singleton within the shared test context, so each
|
||||
* test uses its own username.
|
||||
*/
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
class UserControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
private static String createUserBody(String username) {
|
||||
return """
|
||||
{"username":"%s","password":"secret123","nickname":"Nick","phone":"13800000000"}
|
||||
""".formatted(username);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserReturnsProfileWithoutPassword() throws Exception {
|
||||
mockMvc.perform(post("/internal/users")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(createUserBody("alice_create")))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.code").value(0))
|
||||
.andExpect(jsonPath("$.data.id").isNumber())
|
||||
.andExpect(jsonPath("$.data.username").value("alice_create"))
|
||||
.andExpect(jsonPath("$.data.nickname").value("Nick"))
|
||||
.andExpect(jsonPath("$.data.password").doesNotExist());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserWithDuplicateUsernameReturnsConflict() throws Exception {
|
||||
mockMvc.perform(post("/internal/users")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(createUserBody("bob_dup")))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
mockMvc.perform(post("/internal/users")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(createUserBody("bob_dup")))
|
||||
.andExpect(status().isConflict());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserWithInvalidPayloadReturnsBadRequest() throws Exception {
|
||||
mockMvc.perform(post("/internal/users")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"username\":\"ab\",\"password\":\"123\"}"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void verifyPasswordSucceedsWithCorrectCredentials() throws Exception {
|
||||
mockMvc.perform(post("/internal/users")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(createUserBody("carol_verify")))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
mockMvc.perform(post("/internal/users/verify-password")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"username\":\"carol_verify\",\"password\":\"secret123\"}"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.code").value(0))
|
||||
.andExpect(jsonPath("$.data.userId").isNumber())
|
||||
.andExpect(jsonPath("$.data.username").value("carol_verify"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void verifyPasswordWithWrongPasswordReturnsUnauthorized() throws Exception {
|
||||
mockMvc.perform(post("/internal/users")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(createUserBody("dave_wrongpw")))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
mockMvc.perform(post("/internal/users/verify-password")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"username\":\"dave_wrongpw\",\"password\":\"wrong-password\"}"))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
void verifyPasswordForUnknownUserReturnsUnauthorized() throws Exception {
|
||||
mockMvc.perform(post("/internal/users/verify-password")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"username\":\"no_such_user\",\"password\":\"whatever1\"}"))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getByIdReturnsProfileForExistingUser() throws Exception {
|
||||
String location = mockMvc.perform(post("/internal/users")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(createUserBody("erin_getbyid")))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn().getResponse().getContentAsString();
|
||||
long id = Long.parseLong(location.replaceAll(".*\"id\":(\\d+).*", "$1"));
|
||||
|
||||
mockMvc.perform(get("/internal/users/{id}", id))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.username").value("erin_getbyid"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getByIdForUnknownUserReturnsNotFound() throws Exception {
|
||||
mockMvc.perform(get("/internal/users/{id}", 999999L))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getByUsernameReturnsProfileAndNotFoundForUnknown() throws Exception {
|
||||
mockMvc.perform(post("/internal/users")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(createUserBody("frank_byname")))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
mockMvc.perform(get("/internal/users/by-username/{username}", "frank_byname"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.username").value("frank_byname"));
|
||||
|
||||
mockMvc.perform(get("/internal/users/by-username/{username}", "ghost_user"))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user