Merge pull request 'Adding fixes into master' (#48) from fixes into master

Reviewed-on: http://git.plannaplan.pl/filipizydorczyk/backend/pulls/48
This commit is contained in:
Marcin Woźniak 2021-01-19 15:07:07 +01:00
commit c177314419
18 changed files with 529 additions and 49 deletions

View File

@ -48,8 +48,9 @@ public class User {
}
public void removeGroup(Long id) {
final Groups groupToDelete = this.studentRegisteredGrups.stream().filter(e -> e.getId().equals(id)).findFirst().get();
this.studentRegisteredGrups.remove(groupToDelete);
final Groups groupToDelete = this.studentRegisteredGrups.stream().filter(e -> e.getId().equals(id)).findFirst()
.get();
this.studentRegisteredGrups.remove(groupToDelete);
}
public void claimGroup(Groups group) {
@ -236,6 +237,9 @@ public class User {
* @return ranking points [100;500]
*/
public Integer getRanking() {
if (ranking == null) {
return 100;
}
return ranking;
}

View File

@ -106,4 +106,12 @@ public class AssignmentService {
this.emailService.sendAcceptationResult(e, new EmailAcceptedData(accepted, removed));
});
}
/**
* @param toSave list of entites to save to db
* @return list of assignments entities with ids from db
*/
public List<Assignment> saveAll(List<Assignment> toSave) {
return this.repo.saveAll(toSave);
}
}

View File

