|
| 1 | +import pytest |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +from api.base.settings.defaults import API_BASE |
| 5 | +from osf_tests.factories import AuthUserFactory |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.django_db |
| 9 | +class TestConfirmEmail: |
| 10 | + |
| 11 | + @pytest.fixture() |
| 12 | + def user(self): |
| 13 | + return AuthUserFactory() |
| 14 | + |
| 15 | + @pytest.fixture() |
| 16 | + def user_with_email_verification(self, user): |
| 17 | + |
| 18 | + external_identity = { |
| 19 | + 'ORCID': { |
| 20 | + '0002-0001-0001-0001': 'CREATE', |
| 21 | + } |
| 22 | + } |
| 23 | + token = user.add_unconfirmed_email(email, external_identity=external_identity) |
| 24 | + user.external_identity.update(external_identity) |
| 25 | + user.save() |
| 26 | + return user, token, email |
| 27 | + |
| 28 | + @pytest.fixture() |
| 29 | + def confirm_url(self, user_with_email_verification): |
| 30 | + user, _, _ = user_with_email_verification |
| 31 | + return f'/{API_BASE}users/{user._id}/confirm/' |
| 32 | + |
| 33 | + def test_get_not_allowed(self, app, confirm_url): |
| 34 | + res = app.get(confirm_url, expect_errors=True) |
| 35 | + assert res.status_code == 405 |
| 36 | + |
| 37 | + def test_post_missing_fields(self, app, confirm_url, user_with_email_verification): |
| 38 | + user, _, _ = user_with_email_verification |
| 39 | + res = app.post_json_api( |
| 40 | + confirm_url, |
| 41 | + {'data': {'attributes': {}}}, |
| 42 | + expect_errors=True, |
| 43 | + auth=user.auth |
| 44 | + ) |
| 45 | + assert res.status_code == 400 |
| 46 | + assert res.json['errors'] == [ |
| 47 | + { |
| 48 | + 'source': { |
| 49 | + 'pointer': '/data/attributes/uid' |
| 50 | + }, |
| 51 | + 'detail': 'This field is required.' |
| 52 | + }, |
| 53 | + { |
| 54 | + 'source': { |
| 55 | + 'pointer': '/data/attributes/destination' |
| 56 | + }, |
| 57 | + 'detail': 'This field is required.' |
| 58 | + }, |
| 59 | + { |
| 60 | + 'source': { |
| 61 | + 'pointer': '/data/attributes/token' |
| 62 | + }, |
| 63 | + 'detail': 'This field is required.' |
| 64 | + } |
| 65 | + ] |
| 66 | + |
| 67 | + def test_post_user_not_found(self, app, user_with_email_verification): |
| 68 | + user, _, _ = user_with_email_verification |
| 69 | + fake_user_id = 'abcd1' |
| 70 | + res = app.post_json_api( |
| 71 | + f'/{API_BASE}users/{fake_user_id}/confirm/', |
| 72 | + { |
| 73 | + 'data': { |
| 74 | + 'attributes': { |
| 75 | + 'uid': fake_user_id, |
| 76 | + 'token': 'doesnotmatter', |
| 77 | + 'destination': 'doesnotmatter', |
| 78 | + } |
| 79 | + } |
| 80 | + }, |
| 81 | + expect_errors=True |
| 82 | + ) |
| 83 | + assert res.status_code == 400 |
| 84 | + assert 'user not found' in res.json['errors'][0]['detail'].lower() |
| 85 | + |
| 86 | + def test_post_invalid_token(self, app, confirm_url, user_with_email_verification): |
| 87 | + user, _, _ = user_with_email_verification |
| 88 | + res = app.post_json_api( |
| 89 | + confirm_url, |
| 90 | + { |
| 91 | + 'data': { |
| 92 | + 'attributes': { |
| 93 | + 'uid': user._id, |
| 94 | + 'token': 'badtoken', |
| 95 | + 'destination': 'doesnotmatter', |
| 96 | + } |
| 97 | + } |
| 98 | + }, |
| 99 | + expect_errors=True |
| 100 | + ) |
| 101 | + assert res.status_code == 400 |
| 102 | + assert res.json['errors'] == [{'detail': 'Invalid or expired token.'}] |
| 103 | + |
| 104 | + def test_post_provider_mismatch(self, app, confirm_url, user_with_email_verification): |
| 105 | + user, token, _ = user_with_email_verification |
| 106 | + user.external_identity = {} # clear the valid provider |
| 107 | + user.save() |
| 108 | + |
| 109 | + res = app.post_json_api( |
| 110 | + confirm_url, |
| 111 | + { |
| 112 | + 'data': { |
| 113 | + 'attributes': { |
| 114 | + 'uid': user._id, |
| 115 | + 'token': token, |
| 116 | + 'destination': 'doesnotmatter', |
| 117 | + } |
| 118 | + } |
| 119 | + }, |
| 120 | + expect_errors=True |
| 121 | + ) |
| 122 | + assert res.status_code == 400 |
| 123 | + assert 'provider mismatch' in res.json['errors'][0]['detail'].lower() |
| 124 | + |
| 125 | + @mock.patch('website.mails.send_mail') |
| 126 | + def test_post_success_create(self, mock_send_mail, app, confirm_url, user_with_email_verification): |
| 127 | + user, token, email = user_with_email_verification |
| 128 | + user.is_registered = False |
| 129 | + user.save() |
| 130 | + res = app.post_json_api( |
| 131 | + confirm_url, |
| 132 | + { |
| 133 | + 'data': { |
| 134 | + 'attributes': { |
| 135 | + 'uid': user._id, |
| 136 | + 'token': token, |
| 137 | + 'destination': 'doesnotmatter', |
| 138 | + } |
| 139 | + } |
| 140 | + }, |
| 141 | + expect_errors=True |
| 142 | + ) |
| 143 | + assert res.status_code == 302 |
| 144 | + import urllib.parse |
| 145 | + |
| 146 | + # Extract and decode the service parameter |
| 147 | + location = res.headers['Location'] |
| 148 | + parsed_url = urllib.parse.urlparse(location) |
| 149 | + query = urllib.parse.parse_qs(parsed_url.query) |
| 150 | + service = query.get('service', [None])[0] |
| 151 | + |
| 152 | + assert service is not None |
| 153 | + decoded_service = urllib.parse.unquote(service) |
| 154 | + assert '&new=true' in decoded_service |
| 155 | + |
| 156 | + assert not mock_send_mail.called |
| 157 | + |
| 158 | + user.reload() |
| 159 | + assert user.is_registered |
| 160 | + assert token not in user.email_verifications |
| 161 | + assert user.external_identity == {'ORCID': {'0002-0001-0001-0001': 'VERIFIED'}} |
| 162 | + assert user.emails.filter(address=email.lower()).exists() |
| 163 | + |
| 164 | + @mock.patch('website.mails.send_mail') |
| 165 | + def test_post_success_link(self, mock_send_mail, app, confirm_url, user_with_email_verification): |
| 166 | + import urllib.parse |
| 167 | + |
| 168 | + user, token, email = user_with_email_verification |
| 169 | + user.external_identity['ORCID']['0000-0000-0000-0000'] = 'LINK' |
| 170 | + user.save() |
| 171 | + |
| 172 | + res = app.post_json_api( |
| 173 | + confirm_url, |
| 174 | + { |
| 175 | + 'data': { |
| 176 | + 'attributes': { |
| 177 | + 'uid': user._id, |
| 178 | + 'token': token, |
| 179 | + 'destination': 'doesnotmatter' |
| 180 | + } |
| 181 | + } |
| 182 | + }, |
| 183 | + expect_errors=True |
| 184 | + ) |
| 185 | + assert res.status_code == 302 |
| 186 | + |
| 187 | + # Decode the redirect URL and check that &new=true is NOT present |
| 188 | + location = res.headers['Location'] |
| 189 | + parsed_url = urllib.parse.urlparse(location) |
| 190 | + query = urllib.parse.parse_qs(parsed_url.query) |
| 191 | + service = query.get('service', [None])[0] |
| 192 | + |
| 193 | + assert service is not None |
| 194 | + decoded_service = urllib.parse.unquote(service) |
| 195 | + assert '&new=true' not in decoded_service |
| 196 | + |
| 197 | + assert mock_send_mail.called |
| 198 | + |
| 199 | + user.reload() |
| 200 | + assert user.external_identity['ORCID']['0000-0000-0000-0000'] == 'VERIFIED' |
0 commit comments