UserSerializer.py 1.52 KB
Newer Older
asmundh's avatar
asmundh committed
1
from django.test import TestCase
2
from users.serializers import UserSerializer
Elias Larsen's avatar
Elias Larsen committed
3
from django.contrib.auth import get_user_model
Elias Sagmo Larsen's avatar
Elias Sagmo Larsen committed
4
5
6
7

class UserSerializerTestCase(TestCase):

    def setUp(self):
Elias Larsen's avatar
Elias Larsen committed
8
        self.user_model = get_user_model()
Elias Larsen's avatar
Elias Larsen committed
9
10
11
12
13
14
15
16
17
18
19
20
        user = self.user_model(
            username="test",
            email="test@test.com",
            phone_number="12345678",
            country="Norway",
            city="Oslo",
            street_address="address 10"
        )
        password = "password"
        user.set_password(password)
        user.save()
        self.data = {
Elias Larsen's avatar
Elias Larsen committed
21
22
            "username": "testCreate",
            "email": "test@test.com",
Elias Sagmo Larsen's avatar
Elias Sagmo Larsen committed
23
24
            "password": "1",
            "password1": "1",
Elias Larsen's avatar
Elias Larsen committed
25
            "phone_number": "12345678",
Elias Sagmo Larsen's avatar
Elias Sagmo Larsen committed
26
27
            "country": "Norway",
            "city": "Oslo",
Elias Larsen's avatar
Elias Larsen committed
28
            "street_address": "address 10"
Elias Sagmo Larsen's avatar
Elias Sagmo Larsen committed
29
        }
Elias Larsen's avatar
Elias Larsen committed
30
31
32
33
34
35
36
37
38
39
40
41
42
        self.data1 = {
            "username": "testCreate",
            "email": "test@test.com",
            "password": "",
            "password1": "",
            "phone_number": "12345678",
            "country": "Norway",
            "city": "Oslo",
            "street_address": "address 10"
        }

    def test_create(self):
        user_create = UserSerializer.create(UserSerializer(), validated_data=self.data)
Elias Larsen's avatar
Elias Larsen committed
43
        self.assertEqual(self.user_model.objects.get(username="testCreate"), user_create)
Elias Sagmo Larsen's avatar
Elias Sagmo Larsen committed
44
45

    def test_validate_password(self):
Elias Larsen's avatar
Elias Larsen committed
46
47
48
        pw="password"
        response = UserSerializer(data=self.data1).validate_password(value=pw)
        self.assertEqual(response,pw)