Permalink
Browse files

Make code prettier.

  • Loading branch information...
neaf committed Feb 9, 2012
1 parent 27f5844 commit 10ae402331352d7dc0d5a2408acb7eed120603c8
Showing with 41 additions and 41 deletions.
  1. +24 −25 detector/accounts/models.py
  2. +12 −11 detector/accounts/tests/models.py
  3. +4 −4 detector/accounts/tests/views.py
  4. +1 −1 detector/accounts/views.py
@@ -4,39 +4,38 @@
import requests
from requests.exceptions import RequestException
-class User(models.Model):
- token = models.CharField(max_length=50, default=None, unique=True)
- github_username = models.CharField(max_length=50, default=None)
- avatar_url = models.CharField(max_length=255)
-
- @classmethod
+class UserManager(models.Manager):
def create_from_github(self, username, password):
- data = GithubAuthenticator.authenticate(username, password)
+ data = authenticate_github(username, password)
if not data:
return False
user = User()
user.github_username = data['login']
user.avatar_url = data['avatar_url']
- user.token = User.generate_token()
+ user.token = generate_token()
user.save()
return user
- @staticmethod
- def generate_token():
- return str(uuid4())
+class User(models.Model):
+ token = models.CharField(max_length=50, default=None, unique=True)
+ github_username = models.CharField(max_length=50, default=None)
+ avatar_url = models.CharField(max_length=255)
-class GithubAuthenticator():
- @staticmethod
- def authenticate(username, password):
- try:
- response = requests.get('https://api.github.com/user', auth=(username, password))
- if response.status == 200:
- data = json.loads(response.text)
- return {
- 'login': data['login'],
- 'avatar_url': data['avatar_url']
- }
- else:
- return False
- except RequestException:
+ objects = UserManager()
+
+def authenticate_github(username, password):
+ try:
+ response = requests.get('https://api.github.com/user', auth=(username, password))
+ if response.status == 200:
+ data = json.loads(response.text)
+ return {
+ 'login': data['login'],
+ 'avatar_url': data['avatar_url']
+ }
+ else:
return False
+ except RequestException:
+ return False
+
+def generate_token():
+ return str(uuid4())
@@ -10,7 +10,8 @@
from django.test import TestCase
import mock
from requests.exceptions import RequestException
-from detector.accounts.models import User, GithubAuthenticator
+from accounts.models import generate_token, authenticate_github
+from detector.accounts.models import User
UUID_LENGTH = 36
USERNAME = 'octocat'
@@ -52,15 +53,15 @@
GITHUB_VALID_RESPONSE_DATA = json.loads(GITHUB_VALID_RESPONSE)
class UserTest(TestCase):
- @mock.patch('detector.accounts.models.GithubAuthenticator.authenticate')
- @mock.patch('detector.accounts.models.User.generate_token')
+ @mock.patch('detector.accounts.models.authenticate_github')
+ @mock.patch('detector.accounts.models.generate_token')
def test_create_from_github_valid_data(self, token_mock, auth_mock):
auth_mock.return_value = {
'login': GITHUB_VALID_RESPONSE_DATA['login'],
'avatar_url': GITHUB_VALID_RESPONSE_DATA['avatar_url'],
}
token_mock.return_value = uuid4()
- result = User.create_from_github(USERNAME, PASSWORD)
+ result = User.objects.create_from_github(USERNAME, PASSWORD)
self.assertIsInstance(result, User)
self.assertEqual(result.github_username, GITHUB_VALID_RESPONSE_DATA['login'])
self.assertEqual(result.avatar_url, GITHUB_VALID_RESPONSE_DATA['avatar_url'])
@@ -69,16 +70,16 @@ def test_create_from_github_valid_data(self, token_mock, auth_mock):
self.assertIsNotNone(result.pk)
- @mock.patch('detector.accounts.models.GithubAuthenticator.authenticate')
+ @mock.patch('detector.accounts.models.authenticate_github')
def test_create_from_github_invalid_data(self, m):
m.return_value = False
- result = User.create_from_github(USERNAME, PASSWORD)
+ result = User.objects.create_from_github(USERNAME, PASSWORD)
self.assertFalse(result)
def test_generate_token(self):
- token = User.generate_token()
+ token = generate_token()
self.assertEqual(len(token), UUID_LENGTH)
- another_token = User.generate_token()
+ another_token = generate_token()
self.assertNotEqual(token, another_token)
@@ -89,7 +90,7 @@ def test_authenticate_valid_credentials(self, m):
response.status = 200
response.text = GITHUB_VALID_RESPONSE
m.return_value = response
- result = GithubAuthenticator.authenticate(USERNAME, PASSWORD)
+ result = authenticate_github(USERNAME, PASSWORD)
self.assertEqual(result['login'], GITHUB_VALID_RESPONSE_DATA['login'])
self.assertEqual(result['avatar_url'], GITHUB_VALID_RESPONSE_DATA['avatar_url'])
@@ -98,12 +99,12 @@ def test_authenticate_invalid_credentials(self, m):
response = mock.Mock()
response.status = 401
m.return_value = response
- result = GithubAuthenticator.authenticate(USERNAME, PASSWORD)
+ result = authenticate_github(USERNAME, PASSWORD)
self.assertFalse(result)
@mock.patch('detector.accounts.models.requests.get')
def test_authenticate_invalid_credentials(self, m):
m.side_effect = RequestException()
- result = GithubAuthenticator.authenticate(USERNAME, PASSWORD)
+ result = authenticate_github(USERNAME, PASSWORD)
self.assertFalse(result)
@@ -1,7 +1,7 @@
import json
from django.test import TestCase
import mock
-from accounts.models import User
+from accounts.models import User, generate_token
username = 'test'
password = 'test'
@@ -15,9 +15,9 @@ def test_login_with_valid_data(self):
user = User.objects.create(
github_username='test',
avatar_url='http://example.com/av.png',
- token=User.generate_token()
+ token=generate_token()
)
- with mock.patch('detector.accounts.models.User.create_from_github') as m:
+ with mock.patch('detector.accounts.models.User.objects.create_from_github') as m:
m.return_value = user
resp = self.client.post('/login/', { 'username': username, 'password': password })
data = json.loads(resp.content)
@@ -26,7 +26,7 @@ def test_login_with_valid_data(self):
data['token'] = user.token
def test_login_with_invalid_data(self):
- with mock.patch('detector.accounts.models.User.create_from_github') as m:
+ with mock.patch('detector.accounts.models.User.objects.create_from_github') as m:
m.return_value = False
resp = self.client.post('/login/', { 'username': username, 'password': password })
self.assertEqual(resp.status_code, 401)
@@ -4,7 +4,7 @@
def login(request):
if request.method == 'POST':
- user = User.create_from_github(
+ user = User.objects.create_from_github(
request.POST['username'],
request.POST['password']
)

0 comments on commit 10ae402

Please sign in to comment.