Browse files

Add code that reproduces issue with unit test.

  • Loading branch information...
1 parent 9d21a2d commit e51ea6d1493f7502f8d44983517c19e6a4eb7b6f @pledbrook pledbrook committed Jan 27, 2012
View
2 application.properties
@@ -1,6 +1,6 @@
#Grails Metadata file
#Sun Jan 22 10:26:15 GMT 2012
-app.grails.version=2.0.0
+app.grails.version=2.0.1.BUILD-SNAPSHOT
app.name=site
app.servlet.version=2.4
app.version=1.2-SNAPSHOT
View
16 grails-app/conf/BootStrap.groovy
@@ -1,16 +1,17 @@
import grails.util.Environment
-import org.apache.commons.codec.digest.DigestUtils
import org.grails.*
import org.grails.auth.Role
import org.grails.auth.User
import org.grails.content.Version
import org.grails.downloads.Mirror
+import org.grails.meta.UserInfo
import org.grails.plugin.Plugin
class BootStrap {
def fixtureLoader
def searchableService
+ def userService
def init = { servletContext ->
def (adminRole, editorRole, observerRole) = setUpRoles()
@@ -25,12 +26,13 @@ During the first run you must specify a password to use for the admin account. F
grails -Dinitial.admin.password=changeit run-app""")
}
else {
- admin = new User(login:"admin", email:"info@g2one.com",password:DigestUtils.shaHex(password))
- assert admin.email
- assert admin.addToRoles(adminRole)
- .addToRoles(editorRole)
- .addToRoles(observerRole)
- .save(flush:true, failOnError: true)
+ admin = userService.createStandardUser("admin", "info@g2one.com", password)
+ admin.addToRoles adminRole
+
+ def userInfo = new UserInfo(
+ name: "System Administrator",
+ email: "info@g2one.com")
+ userService.saveUserInfo(userInfo, admin)
}
}
else if (!admin.roles) {
View
4 grails-app/conf/BuildConfig.groovy
@@ -38,7 +38,7 @@ grails.project.dependency.resolution = {
":feeds:1.5",
":grails-ui:1.2.3",
":greenmail:1.2.2",
- ":hibernate:$grailsVersion",
+ ":hibernate:2.0.0",
":jquery:1.6.1.1",
":mail:1.0-SNAPSHOT",
":pretty-time:0.3",
@@ -68,7 +68,7 @@ grails.project.dependency.resolution = {
excludes 'xml-apis'
}
- build ":tomcat:$grailsVersion"
+ build ":tomcat:2.0.0"
}
dependencies {
View
42 grails-app/controllers/org/grails/auth/UserController.groovy
@@ -20,6 +20,7 @@ class UserController {
def scaffold = User
def mailService
+ def userService
String randomPass() {
UUID uuid = UUID.randomUUID()
@@ -47,17 +48,18 @@ class UserController {
}
def profile = {
- def userInfo = UserInfo.findByUser(request.user)
+ def user = request.user
+ def userInfo = UserInfo.findByUser(user)
if(request.method == 'POST') {
- if(!userInfo) userInfo = new UserInfo(user:request.user)
+ if(params.password) {
+ user.updatePassword params.password
+ }
+
+ if(!userInfo) userInfo = new UserInfo()
userInfo.properties = params
- userInfo.save()
- if(params.password) {
- request.user.password = DigestUtils.shaHex(params.password)
- request.user.save()
- }
+ userService.saveUserInfo userInfo, user
}
- return [user:request.user, userInfo:userInfo]
+ return [user:user, userInfo:userInfo]
}
@@ -93,37 +95,31 @@ class UserController {
}
else {
- user = new User(login:params.login, password: (params.password ? DigestUtils.shaHex(params.password) : null), email:params.email)
- .addToRoles(Role.findByName(Role.EDITOR))
- .addToRoles(Role.findByName(Role.OBSERVER))
+ def userInfo = new UserInfo(params)
+ user = userService.createStandardUser(params.login, params.password, params.email)
- if(!user.hasErrors() && user.save(flush:true)) {
- def userInfo = new UserInfo(params)
- userInfo.user = user
- userInfo.save()
-
+ if (userService.saveUserInfo(userInfo, user)) {
def authToken = new UsernamePasswordToken(user.login, params.password)
- SecurityUtils.subject.login(authToken)
-
- if(params.originalURI) {
+ SecurityUtils.subject.login authToken
- redirect(url:params.originalURI, params:params)
+ if (params.originalURI) {
+ redirect url: params.originalURI, params: params
}
else {
- redirect(uri:"/")
+ redirect uri: "/"
}
}
else {
renderParams.model.user = user
- render(renderParams)
+ render renderParams
}
}
}
}
else {
- render(renderParams)
+ render renderParams
}
}
View
17 grails-app/domain/org/grails/auth/User.groovy
@@ -1,5 +1,7 @@
package org.grails.auth
+import org.apache.commons.codec.digest.DigestUtils
+
class User {
String email
String login
@@ -17,6 +19,21 @@ class User {
cache true
}
+ def updatePassword(passwd) {
+ this.password = passwd ? DigestUtils.shaHex(passwd) : null
+ return this
+ }
+
+ def addStandardRoles() {
+ def roles = Role.findAllByNameInList([Role.EDITOR, Role.OBSERVER])
+// println ">> Roles: $roles"
+ for (r in roles) {
+ this.addToRoles r
+ }
+// println ">> This user's roles: ${this.roles}"
+ return this
+ }
+
String toString() {
login
}
View
5 grails-app/domain/org/grails/meta/UserInfo.groovy
@@ -18,7 +18,6 @@ class UserInfo {
static constraints = {
user nullable: true
email blank: false, email: true
- name blank: false
+ name nullable: true, blank: false
}
-
-}
+}
View
21 grails-app/services/org/grails/auth/UserService.groovy
@@ -3,6 +3,27 @@ package org.grails.auth
import grails.plugin.springcache.annotations.Cacheable
class UserService {
+ def createStandardUser(login, password, email) {
+ def user = new User(
+ login: login,
+ email: email)
+ user.updatePassword password
+ user.addStandardRoles()
+ return user
+ }
+
+ def saveUserInfo(userInfo, user = null) {
+ if (userInfo.validate() && user) {
+ userInfo.user = user
+
+ if (!user.save()) return null
+
+ user.email = userInfo.email
+ }
+
+ return userInfo.save(flush: true)
+ }
+
@Cacheable("permissions")
def permissionsForUser(principal) {
def user = User.findByLogin(principal)
View
3 grails-app/views/user/_registerForm.gsp
@@ -18,6 +18,9 @@
<span class="label"><label for="password">Confirm Password:</label></span> <g:field type="password" name="password2" value="${params.password2}" />
</p>
<p>
+ <span class="label"><label for="name">Full name:</label></span> <g:textField name="name" value="${params.name}"/>
+ </p>
+ <p>
<span class="label"><label for="email">Email:</label></span> <g:textField name="email" value="${params.email}"/>
</p>
<p>
View
32 migrations/changelog-1.2.groovy
@@ -32,4 +32,36 @@ databaseChangeLog = {
}
}
+
+ changeSet(author: "pledbrook", id: "multiple-plugin-authors") {
+ createTable(tableName: "plugin_user_info") {
+ column(name: "plugin_authors_id", type: "bigint")
+
+ column(name: "user_info_id", type: "bigint")
+ }
+
+ createIndex(indexName: "FKA7111E96806B062", tableName: "plugin_user_info") {
+ column(name: "plugin_authors_id")
+ }
+
+ createIndex(indexName: "FKA7111E962D69D3D0", tableName: "plugin_user_info") {
+ column(name: "user_info_id")
+ }
+
+ addColumn(tableName: "user_info") {
+ column(name: "email", type: "varchar(255)") {
+ constraints(nullable: "false")
+ }
+ }
+
+ addColumn(tableName: "user_info") {
+ column(name: "name", type: "varchar(255)") {
+ constraints(nullable: "true")
+ }
+ }
+
+ dropNotNullConstraint(columnDataType: "bigint", columnName: "user_id", tableName: "user_info")
+ dropNotNullConstraint(columnDataType: "varchar(255)", columnName: "author_email", tableName: "plugin")
+ }
+
}
View
3 test/unit/org/grails/auth/UserControllerTests.groovy
@@ -86,6 +86,7 @@ class UserControllerTests {
request.method = "POST"
+ controller.userService = new UserService()
controller.register()
assert view == "/user/register"
@@ -111,6 +112,7 @@ class UserControllerTests {
request.method = "POST"
+ controller.userService = new UserService()
controller.register()
assert response.redirectedUrl == params.originalURI
@@ -133,6 +135,7 @@ class UserControllerTests {
request.method = "POST"
+ controller.userService = new UserService()
controller.register()
assert response.redirectedUrl == "/"
View
36 test/unit/org/grails/auth/UserServiceSpec.groovy
@@ -0,0 +1,36 @@
+package org.grails.auth
+
+import grails.plugin.spock.*
+import org.apache.commons.codec.digest.DigestUtils
+import spock.lang.*
+
+@TestFor(UserService)
+@Mock([User, Role])
+class UserServiceSpec extends spock.lang.Specification {
+
+ def setup() {
+ new Role(name: Role.EDITOR).save()
+ new Role(name: Role.OBSERVER).save()
+ new Role(name: Role.ADMINISTRATOR).save(flush: true)
+ }
+
+ def "Creating a user with standard roles"() {
+ given: "A user service and as set of standard roles"
+ def userService = new UserService()
+ def expectedRoles = [Role.EDITOR, Role.OBSERVER]
+
+ when: "I create a standard user"
+ def user = userService.createStandardUser("adam", "pass", "a.bloggs@somewhere.net")
+
+ then: "That user has the given username and password"
+ user.login == "adam"
+ user.email == "a.bloggs@somewhere.net"
+
+ and: "the password is hashed"
+ user.password != "pass"
+ user.password == DigestUtils.shaHex("pass")
+
+ and: "the user has the standard roles"
+ user.roles*.name.intersect(expectedRoles) == expectedRoles
+ }
+}

0 comments on commit e51ea6d

Please sign in to comment.