Grigoriy Petukhov avatar Grigoriy Petukhov committed 773ce15

Refactor tests

Comments (0)

Files changed (1)

 
 class UrlauthTestCase(TestCase):
     urls = 'urlauth.tests'
+    test_url = '/urlauth_test_view/'
+    
+    # Helpers
+    def setUp(self):
+        """
+        Create active and inactive users.
+        """
 
-    def setUp(self):
         User.objects.all().delete()
-        u = User(username='user', email='user@host.com')
-        u.set_password('pass')
-        u.is_active = True
-        u.save()
-        self.user = u
+        self.user = User.objects.create_user('user', 'user@host.com', 'pass')
+        self.ban_user = User.objects.create_user('ban_user', 'ban_user@host.com',
+                                                 'pass')
+        self.ban_user.is_active = False
+        self.ban_user.save()
 
-        u = User(username='ban_user', email='ban_user@host.com')
-        u.set_password('pass')
-        u.is_active = False
-        u.save()
-        self.ban_user = u
+    def process_url(self, url, **kwargs):
+        """
+        Add authkey to the ``url`` and then open that url with django test client.
 
+        Returns:
+            The ID of created ``AuthKey`` instance
+        """
 
-class AuthCodeTestCase(UrlauthTestCase):
+        url = wrap_url(url, **kwargs)
+        path, args = url.split('?')[0], cgi.parse_qs(url.split('?')[1])
+        self.client.get(url, args)
+        hash = args[settings.URLAUTH_AUTHKEY_NAME][0]
+        return hash
+
+    # Test cases
+
     def test_create_key(self):
-        # should not fail
-        AuthKey.objects.create_key(uid=self.user.pk, expired=datetime.now())
-
+        self.assertTrue(AuthKey.objects.create_key(uid=self.user.pk,
+                                                   expired=datetime.now()))
 
     def test_wrap_url(self):
         expired = datetime.now()
 
+        AuthKey.objects.all().delete()
         clean_url = 'http://ya.ru'
         url = wrap_url(clean_url, uid=self.user.pk, expired=expired)
-        key = AuthKey.objects.order_by('-created')[0].id
-        self.assertEqual(url, '%s?%s=%s' % (clean_url, settings.URLAUTH_AUTHKEY_NAME, key))
+        key = AuthKey.objects.get()
+        self.assertEqual(url, '%s?%s=%s' % (clean_url,
+                                            settings.URLAUTH_AUTHKEY_NAME, key.id))
 
         AuthKey.objects.all().delete()
-
         clean_url = 'http://ya.ru?foo=bar'
         url = wrap_url(clean_url, uid=self.user.pk, expired=expired)
-        key = AuthKey.objects.order_by('-created')[0].id
-        self.assertEqual(url, '%s&%s=%s' % (clean_url, settings.URLAUTH_AUTHKEY_NAME, key))
-
+        key = AuthKey.objects.get()
+        self.assertEqual(url, '%s&%s=%s' % (clean_url,
+                                            settings.URLAUTH_AUTHKEY_NAME, key.id))
 
     def test_validate_key(self):
         expired = datetime.now() - timedelta(seconds=1)
         key = AuthKey.objects.create_key(uid=self.user.pk, expired=expired)
         self.assertTrue(load_key(key))
 
-
-class AuthKeyMiddlewareTestCase(UrlauthTestCase):
-    test_url = '/urlauth_test_view/'
-
-    def process_url(self, url, **kwargs):
-       url = wrap_url(url, **kwargs)
-       return url.split('?')[0], cgi.parse_qs(url.split('?')[1])
-
-    def test_activation(self): 
+    def test_authentication(self): 
         expired = datetime.now() + timedelta(days=1)
         resp = self.client.get(self.test_url)
 
 
         ## Simple authorization
         self.client.logout()
-        url, args = self.process_url(self.test_url, uid=self.user.pk, expired=expired)
-        resp = self.client.get(url, args)
+        self.process_url(self.test_url, uid=self.user.pk, expired=expired)
         self.assertEqual(self.client.session['_auth_user_id'], self.user.pk)
 
         ## Baned user can't authorize
         self.client.logout()
-        url, args = self.process_url(self.test_url, uid=self.ban_user.pk, expired=expired)
-        resp = self.client.get(url, args)
+        self.process_url(self.test_url, uid=self.ban_user.pk, expired=expired)
         self.assertFalse('_auth_user_id' in self.client.session)
 
         ## Expired auth key does not work
         self.client.logout()
         expired = datetime.now() - timedelta(seconds=1)
-        url, args = self.process_url(url, uid=self.user.pk, expired=expired)
-        resp = self.client.get(url, args)
+        self.process_url(self.test_url, uid=self.user.pk, expired=expired)
         self.assertFalse('_auth_user_id' in self.client.session)
 
-
     def test_signals(self):
         logs = []
 
         self.assertEqual(0, len(logs))
         self.client.logout()
         expired = datetime.now() + timedelta(days=1)
-        url, args = self.process_url(self.test_url, uid=self.user.pk,
-                                     expired=expired)
-        resp = self.client.get(url, args)
+        self.process_url(self.test_url, uid=self.user.pk, expired=expired)
         self.assertEqual(2, len(logs))
 
     def test_onetime_feature(self):
         self.client.logout()
         expired = datetime.now() + timedelta(days=1)
-        url, args = self.process_url(self.test_url, uid=self.user.pk,
-                                     expired=expired)
-        hash = args['authkey'][0]
-        self.assertTrue(AuthKey.objects.filter(id=hash).count())
-        resp = self.client.get(url, args)
+        hash = self.process_url(self.test_url, uid=self.user.pk,
+                                expired=expired)
         self.assertFalse(AuthKey.objects.filter(id=hash).count())
 
-
         self.client.logout()
         expired = datetime.now() + timedelta(days=1)
-        url, args = self.process_url(self.test_url, uid=self.user.pk,
-                                     expired=expired, onetime=False)
-        hash = args['authkey'][0]
+        hash = self.process_url(self.test_url, uid=self.user.pk,
+                                expired=expired, onetime=False)
         self.assertTrue(AuthKey.objects.filter(id=hash).count())
-        resp = self.client.get(url, args)
-        self.assertTrue(AuthKey.objects.filter(id=hash).count())
-
-
 
 
 def test_view(request):
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.