diff --git a/src/main/java/com/uniovi/services/InsertSampleDataService.java b/src/main/java/com/uniovi/services/InsertSampleDataService.java index f22b1aa8..0b2b1b83 100644 --- a/src/main/java/com/uniovi/services/InsertSampleDataService.java +++ b/src/main/java/com/uniovi/services/InsertSampleDataService.java @@ -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("test@test.com").isPresent()) { + if (playerService.getUserByEmail("test@test.com").isEmpty()) { PlayerDto player = new PlayerDto(); player.setEmail("test@test.com"); 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)); } } diff --git a/src/test/java/com/uniovi/steps/AdminTasksStep.java b/src/test/java/com/uniovi/steps/AdminTasksStep.java new file mode 100644 index 00000000..128f70ad --- /dev/null +++ b/src/test/java/com/uniovi/steps/AdminTasksStep.java @@ -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 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 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 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 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); + } +} diff --git a/src/test/java/com/uniovi/util/SeleniumUtils.java b/src/test/java/com/uniovi/util/SeleniumUtils.java index cea746d9..1faa5988 100644 --- a/src/test/java/com/uniovi/util/SeleniumUtils.java +++ b/src/test/java/com/uniovi/util/SeleniumUtils.java @@ -73,6 +73,14 @@ static public void waitElementNotPresent(WebDriver driver, String xpath, int tim Assertions.assertTrue(resultado); } + static public List 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.. @@ -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); + } } diff --git a/src/test/resources/features/Admin.feature b/src/test/resources/features/Admin.feature new file mode 100644 index 00000000..4a52685c --- /dev/null +++ b/src/test/resources/features/Admin.feature @@ -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: "user1@gmail.com" 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" \ No newline at end of file