From 7d4424f4e089d90ec22389aaa5daff7a22a68cf2 Mon Sep 17 00:00:00 2001 From: ddangelorb Date: Sun, 24 Oct 2021 15:31:10 -0300 Subject: [PATCH] Adding existsBy methods --- index.html | 2 +- .../wpdauth/controller/UsersController.java | 46 ++++++++++++- .../wpdauth/dto/UsersRequestDTO.java | 2 +- .../wpdauth/security/WebSecurityConfig.java | 2 + .../wpdauth/service/UsersService.java | 6 +- .../UsersServiceIntegrationTest.java | 14 ++++ swagger.yaml | 64 ++++++++++++++++++- 7 files changed, 130 insertions(+), 6 deletions(-) diff --git a/index.html b/index.html index 1ba8144..fb5086a 100644 --- a/index.html +++ b/index.html @@ -1,5 +1,5 @@ - + diff --git a/src/main/java/org/waterproofingdata/wpdauth/controller/UsersController.java b/src/main/java/org/waterproofingdata/wpdauth/controller/UsersController.java index 8e4a87f..532d48f 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/controller/UsersController.java +++ b/src/main/java/org/waterproofingdata/wpdauth/controller/UsersController.java @@ -31,6 +31,50 @@ public class UsersController { @Autowired private UsersService userService; + @PostMapping("/existsByUsername") + @ApiOperation( + value = "${UserController.existsByUsername}", + notes = "From a username, this method returns if there is a username in db or not." + ) + @ApiResponses(value = {// + @ApiResponse(code = 400, message = "Something went wrong") + } + ) + public boolean existsByUsername(// + @ApiParam( + name = "username", + type = "String", + value = "username of the user", + example = "This is an unique field, and consumers should be aware of it. By convention, WP6 should send the user phone number (i.e. (99)99999-9999).", + required = true + ) + @RequestParam String username + ) { + return userService.existsByUsername(username); + } + + @PostMapping("/existsByNickname") + @ApiOperation( + value = "${UserController.existsByNickname}", + notes = "From a nickname, this method returns if there is a nickname in db or not." + ) + @ApiResponses(value = {// + @ApiResponse(code = 400, message = "Something went wrong") + } + ) + public boolean existsByNickname(// + @ApiParam( + name = "nickname", + type = "String", + value = "nickname of the user", + example = "This is an unique field, and consumers should be aware of it.", + required = true + ) + @RequestParam String nickname + ) { + return userService.existsByNickname(nickname); + } + @PostMapping("/login") @ApiOperation( value = "${UserController.login}", @@ -77,7 +121,7 @@ public class UsersController { name = "user", value = "Signup User", required = true - ) + ) @RequestBody UsersRequestDTO user ) { return userService.signup(CustomMapper.map(user, Users.class)); diff --git a/src/main/java/org/waterproofingdata/wpdauth/dto/UsersRequestDTO.java b/src/main/java/org/waterproofingdata/wpdauth/dto/UsersRequestDTO.java index 251f0d8..1e647f8 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/dto/UsersRequestDTO.java +++ b/src/main/java/org/waterproofingdata/wpdauth/dto/UsersRequestDTO.java @@ -139,7 +139,7 @@ public class UsersRequestDTO { name = "roles", dataType = "String", value = "roles of the user.", - example = "i.e. ROLE_CLIENT.", + example = "i.e. [ROLE_CLIENT].", required = true ) private List roles; diff --git a/src/main/java/org/waterproofingdata/wpdauth/security/WebSecurityConfig.java b/src/main/java/org/waterproofingdata/wpdauth/security/WebSecurityConfig.java index d5c3510..3193244 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/security/WebSecurityConfig.java +++ b/src/main/java/org/waterproofingdata/wpdauth/security/WebSecurityConfig.java @@ -35,6 +35,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { http.authorizeRequests()// .antMatchers("/users/login").permitAll()// .antMatchers("/users/signup").permitAll()// + .antMatchers("/users/existsByUsername").permitAll()// + .antMatchers("/users/existsByNickname").permitAll()// .antMatchers("/h2-console/**/**").permitAll() // Disallow everything else.. .anyRequest().authenticated(); diff --git a/src/main/java/org/waterproofingdata/wpdauth/service/UsersService.java b/src/main/java/org/waterproofingdata/wpdauth/service/UsersService.java index 24aafcb..d5156c8 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/service/UsersService.java +++ b/src/main/java/org/waterproofingdata/wpdauth/service/UsersService.java @@ -67,6 +67,10 @@ public class UsersService { return usersRepository.existsByUsername(username); } + public boolean existsByNickname(String nickname) { + return usersRepository.existsByNickname(nickname); + } + public Users search(String username) { Users user = usersRepository.findByUsername(username); if (user == null) { @@ -101,7 +105,7 @@ public class UsersService { if (user.getNickname().length() == 0) { throw new CustomException("Nickname must be provided", HttpStatus.UNPROCESSABLE_ENTITY); } - else if (usersRepository.existsByNickname(user.getNickname())) { + else if (existsByNickname(user.getNickname())) { String nickname_unique = usersRepository.findSuggestedNickname(user.getNickname()); throw new CustomException(String.format("Nickname already exists. Would you like to use '%s'?", nickname_unique), HttpStatus.UNPROCESSABLE_ENTITY); } diff --git a/src/test/java/org/waterproofingdata/wpdauth/integrationtest/UsersServiceIntegrationTest.java b/src/test/java/org/waterproofingdata/wpdauth/integrationtest/UsersServiceIntegrationTest.java index 30188eb..414a5fc 100644 --- a/src/test/java/org/waterproofingdata/wpdauth/integrationtest/UsersServiceIntegrationTest.java +++ b/src/test/java/org/waterproofingdata/wpdauth/integrationtest/UsersServiceIntegrationTest.java @@ -16,6 +16,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.HttpStatus; +import org.waterproofingdata.wpdauth.dto.UsersRequestDTO; import org.waterproofingdata.wpdauth.exception.CustomException; import org.waterproofingdata.wpdauth.model.EduCemadenOrganizations; import org.waterproofingdata.wpdauth.model.Roles; @@ -52,6 +53,17 @@ public class UsersServiceIntegrationTest { u.setTermsofusage(true); u.setRoles(new ArrayList(Arrays.asList(role))); String uJson = new Gson().toJson(u); + + UsersRequestDTO urDTO = new UsersRequestDTO(); + urDTO.setUsername(uName); + urDTO.setNickname(uName); + urDTO.setPassword(UUID.randomUUID().toString()); + urDTO.setState("SP"); + urDTO.setCity("São Paulo"); + urDTO.setTermsofusage(true); + urDTO.setRoles(new ArrayList(Arrays.asList(role))); + String urDTOJson = new Gson().toJson(u); + return u; } @@ -90,6 +102,8 @@ public class UsersServiceIntegrationTest { Users u = setUpUserTest("user_", Roles.ROLE_CLIENT); String signup = usersService.signup(u); assertNotNull(signup, "Signup token returned from usersService.signup(user) should not be null"); + assertEquals(true, usersService.existsByUsername(u.getUsername())); + assertEquals(true, usersService.existsByNickname(u.getNickname())); } @Test diff --git a/swagger.yaml b/swagger.yaml index 54f02c4..072fe08 100644 --- a/swagger.yaml +++ b/swagger.yaml @@ -1,7 +1,7 @@ swagger: '2.0' info: description: 'This is a sample JWT authentication service. You can find out more about JWT at [https://jwt.io/](https://jwt.io/). For this sample, you can use the `admin` or `client` users (password: admin and client respectively) to test the authorization filters. Once you have successfully logged in and obtained the token, you should click on the right top button `Authorize` and introduce it with the prefix "Bearer ".' - version: 1.0.3 + version: 1.0.5 title: The authenticator for the Waterproofing Data (WPD) Work Packages contact: email: igsd@warwick.ac.uk @@ -263,6 +263,66 @@ paths: - global - apiKey: [] deprecated: false + /users/existsByNickname: + post: + tags: + - users + summary: ${UserController.existsByNickname} + description: From a nickname, this method returns if there is a nickname in db or not. + operationId: existsByNicknameUsingPOST + consumes: + - application/json + produces: + - '*/*' + parameters: + - name: nickname + in: query + description: nickname of the user + required: true + type: string + allowEmptyValue: false + x-example: This is an unique field, and consumers should be aware of it. + responses: + '200': + description: OK + schema: + type: boolean + '400': + description: Something went wrong + security: + - Authorization: + - global + deprecated: false + /users/existsByUsername: + post: + tags: + - users + summary: ${UserController.existsByUsername} + description: From a username, this method returns if there is a username in db or not. + operationId: existsByUsernameUsingPOST + consumes: + - application/json + produces: + - '*/*' + parameters: + - name: username + in: query + description: username of the user + required: true + type: string + allowEmptyValue: false + x-example: This is an unique field, and consumers should be aware of it. By convention, WP6 should send the user phone number (i.e. (99)99999-9999). + responses: + '200': + description: OK + schema: + type: boolean + '400': + description: Something went wrong + security: + - Authorization: + - global + deprecated: false /users/login: post: tags: @@ -555,7 +615,7 @@ definitions: description: terms of usage of the user. roles: type: array - example: i.e. ROLE_CLIENT. + example: i.e. [ROLE_CLIENT]. description: roles of the user. items: type: string