|
| 1 | +package com.hivemq.api.auth.provider.impl.simple; |
| 2 | + |
| 3 | +import com.hivemq.api.auth.ApiPrincipal; |
| 4 | +import com.hivemq.http.core.UsernamePasswordRoles; |
| 5 | +import org.jetbrains.annotations.NotNull; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; |
| 11 | + |
| 12 | +class SimpleUsernameRolesProviderImplTest { |
| 13 | + |
| 14 | + public static final Set<@NotNull String> TEST_ROLES = Set.of("TEST_ROLE"); |
| 15 | + public static final String USER_NAME = "user1"; |
| 16 | + |
| 17 | + @Test |
| 18 | + public void testUsingCorrectPasswordSucceeds() { |
| 19 | + final var simpleProvider = new SimpleUsernameRolesProviderImpl(); |
| 20 | + simpleProvider.add(new UsernamePasswordRoles(USER_NAME, "pass1".getBytes(), TEST_ROLES)); |
| 21 | + assertThat(simpleProvider.findByUsernameAndPassword(USER_NAME, "pass1".getBytes())) |
| 22 | + .isNotEmpty() |
| 23 | + .get() |
| 24 | + .satisfies(usernamePasswordRoles -> { |
| 25 | + assertThat(usernamePasswordRoles.roles()) |
| 26 | + .containsExactly("TEST_ROLE"); |
| 27 | + assertThat(usernamePasswordRoles.username()) |
| 28 | + .isEqualTo(USER_NAME); |
| 29 | + assertThat(usernamePasswordRoles.toPrincipal()) |
| 30 | + .isEqualTo(new ApiPrincipal(USER_NAME, TEST_ROLES)); |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testUsingIncorrectPasswordFails() { |
| 36 | + final var simpleProvider = new SimpleUsernameRolesProviderImpl(); |
| 37 | + simpleProvider.add(new UsernamePasswordRoles(USER_NAME, "pass1".getBytes(), TEST_ROLES)); |
| 38 | + assertThat(simpleProvider.findByUsernameAndPassword(USER_NAME, "incorrect".getBytes())) |
| 39 | + .isEmpty(); |
| 40 | + } |
| 41 | +} |
0 commit comments