<commit_before>package fedreg.core

import grails.plugin.spock.*

import fedreg.core.*
import fedreg.workflow.*
import grails.plugins.nimble.core.*

class SPSSODescriptorControllerSpec extends IntegrationSpec {
	
    def user, spssoDescriptorService, controller
	
	def cleanup() {
       user.perms = []
	}
	
	def setup () {
        spssoDescriptorService = new SPSSODescriptorService()
        controller = new SPSSODescriptorController(SPSSODescriptorService:spssoDescriptorService)

		user = UserBase.build()
		SpecHelpers.setupShiroEnv(user)
	}
	
	def setupBindings() {
		def soap = new SamlURI(type:SamlURIType.ProtocolBinding, uri:'urn:oasis:names:tc:SAML:2.0:bindings:SOAP', description:'').save()
		def httpRedirect = new SamlURI(type:SamlURIType.ProtocolBinding, uri:'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect', description:'').save()
		def httpPost = new SamlURI(type:SamlURIType.ProtocolBinding, uri:'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST', description:'').save()
		def httpArtifact = new SamlURI(type:SamlURIType.ProtocolBinding, uri:'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact', description:'').save()
	}
	
	def "Validate list"() {
		setup:
		
		(1..25).each { i ->
			def ed = EntityDescriptor.build(entityID:"http://sp.test.com/$i")
			def sp = SPSSODescriptor.build(entityDescriptor: ed)
			sp.save()
		}
		
		when:
		def model = controller.list()

		then:
		model.serviceProviderList.size() == 25
	}
	
	def "Show with no ID"() {		
		when:
		controller.show()
		
		then:
		controller.flash.type == "error"
		controller.flash.message == "fedreg.controllers.namevalue.missing"
		controller.response.redirectedUrl == "/SPSSODescriptor/list"
	}
	
	def "Show with invalid SPSSODescriptor ID"() {
		setup:
		controller.params.id = 200000000
			
		when:
		controller.show()
		
		then:
        controller.flash.type == "error"
		controller.flash.message == "fedreg.core.spssoroledescriptor.nonexistant"
        controller.response.redirectedUrl == "/SPSSODescriptor/list"
	}
	
	def "Validate create"() {
		setup:
		(1..10).each {
			Organization.build(active:true, approved:true).save()
		}
		
		(1..11).each { i ->
			AttributeBase.build(name: "attr$i").save()
		}
		
		(1..12).each { i ->
			SamlURI.build(type:SamlURIType.NameIdentifierFormat).save()
		}
		
		when:
		def model = controller.create()

		then:
		model.serviceProvider != null
		model.serviceProvider instanceof SPSSODescriptor
		model.organizationList.size() == 10
		model.attributeList.size() == 11
		model.nameIDFormatList.size() == 12
	}
	
	def "Validate successful save"() {
		setup:
		def organization = Organization.build().save()
		def entityDescriptor = EntityDescriptor.build(organization:organization).save()
		def serviceProvider = SPSSODescriptor.build(entityDescriptor:entityDescriptor).save()
		
        spssoDescriptorService.metaClass.create = { def p -> 
            return [true, [organization:organization, entityDescriptor:entityDescriptor, serviceProvider:serviceProvider]]  
            //.. deliberately leaving out other return vals here
        } 

		when:
		def model = controller.save()
		
		then:
		controller.response.redirectedUrl == "/SPSSODescriptor/show/${serviceProvider.id}"	
	}
	
	def "Validate failed save"() {
		setup:
		def organization = Organization.build()
		def entityDescriptor = EntityDescriptor.build(organization:organization)
		def serviceProvider = SPSSODescriptor.build(entityDescriptor:entityDescriptor)
		
        spssoDescriptorService.metaClass.create = { def p -> 
            return [false, [organization:organization, entityDescriptor:entityDescriptor, serviceProvider:serviceProvider]] 
            //.. deliberately leaving out other return vals here
        } 

		when:
		def model = controller.save()
		
		then:
		organization == controller.modelAndView.model.organization	
		entityDescriptor == controller.modelAndView.model.entityDescriptor	
		serviceProvider == controller.modelAndView.model.serviceProvider
		
		controller.flash.type == "error"
		controller.flash.message == "fedreg.core.spssoroledescriptor.save.validation.error"	
	}
	
	def "Validate successful update"() {
		setup:
		def organization = Organization.build()
		def entityDescriptor = EntityDescriptor.build(organization:organization)
		def serviceProvider = SPSSODescriptor.build(entityDescriptor:entityDescriptor).save()
		
		controller.params.id = serviceProvider.id
        user.perms.add("descriptor:${serviceProvider.id}:update")
		spssoDescriptorService.metaClass.update = { def p -> 
            return [true, serviceProvider]
        } 

		when:
		def model = controller.update()
		
		then:
		controller.response.redirectedUrl == "/SPSSODescriptor/show/${serviceProvider.id}"	
	}

