Skip to content

Commit

Permalink
Login Steps created
Browse files Browse the repository at this point in the history
  • Loading branch information
xipi3000 committed Mar 6, 2024
1 parent 17b7262 commit 77e2c06
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/test/java/cat/udl/eps/softarch/demo/steps/LoginStepDefs.java
Original file line number Diff line number Diff line change
@@ -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("[email protected]");
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());
}



}

0 comments on commit 77e2c06

Please sign in to comment.