-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from fjelltopp/development
Production deployment
- Loading branch information
Showing
4 changed files
with
103 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -4,60 +4,54 @@ | |
import ckan.tests.factories | ||
import ckanext.emailasusername.authenticator as authenticator | ||
import pytest | ||
import mock | ||
from ckan.logic import ValidationError | ||
|
||
|
||
@pytest.mark.usefixtures('clean_db', 'with_plugins') | ||
@pytest.mark.usefixtures('mail_server') | ||
class TestAuthenticator(object): | ||
|
||
@mock.patch('ckanext.emailasusername.blueprint.h.flash_error') | ||
def test_authenticate(self, flash_mock): | ||
@pytest.mark.parametrize("identity, existing_user, result", [ | ||
({}, False, None), | ||
({'login': 'tester', 'password': 'RandomPassword123'}, True, 'tester'), | ||
({'login': '[email protected]', 'password': 'RandomPassword123'}, True, 'tester'), | ||
({'login': '[email protected]', 'password': 'RandomPassword123'}, True, None), | ||
({'login': 'testerwrong', 'password': 'RandomPassword123'}, True, None), | ||
({'login': '[email protected]', 'password': 'wrongpassword'}, True, None) | ||
], ids=[ | ||
"Empty credentials", | ||
"Correct credentials with username", | ||
"Correct credentials with email", | ||
"Incorrect email", | ||
"Incorrect username", | ||
"Incorrect password" | ||
]) | ||
def test_authenticate(self, existing_user, identity, result): | ||
auth = authenticator.EmailAsUsernameAuthenticator() | ||
if existing_user: | ||
ckan.tests.factories.User( | ||
name='tester', | ||
email='[email protected]', | ||
password='RandomPassword123' | ||
) | ||
assert auth.authenticate({}, identity) == result | ||
|
||
def test_email_authentication_fails_if_multiple_accounts_share_email(self): | ||
auth = authenticator.EmailAsUsernameAuthenticator() | ||
response = auth.authenticate({}, {}) | ||
assert response is None | ||
identity = {'login': 'tester', 'password': 'RandomPassword123'} | ||
ckan.tests.factories.User( | ||
name=identity['login'], | ||
name='tester1', | ||
email='[email protected]', | ||
password=identity['password'] | ||
password='RandomPassword123' | ||
) | ||
|
||
# Test that a correct login returns the username | ||
response = auth.authenticate({}, identity) | ||
assert response == 'tester' | ||
|
||
# Test that a correct email based login returns the username | ||
identity['login'] = '[email protected]' | ||
response = auth.authenticate({}, identity) | ||
assert response == 'tester' | ||
|
||
# Test that an incorrect email based login fails login | ||
identity['login'] = '[email protected]' | ||
response = auth.authenticate({}, identity) | ||
assert response is None | ||
flash_mock.assert_not_called() | ||
|
||
# Test that login fails when two accounts registered with email exists | ||
identity = {'login': 'tester2', 'password': 'RandomPassword123'} | ||
email = '[email protected]' | ||
try: | ||
ckan.tests.factories.User( | ||
name=identity['login'], | ||
email=email, | ||
password=identity['password'] | ||
name="tester2", | ||
email="[email protected]", | ||
password="RandomPassword123" | ||
) | ||
identity['login'] = email | ||
identity = {'login': '[email protected]', 'password': 'RandomPassword123'} | ||
response = auth.authenticate({}, identity) | ||
assert response is None | ||
flash_mock.assert_not_called() | ||
except ValidationError as e: | ||
# CKAN 2.9 does not allow users to have identical emails | ||
assert e.error_summary['Email'] == 'The email address \'{}\' belongs to a registered user.'.format(email) | ||
|
||
# Test that an incorrect password fails login | ||
identity['password'] += '!' | ||
response = auth.authenticate({}, identity) | ||
assert response is None | ||
flash_mock.assert_not_called() | ||
assert "Email" in e.error_summary |
This file contains 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 |
---|---|---|
@@ -1,14 +1,15 @@ | ||
"""Tests for plugin.py.""" | ||
import ckan.plugins | ||
from ckan.plugins import toolkit | ||
import ckan.model | ||
import ckan.logic.schema | ||
import ckan.tests.factories | ||
from ckan.tests.factories import User | ||
import ckan.tests.helpers | ||
from ckan.lib.helpers import url_for | ||
import ckanext.emailasusername.plugin as plugin | ||
from ckanext.emailasusername.helpers import ( | ||
config_require_user_email_input_confirmation | ||
) | ||
from contextlib import nullcontext as does_not_raise | ||
import logging | ||
import pytest | ||
|
||
|
@@ -81,24 +82,47 @@ def test_user_both_emails_entered(self): | |
plugin.user_both_emails_entered(key, data, errors, context) | ||
assert len(errors[('email',)]) == 1 | ||
|
||
def test_email_exists(self): | ||
# Test email exists validator for valid data | ||
key = ('email',) | ||
data = {('email',): '[email protected]'} | ||
errors = {('email',): []} | ||
context = {} | ||
plugin.email_exists(key, data, errors, context) | ||
assert errors, {('email',): []} | ||
|
||
# Test email exists validator for invalid data | ||
# i.e. a pre-existing account already exists with the given email | ||
test_user_dict = ckan.tests.factories.User( | ||
name='tester1', | ||
email='[email protected]' | ||
@pytest.mark.parametrize("existing_user, data, result", [ | ||
( | ||
None, | ||
{('email',): '[email protected]'}, | ||
does_not_raise() | ||
), ( | ||
{'email': '[email protected]'}, | ||
{('email',): '[email protected]'}, | ||
pytest.raises(toolkit.Invalid) | ||
), ( | ||
{'email': '[email protected]', 'state': 'deleted'}, | ||
{('email',): '[email protected]'}, | ||
does_not_raise() | ||
), ( | ||
{'email': '[email protected]', 'state': 'deleted', 'name': 'tester1'}, | ||
{('email',): '[email protected]', 'state': 'active', ('name',): 'tester1'}, | ||
does_not_raise() | ||
), ( | ||
{'email': '[email protected]', 'name': 'tester1'}, | ||
{('email',): '[email protected]', ('name',): 'tester1'}, | ||
does_not_raise() | ||
), ( | ||
{'email': '[email protected]', 'name': 'tester1'}, | ||
{('email',): '[email protected]', ('name',): 'tester2'}, | ||
pytest.raises(toolkit.Invalid) | ||
) | ||
data = {('email',): test_user_dict['email']} | ||
plugin.email_exists(key, data, errors, context) | ||
assert len(errors[('email',)]) == 1 | ||
], ids=[ | ||
"creating user with a unique email address", | ||
"creating user with an existing email address", | ||
"creating user with an email address used by deleted user", | ||
"updating user state to active", | ||
"updating user without changing the email address", | ||
"updating user with an existing email address" | ||
]) | ||
def test_email_is_unique(self, existing_user, data, result): | ||
|
||
if existing_user: | ||
User(**existing_user) | ||
|
||
with result: | ||
plugin.email_is_unique(('email',), data, {('email',): []}, {}) | ||
|
||
def test_emailasusername_new_user_schema(self): | ||
schema = ckan.logic.schema.user_new_form_schema() | ||
|
This file contains 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