-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/test/java/cat/udl/eps/softarch/demo/steps/LoginStepDefs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
|
||
|
||
} |