Skip to content

Commit

Permalink
SHIBUI-2584
Browse files Browse the repository at this point in the history
Added  beacon data collection unit testing
  • Loading branch information
chasegawa committed Jul 7, 2023
1 parent 658b20c commit dce04a0
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public ResponseEntity<?> create(@RequestBody MetadataResolver newResolver) throw
positionOrderContainerService.appendPositionOrderForNew(persistedResolver);
doResolverInitialization(persistedResolver);
beaconDataService.addBeaconEvent(new BeaconEvent(BeaconEventType.METADATA_PROVIDER_CREATED));
newResolver.getMetadataFilters().forEach(f -> beaconDataService.addBeaconEvent(new BeaconEvent(BeaconEventType.METADATA_FILTER_CREATED)));
return ResponseEntity.created(getResourceUriFor(persistedResolver)).body(persistedResolver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -74,7 +73,8 @@ public String getBeaconData() {
return mapper.writeValueAsString(detail);
}

private Map<String, Long> getBeaconEvents() {
@Override
public Map<String, Long> getBeaconEvents() {
List<BeaconEvent> results = beaconEventRepository.getEventsSince(DateUtils.addDays(new Date(), -1));
Map<String, Long> counts = results.stream().collect(Collectors.groupingBy(e -> e.getEventName(), Collectors.counting()));
return counts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import edu.internet2.tier.shibboleth.admin.ui.domain.beacon.BeaconEvent;

import java.util.Map;

public interface IBeaconDataService {
String getBeaconData() throws JsonProcessingException;

Map<String, Long> getBeaconEvents();

void addBeaconEvent(BeaconEvent event);

void addEntityDescEnabledEvent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package edu.internet2.tier.shibboleth.admin.ui.controller
import com.fasterxml.jackson.databind.ObjectMapper
import edu.internet2.tier.shibboleth.admin.ui.AbstractBaseDataJpaTest
import edu.internet2.tier.shibboleth.admin.ui.domain.EntityDescriptor
import edu.internet2.tier.shibboleth.admin.ui.domain.beacon.BeaconEventType
import edu.internet2.tier.shibboleth.admin.ui.domain.oidc.DynamicRegistrationInfo
import edu.internet2.tier.shibboleth.admin.ui.domain.oidc.GrantType
import edu.internet2.tier.shibboleth.admin.ui.exception.ForbiddenException
Expand All @@ -16,6 +17,7 @@ import edu.internet2.tier.shibboleth.admin.ui.security.repository.DynamicRegistr
import edu.internet2.tier.shibboleth.admin.ui.service.DynamicRegistrationService
import edu.internet2.tier.shibboleth.admin.ui.service.EntityDescriptorService
import edu.internet2.tier.shibboleth.admin.ui.service.EntityService
import edu.internet2.tier.shibboleth.admin.ui.service.IBeaconDataService
import edu.internet2.tier.shibboleth.admin.ui.util.WithMockAdmin
import edu.internet2.tier.shibboleth.admin.util.EntityDescriptorConversionUtils
import org.springframework.beans.factory.annotation.Autowired
Expand All @@ -34,6 +36,9 @@ class ActivateControllerTests extends AbstractBaseDataJpaTest {
@Subject
def controller

@Autowired
private IBeaconDataService service

@Autowired
ObjectMapper mapper

Expand Down Expand Up @@ -114,9 +119,9 @@ class ActivateControllerTests extends AbstractBaseDataJpaTest {
entityManager.clear()

EntityDescriptor entityDescriptor = new EntityDescriptor(resourceId: 'uuid-1', entityID: 'eid1', serviceProviderName: 'sp1', serviceEnabled: false, idOfOwner: 'AAA')
entityDescriptor = entityDescriptorRepository.save(entityDescriptor)
def edRep = entDescriptorService.createNew(entityDescriptor)

defaultEntityDescriptorResourceId = entityDescriptor.getResourceId()
defaultEntityDescriptorResourceId = edRep.id

def dynReg = new DynamicRegistrationInfo(resourceId: 'uuid-1', enabled: false, idOfOwner: "AAA", applicationType: 'apptype',
approved: true, contacts: 'contacts', jwks: 'jwks', logoUri: 'logouri', policyUri: 'policyuri',
Expand Down Expand Up @@ -145,6 +150,8 @@ class ActivateControllerTests extends AbstractBaseDataJpaTest {
catch (Exception e) {
e instanceof ForbiddenException
}

service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_ENABLED.name()) == null
}

@WithMockUser(value = "DUser", roles = ["USER"])
Expand All @@ -156,17 +163,26 @@ class ActivateControllerTests extends AbstractBaseDataJpaTest {
catch (Exception e) {
e instanceof ForbiddenException
}

service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_ENABLED.name()) == null
}

@WithMockUser(value = "DUser", roles = ["USER"])
def 'non-owner group cannot activate entity descriptor'() {
given:
EntityDescriptor ed = entityDescriptorRepository.findByResourceId(defaultEntityDescriptorResourceId)
ed.setIdOfOwner('AAA')
entityDescriptorRepository.saveAndFlush(ed)

expect:
try {
mockMvc.perform(patch("/api/activate/entityDescriptor/" + defaultEntityDescriptorResourceId + "/enable"))
}
catch (Exception e) {
e instanceof ForbiddenException
}

service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_ENABLED.name()) == null
}

// @WithMockAdmin
Expand Down Expand Up @@ -196,6 +212,8 @@ class ActivateControllerTests extends AbstractBaseDataJpaTest {
result.andExpect(status().isOk())
.andExpect(jsonPath("\$.id").value(defaultEntityDescriptorResourceId))
.andExpect(jsonPath("\$.serviceEnabled").value(true))

service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_ENABLED.name()).longValue() == 1
}

// @WithMockUser(value = "AUser", roles = ["USER"])
Expand Down Expand Up @@ -231,5 +249,7 @@ class ActivateControllerTests extends AbstractBaseDataJpaTest {
result.andExpect(status().isOk())
.andExpect(jsonPath("\$.id").value('uuid-2'))
.andExpect(jsonPath("\$.serviceEnabled").value(true))

service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_ENABLED.name()).longValue() == 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import edu.internet2.tier.shibboleth.admin.ui.AbstractBaseDataJpaTest
import edu.internet2.tier.shibboleth.admin.ui.configuration.JsonSchemaComponentsConfiguration
import edu.internet2.tier.shibboleth.admin.ui.domain.EntityDescriptor
import edu.internet2.tier.shibboleth.admin.ui.domain.beacon.BeaconEventType
import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.AssertionConsumerServiceRepresentation
import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.EntityDescriptorRepresentation
import edu.internet2.tier.shibboleth.admin.ui.exception.ForbiddenException
Expand All @@ -21,6 +22,7 @@ import edu.internet2.tier.shibboleth.admin.ui.security.model.Role
import edu.internet2.tier.shibboleth.admin.ui.security.model.User
import edu.internet2.tier.shibboleth.admin.ui.service.EntityDescriptorVersionService
import edu.internet2.tier.shibboleth.admin.ui.service.EntityService
import edu.internet2.tier.shibboleth.admin.ui.service.IBeaconDataService
import edu.internet2.tier.shibboleth.admin.ui.service.JPAEntityDescriptorServiceImpl
import edu.internet2.tier.shibboleth.admin.ui.util.RandomGenerator
import edu.internet2.tier.shibboleth.admin.ui.util.TestHelpers
Expand All @@ -40,6 +42,7 @@ import org.springframework.test.web.servlet.result.MockMvcResultHandlers
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.client.RestTemplate
import org.springframework.web.util.NestedServletException
import spock.lang.Subject

import java.nio.charset.StandardCharsets
Expand All @@ -58,6 +61,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.

@Import([JsonSchemaComponentsConfiguration.class, EDCConfig.class])
class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
@Autowired
private IBeaconDataService service

@Autowired
EntityDescriptorSchemaValidatingControllerAdvice controllerAdvice

Expand Down Expand Up @@ -281,6 +287,8 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
} catch (ServletException expected) {
expected.getCause() instanceof InvalidPatternMatchException
}

service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_CREATED.name()) == null
}

