зеркало из https://github.com/mozilla/FlightDeck.git
authentication with tests
This commit is contained in:
Родитель
5d2f57dac5
Коммит
bf53877c77
|
@ -1,6 +1,8 @@
|
|||
from django.contrib.auth.models import User
|
||||
from person.models import Profile
|
||||
|
||||
DEFAULT_AMO_PASSWORD = 'saved in AMO'
|
||||
|
||||
class AMOAuthentication:
|
||||
|
||||
def authenticate(self, username, password):
|
||||
|
@ -11,11 +13,17 @@ class AMOAuthentication:
|
|||
# Try to retrieve AMO session info from db
|
||||
try:
|
||||
user = User.objects.get(username=username)
|
||||
if user.password != DEFAULT_AMO_PASSWORD:
|
||||
" standard authorisation "
|
||||
if user.check_password(password):
|
||||
return user
|
||||
return None
|
||||
amo_session = user.get_profile().amo_session
|
||||
except User.DoesNotExist:
|
||||
user = None
|
||||
amo_session = None
|
||||
if user:
|
||||
amo_session = user.get_profile().amo_session
|
||||
amo_session = None
|
||||
|
||||
|
||||
|
||||
# TODO: here contact AMO and receive authentication status
|
||||
authenticated = False
|
||||
|
@ -26,25 +34,28 @@ class AMOAuthentication:
|
|||
amo_session = "fake"
|
||||
|
||||
if not authenticated:
|
||||
return False
|
||||
return None
|
||||
|
||||
# save user into the database
|
||||
if not user:
|
||||
user = User(
|
||||
username=username,
|
||||
password='saved in AMO',
|
||||
password=DEFAULT_AMO_PASSWORD,
|
||||
# TODO: retrieve from AMO
|
||||
first_name="John",
|
||||
last_name="Doe",
|
||||
email='fake@email.com'
|
||||
)
|
||||
user.save()
|
||||
|
||||
# save current amo_session if different
|
||||
try:
|
||||
profile = user.get_profile()
|
||||
except Profile.DoesNotExist:
|
||||
profile = Profile()
|
||||
|
||||
profile.amo_session = amo_session
|
||||
profile.save()
|
||||
profile = Profile(user=user)
|
||||
|
||||
if amo_session != profile.amo_session:
|
||||
profile.amo_session = amo_session
|
||||
profile.save()
|
||||
|
||||
return user
|
||||
|
|
|
@ -1,23 +1,30 @@
|
|||
"""
|
||||
This file demonstrates two different styles of tests (one doctest and one
|
||||
unittest). These will both pass when you run "manage.py test".
|
||||
|
||||
Replace these with more appropriate tests for your application.
|
||||
"""
|
||||
|
||||
from django.test import TestCase
|
||||
from django.test.client import Client
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth import authenticate
|
||||
|
||||
class SimpleTest(TestCase):
|
||||
def test_basic_addition(self):
|
||||
"""
|
||||
Tests that 1 + 1 always equals 2.
|
||||
"""
|
||||
self.failUnlessEqual(1 + 1, 2)
|
||||
from person.models import Profile
|
||||
from amo.authentication import DEFAULT_AMO_PASSWORD
|
||||
|
||||
__test__ = {"doctest": """
|
||||
Another way to test that 1 + 1 is equal to 2.
|
||||
class AuthTest(TestCase):
|
||||
def test_fake_authentication(self):
|
||||
"""
|
||||
test that any user except with username="fake" is authenticated
|
||||
"""
|
||||
# system should create and authenticate nonexisting user
|
||||
user = authenticate(username="username", password="password")
|
||||
self.failUnless(user)
|
||||
|
||||
>>> 1 + 1 == 2
|
||||
True
|
||||
"""}
|
||||
user = User.objects.get(username="username")
|
||||
self.assertEqual(user.password, DEFAULT_AMO_PASSWORD)
|
||||
self.failUnless(user.get_profile())
|
||||
self.assertEqual(user.get_profile().amo_session, 'fake')
|
||||
|
||||
|
||||
def test_fake_authentication_fail(self):
|
||||
"""
|
||||
authentication should fail if username="fake"
|
||||
"""
|
||||
user = authenticate(username="fake", password="password")
|
||||
self.assertEqual(user, None)
|
||||
|
||||
|
|
|
@ -3,3 +3,4 @@ from django.contrib.auth.models import User
|
|||
|
||||
class Profile(models.Model):
|
||||
amo_session = models.CharField(max_length=255, blank=True, null=True)
|
||||
user = models.ForeignKey(User, unique=True)
|
||||
|
|
Загрузка…
Ссылка в новой задаче