Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blacelle committed Sep 24, 2024
1 parent 4283b9c commit 3b33e05
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package eu.solven.kumite.app.it.security;

import static org.assertj.core.api.Assertions.assertThat;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

Expand All @@ -29,6 +33,24 @@
@Slf4j
public class TestSecurity_WithFakeUser extends TestSecurity_WithOAuth2User {

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void onLoginOptions(Map loginOptions) {
Map<String, ?> asMap = (Map<String, ?>) loginOptions.get("map");
assertThat(asMap).hasSize(3).containsKeys("github", "google", "fakeuser");

Assertions.assertThat((Map) asMap.get("github")).containsEntry("login_url", "/oauth2/authorization/github");

List<Map<String, ?>> asList = (List<Map<String, ?>>) loginOptions.get("list");
assertThat(asList).hasSize(3).anySatisfy(m -> {
Assertions.assertThat((Map) m).containsEntry("login_url", "/oauth2/authorization/github").hasSize(3);
}).anySatisfy(m -> {
Assertions.assertThat((Map) m).containsEntry("login_url", "/oauth2/authorization/google");
}).anySatisfy(m -> {
Assertions.assertThat((Map) m).containsEntry("login_url", "/html/login/basic");
});
}

@Test
@Override
public void testLoginAccessToken() {
Expand Down Expand Up @@ -77,4 +99,25 @@ public void testLoginAccessToken_invalidUser() {
.expectStatus()
.isUnauthorized();
}

@Test
public void testLoginBasic() {
log.debug("About {}", KumiteLoginController.class);

webTestClient

// https://www.baeldung.com/spring-security-csrf
.mutateWith(SecurityMockServerConfigurers.csrf())

.post()
.uri("/api/login/v1/basic")
.accept(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION,
"Basic " + HttpHeaders
.encodeBasicAuth("someUnknownUser", "no_password", StandardCharsets.UTF_8))
.exchange()

.expectStatus()
.isUnauthorized();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -63,10 +62,8 @@ protected String generateAccessToken() {
}

protected String generateRefreshToken() {
return tokenService.generateAccessToken(RandomPlayer.user(),
Set.of(RandomPlayer.PLAYERID_1),
Duration.ofMinutes(1),
true);
return tokenService
.generateAccessToken(RandomPlayer.user(), Set.of(RandomPlayer.PLAYERID_1), Duration.ofMinutes(1), true);
}

@Test
Expand Down Expand Up @@ -120,23 +117,7 @@ public void testLoginOptions() {

.expectStatus()
.isOk()
.expectBody(Map.class)
.value(greeting -> {
Map<String, ?> asMap = (Map<String, ?>) greeting.get("map");
assertThat(asMap).hasSize(2).containsOnlyKeys("github", "google");

Assertions.assertThat((Map) asMap.get("github"))
.containsEntry("login_url", "/oauth2/authorization/github");

List<Map<String, ?>> asList = (List<Map<String, ?>>) greeting.get("list");
assertThat(asList).hasSize(2).anySatisfy(m -> {
Assertions.assertThat((Map) m)
.containsEntry("login_url", "/oauth2/authorization/github")
.hasSize(2);
}).anySatisfy(m -> {
Assertions.assertThat((Map) m).containsEntry("login_url", "/oauth2/authorization/google");
});
});
.expectBody(Map.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,26 @@ public void testLoginOptions() {
.expectStatus()
.isOk()
.expectBody(Map.class)
.value(greeting -> {
Map<String, ?> asMap = (Map<String, ?>) greeting.get("map");
assertThat(asMap).hasSize(2).containsOnlyKeys("github", "google");

Assertions.assertThat((Map) asMap.get("github"))
.containsEntry("login_url", "/oauth2/authorization/github");

List<Map<String, ?>> asList = (List<Map<String, ?>>) greeting.get("list");
assertThat(asList).hasSize(2).anySatisfy(m -> {
Assertions.assertThat((Map) m)
.containsEntry("login_url", "/oauth2/authorization/github")
.hasSize(2);
}).anySatisfy(m -> {
Assertions.assertThat((Map) m).containsEntry("login_url", "/oauth2/authorization/google");
});
.value(loginOptions -> {
onLoginOptions(loginOptions);
});
}

@SuppressWarnings({ "unchecked", "rawtypes" })
protected void onLoginOptions(Map loginOptions) {
Map<String, ?> asMap = (Map<String, ?>) loginOptions.get("map");
assertThat(asMap).hasSize(2).containsKeys("github", "google");

Assertions.assertThat((Map) asMap.get("github")).containsEntry("login_url", "/oauth2/authorization/github");

List<Map<String, ?>> asList = (List<Map<String, ?>>) loginOptions.get("list");
assertThat(asList).hasSize(2).anySatisfy(m -> {
Assertions.assertThat((Map) m).containsEntry("login_url", "/oauth2/authorization/github").hasSize(3);
}).anySatisfy(m -> {
Assertions.assertThat((Map) m).containsEntry("login_url", "/oauth2/authorization/google");
});
}

@Test
public void testLoginUser() {
log.debug("About {}", KumiteLoginController.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ public class TestSecurity_WithoutAuth {
@Autowired
private WebTestClient webTestClient;

// @Bean
// public SessionRepository<?> inmemorySessionRepository() {
// return new MapSessionRepository(new ConcurrentHashMap<>());
// }

@Test
public void testApiPublic() {
log.debug("About {}", GreetingHandler.class);
Expand Down

0 comments on commit 3b33e05

Please sign in to comment.