Skip to content

Commit f0a5443

Browse files
Merge pull request #26 from hamzanassour/master
Unit Testing
2 parents ac7741d + 28b88d6 commit f0a5443

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

search-in-keycloak-with-spring-boot/src/test/java/com/example/searchinkeycloakwithspringboot/SearchInKeycloakWithSpringBootApplicationTests.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.example.searchinkeycloakwithspringboot;
22

33
import java.util.List;
4+
import java.util.Set;
5+
46
import org.junit.jupiter.api.Assertions;
57
import org.junit.jupiter.api.Test;
68
import org.keycloak.admin.client.Keycloak;
@@ -17,14 +19,14 @@ class SearchInKeycloakWithSpringBootApplicationTests {
1719

1820
@Test
1921
void testSearchById() {
20-
String id = "test-id";
22+
String id = "967a26d3-9a37-4a3b-9c22-c774f2933158";
2123
UserResource userResource = keycloak.realm("master").users().get(id);
2224
Assertions.assertEquals(id, userResource.toRepresentation().getId());
2325
}
2426

2527
@Test
2628
void testSearchByEmail() {
27-
String email = "test-email";
29+
String email = "user1@howtodoinjava.com";
2830
List<UserRepresentation> userRepresentationsList = keycloak.realm("master").users()
2931
.searchByEmail(email, true);
3032
Assertions.assertEquals(1, userRepresentationsList.size());
@@ -33,11 +35,29 @@ void testSearchByEmail() {
3335

3436
@Test
3537
void testSearchByUsername() {
36-
String username = "username";
38+
String username = "user1";
3739
List<UserRepresentation> userRepresentationsList = keycloak.realm("master").users()
3840
.searchByUsername(username, true);
3941
Assertions.assertEquals(1, userRepresentationsList.size());
4042
Assertions.assertEquals(username, userRepresentationsList.get(0).getUsername());
4143
}
44+
@Test
45+
void testSearchByPhone() {
46+
String phone = "1234567890";
47+
List<UserRepresentation> userRepresentationsList = keycloak.realm("master").users()
48+
.searchByAttributes("phone:" + phone);
49+
Assertions.assertEquals(1, userRepresentationsList.size());
50+
Assertions.assertEquals(phone, userRepresentationsList.get(0).firstAttribute("phone"));
51+
}
52+
@Test
53+
void testSearchByRole() {
54+
String role = "admin";
55+
Set<UserRepresentation> userRepresentationSet =keycloak.realm("master").roles().get(role)
56+
.getRoleUserMembers();
57+
Assertions.assertEquals(1, userRepresentationSet.size());
58+
for (UserRepresentation userRepresentation : userRepresentationSet) {
59+
Assertions.assertEquals("admin" , userRepresentation.getUsername());
60+
}
61+
}
4262

4363
}

0 commit comments

Comments
 (0)