Quantcast

How to test my Controller with springSecurityService?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to test my Controller with springSecurityService?

xunitc
I install the spring security pluging.
my controller is:
BookController{
        def save() {
                def book= new Book(params);
                def person = Person.get(springSecurityService.principal.id);
                book.person = person;
                //......
        }
}

and my test-case is:
@TestFor(BookController)
@Mock(Book)
class BookControllerTest {
        void testSave() {
                defineBeans {
                        springSecurityService(SpringSecurityService);
                }
                controller.save();
        }
}

and when I run the unit test, it show me:
java.lang.NullPointerException: Cannot get property 'id' on null object

I know this because the springSecurityService.principal is null, and how can I pass the unit-test?
thx.
Xunitc
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to test my Controller with springSecurityService?

bhushan154
Can you please let me know if you found a solution?

I am a beginner to testing. Can anyone please help me out with best practices to test the following code?

def view={        
        def principal = springSecurityService.principal
        def employee = Employee.findByUsername(principal.username)
        [employee:employee]              
    }

def edit={        
        def principal = springSecurityService.principal
        def employee = Employee.findByUsername(principal.username)
        employee.resume.directSupervisorName = params.directSupervisorName
        employee.resume.directSupervisorEmployeeNumber = params.directSupervisorEmployeeNumber
        if (employee.save()){            
            resetEmployeeRolesService.reset(employee.id)
            flash.message = "Supervisor updated successfully."
            redirect (controller:"resume",action:"view")
        }
        else{
            redirect (controller:"resume",action:"fail")
        }        
    }
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to test my Controller with springSecurityService?

ShatyUT
In reply to this post by xunitc
You'll want to check out the mockFor method. This will allow you to mock a service object and define it's behaviors including calls to it's methods. Check out this post:

http://grails.1312388.n4.nabble.com/Grails-mockFor-values-not-returned-in-controller-td2292369.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to test my Controller with springSecurityService?

bhushan154
Worked. Thanks a lot.

Code:
def view={        
        def principal = springSecurityService.principal
        def employee = Employee.findByUsername(principal.username)
        [employee:employee]              
    }

Test Case:
void testView() {        
        //Create two employee objects
        def resumeRiya = new Resume()        
        def Riya = new
Employee(username:'Riya',password:'046',enabled:'true',accountExpired:'true',accountLocked:'true',passwordExpired:'false',name:'riya',email:'[hidden email]',resume:resumeRiya)                
        def resumeNeha = new Resume()        
        def Neha = new
Employee(username:'Riya',password:'046',enabled:'true',accountExpired:'true',accountLocked:'true',passwordExpired:'false',name:'riya',email:'[hidden email]',resume:resumeNeha)                
        //Create mock domain objects
        mockDomain(Employee,[Riya, Neha])        
        //Mock the spring security service
        def mockSpringSecurityService =
mockFor(grails.plugins.springsecurity.SpringSecurityService)        
        //Provide implementation spring security getPrincipal method
        mockSpringSecurityService.demand.getPrincipal() { ->
            ["username":"Riya"]
        }        
        controller.springSecurityService =
mockSpringSecurityService.createMock()        
        def model = controller.view()
        assertEquals "Riya", model["employee"]?.username
    }

--
View this message in context: http://grails.1312388.n4.nabble.com/How-to-test-my-Controller-with-springSecurityService-tp4213803p4222365.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to test my Controller with springSecurityService?

bhushan154
In reply to this post by ShatyUT
Worked. Thanks a lot.

Code
:
def view={        
        def principal = springSecurityService.principal
        def employee = Employee.findByUsername(principal.username)
        [employee:employee]              
    }

Test Case:
void testView() {        
        //Create two employee objects
        def resumeRiya = new Resume()        
        def Riya = new Employee(username:'Riya',password:'046',enabled:'true',accountExpired:'true',accountLocked:'true',passwordExpired:'false',name:'riya',email:'riya@xyz.com',resume:resumeRiya)                
        def resumeNeha = new Resume()        
        def Neha = new Employee(username:'Riya',password:'046',enabled:'true',accountExpired:'true',accountLocked:'true',passwordExpired:'false',name:'riya',email:'riya@xyz.com',resume:resumeNeha)                
        //Create mock domain objects
        mockDomain(Employee,[Riya, Neha])        
        //Mock the spring security service
        def mockSpringSecurityService = mockFor(grails.plugins.springsecurity.SpringSecurityService)        
        //Provide implementation spring security getPrincipal method
        mockSpringSecurityService.demand.getPrincipal() { ->
            ["username":"Riya"]
        }        
        controller.springSecurityService = mockSpringSecurityService.createMock()        
        def model = controller.view()
        assertEquals "Riya", model["employee"]?.username
    }
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to test my Controller with springSecurityService?

xunitc
In reply to this post by bhushan154
I know how to inject the springSecurityService which in the controller, please use this:
void testSome() {
...
defineBeans {
        springSecurityService(SpringSecurityService);
}
...
}
so the springSecurityService which in the controller can be used.
but I do not know the point which can inject the person's springSecurityService when invoke Person.get(id) in the controller.
like this:
[BookController]
class BookController {
    def save(){
        def book = new Book(params);
        def person = Person.read(springSecurityService.principal.id);
        book.person = person;
        book.save();
    }
}
when the book.save(); maybe the person's save() method is invoked too, but now the person has no springSecurityService. so person's method encodePassword() can't pass. but the save() need beforeInsert() , and beforeInsert() need encodePassword(). so the test can't pass.
I think one way to solve it like this :
testSome() {
        ...
        Person.metaClass.encodePassword = {
                return delegate.password = 'ENCODE_PASSWORD';
        }

        def person = new Person(username: 'admin', password: 'admin', enabled: true).save();
        SCH.context.authentication = new UsernamePasswordAuthenticationToken(person, person.password, AuthorityUtils.createAuthorityList('ROLE_ADMIN'));
}
I hope you can pass your test.
GOOD LUCK!
Xunitc
Loading...