Mikhail Korobov avatar Mikhail Korobov committed 911298d

renew_app method for resetting the 'browser' inside tests

Comments (0)

Files changed (3)

django_webtest/__init__.py

         backend_name = 'django_webtest.backends.WebtestUserBackend'
         settings.AUTHENTICATION_BACKENDS.insert(0, backend_name)
 
+    def renew_app(self):
+        """
+        Resets self.app (drops the stored state): cookies, etc.
+        Note: this renews only self.app, not the responses fetched by self.app.
+        """
+        self.app = DjangoTestApp(extra_environ=self.extra_environ)
+
     def __call__(self, result=None):
         self._patch_settings()
-        self.app = DjangoTestApp(extra_environ=self.extra_environ)
+        self.renew_app()
         res = super(WebTest, self).__call__(result)
         self._unpatch_settings()
         return res

django_webtest_tests/templates/form.html

         {{ form.as_p }}
         <input type='submit'>
     </form>
+    <a href='{% url auth_login %}'>Login</a>
 </body>

django_webtest_tests/testapp_tests/tests.py

         self.assertEqual(response.context['spam'], None)
 
 
-class AuthTest(WebTest):
+class BaseAuthTest(WebTest):
+
     def setUp(self):
         self.user = User.objects.create_user('foo', 'example@example.com', '123')
 
+    def _login(self, username, password):
+        form = self.app.get(reverse('auth_login')).form
+        form['username'] = username
+        form['password'] = password
+        return form.submit()
+
+
+class AuthTest(BaseAuthTest):
+
     def test_not_logged_in(self):
         response = self.app.get('/template/index.html')
         user = response.context['user']
         )
 
     def test_standard_auth(self):
-        form = self.app.get(reverse('auth_login')).form
-        form['username'] = self.user.username
-        form['password'] = '123'
-        resp = form.submit().follow()
+        resp = self._login(self.user.username, '123').follow()
         user = resp.context['user']
         self.assertEqual(user, self.user)
 
 
+class RenewAppTest(BaseAuthTest):
+
+    def test_renew_app(self):
+        self._login(self.user.username, '123').follow()
+
+        # auth cookie is preserved between self.app.get calls
+        page1 = self.app.get('/template/form.html')
+        self.assertEqual(page1.context['user'], self.user)
+
+        self.renew_app()
+
+        # cookies were dropped
+        page2 = self.app.get('/template/form.html')
+        self.assertTrue(page2.context['user'].is_anonymous())
+
+        # but cookies are still there while browsing from stored page
+        page1_1 = page1.click('Login')
+        self.assertEqual(page1_1.context['user'], self.user)
+
+
+
 class DjangoAssertsTest(WebTest):
 
     def test_assert_template_used(self):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.