From eb1faa5c4512c201c8077922484943947d732b23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pol=20Escol=C3=A0=20Curc=C3=B3?= Date: Tue, 5 Mar 2024 17:28:11 +0100 Subject: [PATCH 1/6] new login feature --- src/test/resources/features/LoginUser.feature | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/test/resources/features/LoginUser.feature diff --git a/src/test/resources/features/LoginUser.feature b/src/test/resources/features/LoginUser.feature new file mode 100644 index 00000000..d5cadafa --- /dev/null +++ b/src/test/resources/features/LoginUser.feature @@ -0,0 +1,34 @@ +Feature: Login User + In order to use the app + As a user + I want to login myself to access it + + Scenario: Login already registered user + Given I'm not logged in + When I enter my registered username "user" with password "password" + Then The response code is 201 + And I am successfully logged in + + Scenario: Login with a non existing user + Given I'm not logged in + When I enter username "sdafddsaf" and password "password" + Then The response code is 404 + And I am prompt a error saying that the username or password are wrong + + Scenario: Login with a incorrect password + Given I'm not logged in + When I login with username "user" and password "afsdfsaafs" + Then The response code is 404 + And I am prompt a error saying that the username or password are wrong + + Scenario: Login with empty username + Given I'm not logged in + When I log in with username "" and password "passsword" + Then The response code is 406 + And The error message is "must not be blank" + + Scenario: Login with empty password + Given I'm not logged in + When I log in with username "user" and password "" + Then The response code is 406 + And The error message is "must not be blank" \ No newline at end of file From 17b726240d50e540d501859c8edef4e54132c0f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pol=20Escol=C3=A0=20Curc=C3=B3?= Date: Wed, 6 Mar 2024 18:45:15 +0100 Subject: [PATCH 2/6] Features changes --- src/test/resources/features/LoginUser.feature | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/test/resources/features/LoginUser.feature b/src/test/resources/features/LoginUser.feature index d5cadafa..97cbdd1c 100644 --- a/src/test/resources/features/LoginUser.feature +++ b/src/test/resources/features/LoginUser.feature @@ -4,31 +4,35 @@ Feature: Login User I want to login myself to access it Scenario: Login already registered user - Given I'm not logged in - When I enter my registered username "user" with password "password" - Then The response code is 201 - And I am successfully logged in + Given There is a registered user with username "user" and password "password" + And I'm not logged in + When I login with username "user" and password "password" + Then The response code is 200 + And I can login with username "user" and password "password" Scenario: Login with a non existing user - Given I'm not logged in - When I enter username "sdafddsaf" and password "password" + Given There isn't registered user with username "anonymous" + And I'm not logged in + When I login with username "anonymous" and password "password" Then The response code is 404 - And I am prompt a error saying that the username or password are wrong + And The error message is "wrong user or password" Scenario: Login with a incorrect password - Given I'm not logged in - When I login with username "user" and password "afsdfsaafs" + Given There is a registered user with username "user" and password "password" + And I'm not logged in + When I login with username "user" and password "anonymous" Then The response code is 404 - And I am prompt a error saying that the username or password are wrong + And The error message is "wrong user or password" Scenario: Login with empty username Given I'm not logged in - When I log in with username "" and password "passsword" - Then The response code is 406 + When I login with username "" and password "password" + Then The response code is 404 And The error message is "must not be blank" Scenario: Login with empty password - Given I'm not logged in - When I log in with username "user" and password "" - Then The response code is 406 + Given There is a registered user with username "user" and password "password" + And I'm not logged in + When I login with username "user" and password "" + Then The response code is 400 And The error message is "must not be blank" \ No newline at end of file From 77e2c0601b5437d610bbee82a8ad353183d82435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pol=20Escol=C3=A0=20Curc=C3=B3?= Date: Wed, 6 Mar 2024 18:46:25 +0100 Subject: [PATCH 3/6] Login Steps created --- .../softarch/demo/steps/LoginStepDefs.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/test/java/cat/udl/eps/softarch/demo/steps/LoginStepDefs.java diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/LoginStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/LoginStepDefs.java new file mode 100644 index 00000000..027c9326 --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/LoginStepDefs.java @@ -0,0 +1,61 @@ +package cat.udl.eps.softarch.demo.steps; + +import cat.udl.eps.softarch.demo.domain.User; +import cat.udl.eps.softarch.demo.repository.UserRepository; +import io.cucumber.java.en.And; +import io.cucumber.java.en.Given; +import io.cucumber.java.en.When; +import org.junit.Assert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; + +import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +public class LoginStepDefs { + @Autowired + private StepDefs stepDefs; + + @Autowired + private UserRepository userRepository; + + @Given("^There isn't registered user with username \"([^\"]*)\"$") + public void thereIsNoRegisteredUserWithUsername(String user) { + Assert.assertFalse("User \"" + + user + "\"shouldn't exist", + userRepository.existsById(user)); + } + + @Given("^There is a registered user with username \"([^\"]*)\" and password \"([^\"]*)\"$") + public void thereIsARegisteredUserWithUsernameAndPassword(String username, String password) { + if(!userRepository.existsById(username)){ + User user = new User(); + user.setId(username); + user.setEmail("user@user.cat"); + user.setPassword(password); + user.encodePassword(); + userRepository.save(user); + } + } + + @And("^And I'm not logged in$") + public void iMNotLoggedIn(String username) throws Throwable { + + } + + @When("^I login with username \"([^\"]*)\" and password \"([^\"]*)\"$") + public void iLoginWithUsernameAndPassword(String username, String password) throws Exception { + stepDefs.result = stepDefs.mockMvc.perform( + get("/users/{username}", username) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()) + .andExpect(jsonPath("$.password").doesNotExist()); + } + + + +} From ec72d55ac94f9a83fcfe5742e7cf70f0e942545e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pol=20Escol=C3=A0=20Curc=C3=B3?= Date: Thu, 7 Mar 2024 16:43:42 +0100 Subject: [PATCH 4/6] login steps changes --- .../eps/softarch/demo/steps/LoginStepDefs.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/LoginStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/LoginStepDefs.java index 027c9326..f1a066e3 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/LoginStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/LoginStepDefs.java @@ -41,19 +41,18 @@ public void thereIsARegisteredUserWithUsernameAndPassword(String username, Strin } } - @And("^And I'm not logged in$") - public void iMNotLoggedIn(String username) throws Throwable { - } @When("^I login with username \"([^\"]*)\" and password \"([^\"]*)\"$") public void iLoginWithUsernameAndPassword(String username, String password) throws Exception { + AuthenticationStepDefs.currentUsername = username; + AuthenticationStepDefs.currentPassword = password; + stepDefs.result = stepDefs.mockMvc.perform( - get("/users/{username}", username) - .accept(MediaType.APPLICATION_JSON) - .with(AuthenticationStepDefs.authenticate())) - .andDo(print()) - .andExpect(jsonPath("$.password").doesNotExist()); + get("/identity") + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); } From 3f76052f48596f85eb11a3ce09533b19a7c17afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pol=20Escol=C3=A0=20Curc=C3=B3?= Date: Wed, 13 Mar 2024 17:43:49 +0100 Subject: [PATCH 5/6] login response is now generic --- src/test/resources/features/LoginUser.feature | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/test/resources/features/LoginUser.feature b/src/test/resources/features/LoginUser.feature index 97cbdd1c..83aa1069 100644 --- a/src/test/resources/features/LoginUser.feature +++ b/src/test/resources/features/LoginUser.feature @@ -14,25 +14,23 @@ Feature: Login User Given There isn't registered user with username "anonymous" And I'm not logged in When I login with username "anonymous" and password "password" - Then The response code is 404 - And The error message is "wrong user or password" + Then The response code is 401 + #And The error message is "wrong user or password" Scenario: Login with a incorrect password Given There is a registered user with username "user" and password "password" And I'm not logged in When I login with username "user" and password "anonymous" - Then The response code is 404 - And The error message is "wrong user or password" + Then The response code is 401 + #And The error message is "wrong user or password" Scenario: Login with empty username Given I'm not logged in When I login with username "" and password "password" - Then The response code is 404 - And The error message is "must not be blank" + Then The response code is 401 Scenario: Login with empty password Given There is a registered user with username "user" and password "password" And I'm not logged in When I login with username "user" and password "" - Then The response code is 400 - And The error message is "must not be blank" \ No newline at end of file + Then The response code is 401 From 6707b0676c5a2c8474ee89a3d7a4a4055128a445 Mon Sep 17 00:00:00 2001 From: Ant Date: Wed, 13 Mar 2024 17:59:54 +0100 Subject: [PATCH 6/6] implemented missing methods --- .../cat/udl/eps/softarch/demo/repository/PetRepository.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/cat/udl/eps/softarch/demo/repository/PetRepository.java b/src/main/java/cat/udl/eps/softarch/demo/repository/PetRepository.java index f14f319f..6529d265 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/repository/PetRepository.java +++ b/src/main/java/cat/udl/eps/softarch/demo/repository/PetRepository.java @@ -9,6 +9,8 @@ public interface PetRepository extends CrudRepository, PagingAndSortingRepository { List findBySize(@Param("size") String size); List findByName(@Param("name") String name); + List findByIsAdopted(@Param("isAdopted") Boolean isAdopted); + List findByBreed(@Param("breed") String breed); //Pet findByShelter(@Param("id") Long shelterId); }