tests: create global fixtures john.doe and jane.doe (#22732)

This commit is contained in:
Benjamin Dauvergne 2018-03-24 01:48:55 +01:00 committed by Frédéric Péters
parent 8fdd36ee31
commit 4bb382806b
2 changed files with 56 additions and 54 deletions

View File

@ -1,10 +1,31 @@
import pytest
from django.contrib.auth.models import User
import django_webtest
@pytest.fixture
def app(request):
wtm = django_webtest.WebTestMixin()
wtm._patch_settings()
request.addfinalizer(wtm._unpatch_settings)
return django_webtest.DjangoTestApp()
@pytest.fixture
def john_doe():
try:
user = User.objects.get(username='john.doe')
except User.DoesNotExist:
user = User.objects.create_user('john.doe', email='john.doe@example.com', password='john.doe')
return user
@pytest.fixture
def jane_doe():
try:
admin2 = User.objects.get(username='jane.doe')
except User.DoesNotExist:
admin2 = User.objects.create_user('jane.doe', email='jane.doe@example.com', password='jane.doe')
return admin2

View File

@ -20,28 +20,8 @@ pytestmark = pytest.mark.django_db
client = Client()
@pytest.fixture
def user():
try:
admin = User.objects.get(username='admin')
except User.DoesNotExist:
admin = User.objects.create_user('admin', email=None, password='admin')
admin.email = 'admin@example.net'
admin.save()
return admin
@pytest.fixture
def user2():
try:
admin2 = User.objects.get(username='admin2')
except User.DoesNotExist:
admin2 = User.objects.create_user('admin2', email=None, password='admin2')
return admin2
def login(username='admin', password='admin'):
resp = client.post('/login/', {'username': username, 'password': password})
def login(user):
resp = client.post('/login/', {'username': user.username, 'password': user.username})
assert resp.status_code == 302
@ -60,8 +40,8 @@ def regie():
return regie
def test_notification_api(user, user2):
notification = Notification.notify(user, 'notifoo')
def test_notification_api(john_doe, jane_doe):
notification = Notification.notify(john_doe, 'notifoo')
assert Notification.objects.count() == 1
assert notification.summary == 'notifoo'
assert notification.body == ''
@ -70,33 +50,33 @@ def test_notification_api(user, user2):
assert notification.external_id is None
assert notification.end_timestamp - notification.start_timestamp == timedelta(3)
assert notification.acked is False
Notification.objects.visible(user).ack()
Notification.objects.visible(john_doe).ack()
assert Notification.objects.get().acked is True
Notification.notify(user, 'notirefoo', id=str(notification.pk), acked=False)
Notification.notify(john_doe, 'notirefoo', id=str(notification.pk), acked=False)
assert Notification.objects.count() == 1
assert Notification.objects.get().summary == 'notirefoo'
# we updated the notification, it's un-acked
assert Notification.objects.get().acked is False
Notification.notify(user, 'notirefoo', id=str(notification.pk), duration=3600)
Notification.notify(john_doe, 'notirefoo', id=str(notification.pk), duration=3600)
noti = Notification.objects.get()
assert noti.end_timestamp - noti.start_timestamp == timedelta(seconds=3600)
notification = Notification.notify(user, 'notibar', id='ns:notibar')
notification = Notification.notify(john_doe, 'notibar', id='ns:notibar')
assert Notification.objects.count() == 2
notification = Notification.notify(user, 'notirebar', id='ns:notibar')
notification = Notification.notify(john_doe, 'notirebar', id='ns:notibar')
assert Notification.objects.count() == 2
notification = Notification.notify(user2, 'notiother')
notification = Notification.notify(jane_doe, 'notiother')
notification.forget()
assert Notification.objects.filter(user=user2).count() == 1
notification = Notification.objects.filter(user=user2).get()
assert Notification.objects.filter(user=jane_doe).count() == 1
notification = Notification.objects.filter(user=jane_doe).get()
assert notification.end_timestamp < now()
assert notification.acked is True
def test_notification_cell(user, user2):
def test_notification_cell(john_doe, jane_doe):
page = Page(title='notif', slug='test_notification_cell', template_name='standard')
page.save()
cell = NotificationsCell(page=page, placeholder='content', order=0)
@ -106,12 +86,12 @@ def test_notification_cell(user, user2):
context['request'].user = None
assert cell.is_visible(context['request'].user) is False
context['request'].user = user
context['request'].user = john_doe
assert cell.is_visible(context['request'].user) is True
assert cell.get_badge(context) is None
notification1 = Notification.notify(user, 'notibar')
notification2 = Notification.notify(user, 'notifoo')
notification1 = Notification.notify(john_doe, 'notibar')
notification2 = Notification.notify(john_doe, 'notifoo')
content = cell.render(context)
assert 'notibar' in content
assert 'notifoo' in content
@ -123,17 +103,17 @@ def test_notification_cell(user, user2):
assert 'notifoo' not in content
assert cell.get_badge(context) == {'badge': '1'}
Notification.notify(user, 'notirebar', id=str(notification1.pk))
Notification.notify(john_doe, 'notirebar', id=str(notification1.pk))
content = cell.render(context)
assert 'notirebar' in content
assert 'notibar' not in content
Notification.notify(user, 'notiurl', id=str(notification1.pk), url='https://www.example.net/')
Notification.notify(john_doe, 'notiurl', id=str(notification1.pk), url='https://www.example.net/')
content = cell.render(context)
assert 'notiurl' in content
assert 'https://www.example.net/' in content
notification3 = Notification.notify(user, 'ackme')
notification3 = Notification.notify(john_doe, 'ackme')
notification3.ack()
content = cell.render(context)
assert 'acked' in content
@ -143,19 +123,19 @@ def test_notification_cell(user, user2):
content = cell.render(context)
assert cell.get_badge(context) is None
Notification.notify(user2, 'notiother')
Notification.notify(jane_doe, 'notiother')
content = cell.render(context)
assert 'notiurl' in content
assert 'notiother' not in content
assert cell.get_badge(context) is None
context['request'].user = user2
context['request'].user = jane_doe
content = cell.render(context)
assert 'notiurl' not in content
assert 'notiother' in content
assert cell.get_badge(context) == {'badge': '1'}
def test_notification_ws(user):
def test_notification_ws(john_doe):
def notify(data, check_id, count):
resp = client.post(reverse('api-notification-add'), json.dumps(data),
@ -163,10 +143,10 @@ def test_notification_ws(user):
assert resp.status_code == 200
result = json.loads(resp.content)
assert result == {'data': {'id': check_id}, 'err': 0}
assert Notification.objects.filter(user=user).count() == count
return Notification.objects.find(user, check_id).get()
assert Notification.objects.filter(user=john_doe).count() == count
return Notification.objects.find(john_doe, check_id).get()
login()
login(john_doe)
notify({'summary': 'foo'}, '1', 1)
notify({'summary': 'bar'}, '2', 2)
notify({'summary': 'bar', 'id': 'ns:noti3'}, 'ns:noti3', 3)
@ -203,13 +183,13 @@ def test_notification_ws(user):
resp = client.get(reverse('api-notification-forget', kwargs={'notification_id': '5'}))
assert resp.status_code == 200
assert Notification.objects.filter(acked=True).count() == 2
notif = Notification.objects.find(user, '5').get()
notif = Notification.objects.find(john_doe, '5').get()
assert notif.public_id == '5'
assert notif.acked is True
assert notif.end_timestamp < now()
def test_notification_ws_badrequest(user):
def test_notification_ws_badrequest(john_doe):
def check_error(data, message):
resp = client.post(reverse('api-notification-add'),
@ -220,7 +200,7 @@ def test_notification_ws_badrequest(user):
assert result['err'] == 1
assert message in result['err_desc'].values()[0][0]
login()
login(john_doe)
check_error(None, 'required')
check_error('blahblah', 'Invalid data')
check_error({'summary': ''}, 'may not be blank')
@ -251,8 +231,8 @@ def test_notification_ws_check_urls():
kwargs={'notification_id': 'noti1'}) == '/api/notification/forget/noti1/'
def test_notification_id_and_origin(user):
login()
def test_notification_id_and_origin(john_doe):
login(john_doe)
def notify(data):
resp = client.post(reverse('api-notification-add'), json.dumps(data),
@ -263,7 +243,7 @@ def test_notification_id_and_origin(user):
result = notify({'summary': 'foo', 'id': '1'})
assert result['err'] == 1
notification = Notification.notify(user, 'foo')
notification = Notification.notify(john_doe, 'foo')
result = notify({'summary': 'foo', 'id': str(notification.id)})
assert result['err'] == 0
@ -276,7 +256,8 @@ def test_notification_id_and_origin(user):
@mock.patch('combo.utils.requests_wrapper.RequestsSession.request')
def test_notify_remote_items(mock_get, app, user, user2, regie, monkeypatch):
def test_notify_remote_items(mock_get, app, john_doe, jane_doe, regie, monkeypatch):
datetime_format = '%Y-%m-%dT%H:%M:%S'
invoice_now = now()
creation_date = (invoice_now - timedelta(days=1)).date().isoformat()
pay_limit_date = (invoice_now + timedelta(days=20)).date().isoformat()
@ -284,7 +265,7 @@ def test_notify_remote_items(mock_get, app, user, user2, regie, monkeypatch):
FAKE_PENDING_INVOICES = {
"data":
{
"admin": {
john_doe.username: {
"invoices": [
{
'id': '01',
@ -306,7 +287,7 @@ def test_notify_remote_items(mock_get, app, user, user2, regie, monkeypatch):
}
]
},
'admin2': {
jane_doe.username: {
'invoices': [
{
'id': '02',