"""Line API wrapper tests.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from feed_ingestion.flows.line.line_api import LineAPI @pytest.fixture def line_api_authenticated(): """Yield LineAPI Wrapper fixture (authenticated).""" line_api = LineAPI('test', 'secret') line_api._token = 'token' yield line_api @pytest.fixture def line_api(): """Line API Wrapper fixture.""" yield LineAPI('test', 'secret') @pytest.fixture def mock_oauth_success(): """Yield Line API oauth success response.""" requests_path = ( 'feed_ingestion.flows.line.line_api.requests') with patch(requests_path) as requests: response_body = {'access_token': 'token', 'expires_in': 3600} response_mock = MagicMock() response_mock.status_code = 201 response_mock.json = MagicMock(return_value=response_body) requests.post = MagicMock(return_value=response_mock) yield requests @pytest.fixture def mock_oauth_failure(): """Yield Line API oauth failure response.""" requests_path = ( 'feed_ingestion.flows.line.line_api.requests') with patch(requests_path) as requests: response_mock = MagicMock() response_mock.status_code = 401 requests.post = MagicMock(return_value=response_mock) yield requests @pytest.fixture def expected_request_to_file_url_content(): """Mock Tracks Resource response.""" content = {'reports': [{ 'date': '2022-05-11', 'label_id': 'bl00000000029c7cd8', 'url': 'https://vos.line-scdn.net/lmj-label-report/20220511' '/LMA_bl00000000029c7cd8_20220511.zip?AWSAccessKeyId' '=d976a4e895bd460b9b288c9dc2665e3f&Expires=1653983873&Signature' '=bcSAqSvhuqEoZMeRVhyoGpaXXaA%3D', 'url_expires': '2022-05-31T07:57:53.417Z' }] } yield content @pytest.fixture def expected_file_url(): """Mock Tracks Resource response.""" url = 'https://vos.line-scdn.net/lmj-label-report/20220511' \ '/LMA_bl00000000029c7cd8_20220511.zip?AWSAccessKeyId' \ '=d976a4e895bd460b9b288c9dc2665e3f&Expires=1653983873&Signature' \ '=bcSAqSvhuqEoZMeRVhyoGpaXXaA%3D' yield url @pytest.fixture def expected_file_name(): """Mock Tracks Resource response.""" url = 'LMA_bl00000000029c7cd8_20220511.zip' yield url def assert_request(mock_session, date): """Test API request.""" assert mock_session.send.called req = mock_session.send.call_args[0][0] assert date in req.url def test_token_success(mock_oauth_success, line_api): """Test Line API token request success scenario.""" line_api._token = None token = line_api.token assert token == 'token' assert line_api._token == 'token' def test_token_failure(mock_oauth_failure, line_api): """Test Line API token request failure scenario.""" line_api._token = None with pytest.raises(Exception): line_api.token def test_token_cache(mock_oauth_success, line_api): """Should not call Spotify API if token is already cached.""" line_api._token = 'cached-token' token = line_api.token mock_oauth_success.post.assert_not_called() assert token == 'cached-token' @pytest.fixture def mock_request_to_file_url(): """Yield Line API oauth success response.""" requests_path = ( 'feed_ingestion.flows.line.line_api.requests') with patch(requests_path) as requests: response_body = {'reports': [{ 'date': '2022-05-11', 'label_id': 'bl00000000029c7cd8', 'url': 'https://vos.line-scdn.net/lmj-label-report/20220511' '/LMA_bl00000000029c7cd8_20220511.zip?AWSAccessKeyId' '=d976a4e895bd460b9b288c9dc2665e3f&Expires=1653983873' '&Signature=bcSAqSvhuqEoZMeRVhyoGpaXXaA%3D', 'url_expires': '2022-05-31T07:57:53.417Z' }] } response_mock = MagicMock() response_mock.status_code = 200 response_mock.json = MagicMock(return_value=response_body) requests.get = MagicMock(return_value=response_mock) yield requests def test_request_to_file_url(mock_request_to_file_url, line_api_authenticated, expected_request_to_file_url_content): """Test request_to_file_url with date argument.""" d = '2022-05-11' res = line_api_authenticated.request_to_file_url(d) assert res == expected_request_to_file_url_content def test_file_download_url(mock_request_to_file_url, line_api_authenticated, expected_file_url): """Test _extract_file_url_from_json function.""" d = '2022-05-11' res = line_api_authenticated._extract_file_url_from_json( line_api_authenticated.request_to_file_url(d)) assert res == expected_file_url def test_zipped_filename_in_download_url(mock_request_to_file_url, line_api_authenticated, expected_file_name): """Test _extract_file_url_from _json function.""" d = '2022-05-11' res = line_api_authenticated._extract_file_url_from_json( line_api_authenticated.request_to_file_url(d)) assert expected_file_name in res