Commits

Sergey Maranchuk  committed 2f8edfd

change show_user view, change LOGIN_REDIRECT_URL, change LoginTest

  • Participants
  • Parent commits 3bd0dd2

Comments (0)

Files changed (4)

File openauth/settings.py

 ACTIVATION_CONFIRMATION_TYPE = get_config("ACTIVATION_CONFIRMATION_TYPE", 1)
 PASSWORD_RESET_CONFIRMATION_TYPE = get_config("PASSWORD_RESET_CONFIRMATION_TYPE", 2)
 
-LOGIN_REDIRECT_URL = get_config("LOGIN_REDIRECT_URL", "/auth/login/")
+LOGIN_URL = get_config("LOGIN_URL", "/auth/login/")
+LOGIN_REDIRECT_URL = get_config("LOGIN_REDIRECT_URL", "/auth/user/")
 AUTHENTICATION_TYPE = get_config("AUTHENTICATION_TYPE", ["username"])
 
 USERNAME_REGEX = get_config("USERNAME_REGEX", re.compile(r"[a-z0-9][_a-z0-9]*[a-z0-9]$", re.I))

File openauth/tests.py

-import unittest
-
 from django.core.urlresolvers import reverse
 from django.contrib.auth.models import User
-from django.test.client import Client
+from django.test import Client, TestCase
 
 import openauth
 from openauth.models import Profile
 from openauth import views
+from openauth import forms
 
 
-class RegistrationTest(unittest.TestCase):
+class RegistrationTest(TestCase):
 
     def setUp(self):
         openauth.settings.REGISTRATION_ALLOWED = True
-        openauth.settings.ACTIVATION_REQUIRED = True
         self.client = Client()
         self.registration_url = reverse('openauth-registration')
-        self.active_user = User.objects.create_user('ivan', 'ivan@example.com', password='test')
-        self.inactive_user = User.objects.create_user('vova', 'vova@example.com', password='test')
-        self.inactive_user.is_active = False
-        self.inactive_user.save()
-
-    def tearDown(self):
-        self.active_user.delete()
-        self.inactive_user.delete()
 
     def test_registration(self):
         response = self.client.post(self.registration_url, {'username': 'petya', 'email': 'admin@example.com', 'password': 'test', 'password_dup': 'test'}) 
         response = self.client.post(self.registration_url, {'username': 'petya', 'email': 'admin@example.com', 'password': 'test', 'password_dup': 'test'}) 
         self.failUnlessRaises(User.DoesNotExist, User.objects.get, username='petya')
 
-    def test_login(self):
-        #active user login
-        response = self.client.login(username=self.active_user.username, password='test')
-        self.assertTrue(response)
 
-        #active user, incorrect password
-        response = self.client.login(username=self.active_user.username, password='INCORRECT')
-        self.assertFalse(response)
+class LoginTest(TestCase):
 
-        #inactive user, activation enabled
-        response = self.client.login(username=self.inactive_user.username, password='test')
-        self.assertFalse(response)
+    def setUp(self):
+        openauth.settings.REGISTRATION_ALLOWED = True
+        openauth.settings.ACTIVATION_REQUIRED = True
+        self.client = Client()
+        self.login_url = reverse('openauth-login')
+        self.active_user = User.objects.create_user('ivan', 'ivan@example.com', password='test')
+        self.inactive_user = User.objects.create_user('vova', 'vova@example.com', password='test')
+        self.inactive_user.is_active = False
+        self.inactive_user.save()
 
-        #inactive user, activation disabled
+    def tearDown(self):
+        self.active_user.delete()
+        self.inactive_user.delete()
+
+    def test_valid_active_user_activation_enabled(self):
+        response = self.client.post(self.login_url, {'username': self.active_user.username, 'password': 'test'})
+        self.assertRedirects(response, openauth.settings.LOGIN_REDIRECT_URL)
+
+    def test_invalid_active_user_activation_enabled(self):
+        response = self.client.post(self.login_url, {'username': self.active_user.username, 'password': 'INCORRECT'})
+        self.assertEqual(response.status_code, 200) #not redirect to LOGIN_REDIRECT_URL
+
+    def test_inactive_user_activation_enabled(self):
+        response = self.client.post(self.login_url, {'username': self.inactive_user.username, 'password': 'test'})
+        self.assertEqual(response.status_code, 200)
+
+    def test_inactive_user_activation_disabled(self):
         openauth.settings.ACTIVATION_REQUIRED = False
-        response = self.client.login(username=self.inactive_user.username, password='test')
-        self.assertTrue(response)
+        response = self.client.post(self.login_url, {'username': self.inactive_user.username, 'password': 'test'})
+        self.assertRedirects(response, openauth.settings.LOGIN_REDIRECT_URL)

File openauth/urls.py

     url(r'^reset_password/$', views.reset_password, name='openauth-reset-password'),
     url(r'^change_password/$', views.change_password, name='openauth-change-password'),
     url(r'^confirm/$', views.confirm, name='openauth-confirm'),
+    url(r'^user/$', views.show_user, name='openauth-show-user'),
     url(r'^user/(\d+)/$', views.show_user, name='openauth-show-user'),
     url(r'^users/$', views.users, name='openauth-show-users'),
     #url(r'^edit/(\d+)/$', views.edit_user, name='edit-user'),

File openauth/views.py

 
 
 @render_to('openauth/profile.html')
-def show_user(request, user_id):
+def show_user(request, user_id=None):
     """
     Show user profile 
     """
-
+    user_id = user_id or request.user.id
     user = get_object_or_404(User, pk=user_id)
     return {'user': user}