Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null springSecurityService causes encodePassword to fail in Grails 2.0.1

We are working on a new project with Grails 2.0.1 and Spring Security. The create user context fails due to springSecurityService in the User domain object being null. Strangely this occurs only our Linux text box while on all the developer windows boxes it works fine. Not sure if it has anything to do with the environment or whether it is something else. On the linux box this fails consistently.

The user domain class we are using is below (the plugin generated class with a couple of additional fields). The encodePassword is being handled by the beforeInsert(), beforeUpdate() triggers.

Came across this thread which talks about the transient references causing problems in webflows, which I assume are not being used here, so not sure if this is relevant. http://grails.1312388.n4.nabble.com/Spring-Security-Plugin-1-of-the-time-springSecurityService-null-td4349941.html

class User {

    transient springSecurityService

    static constraints = {
        firstName blank: false, nullable: false, size: 2..100
        lastName blank: false, nullable: false, size: 2..100
        username blank: false, nullable: false, unique : true, email: true
        password blank: false, nullable: false, size: 6..255
    }

    static mapping = {
        password column: '`password`'
    }

    String username
    String password
    boolean enabled
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    /* user details */
    String firstName;
    String lastName;

    Set<Role> getAuthorities() {
        UserRole.findAllByUser(this).collect { it.role } as Set
    }

    def beforeInsert() {
        encodePassword()
    }

    def beforeUpdate() {
        if (isDirty('password')) {
            encodePassword()
        }
    }

    protected void encodePassword() {
        password = springSecurityService.encodePassword(password)
    }
}

Thanks in advance

like image 956
techfoobar Avatar asked Mar 15 '12 05:03

techfoobar


1 Answers

You can use Groovy's meta-programming to override the encodePassword method if you only want to make sure the User object is saved and you don't care about the password for your test.

@TestFor(UserService)    
@Mock(User)
class UserServiceTest {
    void testMethod() {
        User.metaClass.encodePassword = { -> }

        service.invokeTestThatSavesUserDomainClass()
    }   
}
like image 73
mrhaki Avatar answered Nov 16 '22 02:11

mrhaki