Skip to content

Commit

Permalink
Changed the validation of creating users, password is now mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
gardeman committed Dec 18, 2009
1 parent 4999acb commit 9c75d5b
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 24 deletions.
4 changes: 2 additions & 2 deletions grails-app/conf/BootStrap.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ class BootStrap {
def createEvent = new UserEventCreate(
username: username,
userRealName:userRealName,
passwd:authenticateService.passwordEncoder(passwd),
passwdRepeat:authenticateService.passwordEncoder(passwd),
passwd: passwd,
passwdRepeat: passwd,
enabled:enabled,
description:description,
email:email
Expand Down
11 changes: 5 additions & 6 deletions grails-app/controllers/UserController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,6 @@ class UserController {
def user = new User()
def createEvent

if( params.passwd || params.passwdRepeat){
params.passwd = authenticateService.encodePassword(params.passwd)
params.passwdRepeat = authenticateService.encodePassword(params.passwdRepeat)
}

user.properties = params
createEvent = new UserEventCreate(user:user)
createEvent.populateFromUser()
Expand All @@ -162,8 +157,12 @@ class UserController {
}
else {
flash.message = null
user.errors = createEvent.errors
createEvent.user.errors = createEvent.errors
createEvent.properties['passwd','passwdRepeat'].each{ println it }
println '----'
createEvent.errors.allErrors.each { println it }
println '----'
createEvent.user.errors.allErrors.each{ println it }
}

return render(template: '/login/register' , model: [ person : createEvent.user ])
Expand Down
2 changes: 1 addition & 1 deletion grails-app/domain/se/qbranch/qanban/Event.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Event implements Comparable {
if(o instanceof Event) {
Event event = (Event) o
if(this.id == event.id)
return true
return true
}
return false
}
Expand Down
6 changes: 2 additions & 4 deletions grails-app/domain/se/qbranch/qanban/User.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ class User {
static constraints = {
username(blank: false, unique: true)
userRealName(blank: false)
passwd(nullable: true)
passwd( nullable: true, validator:{ val, obj ->
if( val && val != obj.passwdRepeat ){
passwd( nullable: false, blank: false, validator:{ val, obj ->
if( val != obj.passwdRepeat ){
return ['userEventCreate.passwd.notEqualRepeat']
}
return true
})
enabled()
domainId( nullable: false, blank: false, unique: true)
Expand Down
10 changes: 7 additions & 3 deletions grails-app/domain/se/qbranch/qanban/UserEventCreate.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ package se.qbranch.qanban

class UserEventCreate extends UserEvent {

def authenticateService

static constraints = {
username(blank: false, unique: true)
userRealName(blank: false)
email(nullable: false, blank: false, email:true)
enabled( nullable: true)
emailShow( nullable: true)
passwd( nullable: true, validator:{ val, obj ->
if( val && val != obj.passwdRepeat ){
passwd( nullable: false, blank: false, validator:{ val, obj ->
if( val != obj.passwdRepeat ){
return ['userEventCreate.passwd.notEqualRepeat']
}
return true
})
}

Expand All @@ -48,6 +49,8 @@ class UserEventCreate extends UserEvent {

def beforeInsert = {
generateDomainId(username,userRealName,email)
passwd = authenticateService.encodePassword(passwd)
passwdRepeat = authenticateService.encodePassword(passwdRepeat)
userDomainId = domainId // You create yourself
}

Expand All @@ -63,6 +66,7 @@ class UserEventCreate extends UserEvent {
Role.list().each{ role ->
role.addToPeople(user)
}


user.save()
}
Expand Down
14 changes: 9 additions & 5 deletions test/unit/UserControllerTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,14 @@ class UserControllerTests extends ControllerUnitTestCase {
}
controller.eventService = eventServiceMock.createMock()

authenticateServiceMock = mockFor(AuthenticateService)
authenticateServiceMock.demand.static.encodePassword(1..2) { pass -> pass }
controller.authenticateService = authenticateServiceMock.createMock()


mockDomain(UserEventCreate)
UserEventCreate.metaClass.beforeInsert = {
generateDomainId(username,userRealName,email)
userDomainId = domainId // You create yourself
}

mockDomain(Role)

}
Expand All @@ -152,8 +155,9 @@ class UserControllerTests extends ControllerUnitTestCase {

controller.save()

assertEquals numberOfPreviusUsers+1, User.list().size()
assertNotNull "The user should have gotten an id from the database", renderArgs.model.person.id
assertEquals numberOfPreviusUsers, User.list().size()
assertEquals 1, renderArgs.model.person.errors.allErrors.size()
assertNull "The user shouldnt have gotten an id from the database", renderArgs.model.person.id

}

Expand Down
41 changes: 38 additions & 3 deletions test/unit/se/qbranch/qanban/UserEventCreateTests.groovy
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package se.qbranch.qanban

import grails.test.*
import org.grails.plugins.springsecurity.service.AuthenticateService

class UserEventCreateTests extends GrailsUnitTestCase {

def authMock

protected void setUp() {
super.setUp()
mockDomain(User)
mockDomain(Role)
mockDomain(UserEventCreate)

authMock = mockFor(AuthenticateService)
authMock.demand.static.encodePassword(1..2) { pass -> pass }


}

protected void tearDown() {
Expand All @@ -19,8 +28,9 @@ class UserEventCreateTests extends GrailsUnitTestCase {
def username = "opsmrkr01"
def userRealname = "mr. Krister"
def email = "mr.krister@mail.com"

def event = new UserEventCreate(username: username, userRealName: userRealname, email: email, enabled: true)
def passwd ="pass"
def event = new UserEventCreate(username: username, userRealName: userRealname, email: email, enabled: true, passwd: passwd, passwdRepeat: passwd)
event.authenticateService = authMock.createMock()

event.beforeInsert()
if( event.save() ){
Expand All @@ -38,8 +48,10 @@ class UserEventCreateTests extends GrailsUnitTestCase {
void testCreatingAUserFromAUserObj(){
assertEquals 0, User.list().size()

def user = new User(username: "opsmrkr01",userRealName: "Mister Krister",email:"mr.kr@gmail.com",enabled: true)
def user = new User(username: "opsmrkr01",userRealName: "Mister Krister",email:"mr.kr@gmail.com",enabled: true,passwd: 'pass',passwdRepeat: 'pass')
def event = new UserEventCreate(user: user)
event.authenticateService = authMock.createMock()

event.populateFromUser()

assertEquals user.username, event.username
Expand All @@ -64,6 +76,8 @@ class UserEventCreateTests extends GrailsUnitTestCase {
def initNoOfUsers = User.list().size()
def user = new User(username: "opsmrkr01",userRealName: "Mister Krister",email:"mr.kr@gmail.com",enabled: true,passwd:"pass1")
def event = new UserEventCreate(user:user)
event.authenticateService = authMock.createMock()

event.populateFromUser()
event.passwdRepeat = "pass1"

Expand All @@ -86,6 +100,8 @@ class UserEventCreateTests extends GrailsUnitTestCase {
def initNoOfUsers = User.list().size()
def user = new User(username: "opsmrkr01",userRealName: "Mister Krister",email:"mr.kr@gmail.com",enabled: true,passwd:"pass1")
def event = new UserEventCreate(user:user)
event.authenticateService = authMock.createMock()

event.populateFromUser()
event.passwdRepeat = "pass2"

Expand All @@ -100,4 +116,23 @@ class UserEventCreateTests extends GrailsUnitTestCase {
assertNull "The user should not have a id", userAfter.id
assertEquals initNoOfUsers, User.list().size()
}

void testCreateWithoutPasswords(){
def initNoOfUsers = User.list().size()
def user = new User(username: "opsmrkr01",userRealName: "Mister Krister",email:"mr.kr@gmail.com",enabled: true)
def event = new UserEventCreate(user:user)
event.authenticateService = authMock.createMock()
event.populateFromUser()

event.beforeInsert()

if(event.save()){
event.process()
}

def userAfter = event.user
assertEquals 1 , event.errors.allErrors.size()
assertNull "The user should not have a id", userAfter.id
assertEquals initNoOfUsers, User.list().size()
}
}

0 comments on commit 9c75d5b

Please sign in to comment.