1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-04-04 21:25:24 +00:00

created backend tests

This commit is contained in:
Bram Wiepjes 2019-07-05 11:48:39 +02:00
parent 3a473f44f5
commit d253575cfe
10 changed files with 164 additions and 3 deletions

View file

@ -1,2 +1,3 @@
flake8==3.7.7
pytest-django==3.5.0
Faker==1.0.7

View file

@ -13,5 +13,5 @@ urlpatterns = [
url(r'^token-auth/$', obtain_jwt_token, name='token_auth'),
url(r'^token-refresh/$', refresh_jwt_token, name='token_refresh'),
url(r'^token-verify/$', verify_jwt_token, name='token_verify'),
url(r'^$', UserView.as_view(), name='user')
url(r'^$', UserView.as_view(), name='index')
]

View file

View file

@ -0,0 +1,75 @@
import pytest
from unittest.mock import patch
from datetime import datetime
from django.shortcuts import reverse
from django.contrib.auth import get_user_model
from rest_framework_jwt.settings import api_settings
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
User = get_user_model()
@pytest.mark.django_db
def test_token_auth(client, data_fixture):
data_fixture.create_user(email='test@test.nl', password='password',
first_name='Test1')
response = client.post(reverse('api:user:token_auth'), {
'username': 'no_existing@test.nl',
'password': 'password'
})
json = response.json()
assert response.status_code == 400
assert len(json['non_field_errors']) > 0
response = client.post(reverse('api:user:token_auth'), {
'username': 'test@test.nl',
'password': 'wrong_password'
})
json = response.json()
assert response.status_code == 400
assert len(json['non_field_errors']) > 0
response = client.post(reverse('api:user:token_auth'), {
'username': 'test@test.nl',
'password': 'password'
})
json = response.json()
assert response.status_code == 200
assert 'token' in json
assert 'user' in json
assert json['user']['username'] == 'test@test.nl'
assert json['user']['first_name'] == 'Test1'
@pytest.mark.django_db
def test_token_refresh(client, data_fixture):
user = data_fixture.create_user(email='test@test.nl', password='password',
first_name='Test1')
response = client.post(reverse('api:user:token_refresh'), {'token': 'WRONG_TOKEN'})
assert response.status_code == 400
payload = jwt_payload_handler(user)
token = jwt_encode_handler(payload)
response = client.post(reverse('api:user:token_refresh'), {'token': token})
assert response.status_code == 200
assert 'token' in response.json()
with patch('rest_framework_jwt.utils.datetime') as mock_datetime:
mock_datetime.utcnow.return_value = datetime(2019, 1, 1, 1, 1, 1, 0)
payload = jwt_payload_handler(user)
token = jwt_encode_handler(payload)
response = client.post(reverse('api:user:token_refresh'), {'token': token})
assert response.status_code == 400

View file

@ -0,0 +1,37 @@
import pytest
from django.contrib.auth import get_user_model
from django.shortcuts import reverse
User = get_user_model()
@pytest.mark.django_db
def test_create_user(client):
response = client.post(reverse('api:user:index'), {
'name': 'Test1',
'email': 'test@test.nl',
'password': 'test12'
})
assert response.status_code == 200
user = User.objects.get(email='test@test.nl')
assert user.first_name == 'Test1'
assert user.email == 'test@test.nl'
assert user.password != ''
response_failed = client.post(reverse('api:user:index'), {
'name': 'Test1',
'email': 'test@test.nl',
'password': 'test12'
})
assert response_failed.status_code == 400
assert response_failed.json()['error'] == 'ERROR_ALREADY_EXISTS'
response_failed_2 = client.post(reverse('api:user:index'), {
'email': 'test'
})
assert response_failed_2.status_code == 400

View file

@ -1,2 +0,0 @@
def test_homepage():
pass

View file

@ -0,0 +1,18 @@
import pytest
from baserow.user.exceptions import UserAlreadyExist
from baserow.user.handler import UserHandler
@pytest.mark.django_db
def test_create_user():
user_handler = UserHandler()
user = user_handler.create_user('Test1', 'test@test.nl', 'password')
assert user.pk
assert user.first_name == 'Test1'
assert user.email == 'test@test.nl'
assert user.username == 'test@test.nl'
with pytest.raises(UserAlreadyExist):
user_handler.create_user('Test1', 'test@test.nl', 'password')

View file

@ -0,0 +1,7 @@
import pytest
@pytest.fixture
def data_fixture():
from .fixtures import Fixtures
return Fixtures()

5
backend/tests/fixtures/__init__.py vendored Normal file
View file

@ -0,0 +1,5 @@
from .user import UserFixtures
class Fixtures(UserFixtures):
pass

20
backend/tests/fixtures/user.py vendored Normal file
View file

@ -0,0 +1,20 @@
from django.contrib.auth import get_user_model
from faker import Faker
fake = Faker()
User = get_user_model()
class UserFixtures:
def create_user(self, **kwargs):
kwargs.setdefault('email', fake.email())
kwargs.setdefault('username', kwargs['email'])
kwargs.setdefault('first_name', fake.name())
kwargs.setdefault('password', 'password')
user = User(**kwargs)
user.set_password(kwargs['password'])
user.save()
return user