Skip to content

Commit

Permalink
SHIBUI-1880
Browse files Browse the repository at this point in the history
Fixed EntitiesControllerTests
  • Loading branch information
chasegawa committed May 18, 2021
1 parent deb5fc8 commit 77d5789
Showing 1 changed file with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import edu.internet2.tier.shibboleth.admin.ui.configuration.Internationalization
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.opensaml.OpenSamlObjects
import edu.internet2.tier.shibboleth.admin.ui.repository.EntityDescriptorRepository
import edu.internet2.tier.shibboleth.admin.ui.security.service.UserService
import edu.internet2.tier.shibboleth.admin.ui.service.JPAEntityDescriptorServiceImpl
import edu.internet2.tier.shibboleth.admin.ui.service.JPAEntityServiceImpl
import net.shibboleth.ext.spring.resource.ResourceHelper
import net.shibboleth.utilities.java.support.resolver.CriteriaSet

import org.opensaml.core.criterion.EntityIdCriterion
import org.opensaml.saml.metadata.resolver.impl.ResourceBackedMetadataResolver
import org.spockframework.spring.SpringBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
Expand All @@ -22,6 +27,7 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders
import spock.lang.Specification
import spock.lang.Subject

import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
Expand All @@ -31,11 +37,14 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@EnableJpaRepositories(basePackages = ["edu.internet2.tier.shibboleth.admin.ui"])
@EntityScan("edu.internet2.tier.shibboleth.admin.ui")
class EntitiesControllerTests extends Specification {

// Controller needs this to spit out the data
def openSamlObjects = new OpenSamlObjects().with {
init()
it
}

// resource will load the ED from the aggregate.xml file for testing
def resource = ResourceHelper.of(new ClassPathResource("/metadata/aggregate.xml"))

def metadataResolver = new ResourceBackedMetadataResolver(resource).with {
Expand All @@ -47,12 +56,19 @@ class EntitiesControllerTests extends Specification {

@Autowired
UserService userService


// This stub will spit out the results from the resolver instead of actually finding them in the DB
@SpringBean
EntityDescriptorRepository edr = Stub(EntityDescriptorRepository) {
findByEntityID("http://test.scaldingspoon.org/test1") >> metadataResolver.resolveSingle(new CriteriaSet(new EntityIdCriterion("http://test.scaldingspoon.org/test1")))
findByEntityID("test") >> metadataResolver.resolveSingle(new CriteriaSet(new EntityIdCriterion("test")))
}

@Subject
def controller = new EntitiesController(
openSamlObjects: openSamlObjects,
entityDescriptorService: new JPAEntityDescriptorServiceImpl(openSamlObjects, new JPAEntityServiceImpl(openSamlObjects), userService),
metadataResolver: metadataResolver
entityDescriptorRepository: edr
)

def mockMvc = MockMvcBuilders.standaloneSetup(controller).build()
Expand Down Expand Up @@ -116,14 +132,21 @@ class EntitiesControllerTests extends Specification {
"current":false
}
'''

when:
def result = mockMvc.perform(get('/api/entities/http%3A%2F%2Ftest.scaldingspoon.org%2Ftest1'))
def result = mockMvc.perform(get('/entities/http%3A%2F%2Ftest.scaldingspoon.org%2Ftest1'))

then:
def x = content()
// Response headers section 2.5
// from the spec https://www.ietf.org/archive/id/draft-young-md-query-14.txt
result.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().json(expectedBody, false))
.andExpect(header().exists(HttpHeaders.CONTENT_TYPE)) // MUST HAVE
// .andExpect(header().exists(HttpHeaders.CONTENT_LENGTH)) // SHOULD HAVE - should end up from etag filter, so skipped for test
// .andExpect(header().exists(HttpHeaders.CACHE_CONTROL)) // SHOULD HAVE - should be included by Spring Security
// .andExpect(header().exists(HttpHeaders.LAST_MODIFIED)) // SHOULD HAVE - should end up from etag filter, so skipped for test
// .andExpect(header().exists(HttpHeaders.ETAG)) // MUST HAVE - is done by filter, so skipped for test
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().json(expectedBody, false))
}

def 'GET /entities/http%3A%2F%2Ftest.scaldingspoon.org%2Ftest1'() {
Expand Down Expand Up @@ -153,6 +176,7 @@ class EntitiesControllerTests extends Specification {
"current":false
}
'''

when:
def result = mockMvc.perform(get('/entities/http%3A%2F%2Ftest.scaldingspoon.org%2Ftest1'))

Expand Down

0 comments on commit 77d5789

Please sign in to comment.