2016-01-21 17:29:24 +00:00
|
|
|
import json
|
2017-02-15 17:41:07 +00:00
|
|
|
import uuid
|
2016-03-17 13:06:17 +00:00
|
|
|
from datetime import (
|
|
|
|
|
datetime,
|
|
|
|
|
timedelta
|
|
|
|
|
)
|
|
|
|
|
|
2017-02-16 15:20:30 +00:00
|
|
|
import pytest
|
2016-06-03 15:15:46 +01:00
|
|
|
from flask import url_for, current_app
|
2017-02-16 15:20:30 +00:00
|
|
|
from freezegun import freeze_time
|
|
|
|
|
|
2016-12-09 14:55:24 +00:00
|
|
|
from app.dao.services_dao import dao_update_service, dao_fetch_service_by_id
|
2016-03-17 13:06:17 +00:00
|
|
|
from app.models import (
|
|
|
|
|
VerifyCode,
|
2016-12-09 14:55:24 +00:00
|
|
|
User,
|
|
|
|
|
Notification
|
2016-03-17 13:06:17 +00:00
|
|
|
)
|
2017-02-14 14:04:11 +00:00
|
|
|
from app import db
|
2017-02-16 15:20:30 +00:00
|
|
|
import app.celery.tasks
|
2016-03-17 13:06:17 +00:00
|
|
|
|
2016-01-21 17:29:24 +00:00
|
|
|
from tests import create_authorization_header
|
2016-03-17 13:06:17 +00:00
|
|
|
|
2016-01-21 17:29:24 +00:00
|
|
|
|
2017-02-17 14:06:16 +00:00
|
|
|
@freeze_time('2016-01-01T12:00:00')
|
|
|
|
|
def test_user_verify_code(client, sample_sms_code):
|
|
|
|
|
sample_sms_code.user.logged_in_at = datetime.utcnow() - timedelta(days=1)
|
2017-02-14 14:04:11 +00:00
|
|
|
assert not VerifyCode.query.first().code_used
|
2017-02-17 14:06:16 +00:00
|
|
|
assert sample_sms_code.user.current_session_id is None
|
2017-02-14 14:04:11 +00:00
|
|
|
data = json.dumps({
|
|
|
|
|
'code_type': sample_sms_code.code_type,
|
|
|
|
|
'code': sample_sms_code.txt_code})
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.verify_user_code', user_id=sample_sms_code.user.id),
|
|
|
|
|
data=data,
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
assert resp.status_code == 204
|
|
|
|
|
assert VerifyCode.query.first().code_used
|
2017-02-17 14:06:16 +00:00
|
|
|
assert sample_sms_code.user.logged_in_at == datetime.utcnow()
|
|
|
|
|
assert sample_sms_code.user.current_session_id is not None
|
2016-01-21 17:29:24 +00:00
|
|
|
|
|
|
|
|
|
2017-02-14 14:04:11 +00:00
|
|
|
def test_user_verify_code_missing_code(client,
|
|
|
|
|
sample_sms_code):
|
|
|
|
|
assert not VerifyCode.query.first().code_used
|
|
|
|
|
data = json.dumps({'code_type': sample_sms_code.code_type})
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.verify_user_code', user_id=sample_sms_code.user.id),
|
|
|
|
|
data=data,
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
assert resp.status_code == 400
|
|
|
|
|
assert not VerifyCode.query.first().code_used
|
|
|
|
|
assert User.query.get(sample_sms_code.user.id).failed_login_count == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_user_verify_code_bad_code_and_increments_failed_login_count(client,
|
|
|
|
|
sample_sms_code):
|
|
|
|
|
assert not VerifyCode.query.first().code_used
|
|
|
|
|
data = json.dumps({
|
|
|
|
|
'code_type': sample_sms_code.code_type,
|
|
|
|
|
'code': "blah"})
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.verify_user_code', user_id=sample_sms_code.user.id),
|
|
|
|
|
data=data,
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
assert resp.status_code == 404
|
|
|
|
|
assert not VerifyCode.query.first().code_used
|
|
|
|
|
assert User.query.get(sample_sms_code.user.id).failed_login_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_user_verify_code_expired_code_and_increments_failed_login_count(
|
|
|
|
|
client,
|
|
|
|
|
sample_sms_code):
|
|
|
|
|
assert not VerifyCode.query.first().code_used
|
|
|
|
|
sample_sms_code.expiry_datetime = (
|
|
|
|
|
datetime.utcnow() - timedelta(hours=1))
|
|
|
|
|
db.session.add(sample_sms_code)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
data = json.dumps({
|
|
|
|
|
'code_type': sample_sms_code.code_type,
|
|
|
|
|
'code': sample_sms_code.txt_code})
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.verify_user_code', user_id=sample_sms_code.user.id),
|
|
|
|
|
data=data,
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
assert resp.status_code == 400
|
|
|
|
|
assert not VerifyCode.query.first().code_used
|
|
|
|
|
assert User.query.get(sample_sms_code.user.id).failed_login_count == 1
|
2016-01-21 17:29:24 +00:00
|
|
|
|
|
|
|
|
|
2016-03-07 15:01:40 +00:00
|
|
|
@freeze_time("2016-01-01 10:00:00.000000")
|
2017-02-17 14:06:16 +00:00
|
|
|
def test_user_verify_password(client, sample_user):
|
|
|
|
|
yesterday = datetime.utcnow() - timedelta(days=1)
|
|
|
|
|
sample_user.logged_in_at = yesterday
|
2017-02-14 14:04:11 +00:00
|
|
|
data = json.dumps({'password': 'password'})
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.verify_user_password', user_id=sample_user.id),
|
|
|
|
|
data=data,
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
assert resp.status_code == 204
|
2017-02-17 14:06:16 +00:00
|
|
|
assert User.query.get(sample_user.id).logged_in_at == yesterday
|
2016-01-21 17:29:24 +00:00
|
|
|
|
|
|
|
|
|
2017-02-14 14:04:11 +00:00
|
|
|
def test_user_verify_password_invalid_password(client,
|
2016-01-27 11:51:02 +00:00
|
|
|
sample_user):
|
2017-02-14 14:04:11 +00:00
|
|
|
data = json.dumps({'password': 'bad password'})
|
|
|
|
|
auth_header = create_authorization_header()
|
2016-01-25 11:14:23 +00:00
|
|
|
|
2017-02-14 14:04:11 +00:00
|
|
|
assert sample_user.failed_login_count == 0
|
2016-01-25 11:14:23 +00:00
|
|
|
|
2017-02-14 14:04:11 +00:00
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.verify_user_password', user_id=sample_user.id),
|
|
|
|
|
data=data,
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
assert resp.status_code == 400
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
assert 'Incorrect password' in json_resp['message']['password']
|
|
|
|
|
assert sample_user.failed_login_count == 1
|
2016-01-21 17:29:24 +00:00
|
|
|
|
|
|
|
|
|
2017-02-14 14:04:11 +00:00
|
|
|
def test_user_verify_password_valid_password_resets_failed_logins(client,
|
2016-01-28 11:32:46 +00:00
|
|
|
sample_user):
|
2017-02-14 14:04:11 +00:00
|
|
|
data = json.dumps({'password': 'bad password'})
|
|
|
|
|
auth_header = create_authorization_header()
|
2016-01-28 11:32:46 +00:00
|
|
|
|
2017-02-14 14:04:11 +00:00
|
|
|
assert sample_user.failed_login_count == 0
|
2016-01-28 11:32:46 +00:00
|
|
|
|
2017-02-14 14:04:11 +00:00
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.verify_user_password', user_id=sample_user.id),
|
|
|
|
|
data=data,
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
assert resp.status_code == 400
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
assert 'Incorrect password' in json_resp['message']['password']
|
2016-01-28 11:32:46 +00:00
|
|
|
|
2017-02-14 14:04:11 +00:00
|
|
|
assert sample_user.failed_login_count == 1
|
2016-01-28 11:32:46 +00:00
|
|
|
|
2017-02-14 14:04:11 +00:00
|
|
|
data = json.dumps({'password': 'password'})
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.verify_user_password', user_id=sample_user.id),
|
|
|
|
|
data=data,
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
2016-01-28 11:32:46 +00:00
|
|
|
|
2017-02-14 14:04:11 +00:00
|
|
|
assert resp.status_code == 204
|
|
|
|
|
assert sample_user.failed_login_count == 0
|
2016-01-28 11:32:46 +00:00
|
|
|
|
|
|
|
|
|
2017-02-14 14:04:11 +00:00
|
|
|
def test_user_verify_password_missing_password(client,
|
2016-01-27 11:51:02 +00:00
|
|
|
sample_user):
|
2017-02-14 14:04:11 +00:00
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.verify_user_password', user_id=sample_user.id),
|
2017-02-15 17:41:07 +00:00
|
|
|
data=json.dumps({'bingo': 'bongo'}),
|
2017-02-14 14:04:11 +00:00
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
assert resp.status_code == 400
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
assert 'Required field missing data' in json_resp['message']['password']
|
2016-01-27 11:51:02 +00:00
|
|
|
|
|
|
|
|
|
2016-12-09 14:55:24 +00:00
|
|
|
@pytest.mark.parametrize('research_mode', [True, False])
|
2016-06-03 15:15:46 +01:00
|
|
|
@freeze_time("2016-01-01 11:09:00.061258")
|
2017-02-16 15:20:30 +00:00
|
|
|
def test_send_user_sms_code(client,
|
2016-06-03 15:15:46 +01:00
|
|
|
sample_user,
|
2016-06-06 11:51:12 +01:00
|
|
|
sms_code_template,
|
2016-12-09 14:55:24 +00:00
|
|
|
mocker,
|
|
|
|
|
research_mode):
|
2016-02-19 11:37:35 +00:00
|
|
|
"""
|
|
|
|
|
Tests POST endpoint /user/<user_id>/sms-code
|
|
|
|
|
"""
|
2017-02-16 15:20:30 +00:00
|
|
|
if research_mode:
|
|
|
|
|
notify_service = dao_fetch_service_by_id(current_app.config['NOTIFY_SERVICE_ID'])
|
|
|
|
|
notify_service.research_mode = True
|
|
|
|
|
dao_update_service(notify_service)
|
2016-06-06 11:51:12 +01:00
|
|
|
|
2017-02-16 15:20:30 +00:00
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
mocked = mocker.patch('app.user.rest.create_secret_code', return_value='11111')
|
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
2016-12-09 14:55:24 +00:00
|
|
|
|
2017-02-16 15:20:30 +00:00
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.send_user_sms_code', user_id=sample_user.id),
|
|
|
|
|
data=json.dumps({}),
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
assert resp.status_code == 204
|
2016-12-09 14:55:24 +00:00
|
|
|
|
2017-02-16 15:20:30 +00:00
|
|
|
assert mocked.call_count == 1
|
|
|
|
|
assert VerifyCode.query.count() == 1
|
|
|
|
|
assert VerifyCode.query.first().check_code('11111')
|
2016-12-09 14:55:24 +00:00
|
|
|
|
2017-02-16 15:20:30 +00:00
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
notification = Notification.query.first()
|
|
|
|
|
assert notification.personalisation == {'verify_code': '11111'}
|
|
|
|
|
assert notification.to == sample_user.mobile_number
|
|
|
|
|
assert str(notification.service_id) == current_app.config['NOTIFY_SERVICE_ID']
|
2016-12-09 14:55:24 +00:00
|
|
|
|
2017-02-16 15:20:30 +00:00
|
|
|
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with(
|
|
|
|
|
([str(notification.id)]),
|
|
|
|
|
queue="notify"
|
|
|
|
|
)
|
2016-02-19 11:37:35 +00:00
|
|
|
|
|
|
|
|
|
2016-06-03 15:15:46 +01:00
|
|
|
@freeze_time("2016-01-01 11:09:00.061258")
|
2017-02-16 15:20:30 +00:00
|
|
|
def test_send_user_code_for_sms_with_optional_to_field(client,
|
2016-06-03 15:15:46 +01:00
|
|
|
sample_user,
|
2016-06-06 11:51:12 +01:00
|
|
|
sms_code_template,
|
2016-06-03 15:15:46 +01:00
|
|
|
mocker):
|
2016-02-19 11:37:35 +00:00
|
|
|
"""
|
2016-12-09 14:55:24 +00:00
|
|
|
Tests POST endpoint /user/<user_id>/sms-code with optional to field
|
2016-06-06 11:51:12 +01:00
|
|
|
"""
|
2017-02-16 15:20:30 +00:00
|
|
|
to_number = '+441119876757'
|
|
|
|
|
mocked = mocker.patch('app.user.rest.create_secret_code', return_value='11111')
|
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.send_user_sms_code', user_id=sample_user.id),
|
|
|
|
|
data=json.dumps({'to': to_number}),
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 204
|
|
|
|
|
assert mocked.call_count == 1
|
|
|
|
|
notification = Notification.query.first()
|
|
|
|
|
assert notification.to == to_number
|
|
|
|
|
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with(
|
|
|
|
|
([str(notification.id)]),
|
|
|
|
|
queue="notify"
|
|
|
|
|
)
|
2016-02-19 11:37:35 +00:00
|
|
|
|
|
|
|
|
|
2017-02-14 14:04:11 +00:00
|
|
|
def test_send_sms_code_returns_404_for_bad_input_data(client):
|
|
|
|
|
uuid_ = uuid.uuid4()
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.send_user_sms_code', user_id=uuid_),
|
2017-02-15 17:41:07 +00:00
|
|
|
data=json.dumps({}),
|
2017-02-14 14:04:11 +00:00
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
assert resp.status_code == 404
|
|
|
|
|
assert json.loads(resp.get_data(as_text=True))['message'] == 'No result found'
|
2016-02-19 11:37:35 +00:00
|
|
|
|
|
|
|
|
|
2017-02-15 16:18:05 +00:00
|
|
|
def test_send_sms_code_returns_204_when_too_many_codes_already_created(client, sample_user):
|
|
|
|
|
for i in range(10):
|
|
|
|
|
verify_code = VerifyCode(
|
|
|
|
|
code_type='sms',
|
|
|
|
|
_code=12345,
|
|
|
|
|
created_at=datetime.utcnow() - timedelta(minutes=10),
|
2017-02-15 17:41:07 +00:00
|
|
|
expiry_datetime=datetime.utcnow() + timedelta(minutes=40),
|
2017-02-15 16:18:05 +00:00
|
|
|
user=sample_user
|
|
|
|
|
)
|
|
|
|
|
db.session.add(verify_code)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
assert VerifyCode.query.count() == 10
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.send_user_sms_code', user_id=sample_user.id),
|
2017-02-15 17:41:07 +00:00
|
|
|
data=json.dumps({}),
|
2017-02-15 16:18:05 +00:00
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
assert resp.status_code == 204
|
|
|
|
|
assert VerifyCode.query.count() == 10
|
|
|
|
|
|
|
|
|
|
|
2016-12-19 15:31:54 +00:00
|
|
|
def test_send_user_email_verification(client,
|
2016-06-09 16:41:20 +01:00
|
|
|
sample_user,
|
|
|
|
|
mocker,
|
|
|
|
|
email_verification_template):
|
2016-12-19 15:31:54 +00:00
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.send_user_email_verification', user_id=str(sample_user.id)),
|
2017-02-15 17:41:07 +00:00
|
|
|
data=json.dumps({}),
|
2016-12-19 15:31:54 +00:00
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
assert resp.status_code == 204
|
|
|
|
|
notification = Notification.query.first()
|
2016-12-20 11:55:26 +00:00
|
|
|
mocked.assert_called_once_with(([str(notification.id)]), queue="notify")
|
2016-12-19 15:31:54 +00:00
|
|
|
|
|
|
|
|
|
2017-02-16 15:20:30 +00:00
|
|
|
def test_send_email_verification_returns_404_for_bad_input_data(client, notify_db_session, mocker):
|
2016-06-09 16:41:20 +01:00
|
|
|
"""
|
|
|
|
|
Tests POST endpoint /user/<user_id>/sms-code return 404 for bad input data
|
|
|
|
|
"""
|
2016-12-20 11:55:26 +00:00
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
2016-12-19 15:31:54 +00:00
|
|
|
uuid_ = uuid.uuid4()
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.send_user_email_verification', user_id=uuid_),
|
2017-02-15 17:41:07 +00:00
|
|
|
data=json.dumps({}),
|
2016-12-19 15:31:54 +00:00
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
assert resp.status_code == 404
|
|
|
|
|
assert json.loads(resp.get_data(as_text=True))['message'] == 'No result found'
|
2016-12-20 11:55:26 +00:00
|
|
|
assert mocked.call_count == 0
|
2017-02-16 15:20:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_user_verify_user_code_valid_code_resets_failed_login_count(client, sample_sms_code):
|
2017-02-16 17:44:04 +00:00
|
|
|
sample_sms_code.user.failed_login_count = 1
|
2017-02-16 15:20:30 +00:00
|
|
|
data = json.dumps({
|
|
|
|
|
'code_type': sample_sms_code.code_type,
|
|
|
|
|
'code': sample_sms_code.txt_code})
|
|
|
|
|
resp = client.post(
|
|
|
|
|
url_for('user.verify_user_code', user_id=sample_sms_code.user.id),
|
|
|
|
|
data=data,
|
|
|
|
|
headers=[('Content-Type', 'application/json'), create_authorization_header()])
|
|
|
|
|
assert resp.status_code == 204
|
|
|
|
|
assert sample_sms_code.user.failed_login_count == 0
|
|
|
|
|
assert sample_sms_code.code_used
|