This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 329
/
init.py
118 lines (94 loc) · 3.7 KB
/
init.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
import random
from string import letters
from django import test
from django.conf import settings
from django.contrib.auth.models import User
import test_utils
import elasticutils.contrib.django.estestcase as estestcase
from elasticutils.contrib.django import get_es
from apps.users.cron import index_all_profiles
class TestCase(test_utils.TestCase):
"""Tests for common package."""
@classmethod
def setUpClass(cls):
super(TestCase, cls).setUpClass()
cls._AUTHENTICATION_BACKENDS = settings.AUTHENTICATION_BACKENDS
settings.AUTHENTICATION_BACKENDS = ['common.backends.TestBackend']
# Create a Mozillian
cls.mozillian = User.objects.create(
email='u000001@mozillians.org', username='7f3a67u000001')
profile = cls.mozillian.get_profile()
profile.is_vouched = True
profile.full_name='Amandeep McIlrath'
profile.save()
# Create another Mozillian
cls.mozillian2 = User.objects.create(
email='u000002@mozillians.org', username='7f3a67u000002')
profile = cls.mozillian2.get_profile()
profile.is_vouched = True
profile.full_name='Amando Brown'
profile.save()
# Create a non-vouched account
cls.pending = User.objects.create(
email='pending@mozillians.org', username='pending')
pending_profile = cls.pending.get_profile()
pending_profile.full_name='Amanda Younger'
pending_profile.save()
def setUp(self):
# TODO: can this be more elegant?
self.client.get('/')
self.mozillian_client = test.Client()
self.mozillian_client.login(email=self.mozillian.email)
self.mozillian_client2 = test.Client()
self.mozillian_client2.login(email=self.mozillian2.email)
self.pending_client = test.Client()
self.pending_client.login(email=self.pending.email)
@classmethod
def tearDownClass(cls):
super(TestCase, cls).tearDownClass()
settings.AUTHENTICATION_BACKENDS = cls._AUTHENTICATION_BACKENDS
User.objects.all().delete()
class ESTestCase(TestCase, estestcase.ESTestCase):
@classmethod
def setUpClass(cls):
"""Runs the :class:`TestCase` setup to add some data.
Also re-creates indexes respecting data mappings, flushes and
refreshes the data so it's searchable via computer.
"""
estestcase.ESTestCase.setUpClass()
TestCase.setUpClass()
es = get_es()
# Add on demand more models here.
index_all_profiles()
es.flush(refresh=True)
@classmethod
def tearDownClass(cls):
TestCase.tearDownClass()
estestcase.ESTestCase.tearDownClass()
def user(**kwargs):
"""Return a user with all necessary defaults filled in.
Default password is 'testpass' unless you say otherwise in a
kwarg.
"""
save = kwargs.pop('save', True)
is_vouched = kwargs.pop('is_vouched', None)
vouched_by = kwargs.pop('vouched_by', None)
defaults = {}
defaults['username'] = kwargs.get('username',
''.join(random.choice(letters)
for x in xrange(15)))
defaults['email'] = kwargs.get(
'email', ''.join(
random.choice(letters) for x in xrange(10)) + '@example.com')
u = User(**defaults)
u.set_password(kwargs.get('password', 'testpass'))
if save:
u.save()
profile = u.get_profile()
if is_vouched is not None:
profile.is_vouched = is_vouched
if vouched_by is not None:
profile.vouched_by = vouched_by
profile.full_name = kwargs.get('full_name', '')
profile.save()
return u