1
|
import pytest
|
2
|
from webtest import TestApp, Upload
|
3
|
|
4
|
from django.core.wsgi import get_wsgi_application
|
5
|
from django.utils.text import slugify
|
6
|
from django.core.urlresolvers import reverse
|
7
|
from django.contrib.auth import get_user_model
|
8
|
|
9
|
from corbo.models import Category, Subscription
|
10
|
|
11
|
pytestmark = pytest.mark.django_db
|
12
|
|
13
|
CATEGORIES = (u'Alerts',)
|
14
|
|
15
|
CSV_CONTENT = """foo@example.net, Foo,
|
16
|
john.doe@example.net, John Doe,
|
17
|
bar@localhost, Bar,
|
18
|
"""
|
19
|
|
20
|
|
21
|
@pytest.fixture
|
22
|
def categories():
|
23
|
categories = []
|
24
|
for category in CATEGORIES:
|
25
|
c, created = Category.objects.get_or_create(name=category, slug=slugify(category))
|
26
|
categories.append(c)
|
27
|
return categories
|
28
|
|
29
|
@pytest.fixture
|
30
|
def admin():
|
31
|
User = get_user_model()
|
32
|
admin = User.objects.create_superuser(username='admin',
|
33
|
password='password', email='admin@example.net')
|
34
|
return admin
|
35
|
|
36
|
def login(app, username='admin', password='password'):
|
37
|
login_page = app.get('/login/')
|
38
|
login_form = login_page.forms[0]
|
39
|
login_form['username'] = username
|
40
|
login_form['password'] = password
|
41
|
resp = login_form.submit()
|
42
|
assert resp.status_int == 302
|
43
|
return app
|
44
|
|
45
|
def test_subscribe_from_csv(admin, categories):
|
46
|
app = login(TestApp(get_wsgi_application()))
|
47
|
for c in categories:
|
48
|
page = app.get(reverse('subscriptions-import', kwargs={'slug': c.slug}))
|
49
|
form = page.form
|
50
|
form['subscribers'] = Upload('users.csv', CSV_CONTENT)
|
51
|
res = form.submit()
|
52
|
assert res.status_code == 302
|
53
|
assert Subscription.objects.filter(category=c).count() == len(CSV_CONTENT.splitlines())
|
54
|
|
55
|
def test_subscribe_from_csv_with_empty_lines(admin, categories):
|
56
|
app = login(TestApp(get_wsgi_application()))
|
57
|
content = CSV_CONTENT + '\n\n\n'
|
58
|
for c in categories:
|
59
|
page = app.get(reverse('subscriptions-import', kwargs={'slug': c.slug}))
|
60
|
form = page.form
|
61
|
form['subscribers'] = Upload('users.csv', content)
|
62
|
res = form.submit()
|
63
|
assert res.status_code == 302
|
64
|
assert Subscription.objects.filter(category=c).count() == len(CSV_CONTENT.splitlines())
|
65
|
|
66
|
def test_subscribe_with_invalid_email(admin, categories):
|
67
|
app = login(TestApp(get_wsgi_application()))
|
68
|
content = CSV_CONTENT + '\nwrong, Wrong user,'
|
69
|
for category in categories:
|
70
|
page = app.get(reverse('subscriptions-import', kwargs={'slug': category.slug}))
|
71
|
form = page.form
|
72
|
form['subscribers'] = Upload('users.csv', content)
|
73
|
page = form.submit()
|
74
|
assert page.status_code == 200
|
75
|
page.mustcontain('Invalid email address at line')
|