diff --git a/README.md b/README.md index bc96534..e7b1daa 100755 --- a/README.md +++ b/README.md @@ -35,11 +35,11 @@ spring.profiles.active=prod Jeżeli chcemy zmienić jakieś opcję dla pordukcji to robimy to w tym sammym katalogi w pliku `application-prod.properties` i dla dev analogicznie w `application-dev.properties`. W paczce dla proda w protpertiesach poufne dane odczytywane są ze zmiennych środowiskowych systemu na którym odpalana jest aplikacja. Ustawić trzeba następujące zmienne: -- PLANNAPLAN_MYSQL_DB_HOST - host bazy danych np `localhost` -- PLANNAPLAN_MYSQL_DB_PORT - port na którym działa baza -- PLANNAPLAN_MYSQL_DB - nazwa bazy dancyh. W profilu **dev** jest to np test -- PLANNAPLAN_MYSQL_DB_USERNAME - nazwa użytkownika bazy -- PLANNAPLAN_MYSQL_DB_PASSWORD - hasło użytkownika bazy +- `PLANNAPLAN_MYSQL_DB_HOST` - host bazy danych np `localhost` +- `PLANNAPLAN_MYSQL_DB_PORT` - port na którym działa baza +- `PLANNAPLAN_MYSQL_DB` - nazwa bazy dancyh. W profilu **dev** jest to np test +- `PLANNAPLAN_MYSQL_DB_USERNAME` - nazwa użytkownika bazy +- `PLANNAPLAN_MYSQL_DB_PASSWORD` - hasło użytkownika bazy ## Packaging diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java index 206fa34..c7af201 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java @@ -12,29 +12,25 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; /** - * UserRepository.getByAuthority: - * Return list of: - * SELECT * FROM User WHERE email = i. + * UserRepository.getByAuthority: Return list of: SELECT * FROM User WHERE email + * = i. * - * Where i, ?1 are equale to variables. + * Where i, ?1 are equale to variables. * - * UserRepository.getByToken: - * Return list of: - * SELECT * FROM User WHERE token = i. + * UserRepository.getByToken: Return list of: SELECT * FROM User WHERE token = + * i. * - * Where i, ?1 are equale to variables. + * Where i, ?1 are equale to variables. * - * UserRepository.searchForUsers: - * Return list of: - * SELECT * FROM User WHERE (name LIKE %?1% OR surname LIKE %?1%). + * UserRepository.searchForUsers: Return list of: SELECT * FROM User WHERE (name + * LIKE %?1% OR surname LIKE %?1%). * - * Where i, ?1 are equale to variables. + * Where i, ?1 are equale to variables. * - * UserRepository.searchForUsers with role: - * Return list of: - * SELECT * FROM User WHERE (name LIKE %?1% OR surname LIKE %?1%) AND role=?2"). + * UserRepository.searchForUsers with role: Return list of: SELECT * FROM User + * WHERE (name LIKE %?1% OR surname LIKE %?1%) AND role=?2"). * - * Where i, ?1 are equale to variables. + * Where i, ?1 are equale to variables. */ @Repository @@ -51,6 +47,9 @@ public interface UserRepository extends JpaRepository { @Query("FROM User WHERE (name LIKE %?1% OR surname LIKE %?1%) AND role=?2") List searchForUsers(@Param("query") String query, @Param("role") UserRoles role); + @Query("FROM User WHERE role=?1") + List getAllByRole(@Param("role") UserRoles role); + @Query("FROM User WHERE usosId = ?1") Optional getByUsosId(@Param("usosId") String usosId); } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java index 9056720..d4d2130 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java @@ -77,4 +77,8 @@ public class UserService { return this.repo.findById(userId); } + public List getAllStudents() { + return this.repo.getAllByRole(UserRoles.STUDENT); + } + } \ No newline at end of file diff --git a/restservice/pom.xml b/restservice/pom.xml index 7c82417..4ba0b83 100755 --- a/restservice/pom.xml +++ b/restservice/pom.xml @@ -17,21 +17,21 @@ http://www.example.com - - dev - - dev - - - true - - - - prod - - prod - - + + dev + + dev + + + true + + + + prod + + prod + + diff --git a/restservice/src/main/java/com/plannaplan/App.java b/restservice/src/main/java/com/plannaplan/App.java index 5e54f59..438335e 100755 --- a/restservice/src/main/java/com/plannaplan/App.java +++ b/restservice/src/main/java/com/plannaplan/App.java @@ -120,6 +120,11 @@ public class App { newuser.setSurname("Sad"); newuser.setRole(UserRoles.STUDENT); this.userService.save(newuser); + + newuser = new User(); + newuser.setEmail("iamnull@st.amu.edu.pl"); + newuser.setRole(UserRoles.STUDENT); + this.userService.save(newuser); } System.out.println(Logo.getStartedInfo(isDev)); diff --git a/restservice/src/main/java/com/plannaplan/Swagger2Config.java b/restservice/src/main/java/com/plannaplan/Swagger2Config.java index 755928d..19ad14d 100755 --- a/restservice/src/main/java/com/plannaplan/Swagger2Config.java +++ b/restservice/src/main/java/com/plannaplan/Swagger2Config.java @@ -24,14 +24,10 @@ public class Swagger2Config extends WebMvcConfigurationSupport { @Bean public Docket createRestApi() { - Parameter authHeader = new ParameterBuilder() - .parameterType("header") - .name("Authorization") - .modelRef(new ModelRef("string")) - .build(); + Parameter authHeader = new ParameterBuilder().parameterType("header").name("Authorization") + .modelRef(new ModelRef("string")).build(); return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() - .apis(RequestHandlerSelectors.basePackage("com.plannaplan")).paths(PathSelectors.any()) - .build() + .apis(RequestHandlerSelectors.basePackage("com.plannaplan")).paths(PathSelectors.any()).build() .globalOperationParameters(Collections.singletonList(authHeader)); } diff --git a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java index 42d2742..0af2431 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java @@ -2,6 +2,7 @@ package com.plannaplan.controllers; import java.util.HashMap; import java.util.List; +import java.util.Optional; import com.plannaplan.App; import com.plannaplan.entities.Groups; @@ -13,9 +14,11 @@ import com.plannaplan.services.GroupService; 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.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -57,4 +60,20 @@ public class GroupController { } return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups), HttpStatus.OK); } + + @PutMapping("/{id}/capacity") + @PreAuthorize("hasRole('ROLE_DEANERY')") + @ApiOperation(value = "Change capacity of given group. You need to provide DEANERY token to be ale to change capacity") + public ResponseEntity updateCapacity( + @PathVariable(name = "id", required = true) @ApiParam(value = "id of group to change capacity") Long id, + @RequestParam(name = "newcapacity", required = true) Integer newcapacity) { + final Optional group = this.groupService.getGroupById(id); + if (group.isEmpty()) { + return new ResponseEntity<>("Given group doens't exist", HttpStatus.NOT_FOUND); + } + final Groups gr = group.get(); + gr.setCapacity(newcapacity); + this.groupService.save(gr); + return new ResponseEntity<>("Success", HttpStatus.OK); + } } \ No newline at end of file diff --git a/restservice/src/main/java/com/plannaplan/controllers/UsersController.java b/restservice/src/main/java/com/plannaplan/controllers/UsersController.java index b6ec259..52900f8 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/UsersController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/UsersController.java @@ -47,7 +47,7 @@ public class UsersController { @PreAuthorize("hasRole('ROLE_DEANERY')") @ApiOperation(value = "Gets all students. You need token with DEANERY role to call this") public ResponseEntity> getAllStudents() { - final List searches = this.userService.searchForStudents(""); + final List searches = this.userService.getAllStudents(); final List response = UserResponseMappers.mapToDefaultResponse(searches); return new ResponseEntity<>(response, HttpStatus.OK); } diff --git a/restservice/src/main/resources/application-dev.properties b/restservice/src/main/resources/application-dev.properties index 7b49197..37863ad 100755 --- a/restservice/src/main/resources/application-dev.properties +++ b/restservice/src/main/resources/application-dev.properties @@ -8,6 +8,7 @@ spring.jpa.hibernate.ddl-auto=create-drop spring.jackson.serialization.fail-on-empty-beans=false spring.main.allow-bean-definition-overriding=true spring.jackson.default-property-inclusion = NON_NULL +logging.level.io.swagger.models.parameters.AbstractSerializableParameter=ERROR server.port=1285 plannaplan.dev = true plannaplan.frontendUrl= http://localhost:3000 \ No newline at end of file diff --git a/restservice/src/main/resources/application-prod.properties b/restservice/src/main/resources/application-prod.properties index 46e3b11..84077c0 100755 --- a/restservice/src/main/resources/application-prod.properties +++ b/restservice/src/main/resources/application-prod.properties @@ -8,6 +8,7 @@ spring.jpa.hibernate.ddl-auto=update spring.jackson.serialization.fail-on-empty-beans=false spring.main.allow-bean-definition-overriding=true spring.jackson.default-property-inclusion = NON_NULL +logging.level.io.swagger.models.parameters.AbstractSerializableParameter=ERROR server.port=1285 diff --git a/restservice/src/test/java/com/plannaplan/controllers/GroupControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/GroupControllerTest.java index 756f046..0d6dde5 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/GroupControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/GroupControllerTest.java @@ -2,6 +2,7 @@ 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; @@ -9,7 +10,17 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; + +import com.plannaplan.entities.Groups; +import com.plannaplan.entities.User; +import com.plannaplan.services.GroupService; +import com.plannaplan.services.UserService; +import com.plannaplan.types.UserRoles; +import com.plannaplan.types.WeekDay; @RunWith(SpringRunner.class) @SpringBootTest @@ -17,6 +28,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. public class GroupControllerTest extends AbstractControllerTest { private static final String GROUPS_BY_COURSE_ENDPOINT = "/api/v1/groups/course"; + private static final String UPDATE_GROUPS_ENDPOINT = "/api/v1/groups/521/capacity"; + + @Autowired + private UserService service; + + @Autowired + private GroupService groupService; @Test public void shouldFailWithNoParaeter() throws Exception { @@ -29,4 +47,59 @@ public class GroupControllerTest extends AbstractControllerTest { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc.perform(get(GROUPS_BY_COURSE_ENDPOINT + "/2")).andExpect(status().isOk()); } + + @Test + public void shouldFailWithNoToken() throws Exception { + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(put(UPDATE_GROUPS_ENDPOINT)).andExpect(status().is4xxClientError()); + } + + @Test + public void shouldFailWithStudentToken() throws Exception { + final User user = this.service + .save(new User(null, null, "shouldFailWithStudentToken@GroupController.test", UserRoles.STUDENT)); + final String token = this.service.login(user).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(put(UPDATE_GROUPS_ENDPOINT).header("Authorization", "Bearer " + token)) + .andExpect(status().is4xxClientError()); + } + + @Test + public void shouldFailWithNoParams() throws Exception { + final User user = this.service + .save(new User(null, null, "shouldFailWithNoParams@GroupController.test", UserRoles.DEANERY)); + final String token = this.service.login(user).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(put(UPDATE_GROUPS_ENDPOINT).header("Authorization", "Bearer " + token)) + .andExpect(status().is4xxClientError()); + } + + @Test + public void shouldFailWithWrongId() throws Exception { + final User user = this.service + .save(new User(null, null, "shouldFailWithWrongId@GroupController.test", UserRoles.DEANERY)); + final String token = this.service.login(user).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(put("/api/v1/groups/" + user.getId() + "/capacity").param("newcapacity", "23") + .header("Authorization", "Bearer " + token)).andExpect(status().isNotFound()); + + } + + @Test + public void shouldChangeCapacity() throws Exception { + final User user = this.service + .save(new User(null, null, "shouldChangeCapacity@GroupController.test", UserRoles.DEANERY)); + final String token = this.service.login(user).getToken(); + final Groups newGroup = this.groupService.save(new Groups(43, "A-Nigzdie", null, 436, WeekDay.FRIDAY, null)); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(put("/api/v1/groups/" + newGroup.getId() + "/capacity").param("newcapacity", "23") + .header("Authorization", "Bearer " + token)).andExpect(status().isOk()); + + assertTrue(this.groupService.getGroupById(newGroup.getId()).get().getCapacity() == 23); + } + }