Added test for exportData

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2021-01-17 17:35:11 +01:00
parent d4514affcc
commit 77c3b9e3a5
Signed by: y0rune
GPG Key ID: F204C385F57EB348

View File

@ -18,6 +18,7 @@ import com.plannaplan.entities.User;
import com.plannaplan.services.UserService; import com.plannaplan.services.UserService;
import com.plannaplan.types.UserRoles; 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.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.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@ -31,11 +32,13 @@ public class CommisionControllerTest extends AbstractControllerTest {
private UserService service; private UserService service;
private static User user; private static User user;
private static User admin;
private static User otherUser; private static User otherUser;
private static User asker; private static User asker;
private static User otherAsker; private static User otherAsker;
private static final String TEST_COMMISIONS_STUDENT_EMAIL = "commisions.student@notexisting.domain"; 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_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_DEANERY_EMAIL = "commisions.deanery@notexisting.domain";
private static final String TEST_COMMISIONS_OTHER_DEANERY_EMAIL = "commisions.deanery2@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_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_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(), private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
@ -254,6 +259,10 @@ public class CommisionControllerTest extends AbstractControllerTest {
UserRoles.STUDENT); UserRoles.STUDENT);
this.service.save(otherUser); 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) { if (CommisionControllerTest.asker == null) {
CommisionControllerTest.asker = new User(null, null, TEST_COMMISIONS_DEANERY_EMAIL, UserRoles.DEANERY); CommisionControllerTest.asker = new User(null, null, TEST_COMMISIONS_DEANERY_EMAIL, UserRoles.DEANERY);
this.service.save(asker); this.service.save(asker);
@ -264,4 +273,41 @@ public class CommisionControllerTest extends AbstractControllerTest {
this.service.save(otherAsker); 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"));
}
} }