Skip to content

Commit

Permalink
SHIBUI-2050
Browse files Browse the repository at this point in the history
Fixing test issues
  • Loading branch information
chasegawa committed Sep 3, 2021
1 parent 7ead267 commit 9cde801
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 146 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package edu.internet2.tier.shibboleth.admin.ui.security.model.listener;

import java.util.Set;
import edu.internet2.tier.shibboleth.admin.ui.security.model.Group;
import edu.internet2.tier.shibboleth.admin.ui.security.model.Ownership;
import edu.internet2.tier.shibboleth.admin.ui.security.repository.OwnershipRepository;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;

import org.springframework.beans.factory.annotation.Autowired;

import edu.internet2.tier.shibboleth.admin.ui.security.model.Group;
import edu.internet2.tier.shibboleth.admin.ui.security.model.Ownership;
import edu.internet2.tier.shibboleth.admin.ui.security.repository.OwnershipRepository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Set;

public class GroupUpdatedEntityListener implements ILazyLoaderHelper {
private static OwnershipRepository ownershipRepository;
Expand All @@ -37,7 +33,6 @@ public synchronized void groupSavedOrFetched(Group group) {
}

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void loadOwnedItems(Group group) {
Set<Ownership> ownedItems = ownershipRepository.findAllByOwner(group);
group.setOwnedItems(ownedItems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import edu.internet2.tier.shibboleth.admin.ui.security.model.User;

public interface ILazyLoaderHelper {
default public void loadOwnedItems(Group g) { }
default void loadOwnedItems(Group g) { }

default public void loadGroups(User u) { }
default void loadGroups(User u) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import edu.internet2.tier.shibboleth.admin.ui.security.repository.GroupsRepository;
import edu.internet2.tier.shibboleth.admin.ui.security.repository.OwnershipRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
Expand Down Expand Up @@ -40,11 +38,12 @@ public synchronized void userSavedOrFetched(User user) {
}

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void loadGroups(User user) {
Set<Ownership> ownerships = ownershipRepository.findAllGroupsForUser(user.getUsername());
HashSet<Group> groups = new HashSet<>();
final boolean isAdmin = user.getRole().equals("ROLE_ADMIN");

// if the user is an admin, clear out the regex value so it isn't sent out to the UI
ownerships.stream().map(ownership -> groupRepository.findByResourceId(ownership.getOwnerId())).forEach(userGroup -> {
if (isAdmin) {
userGroup.setValidationRegex(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ import spock.lang.Specification

import javax.persistence.EntityManager

//@DataJpaTest (properties = ["spring.datasource.url=jdbc:h2:file:/tmp/myApplicationDb;AUTO_SERVER=TRUE",
// "spring.datasource.username=sa",
// "spring.datasource.password=",
// "spring.jpa.hibernate.ddl-auto=create-drop"])
//@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@DataJpaTest
@ContextConfiguration(classes = [BaseDataJpaTestConfiguration])
@EnableJpaRepositories(basePackages = ["edu.internet2.tier.shibboleth.admin.ui"])
@EntityScan("edu.internet2.tier.shibboleth.admin.ui")
abstract class AbstractBaseDataJpaTest extends Specification {
abstract class AbstractBaseDataJpaTest extends Specification implements ResetsDatabase {
@Autowired
EntityManager entityManager

Expand All @@ -43,6 +48,7 @@ abstract class AbstractBaseDataJpaTest extends Specification {
// ensure roles are in a known good state and that we have an admin user and group
@Transactional
def setup() {
// dbsetup()
groupService.clearAllForTesting()
userRepository.deleteAll()
ownershipRepository.deleteAll()
Expand Down Expand Up @@ -72,6 +78,7 @@ abstract class AbstractBaseDataJpaTest extends Specification {

def cleanup() {
entityManager.clear()
// dbcleanup()
}

protected createAdminUser() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package edu.internet2.tier.shibboleth.admin.ui

import groovy.sql.Sql

trait ResetsDatabase {
static final String H2_BACKUP_LOCATION = '/tmp/h2backup.sql'

void dbsetup() {
if (new File(H2_BACKUP_LOCATION).exists()) {
print "Previous test was interrupted and did not clean up after itself. "
loadH2FromBackup()
}
else {
Sql sql = connectToSql()
sql.execute("SCRIPT TO ?", [H2_BACKUP_LOCATION])
}
}

void dbcleanup() {
loadH2FromBackup()
new File(H2_BACKUP_LOCATION).delete()
}

private void loadH2FromBackup() {
println "Restoring H2 from backup location."
Sql sql = connectToSql()
sql.execute("DROP ALL OBJECTS")
sql.execute("RUNSCRIPT FROM ?", [H2_BACKUP_LOCATION])
}

private Sql connectToSql() {
Sql.newInstance('jdbc:h2:file:/tmp/myApplicationDb;AUTO_SERVER=TRUE', 'sa', '', 'org.h2.Driver')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import edu.internet2.tier.shibboleth.admin.ui.repository.CustomEntityAttributeDe
import edu.internet2.tier.shibboleth.admin.ui.service.CustomEntityAttributesDefinitionService
import org.springframework.beans.factory.annotation.Autowired

import javax.persistence.EntityManager

/**
* Tests for <code>edu.internet2.tier.shibboleth.admin.ui.configuration.CustomPropertiesConfiguration</code>
*/
Expand All @@ -21,9 +19,6 @@ class CustomPropertiesConfigurationTests extends AbstractBaseDataJpaTest {

@Autowired
CustomEntityAttributeDefinitionRepository repository

@Autowired
EntityManager entityManager

def "Updating Custom Entity Attribute Definitions will update the CustomPropertiesConfiguration automatically"() {
given: 'Test defaults loaded (ie no CEADs in DB)'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,29 @@
package edu.internet2.tier.shibboleth.admin.ui.scheduled

import edu.internet2.tier.shibboleth.admin.ui.configuration.InternationalizationConfiguration
import edu.internet2.tier.shibboleth.admin.ui.configuration.TestConfiguration
import edu.internet2.tier.shibboleth.admin.ui.configuration.CoreShibUiConfiguration
import edu.internet2.tier.shibboleth.admin.ui.configuration.SearchConfiguration
import edu.internet2.tier.shibboleth.admin.ui.AbstractBaseDataJpaTest
import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.EntityDescriptorRepresentation
import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.OrganizationRepresentation
import edu.internet2.tier.shibboleth.admin.ui.opensaml.OpenSamlObjects
import edu.internet2.tier.shibboleth.admin.ui.repository.EntityDescriptorRepository
import edu.internet2.tier.shibboleth.admin.ui.security.repository.RoleRepository
import edu.internet2.tier.shibboleth.admin.ui.security.repository.UserRepository
import edu.internet2.tier.shibboleth.admin.ui.security.service.IGroupService
import edu.internet2.tier.shibboleth.admin.ui.security.service.UserService
import edu.internet2.tier.shibboleth.admin.ui.service.FileCheckingFileWritingService
import edu.internet2.tier.shibboleth.admin.ui.service.JPAEntityDescriptorServiceImpl
import edu.internet2.tier.shibboleth.admin.ui.service.JPAEntityServiceImpl
import edu.internet2.tier.shibboleth.admin.ui.util.RandomGenerator
import edu.internet2.tier.shibboleth.admin.util.EntityDescriptorConversionUtils

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
import org.springframework.test.context.ContextConfiguration
import org.xmlunit.builder.DiffBuilder
import org.xmlunit.builder.Input
import spock.lang.Specification

/**
* @author Bill Smith (wsmith@unicon.net)
*/
@DataJpaTest
@ContextConfiguration(classes=[CoreShibUiConfiguration, SearchConfiguration, TestConfiguration, InternationalizationConfiguration])
@EnableJpaRepositories(basePackages = ["edu.internet2.tier.shibboleth.admin.ui"])
@EntityScan("edu.internet2.tier.shibboleth.admin.ui")
class EntityDescriptorFilesScheduledTasksTests extends Specification {
class EntityDescriptorFilesScheduledTasksTests extends AbstractBaseDataJpaTest {

@Autowired
OpenSamlObjects openSamlObjects

@Autowired
IGroupService groupService
JPAEntityDescriptorServiceImpl service

def tempPath = "/tmp/shibui"

def directory
Expand All @@ -51,21 +33,18 @@ class EntityDescriptorFilesScheduledTasksTests extends Specification {
def entityDescriptorFilesScheduledTasks

def randomGenerator


def service


def setup() {
randomGenerator = new RandomGenerator()
tempPath = tempPath + randomGenerator.randomRangeInt(10000, 20000)
EntityDescriptorConversionUtils.setOpenSamlObjects(openSamlObjects)
entityDescriptorFilesScheduledTasks = new EntityDescriptorFilesScheduledTasks(tempPath, entityDescriptorRepository, openSamlObjects, new FileCheckingFileWritingService())
directory = new File(tempPath)
directory.mkdir()
service = new JPAEntityDescriptorServiceImpl()
service.openSamlObjects = openSamlObjects
service.groupService = groupService
}

def cleanup() {
directory.deleteDir()
}

def "generateEntityDescriptorFiles properly generates a file from an Entity Descriptor"() {
Expand Down Expand Up @@ -114,21 +93,6 @@ class EntityDescriptorFilesScheduledTasksTests extends Specification {

def "removeDanglingEntityDescriptorFiles properly deletes files"() {
given:
def expectedXml = '''
<md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata"
xmlns:mdattr="urn:oasis:names:tc:SAML:metadata:attribute"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
entityID="http://test.example.org/test1">
<md:Organization>
<md:OrganizationName xml:lang="en">name</md:OrganizationName>
<md:OrganizationDisplayName xml:lang="en">display name</md:OrganizationDisplayName>
<md:OrganizationURL xml:lang="en">http://test.example.org</md:OrganizationURL>
</md:Organization>
</md:EntityDescriptor>
'''

def entityDescriptor = service.createDescriptorFromRepresentation(new EntityDescriptorRepresentation().with {
it.entityId = 'http://test.example.org/test1'
it.organization = new OrganizationRepresentation().with {
Expand All @@ -152,8 +116,4 @@ class EntityDescriptorFilesScheduledTasksTests extends Specification {
def files = new File(tempPath, file)
files.size() == 0
}

def cleanup() {
directory.deleteDir()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,17 @@ class GroupsRepositoryTests extends AbstractBaseDataJpaTest {
it.resourceId = "g1"
it
}
Group savedGroup = groupsRepo.saveAndFlush(group)
Collection all = ownershipRepository.findAllByOwner(savedGroup)
groupsRepo.saveAndFlush(group)
entityManager.clear()
Group groupFromDb = groupsRepo.findByResourceId("g1")
groupFromDb.getOwnedItems()
Collection groupOwnedItems = ownershipRepository.findAllByOwner(groupFromDb)

then:
all.size() == 3
savedGroup.getOwnedItems().size() == 3
all.each {
savedGroup.ownedItems.contains(it)
groupOwnedItems.size() == 3
groupFromDb.getOwnedItems().size() == 3
groupOwnedItems.each {
groupFromDb.ownedItems.contains(it)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.springframework.transaction.annotation.Transactional
class OwnershipRepositoryTests extends AbstractBaseDataJpaTest {
@Transactional
def setup() {
ownershipRepository.deleteAll()
def ownerships = [
new Ownership().with {
it.ownedId = "aaa"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class UserServiceTests extends AbstractBaseDataJpaTest {
then:
result.groupId == "testingGroupBBB"
result.username == "someUser"
result.userGroups.size() == 1
result.getUserGroups().size() == 1

// Raw check that the DB is correct for ownership
Set<Ownership> users = ownershipRepository.findUsersByOwner(gb)
Expand Down Expand Up @@ -68,7 +68,7 @@ class UserServiceTests extends AbstractBaseDataJpaTest {
then:
result.groupId == "testingGroup"
result.username == "someUser"
result.userGroups.size() == 1
result.getUserGroups().size() == 1

// Raw check that the DB is correct for ownership
Set<Ownership> users = ownershipRepository.findUsersByOwner(ga)
Expand Down Expand Up @@ -108,7 +108,7 @@ class UserServiceTests extends AbstractBaseDataJpaTest {
then:
result.groupId == "testingGroup"
result.username == "someUser"
result.userGroups.size() == 1
result.getUserGroups().size() == 1
result.firstName == "Wilma"
}

Expand Down Expand Up @@ -140,7 +140,7 @@ class UserServiceTests extends AbstractBaseDataJpaTest {
def result = userService.save(user)

then:
result.userGroups.size() == 2
result.getUserGroups().size() == 2

// Raw check that the DB is correct for ownership
Set<Ownership> users = ownershipRepository.findUsersByOwner(ga)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
package edu.internet2.tier.shibboleth.admin.ui.service

import edu.internet2.tier.shibboleth.admin.ui.configuration.CoreShibUiConfiguration
import edu.internet2.tier.shibboleth.admin.ui.configuration.InternationalizationConfiguration
import edu.internet2.tier.shibboleth.admin.ui.configuration.SearchConfiguration
import edu.internet2.tier.shibboleth.admin.ui.configuration.TestConfiguration
import edu.internet2.tier.shibboleth.admin.ui.AbstractBaseDataJpaTest
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification

/**
* @author Bill Smith (wsmith@unicon.net)
*/
@DataJpaTest
@ContextConfiguration(classes=[CoreShibUiConfiguration, SearchConfiguration, TestConfiguration, InternationalizationConfiguration])
@EnableJpaRepositories(basePackages = ["edu.internet2.tier.shibboleth.admin.ui"])
@EntityScan("edu.internet2.tier.shibboleth.admin.ui")
class IndexWriterServiceTests extends Specification {

class IndexWriterServiceTests extends AbstractBaseDataJpaTest {
@Autowired
IndexWriterService service

Expand All @@ -34,4 +21,4 @@ class IndexWriterServiceTests extends Specification {
then:
firstIndexWriter == secondIndexWriter
}
}
}
Loading

0 comments on commit 9cde801

Please sign in to comment.