    def "Validate unsuccessful update with incorrect perms"() {
        setup:
        def organization = Organization.build()
        def entityDescriptor = EntityDescriptor.build(organization:organization)
        def serviceProvider = SPSSODescriptor.build(entityDescriptor:entityDescriptor).save()
        
        controller.params.id = serviceProvider.id
<commit_msg>updated test name<commit_after>package fedreg.core

import grails.plugin.spock.*

import fedreg.core.*
import fedreg.workflow.*
import grails.plugins.nimble.core.*

class SPSSODescriptorControllerSpec extends IntegrationSpec {
	
    def user, spssoDescriptorService, controller
	
	def cleanup() {
       user.perms = []
	}
	
	def setup () {
        spssoDescriptorService = new SPSSODescriptorService()
        controller = new SPSSODescriptorController(SPSSODescriptorService:spssoDescriptorService)

		user = UserBase.build()
		SpecHelpers.setupShiroEnv(user)
	}
	
	def setupBindings() {
		def soap = new SamlURI(type:SamlURIType.ProtocolBinding, uri:'urn:oasis:names:tc:SAML:2.0:bindings:SOAP', description:'').save()
		def httpRedirect = new SamlURI(type:SamlURIType.ProtocolBinding, uri:'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect', description:'').save()
		def httpPost = new SamlURI(type:SamlURIType.ProtocolBinding, uri:'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST', description:'').save()
		def httpArtifact = new SamlURI(type:SamlURIType.ProtocolBinding, uri:'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact', description:'').save()
	}
	
	def "Validate list"() {
		setup:
		
		(1..25).each { i ->
			def ed = EntityDescriptor.build(entityID:"http://sp.test.com/$i")
			def sp = SPSSODescriptor.build(entityDescriptor: ed)
			sp.save()
		}
		
		when:
		def model = controller.list()

		then:
		model.serviceProviderList.size() == 25
	}
	
	def "Show with no ID"() {		
		when:
		controller.show()
		
		then:
		controller.flash.type == "error"
		controller.flash.message == "fedreg.controllers.namevalue.missing"
		controller.response.redirectedUrl == "/SPSSODescriptor/list"
	}
	
	def "Show with invalid SPSSODescriptor ID"() {
		setup:
		controller.params.id = 200000000
			
		when:
		controller.show()
		
		then:
        controller.flash.type == "error"
		controller.flash.message == "fedreg.core.spssoroledescriptor.nonexistant"
        controller.response.redirectedUrl == "/SPSSODescriptor/list"
	}
	
	def "Validate create"() {
		setup:
		(1..10).each {
			Organization.build(active:true, approved:true).save()
		}
		
		(1..11).each { i ->
			AttributeBase.build(name: "attr$i").save()
		}
		
		(1..12).each { i ->
			SamlURI.build(type:SamlURIType.NameIdentifierFormat).save()
		}
		
		when:
		def model = controller.create()

		then:
		model.serviceProvider != null
		model.serviceProvider instanceof SPSSODescriptor
		model.organizationList.size() == 10
		model.attributeList.size() == 11
		model.nameIDFormatList.size() == 12
	}
	
	def "Validate successful save"() {
		setup:
		def organization = Organization.build().save()
		def entityDescriptor = EntityDescriptor.build(organization:organization).save()
		def serviceProvider = SPSSODescriptor.build(entityDescriptor:entityDescriptor).save()
		
        spssoDescriptorService.metaClass.create = { def p -> 
            return [true, [organization:organization, entityDescriptor:entityDescriptor, serviceProvider:serviceProvider]]  
            //.. deliberately leaving out other return vals here
        } 

		when:
		def model = controller.save()
		
		then:
		controller.response.redirectedUrl == "/SPSSODescriptor/show/${serviceProvider.id}"	
	}
	
	def "Validate failed save"() {
		setup:
		def organization = Organization.build()
		def entityDescriptor = EntityDescriptor.build(organization:organization)
		def serviceProvider = SPSSODescriptor.build(entityDescriptor:entityDescriptor)
		
        spssoDescriptorService.metaClass.create = { def p -> 
            return [false, [organization:organization, entityDescriptor:entityDescriptor, serviceProvider:serviceProvider]] 
            //.. deliberately leaving out other return vals here
        } 

		when:
		def model = controller.save()
		
		then:
		organization == controller.modelAndView.model.organization	
		entityDescriptor == controller.modelAndView.model.entityDescriptor	
		serviceProvider == controller.modelAndView.model.serviceProvider
		
		controller.flash.type == "error"
		controller.flash.message == "fedreg.core.spssoroledescriptor.save.validation.error"	
	}
	
	def "Validate successful update"() {
		setup:
		def organization = Organization.build()
		def entityDescriptor = EntityDescriptor.build(organization:organization)
		def serviceProvider = SPSSODescriptor.build(entityDescriptor:entityDescriptor).save()
		
		controller.params.id = serviceProvider.id
        user.perms.add("descriptor:${serviceProvider.id}:update")
		spssoDescriptorService.metaClass.update = { def p -> 
            return [true, serviceProvider]
        } 

		when:
		def model = controller.update()
		
		then:
		controller.response.redirectedUrl == "/SPSSODescriptor/show/${serviceProvider.id}"	
	}

    def "Validate update with incorrect perms"() {
        setup:
        def organization = Organization.build()
        def entityDescriptor = EntityDescriptor.build(organization:organization)
        def serviceProvider = SPSSODescriptor.build(entityDescriptor:entityDescriptor).save()
        
        controller.params.id = serviceProvider.id

{'fraction_non_alphanumeric': 0.11902189924383777, 'fraction_numerical': 0.007267013650201316, 'mean_word_length': 4.001964636542239, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3441437', 'n_tokens_mistral': 3335, 'n_tokens_neox': 2983, 'n_words': 541}