|
| 1 | +package integration; |
| 2 | + |
| 3 | +import com.taskagile.TaskAgileApplication; |
| 4 | +import com.taskagile.utils.JsonUtils; |
| 5 | +import com.taskagile.web.payload.RegistrationPayload; |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.runner.RunWith; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 10 | +import org.springframework.boot.test.context.SpringBootTest; |
| 11 | +import org.springframework.http.MediaType; |
| 12 | +import org.springframework.test.context.ActiveProfiles; |
| 13 | +import org.springframework.test.context.junit4.SpringRunner; |
| 14 | +import org.springframework.test.web.servlet.MockMvc; |
| 15 | + |
| 16 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 17 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 18 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 19 | + |
| 20 | +@RunWith(SpringRunner.class) |
| 21 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = TaskAgileApplication.class) |
| 22 | +@ActiveProfiles("test") |
| 23 | +@AutoConfigureMockMvc |
| 24 | +public class RegistrationApiIntegrationTests { |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private MockMvc mvcMock; |
| 28 | + |
| 29 | + private RegistrationPayload payload(String username, String emailAddress) { |
| 30 | + RegistrationPayload payload = new RegistrationPayload(); |
| 31 | + payload.setUsername(username); |
| 32 | + payload.setEmailAddress(emailAddress); |
| 33 | + payload.setPassword("MyPassword!"); |
| 34 | + payload.setFirstName("User"); |
| 35 | + payload.setLastName("Test"); |
| 36 | + return payload; |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void register_blankPayload_shouldFailAndReturn400() throws Exception { |
| 41 | + mvcMock.perform(post("/api/registrations")) |
| 42 | + .andExpect(status().is(400)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void register_validPayload_shouldSucceedAndReturn201() throws Exception { |
| 47 | + RegistrationPayload payload = payload( "sunny", "[email protected]"); |
| 48 | + mvcMock.perform( |
| 49 | + post("/api/registrations") |
| 50 | + .contentType(MediaType.APPLICATION_JSON) |
| 51 | + .content(JsonUtils.toJson(payload))) |
| 52 | + .andExpect(status().is(201)); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void register_existedUsername_shouldFailAndReturn400() throws Exception { |
| 57 | + RegistrationPayload payload = payload( "exist", "[email protected]"); |
| 58 | + mvcMock.perform( |
| 59 | + post("/api/registrations") |
| 60 | + .contentType(MediaType.APPLICATION_JSON) |
| 61 | + .content(JsonUtils.toJson(payload))) |
| 62 | + .andExpect(status().is(201)); |
| 63 | + // Try to register again with the same username |
| 64 | + RegistrationPayload payload2 = payload( "exist", "[email protected]"); |
| 65 | + mvcMock.perform( |
| 66 | + post("/api/registrations") |
| 67 | + .contentType(MediaType.APPLICATION_JSON) |
| 68 | + .content(JsonUtils.toJson(payload2))) |
| 69 | + .andExpect(status().is(400)) |
| 70 | + .andExpect(jsonPath("$.message").value("Username already exists")); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void register_existedEmailAddress_shouldFailAndReturn400() throws Exception { |
| 75 | + RegistrationPayload payload = payload( "test1", "[email protected]"); |
| 76 | + mvcMock.perform( |
| 77 | + post("/api/registrations") |
| 78 | + .contentType(MediaType.APPLICATION_JSON) |
| 79 | + .content(JsonUtils.toJson(payload))) |
| 80 | + .andExpect(status().is(201)); |
| 81 | + // Try to register with the same email address |
| 82 | + RegistrationPayload payload2 = payload( "test2", "[email protected]"); |
| 83 | + mvcMock.perform( |
| 84 | + post("/api/registrations") |
| 85 | + .contentType(MediaType.APPLICATION_JSON) |
| 86 | + .content(JsonUtils.toJson(payload2))) |
| 87 | + .andExpect(status().is(400)) |
| 88 | + .andExpect(jsonPath("$.message").value("Email address already exists")); |
| 89 | + } |
| 90 | +} |
0 commit comments