"""Test verify_login_user.""" import json from owsresponse import response import pytest from pythonfeatures import pythonfeatures from tests.unit import db_utils from tests.unit.factories.alw_user_contact import ALWUserContactFactory from tests.unit.factories.alw_user_vend_contact import ALWUserVendContactFactory # noqa from users.app import app from users.logic import user_info login = 'testname' password = 'passhash' def _populate_values(loginname, password): """Populate test data in db.""" user_id = 88691 contact_email = 'abc@theorchard.com' vend_contact = ALWUserVendContactFactory.build( user_id=user_id, contact_id=1234, login=loginname, password_hash=password, auth0_user_id=None, ) contact = ALWUserContactFactory.build(contact_id=1234, email=contact_email) db_utils.seed_models(vend_contact) db_utils.seed_models(contact) expected_message = { 'password_hash': password, 'login': loginname, 'vend_contact_id': user_id, 'contact_email': contact_email, } return expected_message @db_utils.test_schema def test_verify_login_user(mocker): """Test verify_login_user for valid creds without any FFlag.""" expected_response = _populate_values(login, password) navigator = app.test_client() request = navigator.post( '/users/verify-login', data=json.dumps({'login': login, 'password': password}), content_type='application/json', ) assert request.status_code == 200 response_data = json.loads(request.data.decode('utf-8')) assert response_data == expected_response @db_utils.test_schema def test_verify_login_user_invalid_creds(mocker): """Test verify_login_user with invalid creds.""" _populate_values(login, password) navigator = app.test_client() request = navigator.post( '/users/verify-login', data=json.dumps({'login': login, 'password': 'dummy'}), content_type='application/json', ) assert request.status_code == 404 @pytest.mark.parametrize( ( 'get_attributes_response', 'feature_enabled_response', 'expected_status_code', ), [ # feature enabled ( response.Response( message={'user_id': 'alw:88691', 'vendor_id': 1234, 'subaccount_id': 1} ), response.Response(message='enabled'), 200, ), # feature control ( response.Response( message={'user_id': 'alw:88691', 'vendor_id': 1234, 'subaccount_id': 1} ), response.Response(message='control'), 200, ), ], ) @db_utils.test_schema def test_verify_login_user_feature( mocker, get_attributes_response, feature_enabled_response, expected_status_code ): """Test verify_login_user with feature flag.""" expected_response = _populate_values(login, password) expected_response.update( {'feature': {'whatever': feature_enabled_response.message == ('enabled')}} ) mocker.patch.object( user_info, 'get_attributes_for_vend_contact', return_value=get_attributes_response ) mocker.patch.object( pythonfeatures, 'get_single_feature_by_attributes', return_value=feature_enabled_response ) resp = app.test_client().post( '/users/verify-login?feature=whatever', json={'login': login, 'password': password}, content_type='application/json', ) assert resp.status_code == expected_status_code assert expected_response == json.loads(resp.data.decode('utf-8'))