package com.plannaplan.controllers; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.nio.charset.Charset; import com.plannaplan.entities.User; import com.plannaplan.services.UserService; import com.plannaplan.types.UserRoles; import static org.junit.Assert.assertTrue; import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration public class CommisionControllerTest extends AbstractControllerTest { @Autowired private UserService service; private static User user; private static User admin; private static User otherUser; private static User asker; private static User otherAsker; private static final String TEST_COMMISIONS_STUDENT_EMAIL = "commisions.student@notexisting.domain"; private static final String TEST_COMMISIONS_ADMIN_EMAIL = "commisions.admin@notexisiting,domain"; private static final String TEST_COMMISIONS_OTHER_STUDENT_EMAIL = "commisions.student2@notexisting.domain"; private static final String TEST_COMMISIONS_DEANERY_EMAIL = "commisions.deanery@notexisting.domain"; private static final String TEST_COMMISIONS_OTHER_DEANERY_EMAIL = "commisions.deanery2@notexisting.domain"; private static final String ADD_COMMISION_ENDPOINT = "/api/v1/commisions/user"; private static final String GET_COMMISIONS_ENDPOINT = "/api/v1/commisions/user"; private static final String GET_SOMEONE_COMMISIONS_ENDPOINT = "/api/v1/commisions/user"; private static final String GET_USER_SCHEDULE_ENDPOINT = "/api/v1/commisions/user/schedule"; private static final String GET_ASSIGNMENTS_ENDPOINT = "/api/v1/commisions/user/assignments"; private static final String EXPORT_DATA = "/api/v1/commisions/export/csv"; private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); @Test public void shouldReturn4xxAddingCommision() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ADD_COMMISION_ENDPOINT)).andExpect(status().is4xxClientError()); } @Test public void shouldFailedAddingCommisionDueToNoArgs() throws Exception { this.checkUsers(); final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)) .andExpect(status().is4xxClientError()); } @Test public void shouldFailAddingCommisionDueToWrongTour() throws Exception { this.checkUsers(); final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token) .contentType(APPLICATION_JSON_UTF8).content("[]")).andExpect(status().is4xxClientError()); } @Test public void shouldReturn4xxGettingAllCommisions() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(GET_COMMISIONS_ENDPOINT)).andExpect(status().is4xxClientError()); } @Test public void shouldReturnOkGettingAllCommisions() throws Exception { this.checkUsers(); final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(GET_COMMISIONS_ENDPOINT).header("Authorization", "Bearer " + token)) .andExpect(status().isOk()); } @Test public void shouldFailAddingCommisionWithSelfIdPrividedDueToWrongTour() throws Exception { this.checkUsers(); final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString()) .header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]")) .andExpect(status().is4xxClientError()); } @Test public void shouldFailCommisionWithSomeoneIdPrividedAsStudent() throws Exception { this.checkUsers(); final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherUser.getId().toString()) .header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]")) .andExpect(status().is4xxClientError()); } @Test public void shouldFailCommisionAsDeanaryWithNoId() throws Exception { this.checkUsers(); final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token) .contentType(APPLICATION_JSON_UTF8).content("[]")).andExpect(status().is4xxClientError()); } @Test public void shouldFailCommisionWithSelfIdPrividedAsDeanary() throws Exception { final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.asker.getId().toString()) .header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]")) .andExpect(status().is4xxClientError()); } @Test public void shouldAddCommisionWithSomeoneIdPrividedAsDeanary() throws Exception { this.checkUsers(); final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherUser.getId().toString()) .header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]")) .andExpect(status().isOk()); } @Test public void shouldFailCommisionWithOtherDeanaryIdPrividedAsDeanary() throws Exception { this.checkUsers(); final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherAsker.getId().toString()) .header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]")) .andExpect(status().is4xxClientError()); } @Test public void shouldGetStudentCommisionsListByDeanary() throws Exception { this.checkUsers(); final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(GET_SOMEONE_COMMISIONS_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString()) .header("Authorization", "Bearer " + token)).andExpect(status().isOk()); } @Test public void shouldFailStudentCommisionsListByOtherStudent() throws Exception { this.checkUsers(); final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(GET_SOMEONE_COMMISIONS_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString()) .header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError()); } @Test public void shouldReturn4xxInScheduleEndpoint() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(GET_USER_SCHEDULE_ENDPOINT)).andExpect(status().is4xxClientError()); } @Test public void shouldReturnOkInScheduleEdnpoint() throws Exception { final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(GET_USER_SCHEDULE_ENDPOINT).header("Authorization", "Bearer " + token)) .andExpect(status().isOk()); } @Test public void shouldReturnOkInScheduleEdnpointByDeanery() throws Exception { this.checkUsers(); final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null); final User student = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(GET_COMMISIONS_ENDPOINT + "/" + student.getId() + "/schedule").header("Authorization", "Bearer " + token)).andExpect(status().isOk()); } @Test public void shouldFailInScheduleEdnpointByOtherStudent() throws Exception { this.checkUsers(); final User student = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null); final User otherStudent = this.service.checkForUser(TEST_COMMISIONS_OTHER_STUDENT_EMAIL, null); final String token = this.service.login(student).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(GET_COMMISIONS_ENDPOINT + "/" + otherStudent.getId() + "/schedule").header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError()); } @Test public void shouldFailInScheduleEdnpointByDeaneryDeaneryRelation() throws Exception { this.checkUsers(); final User deanery = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null); final User otherDeanery = this.service.checkForUser(TEST_COMMISIONS_OTHER_DEANERY_EMAIL, null); final String token = this.service.login(deanery).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(GET_COMMISIONS_ENDPOINT + "/" + otherDeanery.getId() + "/schedule").header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError()); } private void checkUsers() { if (CommisionControllerTest.user == null) { CommisionControllerTest.user = new User(null, null, TEST_COMMISIONS_STUDENT_EMAIL, UserRoles.STUDENT); this.service.save(user); } if (CommisionControllerTest.otherUser == null) { CommisionControllerTest.otherUser = new User(null, null, TEST_COMMISIONS_OTHER_STUDENT_EMAIL, UserRoles.STUDENT); this.service.save(otherUser); } if (CommisionControllerTest.admin == null) { CommisionControllerTest.admin = new User(null, null, TEST_COMMISIONS_ADMIN_EMAIL, UserRoles.ADMIN); this.service.save(admin); } if (CommisionControllerTest.asker == null) { CommisionControllerTest.asker = new User(null, null, TEST_COMMISIONS_DEANERY_EMAIL, UserRoles.DEANERY); this.service.save(asker); } if (CommisionControllerTest.otherAsker == null) { CommisionControllerTest.otherAsker = new User(null, null, TEST_COMMISIONS_OTHER_DEANERY_EMAIL, UserRoles.DEANERY); this.service.save(otherAsker); } } @Test public void shouldExportData() throws Exception { this.checkUsers(); final User admin = this.service.checkForUser(TEST_COMMISIONS_ADMIN_EMAIL, null); final String token = this.service.login(admin).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(EXPORT_DATA).header("Authorization", "Bearer " + token)).andExpect(status().isOk()); } @Test public void shouldNotExportDataDueToWrongPermision() throws Exception { this.checkUsers(); final User student = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null); final String token = this.service.login(student).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(EXPORT_DATA).header("Authorization", "Bearer " + token)) .andExpect(status().is4xxClientError()); } @Test public void shouldExportDataBeCsvFile() throws Exception { this.checkUsers(); final User admin = this.service.checkForUser(TEST_COMMISIONS_ADMIN_EMAIL, null); final String token = this.service.login(admin).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); assertTrue( mockMvc.perform(get(EXPORT_DATA).header("Authorization", "Bearer " + token)).andExpect(status().isOk()) .andReturn().getResponse().getContentAsString().contains("user_id, zaj_cykl_id, gr_nr")); } @Test public void shouldRetrunOkForAssignmentEnpoint() throws Exception { this.checkUsers(); final User admin = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null); final String token = this.service.login(admin).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(GET_ASSIGNMENTS_ENDPOINT).header("Authorization", "Bearer " + token)) .andExpect(status().isOk()); } }