"""Test header util.""" import pytest from grass.consts import headers as constants from grass.utils import headers def test_extract_authorization_token(): """Test extraction of an authorization token.""" assert headers.extract_authorization_token('Bearer token') == 'token' def test_extract_authorization_from_invalid_token(): """Test extraction of an invalid authorization token.""" for token in ['Monster token', 'token', 'Bearer token extra']: assert not headers.extract_authorization_token(token) @pytest.mark.parametrize( ('payload', 'profile_type', 'profile_id', 'expected'), [ ({'sub': 'auth0|123', constants.JWT_PROFILES: []}, None, None, False), ({'sub': 'auth0|123'}, None, None, False), # headers don't match profile headers. ({'sub': 'auth0|123', constants.JWT_PROFILES: []}, 'test', '123', False), ( { 'sub': 'auth0|123', constants.JWT_PROFILES: [ {'profile_type': 'otherprofile', 'profile_id': '1234'} ], }, 'test', '123', False, ), ( { 'sub': 'auth0|123', constants.JWT_PROFILES: [ {'profile_type': 'otherprofile', 'profile_id': '1234'}, {'profile_type': 'dummy', 'profile_id': '45656'}, ], }, 'test', '123', False, ), ( { 'sub': 'auth0|123', constants.JWT_PROFILES: [ {'profile_type': 'test', 'profile_id': '1234'}, {'profile_type': 'dummy', 'profile_id': '45656'}, ], }, 'test', '', False, ), ( { 'sub': 'auth0|123', constants.JWT_PROFILES: [ {'profile_type': 'test', 'profile_id': '1234'}, {'profile_type': 'dummy', 'profile_id': '45656'}, ], }, '', '1244', False, ), # headers match profile headers. ( { 'sub': 'auth0|123', constants.JWT_PROFILES: [ {'profile_type': 'test', 'profile_id': '1234'}, {'profile_type': 'dummy', 'profile_id': '45656'}, ], }, 'test', '1234', True, ), ( { 'sub': 'auth0|123', constants.JWT_PROFILES: [ {'profile_type': 'test', 'profile_id': '1234'}, ], }, 'test', '1234', True, ), ], ) def test_profile_exist_in_payload(payload, profile_type, profile_id, expected): """Test profile_exist_in_payload.""" result = headers.profile_exist_in_payload(payload, profile_type, profile_id) assert result == expected @pytest.mark.parametrize( ('payload', 'expected'), [ ({'sub': 'auth0|123', constants.JWT_PROFILES: []}, '123'), ( { 'sub': 'auth0|123', constants.JWT_USER_METADATA: {'orchardIdentityId': 'uuid-234'}, }, '123', ), ( { 'sub': 'auth0|5e905b9af346950c4693958c', constants.JWT_USER_METADATA: {'orchardIdentityId': 'uuid-234'}, }, 'uuid-234', ), ( {'sub': 'auth0|5e905b9af346950c4693958c', constants.JWT_USER_METADATA: {}}, '5e905b9af346950c4693958c', ), ( { 'sub': 'google-apps|rshield@theorchard.com', constants.JWT_USER_METADATA: {'orchardIdentityId': 'uuid-234'}, }, 'uuid-234', ), ( { 'sub': 'waad|rshield@theorchard.com', constants.JWT_USER_METADATA: {'orchardIdentityId': 'uuid-876'}, }, 'uuid-876', ), # to make it compatible with current working. ( { 'sub': 'google-apps|rshield@theorchard.com', constants.JWT_USER_METADATA: {}, }, 'google-apps|rshield@theorchard.com', ), ( {'sub': 'waad|rshield@theorchard.com', constants.JWT_USER_METADATA: {}}, 'waad|rshield@theorchard.com', ), ], ) def test_get_orchard_identity_id(payload, expected): """Test get_orchard_identity_id.""" result = headers.get_orchard_identity_id(payload) assert result == expected @pytest.mark.parametrize( ('payload', 'expected'), [ ({'sub': 'auth0|123', constants.JWT_USER_METADATA: {}}, None), ( { 'sub': 'auth0|5e905b9af346950c4693958c', constants.JWT_USER_METADATA: {'orchardIdentityId': 'uuid-234'}, }, 'uuid-234', ), ], ) def test_get_orchard_identity_uuid(payload, expected): """Test get_orchard_identity_uuid.""" result = headers.get_orchard_identity_uuid(payload) assert result == expected @pytest.mark.parametrize( ('payload',), [ ({},), ({'foo': 'bar'},), ({'sub': 'dummy|1234'},), ], ) def test_get_orchard_identity_id_error(payload): """Test get_orchard_identity_id.""" with pytest.raises(Exception): headers.get_orchard_identity_id(payload)