@@ -237,6 +237,23 @@ def setUp(self):
237237 self .client .opendb = lambda a : self .fail (
238238 "Logged in, but we shouldn't be." )
239239
240+ def assertRaisesMessage (self , exception , callable , message , * args ,
241+ ** kwargs ):
242+ """An extension of assertRaises, which also checks the exception
243+ message. We need this because we rely on exception messages when
244+ redirecting.
245+ """
246+ try :
247+ callable (* args , ** kwargs )
248+ except exception , msg :
249+ self .assertEqual (str (msg ), message )
250+ else :
251+ if hasattr (exception , '__name__' ):
252+ excName = exception .__name__
253+ else :
254+ excName = str (exception )
255+ raise self .failureException , excName
256+
240257 def assertLoginLeavesMessages (self , messages , username = None , password = None ):
241258 if username is not None :
242259 self .form .value .append (MiniFieldStorage ('__login_name' , username ))
@@ -247,6 +264,18 @@ def assertLoginLeavesMessages(self, messages, username=None, password=None):
247264 LoginAction (self .client ).handle ()
248265 self .assertEqual (self .client ._error_message , messages )
249266
267+ def assertLoginRaisesRedirect (self , message , username = None , password = None , came_from = None ):
268+ if username is not None :
269+ self .form .value .append (MiniFieldStorage ('__login_name' , username ))
270+ if password is not None :
271+ self .form .value .append (
272+ MiniFieldStorage ('__login_password' , password ))
273+ if came_from is not None :
274+ self .form .value .append (
275+ MiniFieldStorage ('__came_from' , came_from ))
276+
277+ self .assertRaisesMessage (Redirect , LoginAction (self .client ).handle , message )
278+
250279 def testNoUsername (self ):
251280 self .assertLoginLeavesMessages (['Username required' ])
252281
@@ -272,6 +301,58 @@ def opendb(username):
272301
273302 self .assertLoginLeavesMessages ([], 'foo' , 'right' )
274303
304+ def testCorrectLoginRedirect (self ):
305+ self .client .db .security .hasPermission = lambda * args , ** kwargs : True
306+ def opendb (username ):
307+ self .assertEqual (username , 'foo' )
308+ self .client .opendb = opendb
309+
310+ # basic test with query
311+ self .assertLoginRaisesRedirect ("http://whoami.com/path/issue?%40action=search" ,
312+ 'foo' , 'right' , "http://whoami.com/path/issue?@action=search" )
313+
314+ # test that old messages are removed
315+ self .form .value [:] = [] # clear out last test's setup values
316+ self .assertLoginRaisesRedirect ("http://whoami.com/path/issue?%40action=search" ,
317+ 'foo' , 'right' , "http://whoami.com/path/issue?@action=search&@ok_messagehurrah+we+win&@error_message=blam" )
318+
319+ # test when there is no query
320+ self .form .value [:] = [] # clear out last test's setup values
321+ self .assertLoginRaisesRedirect ("http://whoami.com/path/issue255" ,
322+ 'foo' , 'right' , "http://whoami.com/path/issue255" )
323+
324+ # test if we are logged out; should kill the @action=logout
325+ self .form .value [:] = [] # clear out last test's setup values
326+ self .assertLoginRaisesRedirect ("http://localhost:9017/demo/issue39?%40startwith=0&%40pagesize=50" ,
327+ 'foo' , 'right' , "http://localhost:9017/demo/issue39?@action=logout&@pagesize=50&@startwith=0" )
328+
329+ def testInvalidLoginRedirect (self ):
330+ self .client .db .security .hasPermission = lambda * args , ** kwargs : True
331+
332+ def opendb (username ):
333+ self .assertEqual (username , 'foo' )
334+ self .client .opendb = opendb
335+
336+ # basic test with query
337+ self .assertLoginRaisesRedirect ("http://whoami.com/path/issue?%40error_message=Invalid+login&%40action=search" ,
338+ 'foo' , 'wrong' , "http://whoami.com/path/issue?@action=search" )
339+
340+ # test that old messages are removed
341+ self .form .value [:] = [] # clear out last test's setup values
342+ self .assertLoginRaisesRedirect ("http://whoami.com/path/issue?%40error_message=Invalid+login&%40action=search" ,
343+ 'foo' , 'wrong' , "http://whoami.com/path/issue?@action=search&@ok_messagehurrah+we+win&@error_message=blam" )
344+
345+ # test when there is no __came_from specified
346+ self .form .value [:] = [] # clear out last test's setup values
347+ # I am not sure why this produces three copies of the same error.
348+ # only one copy of the error is displayed to the user in the web interface.
349+ self .assertLoginLeavesMessages (['Invalid login' , 'Invalid login' , 'Invalid login' ], 'foo' , 'wrong' )
350+
351+ # test when there is no query
352+ self .form .value [:] = [] # clear out last test's setup values
353+ self .assertLoginRaisesRedirect ("http://whoami.com/path/issue255?%40error_message=Invalid+login" ,
354+ 'foo' , 'wrong' , "http://whoami.com/path/issue255" )
355+
275356class EditItemActionTestCase (ActionTestCase ):
276357 def setUp (self ):
277358 ActionTestCase .setUp (self )
0 commit comments