@WithMockUser(value = "someUser", roles = ["USER"])
Expand Down Expand Up @@ -311,6 +319,8 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
.andExpect(jsonPath("\$.serviceEnabled").value(false))
.andExpect(jsonPath("\$.idOfOwner").value("testingGroupBBB"))

service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_CREATED.name()).longValue() == 1

when: "ACS url is bad"
expectedEntityId = 'https://shib.org/blah/blah/again'
edRep = new EntityDescriptorRepresentation()
Expand All @@ -333,6 +343,9 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
} catch (ServletException expected) {
expected.getCause() instanceof InvalidPatternMatchException
}

// Still just the one
service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_CREATED.name()).longValue() == 1
}

@WithMockAdmin
Expand Down Expand Up @@ -364,6 +377,8 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
.andExpect(jsonPath("\$.entityId").value("https://shib"))
.andExpect(jsonPath("\$.serviceEnabled").value(true))
.andExpect(jsonPath("\$.idOfOwner").value("admingroup"))

service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_CREATED.name()).longValue() == 1
}

@WithMockUser(value = "someUser", roles = ["USER"])
Expand All @@ -384,6 +399,8 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
catch (Exception e) {
e instanceof ForbiddenException
}

service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_ENABLED.name()) == null
}

@WithMockAdmin
Expand Down Expand Up @@ -424,6 +441,9 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
catch (Exception e) {
e instanceof ObjectIdExistsException
}

