Code

Opened 7 years ago

Closed 6 years ago

#5836 closed (fixed)

Test client does not clear exception info after re-raising an exception that was raised by a view

Reported by: Chris Wagner <cw264701@…> Owned by: nobody
Component: Testing framework Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Consider this test case:

    def test_user_can_only_remove_his_own_notifications(self):

        notification_id = ...  # set notification_id based on some knowledge of what exists in the DB

        self.login_as_user(1)  # this is a custom method defined in a base test class
        try:
            self.client.post("/notifications/remove/%d" % notification_id)
            self.fail("User should not be able to remove notifications " +
              "that do not belong to him")
        except MaliciousOperation:
            pass

        # make sure the notification still exists...
        self.login_as_user(2)
        response = self.client.get("/")
        self.assertEqual(notification_id, response.context[0]['notifications'][0].id)

This causes the exception to be re-raised, by the test client, upon calling self.client.get(), as the exception info was never cleared after/during the call to self.client.post().

This patch seems to fix it:

--- test/client.py      (revision 6626)
+++ test/client.py      (working copy)
@@ -181,7 +181,9 @@
 
         # Look for a signalled exception and reraise it
         if self.exc_info:
-            raise self.exc_info[1], None, self.exc_info[2]
+            exc_info = self.exc_info
+            self.exc_info = None
+            raise exc_info[1], None, exc_info[2]
 
         # Save the client and request that stimulated the response
         response.client = self

Attachments (0)

Change History (4)

comment:1 Changed 7 years ago by marcink

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

I just ran into this problem and wanted to upload exactly the same patch.

comment:2 Changed 6 years ago by anonymous

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 6 years ago by jacob

that was me; sorry.

comment:4 Changed 6 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(In [7583]) Fixed #5836 -- Corrected the logic in the Test Client when an exception raised by a view is caught and re-raised. Thanks for the report, test case, and fix, Chris Wagner.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.