"""Fixtures for integration tests.""" import base64 from datetime import datetime import hashlib import hmac from urllib.parse import urlsplit import pytest from tests.integration.consts import api @pytest.fixture(scope='function') def timestamp(): """Timestamp of now.""" return datetime.now().timestamp() @pytest.fixture def body_bulk_request(): """Body for bulk post request.""" return '{ ' \ '"artist_ids" : [ 824451 , 415078 ] ' \ '}' @pytest.fixture def users_url_path_replaced_no_ad(timestamp): """Users url replaced.""" return api.GET_USER.format(label_id=api.LABEL_ID_NO_AD, user_id=api.USER_ID_NO_AD, timestamp=int(timestamp)) @pytest.fixture def users_url_path_replaced_write_ad(timestamp): """Users url replaced.""" return api.GET_USER.format(label_id=api.LABEL_ID_WRITE_AD, user_id=api.USER_ID_WRITE_AD, timestamp=int(timestamp)) @pytest.fixture def artists_url_path_replaced(timestamp): """Artists url replaced.""" return api.GET_ARTISTS.format(label_id=api.LABEL_ID_NO_AD, timestamp=int(timestamp)) @pytest.fixture def artist_url_path_replaced(timestamp): """Artists url replaced.""" return api.GET_ARTIST.format(label_id=api.LABEL_ID_NO_AD, artist_id=api.ARTIST_ID, timestamp=int(timestamp)) @pytest.fixture def releases_url_path_replaced(timestamp): """Releases url replaced.""" return api.GET_RELEASES.format(label_id=api.LABEL_ID_NO_AD, artist_id=api.ARTIST_ID, timestamp=int(timestamp)) @pytest.fixture def budget_url_path_replaced(timestamp): """Budget url replaced.""" return api.GET_BUDGET.format(label_id=api.LABEL_ID_NO_AD, timestamp=int(timestamp)) @pytest.fixture def budget_cap_url_path_replaced(timestamp): """Budget url replaced.""" return api.GET_BUDGET_CAP.format(timestamp=int(timestamp)) @pytest.fixture def feature_fm_url_path_replaced(timestamp): """Feature fm url replaced.""" return api.GET_FEATURE_FM_FRAME.format(timestamp=int(timestamp), label_id=api.LABEL_ID_NO_AD, user_id=api.USER_ID_NO_AD) @pytest.fixture(scope='function') def token_hmac(url_path_replaced): """Create hmac token from path.""" path_to_hmac = '' + urlsplit(url_path_replaced).path + \ '?' + \ urlsplit(url_path_replaced).query return hmac.new(api.SECRET.encode(), msg=path_to_hmac.encode(), digestmod=hashlib.sha1).hexdigest() @pytest.fixture(scope='function') def token_base64(token_hmac): """Encode in base64 the hmac token.""" return base64.b64encode(token_hmac.encode()) @pytest.fixture def users_url_path_with_token_no_ad(users_url_path_replaced_no_ad): """Users url with token.""" return users_url_path_replaced_no_ad + '&token=' + token_base64( token_hmac=token_hmac(url_path_replaced=users_url_path_replaced_no_ad) ).decode() @pytest.fixture def users_url_path_with_token_read_ad(users_url_path_replaced_read_ad): """Users url with token.""" return users_url_path_replaced_read_ad + '&token=' + token_base64( token_hmac=token_hmac( url_path_replaced=users_url_path_replaced_read_ad ) ).decode() @pytest.fixture def users_url_path_with_token_write_ad(users_url_path_replaced_write_ad): """Users url with token.""" return users_url_path_replaced_write_ad + '&token=' + token_base64( token_hmac=token_hmac( url_path_replaced=users_url_path_replaced_write_ad ) ).decode() @pytest.fixture def artists_url_path_with_token(artists_url_path_replaced): """Artists url with token.""" return artists_url_path_replaced + '&token=' + token_base64( token_hmac=token_hmac(url_path_replaced=artists_url_path_replaced) ).decode() @pytest.fixture def artist_url_path_with_token(artist_url_path_replaced): """Artist url with token.""" return artist_url_path_replaced + '&token=' + token_base64( token_hmac=token_hmac(url_path_replaced=artist_url_path_replaced) ).decode() @pytest.fixture def releases_url_path_with_token(releases_url_path_replaced): """Releases url with token.""" return releases_url_path_replaced + '&token=' + token_base64( token_hmac=token_hmac(url_path_replaced=releases_url_path_replaced) ).decode() @pytest.fixture def budget_url_path_with_token(budget_url_path_replaced): """Releases url with token.""" return budget_url_path_replaced + '&token=' + token_base64( token_hmac=token_hmac(url_path_replaced=budget_url_path_replaced) ).decode() @pytest.fixture def budget_cap_url_path_with_token(budget_cap_url_path_replaced): """Releases url with token.""" return budget_cap_url_path_replaced + '&token=' + token_base64( token_hmac=token_hmac(url_path_replaced=budget_cap_url_path_replaced) ).decode()