Skip to content

Commit 00c6382

Browse files
committed
Coverage
1 parent ea95398 commit 00c6382

20 files changed

Lines changed: 529 additions & 74 deletions

client/src/pages/InvitationForm.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export const InvitationForm = () => {
132132
{(!initial && isEmpty(invitation.invites)) &&
133133
<ErrorIndicator msg={I18n.t("invitation.requiredEmail")}/>}
134134

135-
{authorityOptions.length > 0 &&
135+
{authorityOptions.length > 1 &&
136136
<SelectField
137137
value={authorityOptions.find(option => option.value === invitation.intendedAuthority)
138138
|| authorityOptions[authorityOptions.length - 1]}

server/src/main/java/access/api/ManageController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public ResponseEntity<List<Map<String, Object>>> policies(@Parameter(hidden = tr
127127
public ResponseEntity<Map<String, Object>> createPolicy(User user, @RequestBody Map<String, Object> policy) {
128128
LOG.debug("/createPolicy for " + policy + " for " + user.getEmail());
129129

130-
policyAccessAllowed(user, policy, true);
130+
policyAccessAllowed(user, policy);
131131
return ResponseEntity.ok(manage.createPolicy(policy));
132132
}
133133

@@ -136,7 +136,7 @@ public ResponseEntity<Map<String, Object>> createPolicy(User user, @RequestBody
136136
public ResponseEntity<Map<String, Object>> updatePolicy(User user, @RequestBody Map<String, Object> policy) {
137137
LOG.debug("/updatePolicy for " + policy + " for " + user.getEmail());
138138

139-
policyAccessAllowed(user, policy, true);
139+
policyAccessAllowed(user, policy);
140140
return ResponseEntity.ok(manage.updatePolicy(policy));
141141
}
142142

@@ -147,7 +147,7 @@ public ResponseEntity<Void> deletePolicy(User user, @PathVariable String policyI
147147

148148
LOG.debug("/deletePolicy for " + policy + " for " + user.getEmail());
149149

150-
policyAccessAllowed(user, policy, true);
150+
policyAccessAllowed(user, policy);
151151
manage.deletePolicy(policy);
152152
return ResponseEntity.noContent().build();
153153
}
@@ -190,7 +190,7 @@ public ResponseEntity<Map<String, Object>> rejectChangeRequest(User user, @Reque
190190
return Results.okResult();
191191
}
192192

193-
private void policyAccessAllowed(User user, Map<String, Object> policy, boolean throwException) {
193+
private void policyAccessAllowed(User user, Map<String, Object> policy) {
194194
confirmInstitutionAdmin(user);
195195
//We don't want to use PolicyDefinition as @RequestBody, because the template from Manage is leading
196196
PolicyDefinition policyDefinition = this.objectMapper.convertValue(policy.get("data"), PolicyDefinition.class);

server/src/main/java/access/api/StatisticsController.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package access.api;
22

33
import access.model.User;
4+
import access.stats.Scale;
45
import access.stats.Statistics;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.web.bind.annotation.GetMapping;
@@ -14,7 +15,7 @@
1415
@RequestMapping("/api/v1/stats")
1516
public class StatisticsController {
1617

17-
private Statistics statistics;
18+
private final Statistics statistics;
1819

1920
@Autowired
2021
public StatisticsController(Statistics statistics) {
@@ -26,10 +27,10 @@ public StatisticsController(Statistics statistics) {
2627
public List<Object> loginTimeFrame(User user,
2728
@RequestParam("from") long from,
2829
@RequestParam("to") long to,
29-
@RequestParam("scale") String scale,
30+
@RequestParam("scale") Scale scale,
3031
@RequestParam(value = "spEntityId", required = false) String spEntityId) {
3132
String authenticatingAuthority = user.getAuthenticatingAuthority();
32-
return statistics.loginTimeFrame(from, to, scale, authenticatingAuthority, spEntityId);
33+
return statistics.loginTimeFrame(from, to, scale.name(), authenticatingAuthority, spEntityId);
3334
}
3435

3536
//Used for retrieval of all logins for all SPs

server/src/main/java/access/manage/PolicyAccessRights.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import access.exception.UserRestrictionException;
44
import access.model.User;
55

6+
import java.util.HashSet;
67
import java.util.List;
78
import java.util.Map;
89

@@ -25,7 +26,7 @@ default void confirmPolicyAccess(User user,
2526
.equals(authenticatingAuthority)) {
2627
throwUserRestrictionException(user, policyDefinition);
2728
}
28-
//All SP's must be linked to the IdP of the user (=organizationGUID of the instituitonAdmin)
29+
//All SP's must be linked to the IdP of the user (=authenticatingAuthority of the User)
2930
List<String> serviceProviderIdentifiers = policyDefinition.getServiceProviderIds().stream()
3031
.map(policyProvider -> policyProvider.getName())
3132
.toList();
@@ -35,7 +36,7 @@ default void confirmPolicyAccess(User user,
3536
.stream()
3637
.map(allowedEntry -> allowedEntry.get("name"))
3738
.toList();
38-
if (!allowedEntities.containsAll(serviceProviderIdentifiers)) {
39+
if (!new HashSet<>(allowedEntities).containsAll(serviceProviderIdentifiers)) {
3940
throwUserRestrictionException(user, policyDefinition);
4041
}
4142
}

server/src/main/java/access/manage/PolicyDefinition.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import lombok.Setter;
88

99
import java.util.ArrayList;
10+
import java.util.HashMap;
1011
import java.util.List;
1112
import java.util.Map;
1213

@@ -33,13 +34,13 @@ public class PolicyDefinition {
3334

3435
private String entityid;
3536

36-
private List<PolicyProvider> identityProviderIds;
37+
private List<PolicyProvider> identityProviderIds = new ArrayList<>();
3738

38-
private Map<String, String> metaDataFields;
39+
private Map<String, String> metaDataFields = new HashMap<>();
3940

4041
private String name;
4142

42-
private List<PolicyProvider> serviceProviderIds;
43+
private List<PolicyProvider> serviceProviderIds = new ArrayList<>();
4344

4445
private String type;
4546

server/src/main/java/access/model/User.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ public void nameInvariant() {
161161
}
162162
}
163163

164-
public User(boolean superUser, String eppn, String sub, String schacHomeOrganization, String givenName, String familyName, String email) {
164+
public User(boolean superUser, String eppn, String sub, String schacHomeOrganization, String givenName,
165+
String familyName, String email, String authenticatingAuthority) {
165166
this.superUser = superUser;
166167
this.eduPersonPrincipalName = eppn;
167168
this.sub = sub;
@@ -170,6 +171,7 @@ public User(boolean superUser, String eppn, String sub, String schacHomeOrganiza
170171
this.familyName = familyName;
171172
this.name = String.format("%s %s", givenName, familyName);
172173
this.email = email;
174+
this.authenticatingAuthority = authenticatingAuthority;
173175
this.createdAt = Instant.now();
174176
this.lastActivity = Instant.now();
175177
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package access.stats;
2+
3+
public enum Scale {
4+
5+
minute, hour, day, week, month, quarter, year;
6+
7+
}

server/src/main/java/access/stats/StatisticsMock.java

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public List<Object> loginTimeFrame(long from, long to, String scale, String idpE
3131
List<Object> result = new ArrayList<>();
3232
for (long i = from; i <= to; i += step) {
3333
Map<String, Object> point = new HashMap<>();
34-
point.put("count_user_id", countValue(scale));
34+
point.put("count_user_id", countValue());
3535
if (!"minute".equals(scale) && !"hour".equals(scale)) {
36-
point.put("distinct_count_user_id", countValue(scale));
36+
point.put("distinct_count_user_id", countValue());
3737
}
3838
if (StringUtils.hasText(spEntityId)) {
3939
point.put("sp_entity_id", spEntityId);
@@ -48,23 +48,27 @@ public List<Object> loginTimeFrame(long from, long to, String scale, String idpE
4848
@Override
4949
public List<Object> loginAggregated(String period, String idpEntityId, String spEntityId) {
5050
Calendar today = Calendar.getInstance();
51-
today.set(Calendar.YEAR, Integer.valueOf(period.substring(0, 4)));
51+
today.set(Calendar.YEAR, Integer.parseInt(period.substring(0, 4)));
5252
today.set(Calendar.HOUR_OF_DAY, 0);
5353
today.set(Calendar.MINUTE, 0);
5454
today.set(Calendar.DAY_OF_MONTH, 1);
5555
if (period.length() > 4) {
5656
switch (period.substring(4, 5).toUpperCase()) {
5757
case "Q": {
58-
today.set(Calendar.MONTH, ((Integer.valueOf(period.substring(5)) - 1) * 3));
58+
today.set(Calendar.MONTH, ((Integer.parseInt(period.substring(5)) - 1) * 3));
59+
break;
5960
}
6061
case "M": {
61-
today.set(Calendar.MONTH, Integer.valueOf(period.substring(5)) - 1);
62+
today.set(Calendar.MONTH, Integer.parseInt(period.substring(5)) - 1);
63+
break;
6264
}
6365
case "W": {
64-
today.set(Calendar.WEEK_OF_YEAR, Integer.valueOf(period.substring(5)));
66+
today.set(Calendar.WEEK_OF_YEAR, Integer.parseInt(period.substring(5)));
67+
break;
6568
}
6669
case "D": {
67-
today.set(Calendar.DAY_OF_YEAR, Integer.valueOf(period.substring(5)));
70+
today.set(Calendar.DAY_OF_YEAR, Integer.parseInt(period.substring(5)));
71+
break;
6872
}
6973
}
7074
} else {
@@ -84,8 +88,8 @@ public List<Object> loginAggregated(String period, String idpEntityId, String sp
8488
.filter(sp -> !StringUtils.hasText(spEntityId) || spEntityId.equals(getData(sp).get("entityid")))
8589
.map(sp -> {
8690
Map<String, Object> point = new HashMap<>();
87-
point.put("count_user_id", countValue(periodToScale(period)));
88-
point.put("distinct_count_user_id", countValue(periodToScale(period)) / 2);
91+
point.put("count_user_id", countValue());
92+
point.put("distinct_count_user_id", countValue() / 2);
8993
point.put("sp_entity_id", getData(sp).get("entityid"));
9094
point.put("idp_entity_id", idpEntityId);
9195
point.put("time", date);
@@ -97,59 +101,29 @@ public List<Object> loginAggregated(String period, String idpEntityId, String sp
97101
public List<Object> uniqueLoginCount(long from, long to, String idpEntityId, String spEntityId) {
98102
List<Object> result = new ArrayList<>();
99103
Map<String, Object> point = new HashMap<>();
100-
point.put("count_user_id", countValue("year"));
104+
point.put("count_user_id", countValue());
101105
point.put("sp_entity_id", spEntityId);
102106
point.put("idp_entity_id", idpEntityId);
103107
point.put("time", from);
104108
result.add(point);
105109
return result;
106110
}
107111

108-
private String periodToScale(String period) {
109-
if (period.length() == 4) {
110-
return "year";
111-
}
112-
switch (period.substring(4, 5).toLowerCase()) {
113-
case "d":
114-
return "day";
115-
case "w":
116-
return "week";
117-
case "m":
118-
return "month";
119-
case "q":
120-
return "quarter";
121-
default:
122-
throw new IllegalArgumentException("Unknown period:" + period);
123-
}
124-
}
125-
126-
private long countValue(String scale) {
112+
private long countValue() {
127113
double base = Math.floor(10000 * (Math.random() + 1));
128114
return (long) base;
129115
}
130116

131-
private long doSwitch(String scale, long base) {
132-
switch (scale) {
133-
case "minute":
134-
return base * 60;
135-
case "hour":
136-
return base * 60 * 60;
137-
case "day":
138-
return base * 24 * 60 * 60;
139-
case "week":
140-
return base * 7 * 24 * 60 * 60;
141-
case "month":
142-
return base * 30 * 24 * 60 * 60;
143-
case "quarter":
144-
return base * 90 * 24 * 60 * 60;
145-
case "year":
146-
return base * 365 * 24 * 60 * 60;
147-
default:
148-
throw new IllegalArgumentException("Unknown scale:" + scale);
149-
}
150-
}
151-
152117
private long step(String scale) {
153-
return doSwitch(scale, 1L);
118+
return switch (scale) {
119+
case "minute" -> 60;
120+
case "hour" -> 60 * 60;
121+
case "day" -> 24 * 60 * 60;
122+
case "week" -> 7 * 24 * 60 * 60;
123+
case "month" -> 30 * 24 * 60 * 60;
124+
case "quarter" -> 90 * 24 * 60 * 60;
125+
case "year" -> 365 * 24 * 60 * 60;
126+
default -> throw new IllegalArgumentException("Unknown scale:" + scale);
127+
};
154128
}
155129
}

server/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ invite:
183183

184184
statistics:
185185
enabled: True
186-
url: "http://locallhost:8081"
186+
url: "http://localhost:8081"
187187
user: access
188188
password: secret
189189

server/src/test/java/access/AbstractTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -559,17 +559,17 @@ private void doSeed() {
559559
this.joinRequestRepository.deleteAllInBatch();
560560

561561
User superUser =
562-
new User(true, SUPER_SUB, SUPER_SUB, "example.com", "David", "Doe", "david.doe@example.com");
562+
new User(true, SUPER_SUB, SUPER_SUB, "example.com", "David", "Doe", "david.doe@example.com", "http://mock-idp");
563563
User manager =
564-
new User(false, MANAGE_SUB, MANAGE_SUB, "example.com", "Mary", "Doe", "mary.doe@example.com");
564+
new User(false, MANAGE_SUB, MANAGE_SUB, "example.com", "Mary", "Doe", "mary.doe@example.com", "http://mock-idp");
565565
User guest =
566-
new User(false, GUEST_SUB, GUEST_SUB, "eduid.nl", "Peter", "Doe", "peter.doe@example.com");
566+
new User(false, GUEST_SUB, GUEST_SUB, "eduid.nl", "Peter", "Doe", "peter.doe@example.com","http://mock-idp");
567567
User externalUser =
568-
new User(false, EXTERNAL_USER_SUB, EXTERNAL_USER_SUB, "eduid.nl", "Ex", "Doe", "ex.doe@eduid.nl");
568+
new User(false, EXTERNAL_USER_SUB, EXTERNAL_USER_SUB, "eduid.nl", "Ex", "Doe", "ex.doe@eduid.nl","http://mock-idp");
569569
User multipleOrganizationUser =
570-
new User(true, MULTIPLE_ORG_SUB, MULTIPLE_ORG_SUB, "eduid.nl", "Mos", "Doe", "mos.doe@example.com");
570+
new User(true, MULTIPLE_ORG_SUB, MULTIPLE_ORG_SUB, "eduid.nl", "Mos", "Doe", "mos.doe@example.com","http://mock-idp");
571571
User institutionAdmin =
572-
new User(false, INSTITUTION_ADMIN, INSTITUTION_ADMIN, "example.com", "Pat", "Doe", "pat.doe@example.com");
572+
new User(false, INSTITUTION_ADMIN, INSTITUTION_ADMIN, "example.com", "Pat", "Doe", "pat.doe@example.com", "http://mock-idp");
573573
institutionAdmin.setAuthenticatingAuthority("http://mock-idp");
574574
institutionAdmin.setOrganizationGUID(ORGANISATION_GUID);
575575
institutionAdmin.setInstitutionAdmin(true);

0 commit comments

Comments
 (0)