@ -1,17 +1,23 @@
package com.plannaplan.types;
/**
* GroupType contains types: LECTURE, CLASS, LAB, SEMINAR, CONSERVATORY, PRATICE
* GroupType contains types: LECTURE, CLASS, LAB, SEMINAR, CONSERVATORY, PRATICE
*/
public enum GroupType {
LECTURE("Wykład"), CLASS("Ćwiczenia"), LAB("Laboratorium"), SEMINAR("Seminarium"),CONSERVATORY("Konwersatorium"), PRATICE("Praktyka");
LECTURE("Wykład"), CLASS("Ćwiczenia"), LAB("Laboratorium"), SEMINAR("Seminarium"), CONSERVATORY("Konwersatorium"),
PRATICE("Praktyka");
public final String type;
private GroupType(String type) {
this.type = type;
}
/**
* @param type as string
* @return Enum converted from provided string
*/
public final static GroupType getType(String type) {
for (GroupType d : values()) {
if (d.type.equals(type)) {
@ -20,4 +26,14 @@ public enum GroupType {
}
return null;
}
/**
* @param type group type to check
* @return general convertion to bring down tours to two generals LECTURE and
* CLASS. To class are included: CLASS, LAB, SEMINAR, CONSERVATORY,
* PRATICE.
*/
public final static GroupType isLectureOrClass(GroupType type) {
return type == GroupType.LECTURE ? GroupType.LECTURE : GroupType.CLASS;
}
}

View File

@ -1,9 +1,9 @@
package com.plannaplan.types;
/**
* UserRoles contains types: STUDENT, DEANERY, ADMIN, TEST_USER
* UserRoles contains types: STUDENT, DEANERY, ADMIN, TEST_USER
*/
public enum UserRoles {
STUDENT, DEANERY, ADMIN, TEST_USER
STUDENT, DEANERY, ADMIN, TEST_USER, DEVELOPER
}

View File

@ -0,0 +1,68 @@
package com.plannaplan.types;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class GroupTypeTest {
@Test
public void shouldBeLecture() {
assertTrue(GroupType.isLectureOrClass(GroupType.LECTURE) == GroupType.LECTURE);
}
@Test
public void shouldBeClass1() {
assertTrue(GroupType.isLectureOrClass(GroupType.CLASS) == GroupType.CLASS);
}
@Test
public void shouldBeClass2() {
assertTrue(GroupType.isLectureOrClass(GroupType.CONSERVATORY) == GroupType.CLASS);
}
@Test
public void shouldBeClass3() {
assertTrue(GroupType.isLectureOrClass(GroupType.LAB) == GroupType.CLASS);
}
@Test
public void shouldBeClass4() {
assertTrue(GroupType.isLectureOrClass(GroupType.PRATICE) == GroupType.CLASS);
}
@Test
public void shouldBeClass5() {
assertTrue(GroupType.isLectureOrClass(GroupType.SEMINAR) == GroupType.CLASS);
}
@Test
public void shouldCastFromString1() {
assertTrue(GroupType.getType("Ćwiczenia") == GroupType.CLASS);
}
@Test
public void shouldCastFromString2() {
assertTrue(GroupType.getType("Wykład") == GroupType.LECTURE);
}
@Test
public void shouldCastFromString3() {
assertTrue(GroupType.getType("Laboratorium") == GroupType.LAB);
}
@Test
public void shouldCastFromString4() {
assertTrue(GroupType.getType("Seminarium") == GroupType.SEMINAR);
}
@Test
public void shouldCastFromString5() {
assertTrue(GroupType.getType("Konwersatorium") == GroupType.CONSERVATORY);
}
@Test
public void shouldCastFromString6() {
assertTrue(GroupType.getType("Praktyka") == GroupType.PRATICE);
}
}

View File

@ -110,14 +110,16 @@ public class CommisionController extends TokenBasedController {
final Optional<Commision> comPrev = this.commisionService.getNewestCommision(user);
final Commision com = new Commision(user, asker);
this.commisionService.save(com);
final List<Long> addedGroups = new ArrayList<>();
final List<Assignment> addedAssignments = new ArrayList<>();
groups.stream().forEach((groupId) -> {
Groups group = this.groupServcicxe.getGroupById(groupId)
.orElseThrow(() -> new NullPointerException());
Assignment a = new Assignment(group, com);
this.assignmentService.save(a);
addedAssignments.add(a);
// this.assignmentService.save(a);
addedGroups.add(groupId);
});
@ -127,7 +129,7 @@ public class CommisionController extends TokenBasedController {
while (it.hasNext() && !isBad) {
final Assignment a = it.next();
if (a.isAccepted() && !addedGroups.contains(a.getId())) {
if (a.isAccepted() && !addedGroups.contains(a.getGroup().getId())) {
isBad = true;
}
}
@ -139,6 +141,9 @@ public class CommisionController extends TokenBasedController {
HttpStatus.BAD_REQUEST);
}
this.commisionService.save(com);
this.assignmentService.saveAll(addedAssignments);
return new ResponseEntity<>("Succes", HttpStatus.OK);
} catch (UserNotFoundException exception) {
return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND);

View File

@ -10,6 +10,7 @@ import com.plannaplan.App;
import com.plannaplan.entities.User;
import com.plannaplan.models.ConfigData;
import com.plannaplan.models.TourData;
import com.plannaplan.responses.models.ConfigTourResponse;
import com.plannaplan.security.cas.CasUserIdentity;
import com.plannaplan.security.cas.CasValidationExcepiton;
import com.plannaplan.security.cas.CasValidator;
@ -34,6 +35,7 @@ import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
/**
* Rest controller to Config related endpoints. More detailed api docs is
@ -167,4 +169,16 @@ public class ConfigController {
return new ResponseEntity<>("Internal Server Error", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
/**
* @return api response entity
*/
@GetMapping(path = "/admin/tour")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@ApiOperation("It will return what tour is currently on. You need to provide admin token.")
public ResponseEntity<ConfigTourResponse> getTourDate() {
final ConfigTourResponse response = new ConfigTourResponse(this.contrl.getCurrentConfig().getCurrentState());
return new ResponseEntity<>(response, HttpStatus.OK);
}
}

View File

@ -68,7 +68,7 @@ public class CoursesController {
final HashMap<Long, Integer> ammounts = this.groupService.getTakenPlaces(course.getGroups());
course.getGroups().stream().forEach(group -> {
if (group.getType() == GroupType.CLASS) {
if (GroupType.isLectureOrClass(group.getType()) == GroupType.CLASS) {
classes.add(new GroupWithCapacityResponse(group, ammounts.get(group.getId())));
} else {
lectures.add(new GroupWithCapacityResponse(group, ammounts.get(group.getId())));

View File

@ -0,0 +1,103 @@
package com.plannaplan.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import java.util.Date;
import com.plannaplan.App;
import com.plannaplan.models.TourData;
import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.ConfiguratorService;
import com.plannaplan.services.ExchangeService;
/**
* Rest controller to enpoint that help deveopler test the app
*/
@RestController
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/developer")
@Api(tags = {
"DeveloperController" }, value = "DeveloperController", description = "All endpoints to use in development time to help app testing")
public class DeveloperController {
private static final long ONE_DAY = 86400000;
@Autowired
private AssignmentService assignmentService;
@Autowired
private ExchangeService exchangeService;
@Autowired
private ConfiguratorService configurationController;
/**
* @return if accept algorythm was perfomed
*/
@PreAuthorize("hasRole('ROLE_DEVELOPER')")
@PostMapping(path = "/algorythm/accept")
public ResponseEntity<String> performAcceptAlgorythm() {
this.assignmentService.callAcceptAlgorythm();
return new ResponseEntity<>("Success", HttpStatus.OK);
}
/**
* @return if exchange algorythm was perfomed
*/
@PreAuthorize("hasRole('ROLE_DEVELOPER')")
@PostMapping(path = "/algorythm/exchange")
public ResponseEntity<String> performExchangeAlgorythm() {
this.exchangeService.performExchange();
return new ResponseEntity<>("Success", HttpStatus.OK);
}
/**
* @return if tour was set
*/
@PreAuthorize("hasRole('ROLE_DEVELOPER')")
@PostMapping(path = "/tours/first")
public ResponseEntity<String> setFirstTour() {
this.configurationController.saveTours(
new TourData(new Date(System.currentTimeMillis() - ONE_DAY),
new Date(System.currentTimeMillis() + ONE_DAY)),
new TourData(new Date(System.currentTimeMillis() + 2 * ONE_DAY),
new Date(System.currentTimeMillis() + 3 * ONE_DAY)));
return new ResponseEntity<>("Success", HttpStatus.OK);
}
/**
* @return if tour was set
*/
@PreAuthorize("hasRole('ROLE_DEVELOPER')")
@PostMapping(path = "/tours/second")
public ResponseEntity<String> setSecondTour() {
this.configurationController.saveTours(
new TourData(new Date(System.currentTimeMillis() - 3 * ONE_DAY),
new Date(System.currentTimeMillis() - 2 * ONE_DAY)),
new TourData(new Date(System.currentTimeMillis() - ONE_DAY),
new Date(System.currentTimeMillis() + ONE_DAY)));
return new ResponseEntity<>("Success", HttpStatus.OK);
}
/**
* @return if tour was set
*/
@PreAuthorize("hasRole('ROLE_DEVELOPER')")
@PostMapping(path = "/tours/no")
public ResponseEntity<String> setNoTour() {
this.configurationController.saveTours(
new TourData(new Date(System.currentTimeMillis() - ONE_DAY), new Date(System.currentTimeMillis())),
new TourData(new Date(System.currentTimeMillis() + ONE_DAY),
new Date(System.currentTimeMillis() + 2 * ONE_DAY)));
return new ResponseEntity<>("Success", HttpStatus.OK);
}
}

View File

@ -57,10 +57,10 @@ public class AssignmentResponseMappers {
}
}
if (courseGroups.size() == 2) {
final Groups lecture = courseGroups.stream().filter(o -> o.getType() == GroupType.LECTURE).findFirst()
.get();
final Groups classes = courseGroups.stream().filter(o -> o.getType() == GroupType.CLASS).findFirst()
.get();
final Groups lecture = courseGroups.stream()
.filter(o -> GroupType.isLectureOrClass(o.getType()) == GroupType.LECTURE).findFirst().get();
final Groups classes = courseGroups.stream()
.filter(o -> GroupType.isLectureOrClass(o.getType()) == GroupType.CLASS).findFirst().get();
if (ammounts != null) {
response.add(new AssignmentResponse(course, lecture, classes, ammounts));

View File

@ -83,7 +83,7 @@ public class GroupsMappers {
List<GroupDefaultResponse> classes = new ArrayList<>();
groups.stream().forEach(group -> {
if (group.getType() == GroupType.CLASS) {
if (GroupType.isLectureOrClass(group.getType()) == GroupType.CLASS) {
if (taken != null) {
classes.add(new GroupDefaultResponse(group, taken.get(group.getId())));
} else {
@ -122,7 +122,7 @@ public class GroupsMappers {
List<GroupWithCapacityResponse> classes = new ArrayList<>();
groups.stream().forEach(group -> {
if (group.getType() == GroupType.CLASS) {
if (GroupType.isLectureOrClass(group.getType()) == GroupType.CLASS) {
if (taken != null) {
classes.add(new GroupWithCapacityResponse(group, taken.get(group.getId())));
} else {

View File

@ -50,8 +50,8 @@ public class AssignmentResponse {
* @param group class/lecture entity
*/
public AssignmentResponse(Course course, Groups group) {
this(course, group.getType() == GroupType.LECTURE ? group : null,
group.getType() == GroupType.CLASS ? group : null);
this(course, GroupType.isLectureOrClass(group.getType()) == GroupType.LECTURE ? group : null,
GroupType.isLectureOrClass(group.getType()) == GroupType.CLASS ? group : null);
}
/**
@ -61,8 +61,8 @@ public class AssignmentResponse {
* places
*/
public AssignmentResponse(Course course, Groups group, HashMap<Long, Integer> ammounts) {
this(course, group.getType() == GroupType.LECTURE ? group : null,
group.getType() == GroupType.CLASS ? group : null, ammounts);
this(course, GroupType.isLectureOrClass(group.getType()) == GroupType.LECTURE ? group : null,
GroupType.isLectureOrClass(group.getType()) == GroupType.CLASS ? group : null, ammounts);
}
/**

View File

@ -0,0 +1,26 @@
package com.plannaplan.responses.models;
import com.plannaplan.types.AppState;
/**
* Api response for config tours entpoint
*/
public class ConfigTourResponse {
private String currentTour;
/**
* @param appState to send as a response
*/
public ConfigTourResponse(AppState appState) {
this.currentTour = appState.toString();
}
/**
* @return current app state as string
*/
public String getCurrentTour() {
return currentTour;
}
}

View File

@ -30,7 +30,7 @@ public class CoursesWithGroupsResponse extends CoursesResponse {
public CoursesWithGroupsResponse(Course course) {
super(course);
course.getGroups().stream().forEach(group -> {
if (group.getType() == GroupType.CLASS) {
if (GroupType.isLectureOrClass(group.getType()) == GroupType.CLASS) {
this.classes.add(new GroupWithCapacityResponse(group));
} else {
this.lectures.add(new GroupWithCapacityResponse(group));

View File

@ -51,7 +51,7 @@ public class GroupDefaultResponse {
this.endTime = group.getEndTimeString() != null ? group.getEndTimeString() : "";
this.lecturer = group.getLecturer() != null ? group.getLecturer().toString() : "";
this.room = group.getRoom() != null ? group.getRoom() : "";
this.type = group.getType() != null ? group.getType() : null;
this.type = group.getType() != null ? GroupType.isLectureOrClass(group.getType()) : null;
}
/**

View File

@ -10,7 +10,8 @@ import org.springframework.security.core.GrantedAuthority;
* Users Roles for spring app
*/
public enum AuthorityRoles implements GrantedAuthority {
STUDENT("ROLE_STUDENT"), DEANERY("ROLE_DEANERY"), ADMIN("ROLE_ADMIN"), TEST_USER("ROLE_TESTUSER");
STUDENT("ROLE_STUDENT"), DEANERY("ROLE_DEANERY"), ADMIN("ROLE_ADMIN"), TEST_USER("ROLE_TESTUSER"),
DEVELOPER("ROLE_DEVELOPER");
private String role;
@ -39,6 +40,8 @@ public enum AuthorityRoles implements GrantedAuthority {
return Optional.of(AuthorityRoles.STUDENT);
case TEST_USER:
return Optional.of(AuthorityRoles.TEST_USER);
case DEVELOPER:
return Optional.of(AuthorityRoles.DEVELOPER);
default:
return Optional.empty();
}

View File

@ -40,6 +40,7 @@ public class ConfigControllerTest extends AbstractControllerTest {
private static final String COURSE_ENDPOINT = "/api/v1/configurator/config/courses";
private static final String TOURS_ENDPOINT = "/api/v1/configurator/config/tours";
private static final String ADMIN_INIT_ENDPOINT = "/api/v1/configurator/admin/init";
private static final String TOUR_GET_ENDPOINT = "/api/v1/configurator/admin/tour";
private static final String FIRST_TOUR_START = "firstTourBegin";
private static final String FIRST_TOUR_END = "firstTourEnd";
private static final String SECOND_TOUR_START = "secondTourBegin";
@ -230,7 +231,7 @@ public class ConfigControllerTest extends AbstractControllerTest {
@Test
@Ignore
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldCreateAdminBecouseOfEmptyDatabase() throws Exception{
public void shouldCreateAdminBecouseOfEmptyDatabase() throws Exception {
// have no idea how to make this test independent from user that run this
final String ticket = "";
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
@ -238,7 +239,7 @@ public class ConfigControllerTest extends AbstractControllerTest {
}
@Test
public void shouldFailDueToExistingAdmin() throws Exception{
public void shouldFailDueToExistingAdmin() throws Exception {
this.service.save(new User(null, null, "shouldFailDueToExistingAdmin@ConfigController.Test", UserRoles.ADMIN));
final String ticket = "hfewlhfjlewhipfqwehipqwef";
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
@ -282,10 +283,9 @@ public class ConfigControllerTest extends AbstractControllerTest {
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020")
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020")
.param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "14.12.2020")
.param(SECOND_TOUR_START, "16.12.2020").param(SECOND_TOUR_END, "20.12.2020")
.header("Authorization", "Bearer " + token)).andExpect(status().isOk());
}
@Test
@ -295,10 +295,9 @@ public class ConfigControllerTest extends AbstractControllerTest {
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020")
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020")
.param(SECOND_TOUR_END, "13.12.2020").header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "14.12.2020")
.param(SECOND_TOUR_START, "16.12.2020").param(SECOND_TOUR_END, "13.12.2020")
.header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError());
}
@ -309,10 +308,9 @@ public class ConfigControllerTest extends AbstractControllerTest {
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020")
.param(FIRST_TOUR_END, "10.12.2020").param(SECOND_TOUR_START, "16.12.2020")
.param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "10.12.2020")
.param(SECOND_TOUR_START, "16.12.2020").param(SECOND_TOUR_END, "20.12.2020")
.header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError());
}
@ -323,10 +321,9 @@ public class ConfigControllerTest extends AbstractControllerTest {
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020")
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "13.12.2020")
.param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "14.12.2020")
.param(SECOND_TOUR_START, "13.12.2020").param(SECOND_TOUR_END, "20.12.2020")
.header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError());
}
@ -337,10 +334,41 @@ public class ConfigControllerTest extends AbstractControllerTest {
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020")
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020")
.param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token))
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "14.12.2020")
.param(SECOND_TOUR_START, "16.12.2020").param(SECOND_TOUR_END, "20.12.2020")
.header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError());
}
@Test
public void shouldReturnOKAuthorizedForGetTours() throws Exception {
final String mail = "shouldReturnOKAuthorizedForGetTours@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(TOUR_GET_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().isOk());
}
@Test
public void shouldDenyForGetTours() throws Exception {
final String mail = "shouldDenyForGetTours@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.TEST_USER));
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(TOUR_GET_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldDenyWithNoToken() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(TOUR_GET_ENDPOINT)).andExpect(status().is4xxClientError());
}
}

View File

@ -0,0 +1,205 @@
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.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.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
import com.plannaplan.entities.User;
import com.plannaplan.services.UserService;
import com.plannaplan.types.UserRoles;
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
public class DeveloperControllerTest extends AbstractControllerTest {
private static final String ACCEPT_ENDPOINT = "/api/v1/developer/algorythm/accept";
private static final String EXCHANGE_ENDPOINT = "/api/v1/developer/algorythm/exchange";
private static final String FIRST_TOUR_ENDPOINT = "/api/v1/developer/tours/first";
private static final String NO_TOUR_ENDPOINT = "/api/v1/developer/tours/no";
private static final String SECOND_TOUR_ENDPOINT = "/api/v1/developer/tours/second";
@Autowired
private UserService userService;
/* ACCEPT TESTS */
@Test
public void shouldFailWithWrongAcces() throws Exception {
final String mail = "shouldFailWithWrongAcces@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.TEST_USER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ACCEPT_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkPerformingAcceptAlgotyrhm() throws Exception {
final String mail = "shouldOkPerformingAcceptAlgotyrhm@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.DEVELOPER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ACCEPT_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().isOk());
}
@Test
public void shouldFailWithNoToken() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ACCEPT_ENDPOINT)).andExpect(status().is4xxClientError());
}
/* EXCHANGE TESTS */
@Test
public void shouldFailExchangeWithWrongAcces() throws Exception {
final String mail = "shouldFailExchangeWithWrongAcces@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.TEST_USER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkExchangeAlgotyrhm() throws Exception {
final String mail = "shouldOkExchangeAlgotyrhm@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.DEVELOPER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().isOk());
}
@Test
public void shouldFailExchaneWithNoToken() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(EXCHANGE_ENDPOINT)).andExpect(status().is4xxClientError());
}
/* FIRST TOUR */
@Test
public void shouldFailFirstTourWithWrongAcces() throws Exception {
final String mail = "shouldFailFirstTourWithWrongAcces@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.TEST_USER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(FIRST_TOUR_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkFirstTourAlgotyrhm() throws Exception {
final String mail = "shouldOkFirstTourAlgotyrhm@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.DEVELOPER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(FIRST_TOUR_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldFirstTourWithNoToken() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(FIRST_TOUR_ENDPOINT)).andExpect(status().is4xxClientError());
}
/* SECOND TOUR */
@Test
public void shouldFailSecondTourWithWrongAcces() throws Exception {
final String mail = "shouldFailSecondTourWithWrongAcces@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.TEST_USER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(SECOND_TOUR_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkSecondTourAlgotyrhm() throws Exception {
final String mail = "shouldOkSecondTourAlgotyrhm@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.DEVELOPER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(SECOND_TOUR_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldFailSecondTourWithNoToken() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(SECOND_TOUR_ENDPOINT)).andExpect(status().is4xxClientError());
}
/* NO TOUR */
@Test
public void shouldNoTourWithWrongAcces() throws Exception {
final String mail = "shouldNoTourWithWrongAcces@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.TEST_USER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(NO_TOUR_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkNoAlgotyrhm() throws Exception {
final String mail = "shouldOkNoAlgotyrhm@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.DEVELOPER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(NO_TOUR_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().isOk());
}
@Test
public void shouldFailNoTourWithNoToken() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(NO_TOUR_ENDPOINT)).andExpect(status().is4xxClientError());
}
}