From 77c3b9e3a572f42c945699ec183768764d8f6ff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Sun, 17 Jan 2021 17:35:11 +0100 Subject: [PATCH] Added test for exportData MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../controllers/CommisionControllerTest.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java index 25990f5..e3af390 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java @@ -18,6 +18,7 @@ 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; @@ -31,11 +32,13 @@ public class CommisionControllerTest extends AbstractControllerTest { 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"; @@ -45,6 +48,8 @@ public class CommisionControllerTest extends AbstractControllerTest { 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 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")); @@ -254,6 +259,10 @@ public class CommisionControllerTest extends AbstractControllerTest { 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); @@ -264,4 +273,41 @@ public class CommisionControllerTest extends AbstractControllerTest { 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")); + } }