def test__call__(mocker): auth = SpotifyAPIAuth() mocker.patch.object(auth, "_set_tokens") auth.tokens = {"access_token": "xxxxx"} req = PreparedRequest() req.prepare(method="GET", url="https://spotify.com", auth=auth) assert req.headers["authorization"] == "Bearer xxxxx"
def test_save_new_tokens(mocker): auth = SpotifyAPIAuth() mocker.patch("etl.auth.client.put_parameter") auth._save_new_tokens({}) etl.auth.client.put_parameter.assert_called_once_with( Name=auth.parameter_name, Value="{}", Type="SecureString", Overwrite=True)
def test_check_response_401_try1(mocker, requests_mock): auth = SpotifyAPIAuth() mocker.patch.object(auth, "refresh_tokens") mocker.patch.object(auth, "_set_tokens") url = currently_playing.CURRENT_PLAYBACK_ENDPOINT requests_mock.get(url, status_code=401) res1 = requests.get(url) auth.tokens = {"access_token": "xxx", "refresh_token": "yyy"} requests_mock.get(url, status_code=200) res2 = currently_playing.check_response(res1, auth) auth.refresh_tokens.assert_called_once() assert res2.status_code == 200
def test_set_tokens(mocker): auth = SpotifyAPIAuth() tokens = {"access_token": "xxxxx", "expires_in": 3600} res = { "Parameter": { "LastModifiedDate": datetime.datetime(2020, 1, 1), "Value": json.dumps(tokens), } } mocker.patch("etl.auth.client.get_parameter", return_value=res) mocker.patch.object(auth, "refresh_tokens") auth._set_tokens() assert auth.tokens["access_token"] == tokens["access_token"] auth.refresh_tokens.assert_called_once()
def test_refresh_tokens(mocker, requests_mock): auth = SpotifyAPIAuth() start_access_token = "xxxxx" end_access_token = "aaaaa" refresh_token = "yyyyy" auth.tokens = { "access_tokens": start_access_token, "refresh_token": refresh_token } requests_mock.post(url=auth.token_url, json={"access_token": end_access_token}) mocker.patch.object(auth, "_get_client_secret_auth") mocker.patch.object(auth, "_save_new_tokens") auth.refresh_tokens() auth._save_new_tokens.assert_called_once() assert auth.tokens["access_token"] == end_access_token assert auth.tokens["refresh_token"] == refresh_token
def test_get_client_secret_auth(mocker): auth = ("xxxxx", "yyyyy") res = { "Parameter": { "Value": '{"client_id": "xxxxx", "client_secret": "yyyyy"}' } } mocker.patch("etl.auth.client.get_parameter", return_value=res) assert SpotifyAPIAuth._get_client_secret_auth() == auth
def handler(event, context, save=True): """ Request and save the currently playing tracks by the authenticated user from spotify to s3 """ auth = SpotifyAPIAuth() res = requests.get(CURRENT_PLAYBACK_ENDPOINT, auth=auth) res = check_response(res, auth) if res is None: return data = res.json() log_data(data) if save and data: save_to_s3(data) return data
def test_init(): auth = SpotifyAPIAuth() assert auth.parameter_name == "spotify-api-access-tokens" assert auth.token_url == "https://accounts.spotify.com/api/token"