<commit_before>package com.cabolabs.security

/*
 * This test is complete and updated. 2017-04-16
 * 
 * The UserControllerIntegrationSpec test were added to this unit test
 * because of some issues with integration tests and @Transactional make
 * impossible to implement as integration.
 * 
 * This is a possible solution, should be tried:
 * http://stackoverflow.com/questions/39831355/grails-spock-unit-test-requires-to-mock-transaction-manager
 * 
 * This should be used  to test the UserController.
 */

import java.util.List;

import grails.test.mixin.*
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap

import spock.lang.*
import grails.plugin.springsecurity.SpringSecurityUtils
import grails.plugin.springsecurity.SpringSecurityService

@TestFor(UserController)
@Mock([User, Role, UserRole, Organization])
class UserControllerSpec extends Specification {

   // this is executed before al the tests, allows metaprogramming.
   def setupSpec()
   {
      // without this actions that check permissions fail
      SpringSecurityUtils.metaClass.static.ifAllGranted = { String role ->
         return true
      }
   }
   
   def setup()
   {
      // mock logged in user
      // http://stackoverflow.com/questions/11925705/mock-grails-spring-security-logged-in-user
      def organization = new Organization(name: 'Hospital de Clinicas', number: '1234')
      organization.save(failOnError:true, flush: true)

      def loggedInUser = new User(username:"admin", password:"<PASSWORD>", email:"<EMAIL>") //, organizations:[organization])
      loggedInUser.addToOrganizations(organization)
      loggedInUser.save(failOnError:true, flush: true)
      
      def role = new Role(authority: 'ROLE_ADMIN')
      role.save(failOnError:true, flush: true)
      
      UserRole.create( loggedInUser, role, true )
      
      controller.springSecurityService = [
        encodePassword: '<PASSWORD>',
        reauthenticate: { String u -> true},
        loggedIn: true,
        principal: loggedInUser,
        currentUser: loggedInUser
      ]
      
      
      controller.userService = [
         saveAndNotify : { User userInstance, params ->
            
            userInstance.save(failOnError:true) // need to save the user, if not the save test fails
         },
         updateOrganizations : { User user, List newOrgUids -> return }
      ]
      /* this doesnt work, the mock above does
      controller.userService = new UserService()
      
      // mocking injection
      controller.userService.springSecurityService = controller.springSecurityService
      */
   }
   
   def cleanup()
   {
      def user = User.findByUsername("admin")
      def role = Role.findByAuthority('ROLE_ADMIN')
      
      UserRole.remove(user, role)
      user.delete(flush: true)<commit_msg>test fix<commit_after>package com.cabolabs.security

/*
 * This test is complete and updated. 2017-04-16
 * 
 * The UserControllerIntegrationSpec test were added to this unit test
 * because of some issues with integration tests and @Transactional make
 * impossible to implement as integration.
 * 
 * This is a possible solution, should be tried:
 * http://stackoverflow.com/questions/39831355/grails-spock-unit-test-requires-to-mock-transaction-manager
 * 
 * This should be used  to test the UserController.
 */

import java.util.List;

import grails.test.mixin.*
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap

import spock.lang.*
import grails.plugin.springsecurity.SpringSecurityUtils
import grails.plugin.springsecurity.SpringSecurityService

@TestFor(UserController)
@Mock([User, Role, UserRole, Organization])
class UserControllerSpec extends Specification {

   // this is executed before al the tests, allows metaprogramming.
   def setupSpec()
   {
      // without this actions that check permissions fail
      SpringSecurityUtils.metaClass.static.ifAllGranted = { String role ->
         return true
      }
   }
   
   def setup()
   {
      // mock logged in user
      // http://stackoverflow.com/questions/11925705/mock-grails-spring-security-logged-in-user
      def organization = new Organization(name: 'Hospital de Clinicas', number: '1234')
      organization.save(failOnError:true, flush: true)

      def loggedInUser = new User(username:"orgman", password:"<PASSWORD>", email:"<EMAIL>") //, organizations:[organization])
      loggedInUser.addToOrganizations(organization)
      loggedInUser.save(failOnError:true, flush: true)
      
      def role = new Role(authority: 'ROLE_ORG_MANAGER')
      role.save(failOnError:true, flush: true)
      
      UserRole.create( loggedInUser, role, true )
      
      controller.springSecurityService = [
        encodePassword: '<PASSWORD>',
        reauthenticate: { String u -> true},
        loggedIn: true,
        principal: loggedInUser,
        currentUser: loggedInUser
      ]
      
      
      controller.userService = [
         saveAndNotify : { User userInstance, params ->
            
            userInstance.save(failOnError:true) // need to save the user, if not the save test fails
         },
         updateOrganizations : { User user, List newOrgUids -> return }
      ]
      /* this doesnt work, the mock above does
      controller.userService = new UserService()
      
      // mocking injection
      controller.userService.springSecurityService = controller.springSecurityService
      */
   }
   
   def cleanup()
   {
      def user = User.findByUsername("admin")
      def role = Role.findByAuthority('ROLE_ADMIN')
      
      UserRole.remove(user, role)
      user.delete(flush: true)
{'fraction_non_alphanumeric': 0.09462824617573817, 'fraction_numerical': 0.009960868018498754, 'mean_word_length': 2.9542897327707456, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '977942', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1547, 'n_words': 396}