Skip to content

Commit

Permalink
User management integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pelayori committed Apr 26, 2024
1 parent 6dbbd28 commit 3725378
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ public InsertSampleDataService(PlayerService playerService, QuestionService ques
@Transactional
@EventListener(ApplicationReadyEvent.class) // Uncomment this line to insert sample data on startup
public void insertSampleQuestions() throws InterruptedException, IOException {
if (!playerService.getUserByEmail("[email protected]").isPresent()) {
if (playerService.getUserByEmail("[email protected]").isEmpty()) {
PlayerDto player = new PlayerDto();
player.setEmail("[email protected]");
player.setUsername("test");
player.setPassword("test");
player.setRoles(new String[]{"ROLE_USER"});
if (Arrays.asList(environment.getActiveProfiles()).contains("test"))
player.setRoles(new String[]{"ROLE_USER", "ROLE_ADMIN"});
else
player.setRoles(new String[]{"ROLE_USER"});
playerService.generateApiKey(playerService.addNewPlayer(player));
}
}
Expand Down
86 changes: 86 additions & 0 deletions src/test/java/com/uniovi/steps/AdminTasksStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.uniovi.steps;

import com.uniovi.Wiq_IntegrationTests;
import com.uniovi.util.PropertiesExtractor;
import com.uniovi.util.SeleniumUtils;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Then;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.List;

public class AdminTasksStep extends Wiq_IntegrationTests {
@Then("I should see the admin zone button")
public void iShouldSeeTheAdminZoneButton() {
List<WebElement> elems = SeleniumUtils.waitLoadElements(driver, By.cssSelector("a.btn.btn-primary[href='/player/admin']"), 5);
Assertions.assertEquals(1, elems.size());
}

@And("I enter the admin zone")
public void iEnterTheAdminZone() {
List<WebElement> elems = SeleniumUtils.waitLoadElements(driver, By.cssSelector("a.btn.btn-primary[href='/player/admin']"), 5);
elems.get(0).click();
}

@And("I should see the message {string}")
public void iShouldSeeTheAdminZone(String message) {
SeleniumUtils.waitLoadElementsBy(driver, "text", p.getString(message, PropertiesExtractor.getSPANISH()), 5);
}

@Then("I click {string} button")
public void iClickButton(String text) {
List<WebElement> elems;
if (!text.contains(".")) {
elems = SeleniumUtils.waitLoadElementsBy(driver, "id", text, 5);
} else {
elems = SeleniumUtils.waitLoadElementsBy(driver, "free", "//button[contains(text(),'" + p.getString(text, PropertiesExtractor.getSPANISH()) + "')]", 5);
}
elems.get(0).click();
}

@And("I should see the modal dialog {string}")
public void iShouldSeeTheModalDialog(String title) {
SeleniumUtils.waitLoadElementsBy(driver, "text", p.getString(title, PropertiesExtractor.getSPANISH()), 5);
}

@And("I fill in the new password {string}")
public void iFillInTheNewPassword(String pass) {
WebElement password = driver.findElement(By.id("changePasswordInput"));
password.click();
password.clear();
password.sendKeys(pass);
}

@And("I wait until modal dialog is closed")
public void iWaitUntilModalDialogIsClosed() {
SeleniumUtils.waitInvisibleElement(driver, By.id("changePasswordAdminModal"), 5);
SeleniumUtils.waitInvisibleElement(driver, By.cssSelector(".modal-backdrop.fade"), 5);
}

@Then("I fill in the new role {string}")
public void iFillInTheNewRole(String role) {
WebElement password = driver.findElement(By.id("newRole"));
password.click();
password.clear();
password.sendKeys(role);
}

@And("I see the text {string}")
public void iSeeTheText(String text) {
SeleniumUtils.waitLoadElementsBy(driver, "text", text, 5);
}

@And("I click {string} button for user {string}")
public void iClickButtonForUser(String btn, String user) {
List<WebElement> elems = SeleniumUtils.waitLoadElements(driver, By.cssSelector("button[data-bs-username='"+ user +"']"), 5).stream()
.filter(e -> e.getText().equals(p.getString(btn, PropertiesExtractor.getSPANISH()))).toList();
elems.get(0).click();
}

@Then("I should not see the text {string}")
public void iShouldNotSeeTheText(String text) {
SeleniumUtils.waitInvisibleElement(driver, By.xpath("//*[contains(text(),'" + text + "')]"), 5);
}
}
14 changes: 14 additions & 0 deletions src/test/java/com/uniovi/util/SeleniumUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ static public void waitElementNotPresent(WebDriver driver, String xpath, int tim
Assertions.assertTrue(resultado);
}

static public List<WebElement> waitLoadElements(WebDriver driver, By by, int timeout)
{
WebElement result =
(new WebDriverWait(driver, Duration.ofSeconds(timeout))).until(ExpectedConditions.visibilityOfElementLocated(by));
Assertions.assertNotNull(result);
return driver.findElements(by);
}

/**
* Espera por la visibilidad de un elemento/s en la vista actualmente cargandose en driver. Para ello se empleará una consulta xpath
* según varios criterios..
Expand Down Expand Up @@ -124,4 +132,10 @@ static public void waitSeconds(WebDriver driver, int seconds){
}
}
}

public static void waitInvisibleElement(WebDriver driver, By by, int timeout) {
Boolean resultado =
(new WebDriverWait(driver, Duration.ofSeconds(timeout))).until(ExpectedConditions.invisibilityOfElementLocated(by));
Assertions.assertTrue(resultado);
}
}
59 changes: 59 additions & 0 deletions src/test/resources/features/Admin.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Feature: I do admin tasks
Scenario: I can see the admin tasks
Given I am in the home page
When I click the login button
And I fill in the form with valid data email: "test" password: "test"
And I press the login button
Then I should see the admin zone button
And I enter the admin zone
And I should see the message "navbar.section.admin"

Scenario: I change user password
Given I am in the home page
When I click the login button
And I fill in the form with valid data email: "test" password: "test"
And I press the login button
And I enter the admin zone
And I should see the message "navbar.section.admin"
And I click "user.details" button
And I click "admin.changepassword" button
And I should see the modal dialog "modal.password.title"
And I fill in the new password "1234"
And I click "changePasswordConfirm" button
Then I wait until modal dialog is closed
And I logout
And I fill in the form with valid data email: "test" password: "1234"
And I press the login button
And I should see the admin zone button

Scenario: I add a role to user
Given I am in the home page
When I click the login button
And I fill in the form with valid data email: "test" password: "test"
And I press the login button
And I enter the admin zone
And I should see the message "navbar.section.admin"
And I click "user.details" button
And I click "admin.changeroles" button
And I should see the modal dialog "admin.roles.change"
Then I fill in the new role "ROLE_CUCUMBER"
And I click "changeRolesConfirm" button
And I wait until modal dialog is closed
And I see the text "ROLE_CUCUMBER"

Scenario: A user signs up and I delete it
Given I am not registered or logged in
And I am on the register page
When I fill in the form with valid data username: "user1" email: "[email protected]" password: "password" password_confirmation: "password"
And I press the register button
Then I should see the profile page
And I logout
And I fill in the form with valid data email: "test" password: "test"
And I press the login button
And I enter the admin zone
And I should see the message "navbar.section.admin"
When I click "user.details" button for user "user1"
And I click "admin.user.delete" button for user "user1"
And I should see the modal dialog "admin.user.delete.title"
And I click "deleteModalConfirm" button
Then I should not see the text "user1"

0 comments on commit 3725378

Please sign in to comment.