From a63424e1ad9a6698e1c40a21e9ce884bf142c871 Mon Sep 17 00:00:00 2001 From: Victor Ramirez de la Corte Date: Sat, 29 Dec 2018 21:34:04 +0100 Subject: [PATCH] Closed #13 Authentication fixed and improved (#20) --- base/client.py | 67 +++++++++++++++++++-------------------- base/tests.py | 47 +++++++++++++++++++++++++++ character/models.py | 4 +-- character/serializers.py | 2 -- character/tests.py | 21 ++++++------- contents/factories.py | 1 - contents/tests.py | 36 +++++++++------------ game/tests.py | 41 +++++++++--------------- game/views.py | 1 + socializa/settings.py | 3 +- socializa/urls.py | 5 ++- things/tests.py | 68 ++++++++++++++++------------------------ 12 files changed, 155 insertions(+), 141 deletions(-) diff --git a/base/client.py b/base/client.py index 92d54e6..7b7819a 100644 --- a/base/client.py +++ b/base/client.py @@ -1,21 +1,23 @@ import json +from django.conf import settings from django.test import Client from oauth2_provider.models import Application class BaseClient(Client): - def __init__(self, base_url='/api', version='', *args, **kwargs): + def __init__(self, *args, **kwargs): self.auth_token = '' - self.base_url = base_url - self.version = '/v{0}'.format(version) + self.access_token = '' + version = kwargs.get('version', settings.VERSION) + self.base_url = '/api/v{0}'.format(version) super().__init__(*args, **kwargs) def set_auth_token(self, response): json_res = response.json() token_type = json_res.get('token_type') - access_token = json_res.get('access_token') - self.auth_token = '%s %s' % (token_type, access_token) + self.access_token = json_res.get('access_token') + self.auth_token = '%s %s' % (token_type, self.access_token) def authenticate(self, email, password): app = Application.objects.get(name='local') @@ -25,47 +27,44 @@ def authenticate(self, email, password): 'username': email, 'password': password } - response = self.post('/social/token/', data) + response = self.post('/auth/token/', data) if response.status_code == 200: self.set_auth_token(response) return response def logout(self): - self.auth_token = '' + app = Application.objects.get(name='local') + data = { + 'client_id': app.client_id, + 'grant_type': 'password', + 'token': self.access_token + } + response = self.post('/auth/revoke-token/', data) + if response.status_code == 204: + self.auth_token = '' + return response + + def extra(self): + return { + 'content_type': 'application/json', + 'HTTP_AUTHORIZATION': self.auth_token + } def get(self, url): - return super().get(self.base_url + self.version + url, - content_type='application/json', - HTTP_AUTHORIZATION=self.auth_token) + return super().get(self.base_url + url, **self.extra()) def post(self, url, data=None): - if data is None: - data = {} - return super().post(self.base_url + self.version + url, - json.dumps(data), - content_type='application/json', - HTTP_AUTHORIZATION=self.auth_token) + data = {} if data is None else json.dumps(data) + return super().post(self.base_url + url, data, **self.extra()) def put(self, url, data=None): - if data is None: - data = {} - return super().put(self.base_url + self.version + url, - json.dumps(data), - content_type='application/json', - HTTP_AUTHORIZATION=self.auth_token) + data = {} if data is None else json.dumps(data) + return super().put(self.base_url + url, data, **self.extra()) def patch(self, url, data=None): - if data is None: - data = {} - return super().patch(self.base_url + self.version + url, - json.dumps(data), - content_type='application/json', - HTTP_AUTHORIZATION=self.auth_token) + data = {} if data is None else json.dumps(data) + return super().patch(self.base_url + url, data, **self.extra()) def delete(self, url, data=None): - if data is None: - data = {} - return super().delete(self.base_url + self.version + url, - json.dumps(data), - content_type='application/json', - HTTP_AUTHORIZATION=self.auth_token) + data = {} if data is None else json.dumps(data) + return super().delete(self.base_url + url, data, **self.extra()) diff --git a/base/tests.py b/base/tests.py index e69de29..b69fe27 100644 --- a/base/tests.py +++ b/base/tests.py @@ -0,0 +1,47 @@ +from django.core.management import call_command +from rest_framework.test import APITestCase + +from .client import BaseClient +from character.factories import PlayerFactory +from character.models import Player + + +class BaseTestCase(APITestCase): + + def setUp(self): + call_command('socialapps') + self.client = BaseClient() + self.player = PlayerFactory.create() + + def tearDown(self): + self.client = None + Player.objects.all().delete() + + def test_authenticate(self): + pwd = 'qweqweqwe' + response = self.client.authenticate(self.player.user.username, pwd) + self.assertEqual(response.status_code, 200) + self.assertNotEqual(self.client.auth_token, '') + self.assertEqual( + sorted(list(response.json().keys())), + sorted(['access_token', 'expires_in', 'refresh_token', 'scope', + 'token_type']), + ) + + def test_logout_without_login(self): + response = self.client.logout() + self.assertEqual(response.status_code, 400) + self.assertEqual( + response.json(), + { + 'error': 'invalid_request', + 'error_description': 'Missing token parameter.' + } + ) + + def test_logout(self): + pwd = 'qweqweqwe' + self.client.authenticate(self.player.user.username, pwd) + response = self.client.logout() + self.assertEqual(response.status_code, 204) + self.assertEqual(self.client.auth_token, '') diff --git a/character/models.py b/character/models.py index 868f5ae..9b881f4 100644 --- a/character/models.py +++ b/character/models.py @@ -4,10 +4,8 @@ from django.contrib.auth.models import User -# User email will be unique and username not unique +# User email will be unique User._meta.get_field('email')._unique = True -User._meta.get_field('username')._unique = False - class Character(models.Model): diff --git a/character/serializers.py b/character/serializers.py index 6d29ac7..b19cdff 100644 --- a/character/serializers.py +++ b/character/serializers.py @@ -12,8 +12,6 @@ class Meta: fields = ('username', 'email') -class NPCSerializer(CharacterSerializer): - class CharacterSerializer(serializers.ModelSerializer): user = UserSerializer() diff --git a/character/tests.py b/character/tests.py index f582fc7..e11bd53 100644 --- a/character/tests.py +++ b/character/tests.py @@ -1,22 +1,20 @@ -from django.conf import settings +from urllib.parse import urlencode from django.contrib.auth.models import User from django.core.management import call_command from rest_framework.test import APITestCase -from urllib.parse import urlencode from base.client import BaseClient -from .factories import NPCFactory -from .models import NPC +from .factories import NPCFactory, PlayerFactory +from .models import NPC, Player from .serializers import NPCSerializer class NPCTestCase(APITestCase): def setUp(self): - self.user = User.objects.create_superuser(username='me@socializa.com', - password='qweqweqwe', email='me@socializa.com') + self.player = PlayerFactory.create() call_command('socialapps') - self.client = BaseClient(version=settings.VERSION) + self.client = BaseClient() self.npc = NPCFactory.create() self.data = { 'user': { @@ -27,10 +25,11 @@ def setUp(self): def tearDown(self): self.client = None + Player.objects.all().delete() + NPC.objects.all().delete() - def authenticate(self, username='me@socializa.com', pwd='qweqweqwe'): - response = self.client.authenticate(username, pwd) - self.assertEqual(response.status_code, 200) + def authenticate(self, pwd='qweqweqwe'): + self.client.authenticate(self.player.user.username, pwd) # CREATE def character_create(self, ctype, data, st): @@ -54,7 +53,7 @@ def test_npc_create_bad_request(self): def test_npc_create_exist(self): self.authenticate() data = {'user': { - 'email': self.user.email, + 'email': self.player.user.email, 'password': 'qweqweqwe' }} self.character_create('npc', data, 400) diff --git a/contents/factories.py b/contents/factories.py index 0351340..e43b684 100644 --- a/contents/factories.py +++ b/contents/factories.py @@ -9,7 +9,6 @@ SubFactory ) - from .models import Content from character.factories import NPCFactory, PlayerFactory from things.factories import ItemFactory, KnowledgeFactory, RolFactory diff --git a/contents/tests.py b/contents/tests.py index 432c9c1..f8b31c3 100644 --- a/contents/tests.py +++ b/contents/tests.py @@ -1,5 +1,3 @@ -from django.conf import settings -from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.contrib.gis.geos import Point from django.core.management import call_command @@ -27,11 +25,8 @@ class ContentTestCase(APITestCase): def setUp(self): - User.objects.create_superuser(username='me@socializa.com', - password='qweqweqwe', - email='me@socializa.com') call_command('socialapps') - self.client = BaseClient(version=settings.VERSION) + self.client = BaseClient() self.player = PlayerFactory.create() self.game = GameFactory.create() self.owner = Owner(player=self.player, game=self.game) @@ -82,9 +77,8 @@ def tearDown(self): Knowledge.objects.all().delete() Rol.objects.all().delete() - def authenticate(self, username='me@socializa.com', pwd='qweqweqwe'): - response = self.client.authenticate(username, pwd) - self.assertEqual(response.status_code, 200) + def authenticate(self, pwd='qweqweqwe'): + self.client.authenticate(self.player.user.username, pwd) # LIST def test_list_content_unauth(self): @@ -92,13 +86,13 @@ def test_list_content_unauth(self): self.assertEqual(response.status_code, 401) def test_list_content(self): - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.get('/content/') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 5) def test_list_content_filter_game(self): - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.get('/content/?game_id={}'.format(self.game.pk)) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 5) @@ -109,7 +103,7 @@ def test_list_content_filter_game(self): def test_list_content_filter_bad(self): """ If you use bad query params, return none """ - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.get('/content/?game_id=str') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 0) @@ -127,7 +121,7 @@ def test_create_content_unauth(self): self.assertEqual(response.status_code, 401) def test_create_content_bad_request(self): - self.authenticate(username=self.player.user.username) + self.authenticate() self.data_mode1['content_type'] = 'bad' response = self.client.post('/content/', self.data_mode1) self.assertEqual(response.status_code, 400) @@ -137,7 +131,7 @@ def test_create_content_bad_request(self): self.assertEqual(response.status_code, 400) def test_create_content(self): - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.post('/content/', self.data_mode1) self.assertEqual(response.status_code, 201) @@ -149,13 +143,13 @@ def test_destroy_content_unauth(self): response = self.client.delete('/content/{}/'.format(self.contentItem.pk)) self.assertEqual(response.status_code, 401) - self.owner.delete() - self.authenticate(username=self.player.user.username) + player = PlayerFactory.create() + self.client.authenticate(player.user.username, 'qweqweqwe') response = self.client.delete('/content/{}/'.format(self.contentItem.pk)) self.assertEqual(response.status_code, 404) def test_destroy_content(self): - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.delete('/content/{}/'.format(self.contentItem.pk)) self.assertEqual(response.status_code, 204) @@ -172,7 +166,7 @@ def test_update_content_unauth(self): self.assertEqual(response.status_code, 401) def test_update_content_bad_request(self): - self.authenticate(username=self.player.user.username) + self.authenticate() data = { "portion": { "longitude": 36.261421, @@ -189,8 +183,8 @@ def test_update_content_bad_request(self): self.assertEqual(response.status_code, 400) def test_update_content(self): - self.authenticate(username=self.player.user.username) data = ContentSerializer(self.contentNPC).data + self.authenticate() data.update({ "position": { "longitude": 36.261421, @@ -218,7 +212,7 @@ def test_update_content(self): # RETRIEVE def test_get_content_unexist_pk(self): - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.get('/content/{}/'.format(0)) self.assertEqual(response.status_code, 404) @@ -227,9 +221,9 @@ def test_get_content_unauth(self): self.assertEqual(response.status_code, 401) def test_get_content(self): - self.authenticate(username=self.player.user.username) contents = [self.contentPlayer, self.contentNPC, self.contentItem, self.contentKnowledge, self.contentRol] + self.authenticate() for content in contents: response = self.client.get('/content/{}/'.format(content.pk)) self.assertEqual(response.status_code, 200) diff --git a/game/tests.py b/game/tests.py index 0ea4cbf..6fb9ff3 100644 --- a/game/tests.py +++ b/game/tests.py @@ -1,5 +1,3 @@ -from django.conf import settings -from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.core.management import call_command from django.utils import timezone @@ -20,11 +18,8 @@ class GameTestCase(APITestCase): def setUp(self): - User.objects.create_superuser(username='me@socializa.com', - password='qweqweqwe', - email='me@socializa.com') call_command('socialapps') - self.client = BaseClient(version=settings.VERSION) + self.client = BaseClient() self.player = PlayerFactory.create() self.game = GameFactory.create() self.owner = Owner(player=self.player, game=self.game) @@ -43,9 +38,8 @@ def setUp(self): def tearDown(self): self.client = None - def authenticate(self, username='me@socializa.com', pwd='qweqweqwe'): - response = self.client.authenticate(username, pwd) - self.assertEqual(response.status_code, 200) + def authenticate(self, pwd='qweqweqwe'): + self.client.authenticate(self.player.user.username, pwd) def test_game_create_unauthorized(self): response = self.client.post('/game/', self.data) @@ -53,7 +47,7 @@ def test_game_create_unauthorized(self): def test_game_create_bad_request_1(self): self.data['preferences']['vision_distance'] = 'bad' - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.post('/game/', self.data) self.assertEqual(response.status_code, 400) self.assertTrue('preferences' in response.json()) @@ -61,7 +55,7 @@ def test_game_create_bad_request_1(self): def test_game_create_bad_request_2(self): self.data['start'] = 'bad' - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.post('/game/', self.data) self.assertEqual(response.status_code, 400) self.assertTrue('start' in response.json()) @@ -70,7 +64,7 @@ def test_game_create(self): games_amount = Game.objects.count() owners_amount = Owner.objects.count() - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.post('/game/', self.data) self.assertEqual(response.status_code, 201) self.assertEqual(Game.objects.count(), games_amount + 1) @@ -96,7 +90,7 @@ def test_game_destroy_unauthorized(self): self.assertEqual(response.status_code, 401) def test_game_destroy(self): - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.delete('/game/{}/'.format(self.game.pk)) self.assertEqual(response.status_code, 204) @@ -106,7 +100,7 @@ def test_game_update_unauthorized(self): self.assertEqual(response.status_code, 401) def test_game_update_bad_request_1(self): - self.authenticate(username=self.player.user.username) + self.authenticate() self.data['preferences']['vision_distance'] = True response = self.client.put('/game/{}/'.format(self.game.pk), self.data) self.assertEqual(response.status_code, 400) @@ -114,14 +108,14 @@ def test_game_update_bad_request_1(self): self.assertTrue('vision_distance' in response.json().get('preferences')) def test_game_update_bad_request_2(self): - self.authenticate(username=self.player.user.username) + self.authenticate() self.data['start'] = 'bad' response = self.client.put('/game/{}/'.format(self.game.pk), self.data) self.assertEqual(response.status_code, 400) self.assertTrue('start' in response.json()) def test_game_update(self): - self.authenticate(username=self.player.user.username) + self.authenticate() self.data['preferences']['vision_distance'] = 80 response = self.client.put('/game/{}/'.format(self.game.pk), self.data) self.assertEqual(response.status_code, 200) @@ -135,7 +129,7 @@ def test_game_update(self): self.assertEqual(field, value) def test_game_update_partial(self): - self.authenticate(username=self.player.user.username) + self.authenticate() data = {'description': 'other', 'preferences': {'vision_distance': 80}} response = self.client.patch('/game/{}/'.format(self.game.pk), data) self.assertEqual(response.status_code, 200) @@ -152,11 +146,8 @@ def test_game_update_partial(self): class GameContentTestCase(APITestCase): def setUp(self): - User.objects.create_superuser(username='me@socializa.com', - password='qweqweqwe', - email='me@socializa.com') call_command('socialapps') - self.client = BaseClient(version=settings.VERSION) + self.client = BaseClient() self.player = PlayerFactory.create() self.item = ItemFactory.create() @@ -168,15 +159,13 @@ def setUp(self): def tearDown(self): self.client = None - User.objects.all().delete() Player.objects.all().delete() - def authenticate(self, username='me@socializa.com', pwd='qweqweqwe'): - response = self.client.authenticate(username, pwd) - self.assertEqual(response.status_code, 200) + def authenticate(self, pwd='qweqweqwe'): + self.client.authenticate(self.player.user.username, pwd) def test_create_game_contents(self): - self.authenticate(username=self.player.user.username) + self.authenticate() # Complete GAME in JSON data = { diff --git a/game/views.py b/game/views.py index c93a760..deffba0 100644 --- a/game/views.py +++ b/game/views.py @@ -1,4 +1,5 @@ from django_filters.rest_framework import DjangoFilterBackend +from django.core.exceptions import FieldError from rest_framework import generics from rest_framework.permissions import IsAuthenticatedOrReadOnly, SAFE_METHODS diff --git a/socializa/settings.py b/socializa/settings.py index 71f7396..b1e75fa 100644 --- a/socializa/settings.py +++ b/socializa/settings.py @@ -238,8 +238,9 @@ USE_TZ = True -# Current version +# Versions and current version +VERSIONS = [1.0] VERSION = 1.0 diff --git a/socializa/urls.py b/socializa/urls.py index 2bf7b38..6de15f5 100644 --- a/socializa/urls.py +++ b/socializa/urls.py @@ -13,6 +13,7 @@ 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ +from django.conf import settings from django.contrib import admin from django.urls import ( include, @@ -28,10 +29,12 @@ urlpatterns = [ path('admin/', admin.site.urls), path('api/v/', include([ - path('social/', include('rest_framework_social_oauth2.urls')), path('character/', include('character.urls')), path('content/', include('contents.urls')), path('game/', include('game.urls')), path('thing/', include('things.urls')), ])), ] +for version in settings.VERSIONS: + urlpatterns.append(path('api/v{}/auth/'.format(version), + include('rest_framework_social_oauth2.urls'))) diff --git a/things/tests.py b/things/tests.py index d51207c..47b613e 100644 --- a/things/tests.py +++ b/things/tests.py @@ -1,5 +1,3 @@ -from django.conf import settings -from django.contrib.auth.models import User from django.core.management import call_command from rest_framework.test import APITestCase @@ -19,20 +17,16 @@ class ItemTestCase(APITestCase): def setUp(self): - User.objects.create_superuser(username='me@socializa.com', - password='qweqweqwe', - email='me@socializa.com') call_command('socialapps') - self.client = BaseClient(version=settings.VERSION) + self.client = BaseClient() self.player = PlayerFactory.create() self.item = ItemFactory.create() def tearDown(self): self.client = None - def authenticate(self, username='me@socializa.com', pwd='qweqweqwe'): - response = self.client.authenticate(username, pwd) - self.assertEqual(response.status_code, 200) + def authenticate(self, pwd='qweqweqwe'): + self.client.authenticate(self.player.user.username, pwd) def test_item_create(self): items_quantity = Item.objects.count() @@ -46,7 +40,7 @@ def test_item_create(self): response = self.client.post('/thing/item/', data) self.assertEqual(response.status_code, 401) - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.post('/thing/item/', data) self.assertEqual(response.status_code, 201) self.assertEqual(Item.objects.count(), items_quantity + 1) @@ -71,7 +65,7 @@ def test_item_destroy_unauthorized(self): self.assertEqual(response.status_code, 401) def test_item_destroy(self): - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.delete('/thing/item/{}/'.format(self.item.pk)) self.assertEqual(response.status_code, 204) @@ -87,7 +81,7 @@ def test_item_update_unauthorized(self): self.assertEqual(response.status_code, 401) def test_item_update(self): - self.authenticate(username=self.player.user.username) + self.authenticate() data = { 'name': 'Item', 'description': 'Item description', @@ -106,20 +100,16 @@ def test_item_update(self): class KnowledgeTestCase(APITestCase): def setUp(self): - User.objects.create_superuser(username='me@socializa.com', - password='qweqweqwe', - email='me@socializa.com') call_command('socialapps') - self.client = BaseClient(version=settings.VERSION) + self.client = BaseClient() self.player = PlayerFactory.create() - self.knowledge = KnowledgeFactory.create() + self.kn = KnowledgeFactory.create() def tearDown(self): self.client = None - def authenticate(self, username='me@socializa.com', pwd='qweqweqwe'): - response = self.client.authenticate(username, pwd) - self.assertEqual(response.status_code, 200) + def authenticate(self, pwd='qweqweqwe'): + self.client.authenticate(self.player.user.username, pwd) def test_knowledge_create(self): knowledges_quantity = Knowledge.objects.count() @@ -131,7 +121,7 @@ def test_knowledge_create(self): response = self.client.post('/thing/knowledge/', data) self.assertEqual(response.status_code, 401) - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.post('/thing/knowledge/', data) self.assertEqual(response.status_code, 201) self.assertEqual(Knowledge.objects.count(), knowledges_quantity + 1) @@ -140,24 +130,24 @@ def test_knowledge_list(self, ): response = self.client.get('/thing/knowledge/') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 1) - self.assertEqual(response.json(), [KnowledgeSerializer(self.knowledge).data]) + self.assertEqual(response.json(), [KnowledgeSerializer(self.kn).data]) def test_knowledge_retrieve(self): - response = self.client.get('/thing/knowledge/{}/'.format(self.knowledge.pk)) + response = self.client.get('/thing/knowledge/{}/'.format(self.kn.pk)) self.assertEqual(response.status_code, 200) - self.assertEqual(response.json(), KnowledgeSerializer(self.knowledge).data) + self.assertEqual(response.json(), KnowledgeSerializer(self.kn).data) def test_knowledge_retrieve_unknow(self): response = self.client.get('/thing/knowledge/-1/') self.assertEqual(response.status_code, 404) def test_knowledge_destroy_unauthorized(self): - response = self.client.delete('/thing/knowledge/{}/'.format(self.knowledge.pk)) + response = self.client.delete('/thing/knowledge/{}/'.format(self.kn.pk)) self.assertEqual(response.status_code, 401) def test_knowledge_destroy(self): - self.authenticate(username=self.player.user.username) - response = self.client.delete('/thing/knowledge/{}/'.format(self.knowledge.pk)) + self.authenticate() + response = self.client.delete('/thing/knowledge/{}/'.format(self.kn.pk)) self.assertEqual(response.status_code, 204) def test_knowledge_update_unauthorized(self): @@ -166,19 +156,19 @@ def test_knowledge_update_unauthorized(self): 'description': 'Knowledge description', 'shareable': True, } - response = self.client.put('/thing/knowledge/{}/'.format(self.knowledge.pk), data) + response = self.client.put('/thing/knowledge/{}/'.format(self.kn.pk), data) self.assertEqual(response.status_code, 401) def test_knowledge_update(self): - self.authenticate(username=self.player.user.username) + self.authenticate() data = { 'name': 'Knowledge', 'description': 'Knowledge description', 'shareable': True, } - response = self.client.put('/thing/knowledge/{}/'.format(self.knowledge.pk), data) + response = self.client.put('/thing/knowledge/{}/'.format(self.kn.pk), data) self.assertEqual(response.status_code, 200) - knowledge_changed = Knowledge.objects.get(pk=self.knowledge.pk) + knowledge_changed = Knowledge.objects.get(pk=self.kn.pk) for key, value in data.items(): field = getattr(knowledge_changed, key) self.assertEqual(field, value) @@ -187,20 +177,16 @@ def test_knowledge_update(self): class RolTestCase(APITestCase): def setUp(self): - User.objects.create_superuser(username='me@socializa.com', - password='qweqweqwe', - email='me@socializa.com') call_command('socialapps') - self.client = BaseClient(version=settings.VERSION) + self.client = BaseClient() self.player = PlayerFactory.create() self.rol = RolFactory.create() def tearDown(self): self.client = None - def authenticate(self, username='me@socializa.com', pwd='qweqweqwe'): - response = self.client.authenticate(username, pwd) - self.assertEqual(response.status_code, 200) + def authenticate(self, pwd='qweqweqwe'): + self.client.authenticate(self.player.user.username, pwd) def test_rol_create(self): rols_quantity = Rol.objects.count() @@ -211,7 +197,7 @@ def test_rol_create(self): response = self.client.post('/thing/rol/', data) self.assertEqual(response.status_code, 401) - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.post('/thing/rol/', data) self.assertEqual(response.status_code, 201) self.assertEqual(Rol.objects.count(), rols_quantity + 1) @@ -236,7 +222,7 @@ def test_rol_destroy_unauthorized(self): self.assertEqual(response.status_code, 401) def test_rol_destroy(self): - self.authenticate(username=self.player.user.username) + self.authenticate() response = self.client.delete('/thing/rol/{}/'.format(self.rol.pk)) self.assertEqual(response.status_code, 204) @@ -249,7 +235,7 @@ def test_rol_update_unauthorized(self): self.assertEqual(response.status_code, 401) def test_rol_update(self): - self.authenticate(username=self.player.user.username) + self.authenticate() data = { 'name': 'Rol', 'description': 'Rol description',