-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.py
146 lines (122 loc) · 5 KB
/
tests.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import random
import datetime
from django.test import TestCase, Client
from unittest.mock import patch
from django.urls import reverse
from django.contrib.auth.models import User
from core.models import UserProfile
from core.pipeline import save_profile
class UserProfileModelTestCase(TestCase):
"""Test the UserProfile model."""
def setUp(self):
test_user = User.objects.create_user(
username='testuser',
password='12345678'
)
self.test_userprofile = UserProfile.objects.create(
user=test_user,
name='Test User'
)
def test_user_label(self):
self.assertTrue(isinstance(self.test_userprofile.user, User))
self.assertEqual(str(self.test_userprofile.user), 'testuser')
def test_name_label(self):
self.assertTrue(isinstance(self.test_userprofile.name, str))
self.assertEqual(self.test_userprofile.name, 'Test User')
def test_contributions_label(self):
self.assertTrue(isinstance(self.test_userprofile.contributions, int))
self.assertEqual(self.test_userprofile.contributions, 0)
def test_contribution_points_label(self):
self.assertTrue(
isinstance(self.test_userprofile.contribution_points, int)
)
self.assertEqual(self.test_userprofile.contribution_points, 0)
def test_last_updated_label(self):
self.assertTrue(
isinstance(self.test_userprofile.last_updated, datetime.datetime)
)
def test_object_name_is_username(self):
self.assertEqual(self.test_userprofile.user.username,
str(self.test_userprofile))
class SaveProfilePipelineTestCase(TestCase):
"""Test the save_profile pipeline."""
def setUp(self):
self.response = {
'name': "Test User", 'avatar_url': "https://github.com/test"
}
self.test_user = User.objects.create_user(
username='testuser',
password='12345678'
)
@patch('social_core.backends.github.GithubOAuth2')
def test_when_backend_is_not_github(self, mock_backend):
mock_backend.name = ''
save_profile(mock_backend, self.test_user, self.response)
self.assertEqual(len(UserProfile.objects.all()), 0)
@patch('social_core.backends.github.GithubOAuth2')
def test_when_backend_is_github_and_user_not_present(self, mock_backend):
mock_backend.name = 'github'
save_profile(mock_backend, self.test_user, self.response)
self.assertEqual(len(UserProfile.objects.all()), 1)
self.assertEqual(
UserProfile.objects.get(user=self.test_user).name,
"Test User"
)
@patch('social_core.backends.github.GithubOAuth2')
def test_when_backend_is_github_and_user_is_present(self, mock_backend):
mock_backend.name = 'github'
UserProfile.objects.create(user=self.test_user, name="Test User 2")
save_profile(mock_backend, self.test_user, self.response)
self.assertEqual(len(UserProfile.objects.all()), 1)
self.assertEqual(
UserProfile.objects.get(user=self.test_user).name,
"Test User 2"
)
class HomeViewTestCase(TestCase):
"""Test the HomeView."""
@classmethod
def setUpTestData(cls):
cls.client = Client()
cls.test_users = []
for i in range(13):
cls.test_users.append(User.objects.create_user(
username='testuser{}'.format(i+1),
password='12345678'
))
for user in cls.test_users:
UserProfile.objects.create(
user=user,
contributions=random.randint(70, 401),
contribution_points=random.randint(100, 1001)
)
def test_get_request_to_the_view(self):
url = reverse('index')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
def test_users_context(self):
response = self.client.get(reverse('index'))
queryset = response.context['users']
ordered_queryset = UserProfile.objects.all().order_by(
'-contributions', '-contribution_points'
)[:10]
self.assertEqual(len(queryset), 10)
self.assertFalse(response.context['is_authenticated'])
for i in range(len(queryset)):
self.assertEqual(
queryset[i].user.username,
ordered_queryset[i].user.username
)
def test_other_context_when_logged_out(self):
response = self.client.get(reverse('index'))
self.assertFalse(response.context['is_authenticated'])
self.assertEqual(
response.context['current_user'], None
)
def test_other_context_when_logged_in(self):
self.client.login(username='testuser1', password='12345678')
response = self.client.get(reverse('index'))
self.assertTrue(response.context['is_authenticated'])
self.assertEqual(
response.context['current_user'].user.username,
'testuser1'
)