// because we created the test objects directly in the repository, there should be no beacon records
service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_CREATED.name()) == null
}

@WithMockAdmin
Expand Down Expand Up @@ -614,12 +634,15 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
.andExpect(jsonPath("\$.assertionConsumerServices[0].makeDefault").value(false))
.andExpect(jsonPath("\$.assertionConsumerServices[0].locationUrl").value("https://test.scaldingspoon.org/test1/acs"))
service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_CREATED.name()).longValue() == 1
try {
mockMvc.perform(post("/api/EntityDescriptor").contentType(APPLICATION_XML).content(postedBody).param("spName", spName))
}
catch (Exception e) {
e instanceof ObjectIdExistsException
}
service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_CREATED.name()).longValue() == 1
}
@WithMockAdmin
Expand Down Expand Up @@ -659,6 +682,8 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
catch (Exception e) {
e instanceof ObjectIdExistsException
}
service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_CREATED.name()) == null
}
@WithMockAdmin
Expand All @@ -685,6 +710,8 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
.andExpect(jsonPath("\$.serviceEnabled").value(false))
.andExpect(jsonPath("\$.idOfOwner").value("admingroup"))
.andExpect(jsonPath("\$.serviceProviderName").value("newName"))
service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_MODIFIED.name()).longValue() == 1
}
@WithMockUser(value = "someUser", roles = ["USER"])
Expand All @@ -710,6 +737,8 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
catch (Exception e) {
e instanceof ForbiddenException
}
service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_MODIFIED.name()) == null
}
@WithMockUser(value = "someUser", roles = ["USER"])
Expand All @@ -735,6 +764,8 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
catch (Exception e) {
e instanceof ForbiddenException
}
service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_MODIFIED.name()) == null
}
@WithMockAdmin
Expand All @@ -758,6 +789,8 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
catch (Exception e) {
e instanceof ConcurrentModificationException
}
service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_MODIFIED.name()) == null
}
@WithMockAdmin
Expand Down Expand Up @@ -796,6 +829,8 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
.andExpect(jsonPath(shortNameToOAuth + "defaultAcrValues").isArray())
.andExpect(jsonPath(shortNameToOAuth + "attributes.requireAuthTime").value(Boolean.FALSE))
.andExpect(jsonPath(shortNameToOAuth + "attributes.defaultMaxAge").value(Integer.valueOf(0)))
service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_CREATED.name()).longValue() == 1
}
@WithMockAdmin
Expand Down Expand Up @@ -852,6 +887,8 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
.andExpect(jsonPath(shortNameToOAuth + "defaultAcrValues").isArray())
.andExpect(jsonPath(shortNameToOAuth + "attributes.requireAuthTime").value(Boolean.FALSE))
.andExpect(jsonPath(shortNameToOAuth + "attributes.defaultMaxAge").value(Integer.valueOf(0)))
service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_CREATED.name()).longValue() == 1
}
@SneakyThrows
Expand Down
Loading

0 comments on commit dce04a0

Please sign in to comment.