"""Test zendesk logic.""" import json from owsresponse import response import pytest from users import constants from users.logic import zendesk @pytest.mark.parametrize('brand_experience', (constants.ORCHARD_BRAND, constants.AWAL_BRAND, None)) def test_generate_zendesk_token(brand_experience, mocker): """Test zendesk token generation.""" identity_id = '' version = '' generator_mock = mocker.Mock() mocker.patch.dict(zendesk.JWT_GENERATOR_MAP, {version: generator_mock}) zendesk.generate_zendesk_token(identity_id, brand_experience, version) generator_mock.assert_called_once_with(identity_id, brand_experience) def test_generate_zendesk_token_unsupported_version(mocker): """Test zendesk token not been generated because of unsupported version.""" identity_id = '' token_version = '' generator_v1_mock = mocker.patch('users.logic.zendesk.generate_zendesk_token_v1') generator_v2_mock = mocker.patch('users.logic.zendesk.generate_zendesk_token_v2') result = zendesk.generate_zendesk_token(identity_id, None, token_version) generator_v1_mock.assert_not_called() generator_v2_mock.assert_not_called() assert result.status == 400 assert result.errors['message'] == f'"{token_version}" is not supported.' # ---------------------------------- v1 ---------------------------------- @pytest.mark.parametrize( 'brand_experience, default_brand, expected_brand', ( (constants.ORCHARD_BRAND, constants.AWAL_BRAND, constants.ORCHARD_BRAND), (constants.AWAL_BRAND, constants.AWAL_BRAND, constants.AWAL_BRAND), (None, constants.ORCHARD_BRAND, constants.ORCHARD_BRAND), ), ) def test_generate_zendesk_token_v1_success(brand_experience, default_brand, expected_brand, mocker): """Test zendesk token v1 generation.""" identity_id = '' mocker.patch( 'users.config.ZENDESK_SECRET_KEY', json.dumps( { constants.ORCHARD_BRAND: f'<{constants.ORCHARD_BRAND.upper()}_ZENDESK_SECRET_KEY>', constants.AWAL_BRAND: f'<{constants.AWAL_BRAND.upper()}_ZENDESK_SECRET_KEY>', } ), ) get_identity_mock = mocker.patch( 'users.models.identities.get_identity', return_value=response.Response( { 'name': '', 'email': '', 'default_brand': default_brand, } ), ) jwt_encode_mock = mocker.patch('jwt.encode', return_value='') mocker.patch('users.logic.zendesk.time', return_value=123456) mocker.patch('users.logic.zendesk.uuid4', return_value='') result = zendesk.generate_zendesk_token_v1(identity_id, brand_experience) get_identity_mock.assert_called_once_with(identity_id) jwt_encode_mock.assert_called_once_with( {'name': '', 'email': '', 'iat': 123456, 'jti': ''}, f'<{expected_brand.upper()}_ZENDESK_SECRET_KEY>', algorithm='HS256', ) assert result.status == 200 assert result.message == {'value': ''} @pytest.mark.parametrize( 'identity, brand_experience, secret_key, expected_code, expected_message', ( (response.create_not_found_response(''), None, None, 404, ''), ( response.Response({'name': '', 'email': ''}), None, None, 500, 'Can not get brand for identity', ), ( response.Response( {'name': '', 'email': '', 'default_brand': constants.ORCHARD_BRAND} ), constants.AWAL_BRAND, None, 500, 'ZENDESK_SECRET_KEY must be set', ), ( response.Response( {'name': '', 'email': '', 'default_brand': constants.ORCHARD_BRAND} ), constants.ORCHARD_BRAND, None, 500, 'ZENDESK_SECRET_KEY must be set', ), ( response.Response( {'name': '', 'email': '', 'default_brand': constants.ORCHARD_BRAND} ), constants.ORCHARD_BRAND, '', 500, 'ZENDESK_SECRET_KEY must be json', ), ( response.Response( {'name': '', 'email': '', 'default_brand': constants.ORCHARD_BRAND} ), constants.ORCHARD_BRAND, json.dumps({constants.AWAL_BRAND: ''}), 500, 'ZENDESK_SECRET_KEY is not defined for brand theorchard', ), ), ) def test_generate_zendesk_token_v1_error( identity, brand_experience, secret_key, expected_code, expected_message, mocker ): """Test no zendesk token v1 generated.""" identity_id = '' mocker.patch('users.config.ZENDESK_SECRET_KEY', secret_key) mocker.patch('users.models.identities.get_identity', return_value=identity) result = zendesk.generate_zendesk_token_v1(identity_id, brand_experience) assert result.status == expected_code assert result.errors['message'] == expected_message # ---------------------------------- v2 ---------------------------------- @pytest.mark.parametrize( 'brand_experience, default_brand, expected_brand', ( (constants.ORCHARD_BRAND, constants.AWAL_BRAND, constants.ORCHARD_BRAND), (constants.AWAL_BRAND, constants.AWAL_BRAND, constants.AWAL_BRAND), (None, constants.ORCHARD_BRAND, constants.ORCHARD_BRAND), ), ) def test_generate_zendesk_token_v2(brand_experience, default_brand, expected_brand, mocker): """Test zendesk token v1 generation.""" identity_id = '' mocker.patch( 'users.config.ZENDESK_MESSAGING_SECRET_KEY', json.dumps( { constants.ORCHARD_BRAND: ( f'<{constants.ORCHARD_BRAND.upper()}_ZENDESK_MESSAGING_SECRET>' ), constants.AWAL_BRAND: f'<{constants.AWAL_BRAND.upper()}_ZENDESK_MESSAGING_SECRET>', } ), ) mocker.patch( 'users.config.ZENDESK_KID', json.dumps( { constants.ORCHARD_BRAND: f'<{constants.ORCHARD_BRAND.upper()}_ZENDESK_KID>', constants.AWAL_BRAND: f'<{constants.AWAL_BRAND.upper()}_ZENDESK_KID>', } ), ) get_identity_mock = mocker.patch( 'users.models.identities.get_identity', return_value=response.Response( { 'name': '', 'email': '', 'id': '', 'default_brand': default_brand, } ), ) jwt_encode_mock = mocker.patch('jwt.encode', return_value='') result = zendesk.generate_zendesk_token_v2(identity_id, brand_experience) get_identity_mock.assert_called_once_with(identity_id) jwt_encode_mock.assert_called_once_with( { 'external_id': '', 'email': '', 'email_verified': True, 'name': '', 'scope': 'user', }, f'<{expected_brand.upper()}_ZENDESK_MESSAGING_SECRET>', headers={'alg': 'HS256', 'typ': 'JWT', 'kid': f'<{expected_brand.upper()}_ZENDESK_KID>'}, ) assert result.status == 200 assert result.message == {'value': ''} @pytest.mark.parametrize( 'identity, brand_experience, secret_key, kid, expected_code, expected_message', ( ( response.create_not_found_response(''), None, None, None, 404, '', ), ( response.Response({'name': '', 'email': ''}), None, None, None, 500, 'Can not get brand for identity', ), ( response.Response( {'name': '', 'email': '', 'default_brand': constants.ORCHARD_BRAND} ), constants.AWAL_BRAND, None, json.dumps( { constants.AWAL_BRAND: f'<{constants.AWAL_BRAND}_ZENDESK_KID>', constants.ORCHARD_BRAND: f'<{constants.ORCHARD_BRAND}_ZENDESK_KID>', } ), 500, 'ZENDESK_MESSAGING_SECRET_KEY must be set', ), ( response.Response( {'name': '', 'email': '', 'default_brand': constants.ORCHARD_BRAND} ), constants.ORCHARD_BRAND, '', json.dumps( { constants.AWAL_BRAND: f'<{constants.AWAL_BRAND}_ZENDESK_KID>', constants.ORCHARD_BRAND: f'<{constants.ORCHARD_BRAND}_ZENDESK_KID>', } ), 500, 'ZENDESK_MESSAGING_SECRET_KEY must be json', ), ( response.Response( {'name': '', 'email': '', 'default_brand': constants.ORCHARD_BRAND} ), constants.ORCHARD_BRAND, json.dumps( { constants.AWAL_BRAND: f'<{constants.AWAL_BRAND}_ZENDESK_MESSAGING_SECRET_KEY>', } ), json.dumps( { constants.AWAL_BRAND: f'<{constants.AWAL_BRAND}_ZENDESK_KID>', constants.ORCHARD_BRAND: f'<{constants.ORCHARD_BRAND}_ZENDESK_KID>', } ), 500, 'ZENDESK_MESSAGING_SECRET_KEY is not defined for brand theorchard', ), ( response.Response( {'name': '', 'email': '', 'default_brand': constants.ORCHARD_BRAND} ), constants.ORCHARD_BRAND, json.dumps( { constants.AWAL_BRAND: f'<{constants.AWAL_BRAND}_ZENDESK_MESSAGING_SECRET_KEY>', constants.ORCHARD_BRAND: ( f'<{constants.ORCHARD_BRAND}_ZENDESK_MESSAGING_SECRET_KEY>' ), } ), None, 500, 'ZENDESK_KID must be set', ), ( response.Response( {'name': '', 'email': '', 'default_brand': constants.ORCHARD_BRAND} ), constants.ORCHARD_BRAND, json.dumps( { constants.AWAL_BRAND: f'<{constants.AWAL_BRAND}_ZENDESK_MESSAGING_SECRET_KEY>', constants.ORCHARD_BRAND: ( f'<{constants.ORCHARD_BRAND}_ZENDESK_MESSAGING_SECRET_KEY>' ), } ), '', 500, 'ZENDESK_KID must be json', ), ( response.Response( {'name': '', 'email': '', 'default_brand': constants.ORCHARD_BRAND} ), constants.ORCHARD_BRAND, json.dumps( { constants.AWAL_BRAND: f'<{constants.AWAL_BRAND}_ZENDESK_MESSAGING_SECRET_KEY>', constants.ORCHARD_BRAND: ( f'<{constants.ORCHARD_BRAND}_ZENDESK_MESSAGING_SECRET_KEY>' ), } ), json.dumps( { constants.AWAL_BRAND: f'<{constants.AWAL_BRAND}_ZENDESK_KID>', } ), 500, 'ZENDESK_KID is not defined for brand theorchard', ), ), ) def test_generate_zendesk_token_v2_error( identity, brand_experience, secret_key, kid, expected_code, expected_message, mocker ): """Test no zendesk token v1 generated.""" identity_id = '' mocker.patch('users.config.ZENDESK_MESSAGING_SECRET_KEY', secret_key) mocker.patch('users.config.ZENDESK_KID', kid) mocker.patch('users.models.identities.get_identity', return_value=identity) result = zendesk.generate_zendesk_token_v2(identity_id, brand_experience) assert result.status == expected_code assert result.errors['message'] == expected_message