Skip to content

Commit

Permalink
Tests for SecurityService
Browse files Browse the repository at this point in the history
  • Loading branch information
joshareed committed Oct 13, 2011
1 parent 89ed863 commit d720b08
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ class SecurityService {
}

User authenticate(String email, String password) {
def u = User.mongoCollection.find(email: email, password: encodePassword(password), enabled: true)
if (u) {
return new User(u)
} else {
return null
}
User.findInstance(email: email, password: encodePassword(password), enabled: true)
}

def getRequiredRoles(String url) {
Expand Down
75 changes: 67 additions & 8 deletions common/test/unit/coreref/security/SecurityServiceTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,80 @@ package coreref.security

import grails.test.*

import coreref.mongo.MongoService
import coreref.common.User
import com.mongodb.*

class SecurityServiceTests extends GrailsUnitTestCase {
def service
def mongoService

protected void setUp() {
super.setUp()

mongoService = new MongoService()
mongoService.map(User)
assert User.mongoCollection

protected void setUp() {
super.setUp()
service = new SecurityService()
}
service.mongoService = mongoService
}

protected void tearDown() {
super.tearDown()
}
protected void tearDown() {
super.tearDown()

void testEncodePassword() {
User.mongoCollection.drop()
}

void testEncodePassword() {
def pwd = service.encodePassword('test-pass')
assert pwd
assert pwd == service.encodePassword('test-pass')
}
}

void testUncap() {
assert 'camelCase' == service.uncap('CamelCase')
}

void testAuthenticate() {
def pass = service.encodePassword('test-pass')

assert null == service.authenticate('test@example.com', 'test-pass')

User.mongoCollection.add(email: 'text@example.com', password: pass, enabled: true)
assert 1 == User.mongoCollection.count()

def u = service.authenticate('text@example.com', 'test-pass')
assert u
assert u instanceof User
}

void testRequiredRoles() {
service.grailsApplication = [
controllerClasses: [[name: 'Test', clazz: TestController]]
]
service.initialize()

assert ['USER'] == service.getRequiredRoles('/test') as List
assert ['EDITOR'] == service.getRequiredRoles('/test/method') as List
assert ['ADMIN'] == service.getRequiredRoles('/test/secure') as List
assert ['USER'] == service.getRequiredRoles('/test/foo') as List
assert ['OWNER_100'] == service.getRequiredRoles('/test/owner/100')
assert null == service.getRequiredRoles('/foo')
}
}

@Secured('USER')
class TestController {

def show = { }

@Secured('ADMIN')
def secure = { }

@Secured('EDITOR')
def method() { }

@Secured('OWNER_')
def owner = { }
}

0 comments on commit d720b08

Please sign in to comment.