Skip to content

Commit

Permalink
New GroupProvider for the Invite app
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Mar 13, 2024
1 parent 5e432d4 commit 8b04aaf
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.openconext</groupId>
<artifactId>voot-service</artifactId>
<version>5.0.1</version>
<version>6.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>voot-service</name>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/voot/ExternalGroupsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public Set<Group> getAllGroups() {

protected Set<Group> filterDuplicatesWithLowerImportance(List<Group> groups) {
return groups.stream().collect(groupingBy(group -> group.id)).values().stream()
.map(groupList -> groupList.stream().max(Comparator.comparing(group -> group.membership)).get()).collect(toSet());
.map(groupList -> groupList.stream().max(Comparator.comparing(group -> group.membership)).get())
.collect(toSet());
}

@SneakyThrows
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/voot/VootServiceApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public ExternalGroupsService externalGroupsService(
return new OpenSocialMembersClient(configuration);
case GUESTS:
return new EduIDGuestProvider(configuration);
case INVITE:
return new InviteProvider(configuration);
default:
throw new IllegalArgumentException("Unknown external provider-type: " + type);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/voot/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
@Getter
public class User {

private String clientId;
private String unspecifiedId;
private String schacHomeOrganization;
private final String clientId;
private final String unspecifiedId;
private final String schacHomeOrganization;

public User(BearerTokenAuthentication bearerTokenAuthentication) {
Map<String, Object> tokenAttributes = bearerTokenAuthentication.getTokenAttributes();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/voot/provider/GroupProviderType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public enum GroupProviderType {

TEAMS, OPEN_SOCIAL, OPEN_SOCIAL_MEMBERS, VOOT2, GUESTS
TEAMS, OPEN_SOCIAL, OPEN_SOCIAL_MEMBERS, VOOT2, GUESTS, INVITE

}
63 changes: 63 additions & 0 deletions src/main/java/voot/provider/InviteProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package voot.provider;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import voot.model.Group;
import voot.model.Member;
import voot.model.Membership;

import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;

public class InviteProvider extends AbstractProvider {

private final String groupMembershipsUrlTemplate;

public InviteProvider(Configuration configuration) {
super(configuration);
groupMembershipsUrlTemplate = "%s/api/voot/%s";
}

@Override
public boolean shouldBeQueriedForMemberships(String schacHomeOrganization) {
return true;
}

@Override
public Set<Group> getGroupMemberships(String uid) {
String uri = String.format(groupMembershipsUrlTemplate, configuration.url, uid);
RequestEntity<List<Map<String, String>>> requestEntity = new RequestEntity<>(HttpMethod.GET, URI.create(uri));
return restTemplate.exchange(requestEntity, new ParameterizedTypeReference<List<Map<String, String>>>() {
}).getBody().stream()
.map(this::parseGroup)
.collect(Collectors.toSet());
}

private Group parseGroup(Map<String, String> map) {
String name = map.get("name");
return new Group(map.get("urn"), name, name, "Invite", Membership.MEMBER);
}

@Override
public Set<Group> getAllGroups() {
return Collections.emptySet();
}

@Override
public Optional<Group> getGroupMembership(String uid, String groupId) {
return Optional.empty();
}

@Override
public Set<Member> getMembers(String groupId) {
return Collections.emptySet();
}

@Override
public Set<Member> getMembers(String personId, String groupId) {
return Collections.emptySet();
}
}
4 changes: 2 additions & 2 deletions src/test/java/voot/VootServiceApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ void externalGroupsService() throws IOException {
ExternalGroupsService externalGroupsService =
app.externalGroupsService(new ClassPathResource("/testAllExternalProviders.yml"));
Object providers = ReflectionTestUtils.getField(externalGroupsService, "providers");
assertEquals(5, ((List) providers).size());
assertEquals(6, ((List) providers).size());
}


@Test
void externalGroupsServiceUnknown() throws IOException {
void externalGroupsServiceUnknown() {
VootServiceApplication app = new VootServiceApplication();
Assertions.assertThrows(IllegalArgumentException.class,
() -> app.externalGroupsService(new ClassPathResource("/testUnknownExternalProviders.yml")));
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/voot/provider/InviteProviderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package voot.provider;

import com.nimbusds.jose.util.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import voot.AbstractTest;
import voot.model.Group;

import java.io.IOException;
import java.util.Set;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;


class InviteProviderTest extends AbstractTest {

private final InviteProvider subject = new InviteProvider(
new Provider.Configuration(GroupProviderType.INVITE, "http://localhost:8889",
new Provider.Configuration.Credentials("user", "password"),
2000, "N/A", "invite"));

@Test
void getGroupMemberships() throws IOException {
String json = IOUtils.readInputStreamToString(new ClassPathResource("json/invite/group_memberships.json").getInputStream());
String urn = "urn:collab:person:example.com:admin";
stubFor(get(urlPathEqualTo("/api/voot/" + urn))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-type", "application/json").
withBody(json)));
Set<Group> groupMemberships = subject.getGroupMemberships(urn);
assertEquals(2, groupMemberships.size());
}

@Test
void getAllGroups() {
assertEquals(0, subject.getAllGroups().size());
}

@Test
void getGroupMembership() {
assertFalse(subject.getGroupMembership("uid", "groupId").isPresent());

}

@Test
void getMembers() {
assertEquals(0, subject.getMembers("groupId").size());
}

@Test
void getMembersByPersonId() {
assertEquals(0, subject.getMembers("personId", "groupId").size());
}
}
10 changes: 10 additions & 0 deletions src/test/resources/json/invite/group_memberships.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"urn": "urn1",
"name": "administrator"
},
{
"urn": "urn2",
"name": "guest"
}
]
11 changes: 11 additions & 0 deletions src/test/resources/testAllExternalProviders.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ externalGroupProviders:
name: "Bar",
timeoutMillis: 5000
}
- {
type: "invite",
url: "http://localhost:8999/",
credentials: {
username: "foo",
secret: "bar"
},
schacHomeOrganization: "N/A",
name: "Invite",
timeoutMillis: 5000
}

0 comments on commit 8b04aaf

Please sign in to comment.