From a325fb7e313fd253f7d8c6031ebb89c3cf186f5f Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sat, 24 Mar 2018 01:48:55 +0100 Subject: [PATCH 1/5] tests: create global fixtures john.doe and jane.doe (#22732) --- tests/conftest.py | 21 +++++++++++ tests/test_notification.py | 93 ++++++++++++++++++---------------------------- 2 files changed, 57 insertions(+), 57 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 6a58cb4..9000d12 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 diff --git a/tests/test_notification.py b/tests/test_notification.py index 26c9ec9..0dbb1b5 100644 --- a/tests/test_notification.py +++ b/tests/test_notification.py @@ -4,7 +4,6 @@ import mock import pytest from decimal import Decimal -from django.contrib.auth.models import User from django.test.client import RequestFactory from django.utils.timezone import timedelta, now from django.core.urlresolvers import reverse @@ -20,28 +19,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 +39,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 +49,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 +85,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 +102,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 +122,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 +142,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 +182,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 +199,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 +230,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 +242,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 +255,7 @@ def test_notification_id_and_origin(user): @mock.patch('combo.apps.lingo.models.requests.get') -def test_notify_remote_items(mock_get, app, user, user2, regie): +def test_notify_remote_items(mock_get, app, john_doe, jane_doe, regie): datetime_format = '%Y-%m-%dT%H:%M:%S' invoice_now = now() @@ -286,7 +265,7 @@ def test_notify_remote_items(mock_get, app, user, user2, regie): FAKE_PENDING_INVOICES = { "data": { - "admin": { + john_doe.username: { "invoices": [ { 'id': '01', @@ -308,7 +287,7 @@ def test_notify_remote_items(mock_get, app, user, user2, regie): } ] }, - 'admin2': { + jane_doe.username: { 'invoices': [ { 'id': '02', @@ -357,8 +336,8 @@ def test_notify_remote_items(mock_get, app, user, user2, regie): cell = ActiveItems(page=page, placeholder='content', order=0) cell.save() - for user in FAKE_PENDING_INVOICES['data']: - for invoice in FAKE_PENDING_INVOICES['data'][user]['invoices']: + for john_doe in FAKE_PENDING_INVOICES['data']: + for invoice in FAKE_PENDING_INVOICES['data'][john_doe]['invoices']: invoice['pay_limit_date'] = new_pay_limit_date # create remind notifications -- 2.14.2