Merge lp:~canonical-isd-hackers/canonical-identity-provider/fix-captcha-tests-on-py2.7 into lp:canonical-identity-provider/release

Proposed by Łukasz Czyżykowski
Status: Merged
Approved by: Ricardo Kirkner
Approved revision: no longer in the source branch.
Merged at revision: 166
Proposed branch: lp:~canonical-isd-hackers/canonical-identity-provider/fix-captcha-tests-on-py2.7
Merge into: lp:canonical-identity-provider/release
Diff against target: 109 lines (+32/-33)
1 file modified
identityprovider/tests/test_captcha.py (+32/-33)
To merge this branch: bzr merge lp:~canonical-isd-hackers/canonical-identity-provider/fix-captcha-tests-on-py2.7
Reviewer Review Type Date Requested Status
Ricardo Kirkner (community) Approve
Review via email: mp+65518@code.launchpad.net

Commit message

Fix for captcha tests in python 2.7.

Description of the change

Overview
========
This branch fixes test case for captcha which was failing on python 2.7.

Details
=======
Because of changes in the deepcopy in the python 2.7, the way in which the settings were saved for each test is no longer works. Because of that there's new approach, based on context manager (I could swear that there was something similar somewhere in our codebase) was created.

Testing
=======
To test run:

$ fab bootstrap setup_postgresql_server test

Making sure that you're using python 2.7.

To post a comment you must log in.
Revision history for this message
Ricardo Kirkner (ricardokirkner) wrote :

The context manager patch_settings is provided with isd-test-utils, you *could* depend on it, but since it's just the context manager you need, and that the namespace of that package will change (soon?), it's ok to duplicate this code now (as you would be depending on yet another library just for it).

Lines 84-86 need some attention though:
l. 84, I think this could go before the patch_settings call
l. 85-86 these are not really needed

Thanks for making the bootstrap work on python2.7 :)

review: Needs Fixing
Revision history for this message
Łukasz Czyżykowski (lukasz-czyzykowski) wrote :

Updated the code.

Revision history for this message
Ricardo Kirkner (ricardokirkner) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'identityprovider/tests/test_captcha.py'
2--- identityprovider/tests/test_captcha.py 2011-01-20 21:48:43 +0000
3+++ identityprovider/tests/test_captcha.py 2011-06-23 08:26:06 +0000
4@@ -1,62 +1,62 @@
5 # Copyright 2010 Canonical Ltd. This software is licensed under the
6 # GNU Affero General Public License version 3 (see the file LICENSE).
7-
8 from urllib2 import HTTPError, URLError
9-from copy import deepcopy
10 from unittest import TestCase
11-
12 from django.conf import settings
13+from mock import Mock, patch
14
15 from identityprovider.models.captcha import Captcha
16
17-from mock import Mock, patch
18+
19+class patch_settings(object):
20+
21+ def __init__(self, **kwargs):
22+ self.marker = object()
23+ self.old_settings = {}
24+ self.kwargs = kwargs
25+
26+ def __enter__(self):
27+ for setting, new_value in self.kwargs.items():
28+ self.old_settings[setting] = getattr(settings, setting, self.marker)
29+ setattr(settings, setting, new_value)
30+
31+ def __exit__(self, exc_type, exc_val, exc_tb):
32+ for setting, old_value in self.old_settings.items():
33+ if old_value is self.marker:
34+ delattr(settings, setting)
35+ else:
36+ setattr(settings, setting, old_value)
37
38
39 class CaptchaTestCase(TestCase):
40
41- def setUp(self):
42- # save Django settings, to be restored later (that's poking into Django
43- # internals)
44- self._settings = settings._wrapped
45- settings._wrapped = deepcopy(self._settings)
46-
47- def tearDown(self):
48- settings._wrapped = self._settings
49-
50 def test_opener_is_created_during_init(self):
51 Captcha.opener = None
52
53- captcha = Captcha('test')
54+ Captcha('test')
55
56 self.assertTrue(Captcha.opener is not None)
57
58-
59 @patch('urllib.urlencode')
60- #@patch('django.conf.settings.DISABLE_CAPTCHA_VERIFICATION', False)
61 def test_non_ascii_captach_solution(self, mock_urlencode):
62- disable_captcha_verification = settings.DISABLE_CAPTCHA_VERIFICATION
63- settings.DISABLE_CAPTCHA_VERIFICATION = False
64- try:
65- captcha_solution = u'\xa3'
66- encoded_solution = captcha_solution.encode('utf-8')
67- mock_urlencode.return_value = 'some text'
68+ captcha_solution = u'\xa3'
69+ encoded_solution = captcha_solution.encode('utf-8')
70+ mock_urlencode.return_value = 'some text'
71
72+ with patch_settings(DISABLE_CAPTCHA_VERIFICATION=False):
73 captcha = Captcha(None)
74 captcha._open = Mock()
75 captcha._open.return_value.is_error = True
76 captcha.verify(captcha_solution, 'foobar')
77
78- captcha_response = mock_urlencode.call_args[0][0]['response']
79- self.assertEqual(captcha_response, encoded_solution)
80- finally:
81- settings.DISABLE_CAPTCHA_VERIFICATION = disable_captcha_verification
82+ captcha_response = mock_urlencode.call_args[0][0]['response']
83+ self.assertEqual(captcha_response, encoded_solution)
84
85 def test_opener_is_created_when_proxy_is_required(self):
86 Captcha.opener = None
87- settings.CAPTCHA_USE_PROXY = True
88- settings.CAPTCHA_PROXIES = {}
89
90- captcha = Captcha('test')
91+ with patch_settings(CAPTCHA_USE_PROXY=True, CAPTCHA_PROXIES={}):
92+ Captcha('test')
93
94 self.assertTrue(Captcha.opener is not None)
95
96@@ -77,10 +77,9 @@
97 self.assertTrue(response.is_error)
98
99 def test_verify_is_short_circuited_when_disabled_in_settings(self):
100- settings.DISABLE_CAPTCHA_VERIFICATION = True
101-
102- captcha = Captcha('test')
103- verify_result = captcha.verify('solution', '127.0.0.1')
104+ with patch_settings(DISABLE_CAPTCHA_VERIFICATION=True):
105+ captcha = Captcha('test')
106+ verify_result = captcha.verify('solution', '127.0.0.1')
107
108 self.assertTrue(verify_result)
109 self.assertTrue(captcha.response is None)