-
Notifications
You must be signed in to change notification settings - Fork 188
/
test_views.py
57 lines (45 loc) · 1.87 KB
/
test_views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from django.urls import reverse
from django.contrib.auth.hashers import check_password
from rest_framework.test import APITestCase
from rest_framework import status
from faker import Faker
import factory
import pytest
from ..models import User
from .factories import UserFactory
fake = Faker()
@pytest.mark.django_db
class TestUserListTestCase(APITestCase):
"""
Tests /users list operations.
"""
def setUp(self):
self.url = reverse('user-list')
self.user_data = factory.build(dict, FACTORY_CLASS=UserFactory)
def test_post_request_with_no_data_fails(self):
response = self.client.post(self.url, {})
assert response.status_code == status.HTTP_400_BAD_REQUEST
def test_post_request_with_valid_data_succeeds(self):
response = self.client.post(self.url, self.user_data)
assert response.status_code == status.HTTP_201_CREATED
user = User.objects.get(pk=response.data.get('id'))
assert user.username == self.user_data.get('username')
assert check_password(self.user_data.get('password'), user.password)
class TestUserDetailTestCase(APITestCase):
"""
Tests /users detail operations.
"""
def setUp(self):
self.user = UserFactory()
self.url = reverse('user-detail', kwargs={'pk': self.user.pk})
self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.user.auth_token}')
def test_get_request_returns_a_given_user(self):
response = self.client.get(self.url)
assert response.status_code == status.HTTP_200_OK
def test_put_request_updates_a_user(self):
new_first_name = fake.first_name()
payload = {'first_name': new_first_name}
response = self.client.put(self.url, payload)
assert response.status_code == status.HTTP_200_OK
user = User.objects.get(pk=self.user.id)
assert user.first_name == new_first_name