-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #79: Refactor UserController as demonstrated in PR 13
This is step one of the refactoring which: 1. pulls the existing code used to build the `roleMap` out into its own, testable method 2. implements a test to verify existing functionality
- Loading branch information
Showing
2 changed files
with
51 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
plugin/src/test/groovy/grails/plugin/springsecurity/ui/UserControllerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package grails.plugin.springsecurity.ui | ||
|
||
import grails.testing.web.controllers.ControllerUnitTest | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
@Unroll | ||
class UserControllerSpec extends Specification implements ControllerUnitTest<UserController> { | ||
static final Map ADMIN_ROLE = [authority: "ROLE_ADMIN"] | ||
static final Map SUPER_ADMIN_ROLE = [authority: "ROLE_SUPER_ADMIN"] | ||
static final Map USER_ROLE = [authority: "ROLE_USER"] | ||
|
||
void "verify proper construction of roleMap for user with roles #rolesAssignedToUser"() { | ||
given: "the authority name field has been set to the default name of 'authority'" | ||
controller.authorityNameField = "authority" | ||
|
||
and: "we mock the returning of all Role instances within the database" | ||
controller.metaClass.sortedRoles = { | ||
[ | ||
ADMIN_ROLE, | ||
SUPER_ADMIN_ROLE, | ||
USER_ROLE | ||
] | ||
} | ||
|
||
when: "we call buildRoleMap with the role names associated to the user" | ||
Map results = controller.buildRoleMap(rolesAssignedToUser) | ||
|
||
then: "the user is only granted access to roles with which they are associated" | ||
results == expectedResults | ||
|
||
where: | ||
rolesAssignedToUser | expectedResults | ||
[ADMIN_ROLE.authority, USER_ROLE.authority] as Set | [(ADMIN_ROLE): true, (SUPER_ADMIN_ROLE): false, (USER_ROLE): true] | ||
[] as Set | [:] | ||
null | [:] | ||
} | ||
} |