Skip to content

Commit

Permalink
add test to ensure permissions are active without login
Browse files Browse the repository at this point in the history
  • Loading branch information
bseber authored and derTobsch committed May 24, 2024
1 parent 38cf87a commit 14549f7
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
<input
type="checkbox"
id="permissions-permissions-edit-all-checkbox"
data-test-id="permissions-edit-all-checkbox"
th:field="*{permissionsEditAll}"
/>
<label
Expand All @@ -129,6 +130,7 @@
<button
type="submit"
class="bg-blue-700 border border-blue-700 text-white font-medium rounded px-4 py-2 flex items-center gap-2 justify-center sm:justify-start"
data-test-id="permissions-submit-button"
>
<svg th:replace="~{icons/save::svg(className='w-5 h-5')}"></svg>
<th:block th:text="#{usermanagement.save}">
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/templates/usermanagement/users.html
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ <h1 class="text-xl font-bold" th:text="#{usermanagement.heading}">
data-turbo-frame="person-frame"
data-turbo-action="advance"
th:text="#{usermanagement.navigation.permissions}"
data-test-id="users-permissions-link"
>
Berechtigungen
</a>
Expand Down
87 changes: 87 additions & 0 deletions src/test/java/de/focusshift/zeiterfassung/ui/PermissionUIIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package de.focusshift.zeiterfassung.ui;

import com.microsoft.playwright.Page;
import com.microsoft.playwright.Response;
import de.focusshift.zeiterfassung.TestKeycloakContainer;
import de.focusshift.zeiterfassung.TestPostgreSQLContainer;
import de.focusshift.zeiterfassung.ui.extension.UiTest;
import de.focusshift.zeiterfassung.ui.pages.LoginPage;
import de.focusshift.zeiterfassung.ui.pages.NavigationPage;
import de.focusshift.zeiterfassung.ui.pages.UsersPage;
import de.focusshift.zeiterfassung.ui.pages.users.UserPermissionsPage;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@SpringBootTest(webEnvironment = RANDOM_PORT)
@Testcontainers
@UiTest
class PermissionUIIT {

@LocalServerPort
private int port;

@Container
private static final TestPostgreSQLContainer postgre = new TestPostgreSQLContainer();
@Container
private static final TestKeycloakContainer keycloak = new TestKeycloakContainer();

@DynamicPropertySource
static void containerProperties(DynamicPropertyRegistry registry) {
postgre.configureSpringDataSource(registry);
keycloak.configureSpringDataSource(registry);
}

@Test
void ensurePermissionsAreImmediatelyActiveWithoutNewLogin(Page boss_page) {

final NavigationPage boss_navigationPage = new NavigationPage(boss_page);
final UsersPage boss_usersPage = new UsersPage(boss_page);
final UserPermissionsPage boss_userPermissionsPage = new UserPermissionsPage(boss_page);

final String user_username = "Klaus Müller";
final Page user_page = boss_page.context().browser().newContext().newPage();
final NavigationPage user_navigationPage = new NavigationPage(user_page);

login("boss", "secret", boss_page);

login("user", "secret", user_page);
assertThat(user_navigationPage.usersLink()).not().isAttached();

// Boss: add permission to user
boss_navigationPage.goToUsersPage();
boss_usersPage.selectPerson(user_username);
boss_usersPage.goToPermissionsSettings();
assertThat(boss_userPermissionsPage.getAllowedToEditPermissionsCheckbox()).not().isChecked();
boss_userPermissionsPage.getAllowedToEditPermissionsCheckbox().click();
boss_userPermissionsPage.submit();

// User: navigation should be updated
user_page.waitForResponse(Response::ok, user_page::reload);
assertThat(user_navigationPage.usersLink()).isAttached();

// Boss: remove permission from user
assertThat(boss_userPermissionsPage.getAllowedToEditPermissionsCheckbox()).isChecked();
boss_userPermissionsPage.getAllowedToEditPermissionsCheckbox().click();
boss_userPermissionsPage.submit();

// User: navigation should be updated
user_page.waitForResponse(Response::ok, user_page::reload);
assertThat(user_navigationPage.usersLink()).not().isAttached();

user_navigationPage.logout();
boss_navigationPage.logout();
}

private void login(String username, String password, Page page) {
page.navigate("http://localhost:" + port + "/oauth2/authorization/keycloak");
new LoginPage(page).login(new LoginPage.Credentials(username, password));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public void goToWorkingTimeAccountSettings() {
goTo("[data-test-id=users-working-time-account-link]");
}

public void goToPermissionsSettings() {
goTo("[data-test-id=users-permissions-link]");
}

private void goTo(String selector) {
page.waitForResponse(Response::ok, () -> page.locator(selector).click());
page.waitForLoadState(DOMCONTENTLOADED);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.focusshift.zeiterfassung.ui.pages.users;

import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Response;

import static com.microsoft.playwright.options.LoadState.DOMCONTENTLOADED;

public class UserPermissionsPage {

private final Page page;

public UserPermissionsPage(Page page) {
this.page = page;
}

public Locator getAllowedToEditPermissionsCheckbox() {
return page.locator("[data-test-id=permissions-edit-all-checkbox]");
}

public void submit() {
final Locator submitButton = page.locator("[data-test-id=permissions-submit-button]");
page.waitForResponse(Response::ok, submitButton::click);
page.waitForLoadState(DOMCONTENTLOADED);
}
}

0 comments on commit 14549f7

Please sign in to comment.