Skip to content

Commit 3b33e05

Browse files
committed
Fix tests
1 parent 4283b9c commit 3b33e05

File tree

4 files changed

+63
-42
lines changed

4 files changed

+63
-42
lines changed

server/src/test/java/eu/solven/kumite/app/it/security/TestSecurity_WithFakeUser.java

+43
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package eu.solven.kumite.app.it.security;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
35
import java.nio.charset.StandardCharsets;
6+
import java.util.List;
47
import java.util.Map;
58

69
import org.assertj.core.api.Assertions;
710
import org.junit.jupiter.api.Test;
811
import org.junit.jupiter.api.extension.ExtendWith;
912
import org.springframework.http.HttpHeaders;
1013
import org.springframework.http.MediaType;
14+
import org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers;
1115
import org.springframework.test.context.ActiveProfiles;
1216
import org.springframework.test.context.junit.jupiter.SpringExtension;
1317

@@ -29,6 +33,24 @@
2933
@Slf4j
3034
public class TestSecurity_WithFakeUser extends TestSecurity_WithOAuth2User {
3135

36+
@Override
37+
@SuppressWarnings({ "unchecked", "rawtypes" })
38+
protected void onLoginOptions(Map loginOptions) {
39+
Map<String, ?> asMap = (Map<String, ?>) loginOptions.get("map");
40+
assertThat(asMap).hasSize(3).containsKeys("github", "google", "fakeuser");
41+
42+
Assertions.assertThat((Map) asMap.get("github")).containsEntry("login_url", "/oauth2/authorization/github");
43+
44+
List<Map<String, ?>> asList = (List<Map<String, ?>>) loginOptions.get("list");
45+
assertThat(asList).hasSize(3).anySatisfy(m -> {
46+
Assertions.assertThat((Map) m).containsEntry("login_url", "/oauth2/authorization/github").hasSize(3);
47+
}).anySatisfy(m -> {
48+
Assertions.assertThat((Map) m).containsEntry("login_url", "/oauth2/authorization/google");
49+
}).anySatisfy(m -> {
50+
Assertions.assertThat((Map) m).containsEntry("login_url", "/html/login/basic");
51+
});
52+
}
53+
3254
@Test
3355
@Override
3456
public void testLoginAccessToken() {
@@ -77,4 +99,25 @@ public void testLoginAccessToken_invalidUser() {
7799
.expectStatus()
78100
.isUnauthorized();
79101
}
102+
103+
@Test
104+
public void testLoginBasic() {
105+
log.debug("About {}", KumiteLoginController.class);
106+
107+
webTestClient
108+
109+
// https://www.baeldung.com/spring-security-csrf
110+
.mutateWith(SecurityMockServerConfigurers.csrf())
111+
112+
.post()
113+
.uri("/api/login/v1/basic")
114+
.accept(MediaType.APPLICATION_JSON)
115+
.header(HttpHeaders.AUTHORIZATION,
116+
"Basic " + HttpHeaders
117+
.encodeBasicAuth("someUnknownUser", "no_password", StandardCharsets.UTF_8))
118+
.exchange()
119+
120+
.expectStatus()
121+
.isUnauthorized();
122+
}
80123
}

server/src/test/java/eu/solven/kumite/app/it/security/TestSecurity_WithJwtUser.java

+3-22
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44

55
import java.time.Duration;
6-
import java.util.List;
76
import java.util.Map;
87
import java.util.Set;
98

@@ -63,10 +62,8 @@ protected String generateAccessToken() {
6362
}
6463

6564
protected String generateRefreshToken() {
66-
return tokenService.generateAccessToken(RandomPlayer.user(),
67-
Set.of(RandomPlayer.PLAYERID_1),
68-
Duration.ofMinutes(1),
69-
true);
65+
return tokenService
66+
.generateAccessToken(RandomPlayer.user(), Set.of(RandomPlayer.PLAYERID_1), Duration.ofMinutes(1), true);
7067
}
7168

7269
@Test
@@ -120,23 +117,7 @@ public void testLoginOptions() {
120117

121118
.expectStatus()
122119
.isOk()
123-
.expectBody(Map.class)
124-
.value(greeting -> {
125-
Map<String, ?> asMap = (Map<String, ?>) greeting.get("map");
126-
assertThat(asMap).hasSize(2).containsOnlyKeys("github", "google");
127-
128-
Assertions.assertThat((Map) asMap.get("github"))
129-
.containsEntry("login_url", "/oauth2/authorization/github");
130-
131-
List<Map<String, ?>> asList = (List<Map<String, ?>>) greeting.get("list");
132-
assertThat(asList).hasSize(2).anySatisfy(m -> {
133-
Assertions.assertThat((Map) m)
134-
.containsEntry("login_url", "/oauth2/authorization/github")
135-
.hasSize(2);
136-
}).anySatisfy(m -> {
137-
Assertions.assertThat((Map) m).containsEntry("login_url", "/oauth2/authorization/google");
138-
});
139-
});
120+
.expectBody(Map.class);
140121
}
141122

142123
@Test

server/src/test/java/eu/solven/kumite/app/it/security/TestSecurity_WithOAuth2User.java

+17-15
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,26 @@ public void testLoginOptions() {
111111
.expectStatus()
112112
.isOk()
113113
.expectBody(Map.class)
114-
.value(greeting -> {
115-
Map<String, ?> asMap = (Map<String, ?>) greeting.get("map");
116-
assertThat(asMap).hasSize(2).containsOnlyKeys("github", "google");
117-
118-
Assertions.assertThat((Map) asMap.get("github"))
119-
.containsEntry("login_url", "/oauth2/authorization/github");
120-
121-
List<Map<String, ?>> asList = (List<Map<String, ?>>) greeting.get("list");
122-
assertThat(asList).hasSize(2).anySatisfy(m -> {
123-
Assertions.assertThat((Map) m)
124-
.containsEntry("login_url", "/oauth2/authorization/github")
125-
.hasSize(2);
126-
}).anySatisfy(m -> {
127-
Assertions.assertThat((Map) m).containsEntry("login_url", "/oauth2/authorization/google");
128-
});
114+
.value(loginOptions -> {
115+
onLoginOptions(loginOptions);
129116
});
130117
}
131118

119+
@SuppressWarnings({ "unchecked", "rawtypes" })
120+
protected void onLoginOptions(Map loginOptions) {
121+
Map<String, ?> asMap = (Map<String, ?>) loginOptions.get("map");
122+
assertThat(asMap).hasSize(2).containsKeys("github", "google");
123+
124+
Assertions.assertThat((Map) asMap.get("github")).containsEntry("login_url", "/oauth2/authorization/github");
125+
126+
List<Map<String, ?>> asList = (List<Map<String, ?>>) loginOptions.get("list");
127+
assertThat(asList).hasSize(2).anySatisfy(m -> {
128+
Assertions.assertThat((Map) m).containsEntry("login_url", "/oauth2/authorization/github").hasSize(3);
129+
}).anySatisfy(m -> {
130+
Assertions.assertThat((Map) m).containsEntry("login_url", "/oauth2/authorization/google");
131+
});
132+
}
133+
132134
@Test
133135
public void testLoginUser() {
134136
log.debug("About {}", KumiteLoginController.class);

server/src/test/java/eu/solven/kumite/app/it/security/TestSecurity_WithoutAuth.java

-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ public class TestSecurity_WithoutAuth {
4242
@Autowired
4343
private WebTestClient webTestClient;
4444

45-
// @Bean
46-
// public SessionRepository<?> inmemorySessionRepository() {
47-
// return new MapSessionRepository(new ConcurrentHashMap<>());
48-
// }
49-
5045
@Test
5146
public void testApiPublic() {
5247
log.debug("About {}", GreetingHandler.class);

0 commit comments

Comments
 (0)