mirror of
https://github.com/mediacms-io/mediacms.git
synced 2025-11-19 21:26:05 -05:00
54
tests/api/test_user_login.py
Normal file
54
tests/api/test_user_login.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from django.test import Client, TestCase
|
||||
from rest_framework.authtoken.models import Token
|
||||
|
||||
from files.tests import create_account
|
||||
|
||||
API_V1_LOGIN_URL = '/api/v1/login'
|
||||
|
||||
|
||||
class TestUserLogin(TestCase):
|
||||
fixtures = ["fixtures/categories.json", "fixtures/encoding_profiles.json"]
|
||||
|
||||
def setUp(self):
|
||||
self.password = 'this_is_a_fake_password'
|
||||
self.user = create_account(password=self.password)
|
||||
|
||||
def test_login_endpoint(self):
|
||||
client = Client()
|
||||
response = client.get(API_V1_LOGIN_URL)
|
||||
self.assertEqual(
|
||||
response.status_code,
|
||||
405,
|
||||
"GET not allowed here",
|
||||
)
|
||||
|
||||
response = client.post(API_V1_LOGIN_URL, {'username': 'fake', 'password': 'fake'})
|
||||
self.assertTrue('User not found' in str(response.content), 'Expected user not to be there')
|
||||
|
||||
user = self.user
|
||||
response = client.post(API_V1_LOGIN_URL, {'username': user.username, 'password': self.password})
|
||||
|
||||
self.assertEqual(
|
||||
response.status_code,
|
||||
200,
|
||||
"Expected 200",
|
||||
)
|
||||
data = response.data
|
||||
|
||||
self.assertEqual(
|
||||
data.get('email'),
|
||||
user.email,
|
||||
"Expected user email",
|
||||
)
|
||||
self.assertEqual(
|
||||
data.get('username'),
|
||||
user.username,
|
||||
"Expected username",
|
||||
)
|
||||
|
||||
token = Token.objects.filter(user=user).first()
|
||||
self.assertEqual(
|
||||
data.get('token'),
|
||||
token.key,
|
||||
"Expected valid token",
|
||||
)
|
||||
49
tests/api/test_user_token.py
Normal file
49
tests/api/test_user_token.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from django.test import Client, TestCase
|
||||
from rest_framework.authtoken.models import Token
|
||||
|
||||
from files.tests import create_account
|
||||
|
||||
API_V1_USER_TOKEN_URL = '/api/v1/user/token'
|
||||
|
||||
|
||||
class TestUserToken(TestCase):
|
||||
fixtures = ["fixtures/categories.json", "fixtures/encoding_profiles.json"]
|
||||
|
||||
def setUp(self):
|
||||
self.password = 'this_is_a_fake_password'
|
||||
self.user = create_account(password=self.password)
|
||||
|
||||
def test_user_token_endpoint(self):
|
||||
client = Client()
|
||||
response = client.get(API_V1_USER_TOKEN_URL)
|
||||
self.assertEqual(
|
||||
response.status_code,
|
||||
403,
|
||||
"FORBIDDEN",
|
||||
)
|
||||
|
||||
user = self.user
|
||||
client.force_login(user=user)
|
||||
|
||||
response = client.post(API_V1_USER_TOKEN_URL)
|
||||
self.assertEqual(
|
||||
response.status_code,
|
||||
405,
|
||||
"method not allowed here",
|
||||
)
|
||||
|
||||
response = client.get(API_V1_USER_TOKEN_URL)
|
||||
data = response.data
|
||||
|
||||
self.assertEqual(
|
||||
response.status_code,
|
||||
200,
|
||||
"expected 200",
|
||||
)
|
||||
|
||||
token = Token.objects.filter(user=user).first()
|
||||
self.assertEqual(
|
||||
data.get('token'),
|
||||
token.key,
|
||||
"Expected valid token",
|
||||
)
|
||||
41
tests/api/test_user_whoami.py
Normal file
41
tests/api/test_user_whoami.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from django.test import Client, TestCase
|
||||
|
||||
from files.tests import create_account
|
||||
|
||||
API_V1_LOGIN_URL = '/api/v1/whoami'
|
||||
|
||||
|
||||
class TestUserWhoami(TestCase):
|
||||
fixtures = ["fixtures/categories.json", "fixtures/encoding_profiles.json"]
|
||||
|
||||
def setUp(self):
|
||||
self.user = create_account()
|
||||
|
||||
def test_whoami_endpoint(self):
|
||||
client = Client()
|
||||
response = client.get(API_V1_LOGIN_URL)
|
||||
self.assertEqual(
|
||||
response.status_code,
|
||||
403,
|
||||
"Expected 403",
|
||||
)
|
||||
|
||||
user = self.user
|
||||
client.force_login(user=user)
|
||||
response = client.get(API_V1_LOGIN_URL)
|
||||
self.assertEqual(
|
||||
response.status_code,
|
||||
200,
|
||||
"Expected 200",
|
||||
)
|
||||
data = response.data
|
||||
self.assertEqual(
|
||||
data.get('description'),
|
||||
user.description,
|
||||
"Expected user description",
|
||||
)
|
||||
self.assertEqual(
|
||||
data.get('username'),
|
||||
user.username,
|
||||
"Expected username",
|
||||
)
|
||||
Reference in New Issue
Block a user