Repository navigation
Expand file tree
/
Copy pathSsoUserMapperTest.java
More file actions
135 lines (107 loc) · 4.41 KB
/
Copy pathSsoUserMapperTest.java
File metadata and controls
135 lines (107 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package utils;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.junit.jupiter.api.Test;
public class SsoUserMapperTest {
private static Properties ssoProperties() {
Properties props = new Properties();
props.setProperty("sso.saml.ssoName", "uid");
props.setProperty("sso.saml.userName", "displayName");
props.setProperty("sso.saml.affiliation", "groups");
props.setProperty("sso.saml.adminAffiliation", "staff, teachers");
props.setProperty("sso.saml.playerAffiliation", "students");
return props;
}
private static Map<String, Object> attributes(Object groups) {
Map<String, Object> attributes = new HashMap<>();
attributes.put("uid", Collections.singletonList("jdoe"));
attributes.put("displayName", Collections.singletonList("Jane Doe"));
attributes.put("groups", groups);
return attributes;
}
@Test
public void map_AdminAffiliation_IsAdmin() {
SsoUserMapper.Result result =
SsoUserMapper.map(attributes(Arrays.asList("students", "teachers")), ssoProperties());
assertTrue(result.isValid());
assertEquals("jdoe", result.ssoName);
assertEquals("Jane Doe", result.userName);
assertEquals("admin", result.userRole);
}
@Test
public void map_PlayerAffiliation_IsPlayer() {
SsoUserMapper.Result result =
SsoUserMapper.map(attributes(Collections.singletonList("students")), ssoProperties());
assertTrue(result.isValid());
assertEquals("player", result.userRole);
}
@Test
public void map_SingleValuedAttributes_AreRead() {
Map<String, Object> attributes = new HashMap<>();
attributes.put("uid", "jdoe");
attributes.put("displayName", "Jane Doe");
attributes.put("groups", "staff");
SsoUserMapper.Result result = SsoUserMapper.map(attributes, ssoProperties());
assertTrue(result.isValid());
assertEquals("jdoe", result.ssoName);
assertEquals("admin", result.userRole);
}
@Test
public void map_NoMatchingAffiliation_ListsAllowedGroups() {
SsoUserMapper.Result result =
SsoUserMapper.map(attributes(Collections.singletonList("alumni")), ssoProperties());
assertFalse(result.isValid());
assertEquals(
"Authorization failed. Please ensure that you are a member of one of the following"
+ " groups: staff, teachers, students, ",
result.error);
}
@Test
public void map_NoAffiliationAttribute_IsRejected() {
Map<String, Object> attributes = attributes(null);
attributes.remove("groups");
assertFalse(SsoUserMapper.map(attributes, ssoProperties()).isValid());
}
@Test
public void map_MissingUserName_IsRejected() {
Map<String, Object> attributes = attributes(Collections.singletonList("staff"));
attributes.remove("displayName");
SsoUserMapper.Result result = SsoUserMapper.map(attributes, ssoProperties());
assertFalse(result.isValid());
assertEquals("Required SSO attributes are missing", result.error);
}
@Test
public void map_BlankSsoName_IsRejected() {
for (String blank : new String[] {"", " "}) {
Map<String, Object> attributes = attributes(Collections.singletonList("staff"));
attributes.put("uid", Collections.singletonList(blank));
SsoUserMapper.Result result = SsoUserMapper.map(attributes, ssoProperties());
assertFalse(result.isValid());
assertEquals("Required SSO attributes are missing", result.error);
}
}
@Test
public void map_BlankUserName_IsRejected() {
Map<String, Object> attributes = attributes(Collections.singletonList("staff"));
attributes.put("displayName", " ");
assertFalse(SsoUserMapper.map(attributes, ssoProperties()).isValid());
}
@Test
public void map_NonBlankValue_IsKeptAsSent() {
Map<String, Object> attributes = attributes(Collections.singletonList("staff"));
attributes.put("uid", Collections.singletonList(" jdoe "));
assertEquals(" jdoe ", SsoUserMapper.map(attributes, ssoProperties()).ssoName);
}
@Test
public void map_NoAttributes_IsRejected() {
SsoUserMapper.Result result = SsoUserMapper.map(Collections.emptyMap(), ssoProperties());
assertFalse(result.isValid());
assertEquals("You don't have any attributes", result.error);
}
}