1
|
import pytest
|
2
|
import django_webtest
|
3
|
import copy
|
4
|
import tempfile
|
5
|
import os
|
6
|
import json
|
7
|
import shutil
|
8
|
|
9
|
import django.core.mail.backends.locmem
|
10
|
from django.core.mail.backends.locmem import EmailBackend as DjangoEmailBackend
|
11
|
from django.utils.text import slugify
|
12
|
|
13
|
from corbo.models import Category, Announce, Broadcast, Subscription
|
14
|
|
15
|
CATEGORIES = ('Alerts', 'News')
|
16
|
|
17
|
SUBSCRIBERS = [{'uuid': 'uuid1', 'email': 'foo@example.net', 'mobile': '0102030405'},
|
18
|
{'uuid': 'uuid2', 'email': 'bar@example.net', 'mobile': '0607080900'},
|
19
|
{'uuid': '', 'email': 'john@example.net', 'mobile': '0304050607'},
|
20
|
{'uuid': None, 'email': 'john2@example.net', 'mobile': '0405060708'}]
|
21
|
|
22
|
|
23
|
|
24
|
class MockedEmailBackend(object):
|
25
|
|
26
|
def create_email_backend(self, *args, **kwargs):
|
27
|
class EmailBackend(DjangoEmailBackend):
|
28
|
def __init__(self, *args, **kwargs):
|
29
|
super(DjangoEmailBackend, self).__init__(*args, **kwargs)
|
30
|
|
31
|
def send_messages(self, messages):
|
32
|
new_messages = []
|
33
|
for message in messages:
|
34
|
new_messages.append(copy.deepcopy(message))
|
35
|
return super(EmailBackend, self).send_messages(new_messages)
|
36
|
|
37
|
return EmailBackend
|
38
|
|
39
|
def __enter__(self):
|
40
|
import django.core.mail.backends.locmem
|
41
|
self.locmem_emailbackend = django.core.mail.backends.locmem.EmailBackend
|
42
|
django.core.mail.backends.locmem.EmailBackend = self.create_email_backend()
|
43
|
return self
|
44
|
|
45
|
def __exit__(self, *args, **kwargs):
|
46
|
django.core.mail.backends.locmem.EmailBackend = self.locmem_emailbackend
|
47
|
|
48
|
|
49
|
@pytest.fixture
|
50
|
def custom_mailoutbox():
|
51
|
with MockedEmailBackend() as mock:
|
52
|
yield mock
|
53
|
|
54
|
@pytest.fixture
|
55
|
def app(request):
|
56
|
wtm = django_webtest.WebTestMixin()
|
57
|
wtm._patch_settings()
|
58
|
request.addfinalizer(wtm._unpatch_settings)
|
59
|
return django_webtest.DjangoTestApp()
|
60
|
|
61
|
|
62
|
@pytest.fixture
|
63
|
def categories():
|
64
|
categories = []
|
65
|
for category in CATEGORIES:
|
66
|
c, created = Category.objects.get_or_create(name=category, slug=slugify(category))
|
67
|
categories.append(c)
|
68
|
return categories
|
69
|
|
70
|
|
71
|
@pytest.fixture
|
72
|
def subscriptions(categories):
|
73
|
subscriptions = []
|
74
|
for category in categories:
|
75
|
for subscriber in SUBSCRIBERS:
|
76
|
kwargs = {'category': category, 'identifier': 'mailto:%(email)s' % subscriber}
|
77
|
uuid = subscriber['uuid']
|
78
|
if uuid is not None:
|
79
|
kwargs['uuid'] = uuid
|
80
|
subscriptions.append(Subscription.objects.create(**kwargs))
|
81
|
kwargs['identifier'] = 'sms:%(mobile)s' % subscriber
|
82
|
subscriptions.append(Subscription.objects.create(**kwargs))
|
83
|
return subscriptions
|
84
|
|
85
|
|
86
|
@pytest.fixture
|
87
|
def tenant_base(request, settings):
|
88
|
base = tempfile.mkdtemp('corbo-tenant-base')
|
89
|
settings.TENANT_BASE = base
|
90
|
|
91
|
def fin():
|
92
|
shutil.rmtree(base)
|
93
|
request.addfinalizer(fin)
|
94
|
return base
|
95
|
|
96
|
|
97
|
@pytest.fixture(scope='function')
|
98
|
def tenant(transactional_db, request, tenant_base):
|
99
|
from hobo.multitenant.models import Tenant
|
100
|
base = tenant_base
|
101
|
|
102
|
@pytest.mark.django_db
|
103
|
def make_tenant(name):
|
104
|
tenant_dir = os.path.join(base, name)
|
105
|
os.mkdir(tenant_dir)
|
106
|
with open(os.path.join(tenant_dir, 'hobo.json'), 'w') as fd:
|
107
|
json.dump({
|
108
|
'variables': {
|
109
|
'hobo_test_variable': True,
|
110
|
'other_variable': 'foo',
|
111
|
},
|
112
|
'services': [
|
113
|
{'slug': 'test',
|
114
|
'service-id': 'corbo',
|
115
|
'title': 'Test',
|
116
|
'this': True,
|
117
|
'secret_key': '12345',
|
118
|
'base_url': 'http://%s' % name,
|
119
|
'saml-sp-metadata-url': 'http://%s/metadata/' % name,
|
120
|
'variables': {
|
121
|
'other_variable': 'bar',
|
122
|
}
|
123
|
},
|
124
|
{'slug': 'passerelle',
|
125
|
'title': 'Webserivces',
|
126
|
'service-id': 'passerelle',
|
127
|
'secret_key': 'abcdef',
|
128
|
'base_url': 'http://passerelle.example.net',
|
129
|
'saml-sp-metadata-url': 'http://passerelle.example.net/metadata/' },
|
130
|
]}, fd)
|
131
|
return Tenant(domain_url=name,
|
132
|
schema_name=name.replace('-', '_').replace('.', '_'))
|
133
|
return make_tenant('corbo.example.net')
|
134
|
|
135
|
|
136
|
@pytest.fixture(params=['06 10 20 30 40', '+33 6 10 20 30 40',
|
137
|
'0033610203040', '+33 (0) 6 10 20 30 40', '0033+610203040+'])
|
138
|
def phonenumber(request):
|
139
|
return request.param
|