My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
class LoginControllerTests extends GroovyTestCase {

def session
def params
def redirectParams
def flash

/** Setup metaclass fixtures for mocking. */
void setUp() {

session = [ : ]
LoginController.metaClass.getSession = { -> session }

params = [ : ]
LoginController.metaClass.getParams = { -> params }

redirectParams = [ : ]
LoginController.metaClass.redirect = { Map args -> redirectParams = args }

flash = [ : ]
LoginController.metaClass.getFlash = { -> flash }

def logger = new Expando( debug: { println it }, info: { println it },
warn: { println it }, error: { println it } )
LoginController.metaClass.getLog = { -> logger }

}

/** Remove metaclass fixtures for mocking. */
void tearDown() {
def remove = GroovySystem.metaClassRegistry.&removeMetaClass
remove LoginController
remove LoginCommand
remove Account
remove String

}


void testIndexRedirect() {

LoginController lc = new LoginController()
lc.index()
assertEquals "form" , redirectParams.action


}



void testLogin() {

String.metaClass.encodeAsSha1 = { -> delegate }

params['blog'] = 'demo'

Account.metaClass.static.findByUserIdAndPassword = { String userId, String password ->
if (userId == 'glen' && password == 'sesame' ) {
return [ userId: 'glen' ]
} else {
return null
}

}

LoginCommand.metaClass.hasErrors = { false }

LoginCommand goodUser = new LoginCommand(userId: 'glen', password: 'sesame')

LoginController lc = new LoginController()
lc.login(goodUser)

assertEquals "glen", session.account.userId
assertEquals "/demo/", redirectParams.uri
assertNull flash.loginError

LoginCommand badUser = new LoginCommand(userId: 'glen', password: 'unlucky')
lc.login(badUser)

assertNull session.account
assertNotNull flash.loginError

}



void testLogout() {

session['account'] = "user"
params['blog'] = 'demo'

LoginController lc = new LoginController()
assertNotNull lc.session.account

lc.logout()

assertEquals "/demo/", redirectParams.uri

assertNull lc.session.account


}

void testForm() {

def controller = new LoginController()

controller.form()

// not sure how to test this yet...
// assertEquals "", renderParams.view
}




}
Show details Hide details

Change log

r117 by glen.smith on Mar 11, 2008   Diff
Tidied up things in setup() to promote
reuse.
Go to: 
Project members, sign in to write a code review

Older revisions

r114 by glen.smith on Mar 11, 2008   Diff
Basic controller test script now
working.
r109 by glen.smith on Mar 03, 2008   Diff
Started work on Controller tests.
Removed dead tests.
All revisions of this file

File info

Size: 2786 bytes, 117 lines
Hosted by Google Code