"""Functional tests for has explicit lyrics. Test endpoint: GET /has-explicit-lyrics?lyrics=lyrics """ import json from unittest.mock import patch from oto import status as http_status import pytest authorization_header = {'Authorization': 'ows-track/ows-lyrics:randomhmac123'} blank_authorization_header = {'Authorization': ''} @pytest.mark.parametrize( 'headers, response_status, response_data', ((authorization_header, http_status.OK, {'has_explicit_lyrics': True}), ({}, http_status.FORBIDDEN, {'code': 'authorization_error', 'message': 'user is forbidden'}), (blank_authorization_header, http_status.FORBIDDEN, {'code': 'authorization_error', 'message': 'user is forbidden'}))) @patch('lyrics.features.is_feature_flag_enabled', return_value=False) def test_get_has_explicit_lyrics_with_ff_off( is_feature_flag_enabled, client, authorization_mock, headers, response_status, response_data): """Check that get has-explicit-lyrics works when feature is disabled.""" response = client.get('/has-explicit-lyrics?lyrics=Fuck', headers=headers) data = json.loads(response.data.decode()) assert data == response_data assert response.status_code == response_status @pytest.mark.parametrize( 'headers, response_status, response_data', ((authorization_header, http_status.OK, {'has_explicit_lyrics': True}), ({}, http_status.FORBIDDEN, {'code': 'authorization_error', 'message': 'user is forbidden'}), (blank_authorization_header, http_status.FORBIDDEN, {'code': 'authorization_error', 'message': 'user is forbidden'}))) @patch('lyrics.features.is_feature_flag_enabled', return_value=True) def test_get_has_explicit_lyrics_with_ff_on( is_feature_flag_enabled, client, authorization_mock, headers, response_status, response_data): """Check that get has-explicit-lyrics works when feauture is enabled.""" response = client.get('/has-explicit-lyrics?lyrics=Fuck', headers=headers) data = json.loads(response.data.decode()) assert data == response_data assert response.status_code == response_status @pytest.mark.parametrize( 'headers, response_status, response_data', ((authorization_header, http_status.OK, {'has_explicit_lyrics': False}), ({}, http_status.FORBIDDEN, {'code': 'authorization_error', 'message': 'user is forbidden'}))) @patch('lyrics.features.is_feature_flag_enabled') def test_get_has_no_explicit_lyrics( is_feature_flag_enabled, client, authorization_mock, headers, response_status, response_data): """Check that get has-explicit-lyrics works when feature is enabled.""" response = client.get('/has-explicit-lyrics?lyrics=Fucka', headers=headers) data = json.loads(response.data.decode()) assert data == response_data assert response.status_code == response_status @pytest.mark.parametrize( 'headers, response_status, response_data', ((authorization_header, http_status.OK, {'has_explicit_lyrics': True}), ({}, http_status.FORBIDDEN, {'code': 'authorization_error', 'message': 'user is forbidden'}), (blank_authorization_header, http_status.FORBIDDEN, {'code': 'authorization_error', 'message': 'user is forbidden'}))) @patch('lyrics.features.is_feature_flag_enabled', return_value=False) def test_post_has_explicit_lyrics_with_ff_off( is_feature_flag_enabled, client, authorization_mock, headers, response_status, response_data, client_headers): """Check that post has-explicit-lyrics works when feature is disabled.""" client_headers.update(headers) response = client.post( '/has-explicit-lyrics', data=json.dumps({'lyrics': 'Fuck'}), headers=client_headers) data = json.loads(response.data.decode()) assert data == response_data assert response.status_code == response_status @pytest.mark.parametrize( 'headers, response_status, response_data', ((authorization_header, http_status.OK, {'has_explicit_lyrics': True}), ({}, http_status.FORBIDDEN, {'code': 'authorization_error', 'message': 'user is forbidden'}), (blank_authorization_header, http_status.FORBIDDEN, {'code': 'authorization_error', 'message': 'user is forbidden'}))) @patch('lyrics.features.is_feature_flag_enabled', return_value=True) def test_post_has_explicit_lyrics_with_ff_on( is_feature_flag_enabled, client, authorization_mock, headers, response_status, response_data, client_headers): """Check that post has-explicit-lyrics works when feature is enabled.""" client_headers.update(headers) response = client.post( '/has-explicit-lyrics', data=json.dumps({'lyrics': 'Fuck'}), headers=client_headers) data = json.loads(response.data.decode()) assert data == response_data assert response.status_code == response_status @pytest.mark.parametrize( 'headers, response_status, response_data', ((authorization_header, http_status.OK, {'has_explicit_lyrics': False}), ({}, http_status.FORBIDDEN, {'code': 'authorization_error', 'message': 'user is forbidden'}))) @patch('lyrics.features.is_feature_flag_enabled', return_value=True) def test_post_has_no_explicit_lyrics( is_feature_flag_enabled, client, authorization_mock, headers, response_status, response_data, client_headers): """Check that post has-explicit-lyrics works when feature is enabled.""" client_headers.update(headers) response = client.post( '/has-explicit-lyrics', data=json.dumps({'lyrics': 'Fucka'}), headers=client_headers) data = json.loads(response.data.decode()) assert data == response_data assert response.status_code == response_status