|
| 1 | +from pytest import mark |
| 2 | +from django.urls import reverse |
| 3 | +from conference.api import ApiError |
| 4 | +from conference.models import TALK_STATUS |
| 5 | +from .common_tools import ( |
| 6 | + get_default_conference, |
| 7 | + make_user, |
| 8 | +) |
| 9 | +from .factories import ( |
| 10 | + TalkFactory, |
| 11 | + TalkSpeakerFactory, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +@mark.django_db |
| 16 | +def test_non_post_error(client): |
| 17 | + payload = {'key': 'does not matter'} |
| 18 | + response = client.get(reverse('api:isauth'), payload, |
| 19 | + content_type='Application/json') |
| 20 | + result = response.json() |
| 21 | + assert result['error'] == ApiError.BAD_REQUEST.value |
| 22 | + |
| 23 | + |
| 24 | +@mark.django_db |
| 25 | +def test_non_json_post_error(client): |
| 26 | + payload = {'key': 'does not matter'} |
| 27 | + response = client.post(reverse('api:isauth'), payload) |
| 28 | + result = response.json() |
| 29 | + assert result['error'] == ApiError.BAD_REQUEST.value |
| 30 | + |
| 31 | + |
| 32 | +@mark.django_db |
| 33 | +def test_user_does_not_exist(client): |
| 34 | + payload = { 'email': '[email protected]', 'password': 'hahahaha'} |
| 35 | + response = client.post(reverse('api:isauth'), payload, |
| 36 | + content_type='application/json') |
| 37 | + result = response.json() |
| 38 | + assert result['error'] == ApiError.AUTH_ERROR.value |
| 39 | + |
| 40 | + |
| 41 | +@mark.django_db |
| 42 | +def test_user_password_error(client): |
| 43 | + user = make_user(is_staff=False) |
| 44 | + payload = {'email': user.email, 'password': user.password + 'hahahaha'} |
| 45 | + response = client.post(reverse('api:isauth'), payload, |
| 46 | + content_type='application/json') |
| 47 | + result = response.json() |
| 48 | + assert result['error'] == ApiError.AUTH_ERROR.value |
| 49 | + |
| 50 | + |
| 51 | +@mark.django_db |
| 52 | +def test_non_staff_non_speaker_user_auth_success(client): |
| 53 | + get_default_conference() |
| 54 | + user = make_user(is_staff=False) |
| 55 | + payload = {'email': user.email, 'password': 'password123'} |
| 56 | + response = client.post(reverse('api:isauth'), payload, |
| 57 | + content_type='application/json') |
| 58 | + result = response.json() |
| 59 | + assert result['email'] == user.email |
| 60 | + assert result['first_name'] == user.first_name |
| 61 | + assert result['last_name'] == user.last_name |
| 62 | + assert result['is_staff'] is False |
| 63 | + assert result['is_speaker'] is False |
| 64 | + |
| 65 | + |
| 66 | +@mark.django_db |
| 67 | +def test_staff_non_speaker_user_auth_success(client): |
| 68 | + get_default_conference() |
| 69 | + user = make_user(is_staff=True) |
| 70 | + payload = {'email': user.email, 'password': 'password123'} |
| 71 | + response = client.post(reverse('api:isauth'), payload, |
| 72 | + content_type='application/json') |
| 73 | + result = response.json() |
| 74 | + assert result['email'] == user.email |
| 75 | + assert result['first_name'] == user.first_name |
| 76 | + assert result['last_name'] == user.last_name |
| 77 | + assert result['is_staff'] is True |
| 78 | + assert result['is_speaker'] is False |
| 79 | + |
| 80 | + |
| 81 | +@mark.django_db |
| 82 | +def test_staff_speaker_user_auth_success(client): |
| 83 | + get_default_conference() |
| 84 | + user = make_user(is_staff=True) |
| 85 | + talk = TalkFactory(created_by=user, status=TALK_STATUS.accepted) |
| 86 | + TalkSpeakerFactory(talk=talk, speaker__user=user) |
| 87 | + |
| 88 | + payload = {'email': user.email, 'password': 'password123'} |
| 89 | + response = client.post(reverse('api:isauth'), payload, |
| 90 | + content_type='application/json') |
| 91 | + result = response.json() |
| 92 | + assert result['email'] == user.email |
| 93 | + assert result['first_name'] == user.first_name |
| 94 | + assert result['last_name'] == user.last_name |
| 95 | + assert result['is_staff'] is True |
| 96 | + assert result['is_speaker'] is True |
| 97 | + |
| 98 | + |
| 99 | +@mark.django_db |
| 100 | +def test_staff_proposed_speaker_user_auth_success(client): |
| 101 | + get_default_conference() |
| 102 | + user = make_user(is_staff=True) |
| 103 | + talk = TalkFactory(created_by=user, status=TALK_STATUS.proposed) |
| 104 | + TalkSpeakerFactory(talk=talk, speaker__user=user) |
| 105 | + |
| 106 | + payload = {'email': user.email, 'password': 'password123'} |
| 107 | + response = client.post(reverse('api:isauth'), payload, |
| 108 | + content_type='application/json') |
| 109 | + result = response.json() |
| 110 | + assert result['email'] == user.email |
| 111 | + assert result['first_name'] == user.first_name |
| 112 | + assert result['last_name'] == user.last_name |
| 113 | + assert result['is_staff'] is True |
| 114 | + assert result['is_speaker'] is False |
0 commit comments