-
Notifications
You must be signed in to change notification settings - Fork 344
[ENG-7965] Add v2 confirmation endpoint #11139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Johnetordoff
wants to merge
1
commit into
CenterForOpenScience:feature/pbs-25-10
Choose a base branch
from
Johnetordoff:add-v2-confirmation-endpoint
base: feature/pbs-25-10
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
import pytest | ||
from unittest import mock | ||
|
||
from api.base.settings.defaults import API_BASE | ||
from osf_tests.factories import AuthUserFactory | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestConfirmEmail: | ||
|
||
@pytest.fixture() | ||
def user(self): | ||
return AuthUserFactory() | ||
|
||
@pytest.fixture() | ||
def user_with_email_verification(self, user): | ||
email = '[email protected]' | ||
external_identity = { | ||
'ORCID': { | ||
'0002-0001-0001-0001': 'CREATE', | ||
} | ||
} | ||
token = user.add_unconfirmed_email(email, external_identity=external_identity) | ||
user.external_identity.update(external_identity) | ||
user.save() | ||
return user, token, email | ||
|
||
@pytest.fixture() | ||
def confirm_url(self, user_with_email_verification): | ||
user, _, _ = user_with_email_verification | ||
return f'/{API_BASE}users/{user._id}/confirm/' | ||
|
||
def test_get_not_allowed(self, app, confirm_url): | ||
res = app.get(confirm_url, expect_errors=True) | ||
assert res.status_code == 405 | ||
|
||
def test_post_missing_fields(self, app, confirm_url, user_with_email_verification): | ||
user, _, _ = user_with_email_verification | ||
res = app.post_json_api( | ||
confirm_url, | ||
{'data': {'attributes': {}}}, | ||
expect_errors=True, | ||
auth=user.auth | ||
) | ||
assert res.status_code == 400 | ||
assert res.json['errors'] == [ | ||
{ | ||
'source': { | ||
'pointer': '/data/attributes/uid' | ||
}, | ||
'detail': 'This field is required.' | ||
}, | ||
{ | ||
'source': { | ||
'pointer': '/data/attributes/destination' | ||
}, | ||
'detail': 'This field is required.' | ||
}, | ||
{ | ||
'source': { | ||
'pointer': '/data/attributes/token' | ||
}, | ||
'detail': 'This field is required.' | ||
} | ||
] | ||
|
||
def test_post_user_not_found(self, app, user_with_email_verification): | ||
user, _, _ = user_with_email_verification | ||
fake_user_id = 'abcd1' | ||
res = app.post_json_api( | ||
f'/{API_BASE}users/{fake_user_id}/confirm/', | ||
{ | ||
'data': { | ||
'attributes': { | ||
'uid': fake_user_id, | ||
'token': 'doesnotmatter', | ||
'destination': 'doesnotmatter', | ||
} | ||
} | ||
}, | ||
expect_errors=True | ||
) | ||
assert res.status_code == 400 | ||
assert 'user not found' in res.json['errors'][0]['detail'].lower() | ||
|
||
def test_post_invalid_token(self, app, confirm_url, user_with_email_verification): | ||
user, _, _ = user_with_email_verification | ||
res = app.post_json_api( | ||
confirm_url, | ||
{ | ||
'data': { | ||
'attributes': { | ||
'uid': user._id, | ||
'token': 'badtoken', | ||
'destination': 'doesnotmatter', | ||
} | ||
} | ||
}, | ||
expect_errors=True | ||
) | ||
assert res.status_code == 400 | ||
assert res.json['errors'] == [{'detail': 'Invalid or expired token.'}] | ||
|
||
def test_post_provider_mismatch(self, app, confirm_url, user_with_email_verification): | ||
user, token, _ = user_with_email_verification | ||
user.external_identity = {} # clear the valid provider | ||
user.save() | ||
|
||
res = app.post_json_api( | ||
confirm_url, | ||
{ | ||
'data': { | ||
'attributes': { | ||
'uid': user._id, | ||
'token': token, | ||
'destination': 'doesnotmatter', | ||
} | ||
} | ||
}, | ||
expect_errors=True | ||
) | ||
assert res.status_code == 400 | ||
assert 'provider mismatch' in res.json['errors'][0]['detail'].lower() | ||
|
||
@mock.patch('website.mails.send_mail') | ||
def test_post_success_create(self, mock_send_mail, app, confirm_url, user_with_email_verification): | ||
user, token, email = user_with_email_verification | ||
user.is_registered = False | ||
user.save() | ||
res = app.post_json_api( | ||
confirm_url, | ||
{ | ||
'data': { | ||
'attributes': { | ||
'uid': user._id, | ||
'token': token, | ||
'destination': 'doesnotmatter', | ||
} | ||
} | ||
}, | ||
expect_errors=True | ||
) | ||
assert res.status_code == 302 | ||
import urllib.parse | ||
|
||
# Extract and decode the service parameter | ||
location = res.headers['Location'] | ||
parsed_url = urllib.parse.urlparse(location) | ||
query = urllib.parse.parse_qs(parsed_url.query) | ||
service = query.get('service', [None])[0] | ||
|
||
assert service is not None | ||
decoded_service = urllib.parse.unquote(service) | ||
assert '&new=true' in decoded_service | ||
|
||
assert not mock_send_mail.called | ||
|
||
user.reload() | ||
assert user.is_registered | ||
assert token not in user.email_verifications | ||
assert user.external_identity == {'ORCID': {'0002-0001-0001-0001': 'VERIFIED'}} | ||
assert user.emails.filter(address=email.lower()).exists() | ||
|
||
@mock.patch('website.mails.send_mail') | ||
def test_post_success_link(self, mock_send_mail, app, confirm_url, user_with_email_verification): | ||
import urllib.parse | ||
|
||
user, token, email = user_with_email_verification | ||
user.external_identity['ORCID']['0000-0000-0000-0000'] = 'LINK' | ||
user.save() | ||
|
||
res = app.post_json_api( | ||
confirm_url, | ||
{ | ||
'data': { | ||
'attributes': { | ||
'uid': user._id, | ||
'token': token, | ||
'destination': 'doesnotmatter' | ||
} | ||
} | ||
}, | ||
expect_errors=True | ||
) | ||
assert res.status_code == 302 | ||
|
||
# Decode the redirect URL and check that &new=true is NOT present | ||
location = res.headers['Location'] | ||
parsed_url = urllib.parse.urlparse(location) | ||
query = urllib.parse.parse_qs(parsed_url.query) | ||
service = query.get('service', [None])[0] | ||
|
||
assert service is not None | ||
decoded_service = urllib.parse.unquote(service) | ||
assert '&new=true' not in decoded_service | ||
|
||
assert mock_send_mail.called | ||
|
||
user.reload() | ||
assert user.external_identity['ORCID']['0000-0000-0000-0000'] == 'VERIFIED' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor because it's now shared