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 Jun 30, 2023
1 parent 4e2282c commit f996848
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 7 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 @@ -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.frontend.AssertionConsumerServiceRepresentation
import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.EntityDescriptorRepresentation
import edu.internet2.tier.shibboleth.admin.ui.exception.ForbiddenException
Expand All @@ -17,6 +18,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 Down Expand Up @@ -50,6 +52,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
@Autowired
private IBeaconDataService service

@Autowired
EntityDescriptorRepository entityDescriptorRepository

Expand Down Expand Up @@ -235,6 +240,8 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
} catch (NestedServletException expected) {
expected.getCause() instanceof InvalidPatternMatchException
}

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

@WithMockUser(value = "someUser", roles = ["USER"])
Expand Down Expand Up @@ -264,6 +271,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 @@ -285,6 +294,9 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
} catch (NestedServletException expected) {
expected.getCause() instanceof InvalidPatternMatchException
}

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

@WithMockAdmin
Expand Down Expand Up @@ -321,6 +333,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 Down Expand Up @@ -357,6 +371,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 @@ -397,6 +413,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 @@ -587,12 +606,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 @@ -632,6 +654,8 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
catch (Exception e) {
e instanceof ObjectIdExistsException
}
service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_CREATED.name()) == null
}
@WithMockAdmin
Expand All @@ -658,6 +682,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 @@ -683,6 +709,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 @@ -708,6 +736,8 @@ class EntityDescriptorControllerTests extends AbstractBaseDataJpaTest {
catch (Exception e) {
e instanceof ForbiddenException
}
service.getBeaconEvents().get(BeaconEventType.METADATA_SOURCE_MODIFIED.name()) == null
}
@WithMockAdmin
Expand All @@ -731,6 +761,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 @@ -769,6 +801,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 @@ -825,6 +859,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
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import edu.internet2.tier.shibboleth.admin.ui.configuration.CustomPropertiesConfiguration
import edu.internet2.tier.shibboleth.admin.ui.domain.beacon.BeaconEventType
import edu.internet2.tier.shibboleth.admin.ui.domain.filters.EntityAttributesFilter
import edu.internet2.tier.shibboleth.admin.ui.domain.filters.EntityAttributesFilterTarget
import edu.internet2.tier.shibboleth.admin.ui.domain.resolvers.opensaml.OpenSamlChainingMetadataResolver
import edu.internet2.tier.shibboleth.admin.ui.repository.BeaconEventRepository
import edu.internet2.tier.shibboleth.admin.ui.repository.MetadataResolverRepository
import edu.internet2.tier.shibboleth.admin.ui.service.IBeaconDataService
import edu.internet2.tier.shibboleth.admin.ui.service.MetadataResolverConverterService
import edu.internet2.tier.shibboleth.admin.ui.util.TestObjectGenerator
import edu.internet2.tier.shibboleth.admin.util.AttributeUtility
Expand Down Expand Up @@ -55,6 +58,12 @@ class MetadataFiltersControllerIntegrationTests extends Specification {
@Autowired
MetadataResolver chainingMetadataResolver

@Autowired
private IBeaconDataService beaconDataService

@Autowired
private BeaconEventRepository beaconEventRepository

ObjectMapper mapper
TestObjectGenerator generator

Expand All @@ -71,6 +80,7 @@ class MetadataFiltersControllerIntegrationTests extends Specification {

def cleanup() {
metadataResolverRepository.deleteAll()
beaconEventRepository.deleteAll()
}

def "PUT EntityAttributesFilter"() {
Expand All @@ -94,7 +104,9 @@ class MetadataFiltersControllerIntegrationTests extends Specification {
createRequestHttpEntityFor { JsonOutput.toJson(existingFilterMap) }, String)

then:
updatedResultFromPUT.statusCode.value() == 200
updatedResultFromPUT.statusCode.value() == 200

beaconDataService.getBeaconEvents().get(BeaconEventType.METADATA_FILTER_MODIFIED.name()).longValue() == 1
}

def "PUT EntityAttributesFilter and update it"() {
Expand All @@ -120,6 +132,7 @@ class MetadataFiltersControllerIntegrationTests extends Specification {

then:
updatedResultFromPUT.statusCode.value() == 200
beaconDataService.getBeaconEvents().get(BeaconEventType.METADATA_FILTER_MODIFIED.name()).longValue() == 1
}

def "DELETE Filter"() {
Expand Down Expand Up @@ -215,6 +228,7 @@ class MetadataFiltersControllerIntegrationTests extends Specification {
}

def "EntityAttributesFilter with invalid script does not result in persisting that filter"() {
given:
def resolver = generator.buildRandomMetadataResolverOfType('FileBacked')
def resolverResourceId = resolver.resourceId
metadataResolverRepository.save(resolver)
Expand All @@ -233,6 +247,9 @@ class MetadataFiltersControllerIntegrationTests extends Specification {
it
}

expect:
beaconDataService.getBeaconEvents().get(BeaconEventType.METADATA_FILTER_CREATED.name()) == null

when:
def result = restTemplate.postForEntity("$BASE_URI/$resolverResourceId/Filters", filter, String)

Expand All @@ -244,6 +261,7 @@ class MetadataFiltersControllerIntegrationTests extends Specification {

then:
result.body.metadataFilters.size == 0
beaconDataService.getBeaconEvents().get(BeaconEventType.METADATA_FILTER_CREATED.name()) == null
}

private HttpEntity<String> createRequestHttpEntityFor(Closure jsonBodySupplier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import edu.internet2.tier.shibboleth.admin.ui.AbstractBaseDataJpaTest
import edu.internet2.tier.shibboleth.admin.ui.configuration.CustomPropertiesConfiguration
import edu.internet2.tier.shibboleth.admin.ui.domain.beacon.BeaconEventType
import edu.internet2.tier.shibboleth.admin.ui.domain.exceptions.MetadataFileNotFoundException
import edu.internet2.tier.shibboleth.admin.ui.domain.filters.MetadataFilter
import edu.internet2.tier.shibboleth.admin.ui.domain.resolvers.MetadataResolver
Expand Down Expand Up @@ -244,8 +245,8 @@ class MetadataFiltersControllerTests extends AbstractBaseDataJpaTest {
then:
def expectedJson = new JsonSlurper().parseText(updatedFilterJson)
expectedJson << [version: updatedFilter.getVersion()]
result.andExpect(status().isOk())
.andExpect(content().json(JsonOutput.toJson(expectedJson), true))
result.andExpect(status().isOk()).andExpect(content().json(JsonOutput.toJson(expectedJson), true))
beaconDataService.getBeaconEvents().get(BeaconEventType.METADATA_FILTER_MODIFIED.name()).longValue() == 1
where:
filterType | _
Expand Down Expand Up @@ -281,6 +282,7 @@ class MetadataFiltersControllerTests extends AbstractBaseDataJpaTest {
then:
result.andExpect(status().is(409))
beaconDataService.getBeaconEvents().get(BeaconEventType.METADATA_FILTER_MODIFIED.name()) == null
}
@TestConfiguration
Expand Down
Loading

0 comments on commit f996848

Please sign in to comment.