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
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With