I am using Spring Security in my grails project. I installed the spring -security-core plugin and the spring -security-ui plugin.
The domain classes that I used for Person and Power are User and Role respectively
In accordance with the requirements of the project, I changed my User.groovy class, and the code for it is below:
class User {
transient springSecurityService
String employeeId
String firstName
String lastName
String password
String emailId
String mobileNumber
String address
String city
String zipCode
User manager
static hasMany = [previousPasswords: String]
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static transients = ['springSecurityService']
static constraints = {
employeeId blank: false, unique: true
firstName blank: false
lastName blank: false
password blank: false, password: true, validator: { val, obj ->
if(obj.previousPasswords) {
return !obj.previousPasswords.contains(encode(val.toUpperCase()))
}
return true
}
emailId blank: false, email: true
mobileNumber nullable: true
address nullable: true
city nullable: true
zipCode nullable: true
manager nullable: true
previousPasswords display: false, editable: false
}
static mapping = {
password column: '`password`'
}
Set<Role> getAuthorities() {
UserRole.findAllByUser(this).collect { it.role } as Set
}
def beforeInsert() {
previousPasswords = [encode(password.toUpperCase())]
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
previousPasswords << encode(password.toUpperCase())
encodePassword()
}
}
protected String encode(String pwd) {
return springSecurityService.encodePassword(pwd)
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
}
I am trying to write unit tests to check the limits for this class of devices
One of my tests looks below
void "test if employeeId can be blank or non-unique"() {
given:
def springSecurityService = mockFor(SpringSecurityService,true)
springSecurityService.encodePassword(){String pwd -> return null}
when: 'employeeId is blank'
def user = new User(employeeId: " ")
user.springSecurityService= springSecurityService
then: 'validation fails'
!user.validate()
user.errors.getFieldError("employeeId").codes.contains("nullable")
when: 'employeeId is unique'
user = new User(employeeId: "empId1", firstName: "f_name", lastName: "l_name", password: "password", emailId: "test@hptest.com")
user.springSecurityService= springSecurityService
then: 'validation succeeds'
user.validate()
user.save(flush: true, failOnError: true)
mockForConstraintsTests(User, [user])
when: 'employeeId is non unique'
def user_2 = new User(employeeId: "empId1")
user_2.springSecurityService= springSecurityService
then: 'validation fails'
!user_2.validate()
user_2.errors.getFieldError("employeeId").codes.contains("unique")
}
I try to play SpringSecurityService differently, but everyone seems to fail. Can someone suggest a way to mock this service.
I am currently getting this error.
groovy.lang.MissingMethodException: No signature of method: grails.test.GrailsMock.encodePassword() is applicable for argument types: (com.hp.bots.UserSpec$_$spock_feature_0_0_closure1) values: [com.hp.bots.UserSpec$_$spock_feature_0_0_closure1@18324cd]
at com.hp.bots.UserSpec.test if employeeId can be blank or non-unique(UserSpec.groovy:25)
, ( ). , .