import json from unittest.mock import Mock import pytest from src import lambda_handler from .constants import NOT_EXISTING_ID, PLAYLIST_SYNC_FIXTURE, PLAYLIST_SYNC_FIXTURE_2, PLAYLIST_SYNC_CREATE_REQUEST, \ PLAYLIST_SYNC_UPDATE_REQUEST, PLAYLIST_SYNC_CREATE_RESPONSE, PLAYLIST_SYNC_UPDATE_RESPONSE, \ PLAYLIST_SYNC_LIST_GET_RESPONSE, PLAYLIST_SYNC_IDENTICAL_FROM_TO_CREATE_REQUEST, \ PLAYLIST_SYNC_CREATE_SOURCE_PLAYLIST_DOES_NOT_EXISTS_REQUEST, SYNC_MANAGER_MOCK @pytest.mark.skip(reason="outdated") @pytest.mark.parametrize("playlist_sync, expected_code, expected_result, expected_index", ( (None, 200, [], None), (PLAYLIST_SYNC_FIXTURE, 200, PLAYLIST_SYNC_LIST_GET_RESPONSE, 0), ), indirect=("playlist_sync",)) def test_list_playlist_sync(aws_context, application_instance, service_account, playlist_sync, expected_code, expected_result, expected_index): event = { "path": f"/playlistsync/{application_instance.id}/playlists", "httpMethod": "GET", "headers": {"Content-Type": "application/json", "Authorization": "test"}, } result = lambda_handler(event, aws_context) assert result["statusCode"] == expected_code, f"Something went wrong. Body: {result['body']}" if expected_result is not None: body = json.loads(result["body"]) if expected_index is None: assert body == expected_result else: body = body[expected_index] assert body.pop("id") == playlist_sync.id assert body.pop("applicationId") == application_instance.id assert body.pop("toServiceAccountId") == service_account.id assert body.pop("lastUpdated") assert body.pop("createdAt") assert body == expected_result @pytest.mark.skip(reason="outdated") @pytest.mark.parametrize("playlist_sync, expected_code, expected_result", ( (None, 404, None), (PLAYLIST_SYNC_FIXTURE, 200, PLAYLIST_SYNC_LIST_GET_RESPONSE), ), indirect=("playlist_sync",)) def test_get_playlist_sync(aws_context, application_instance, service_account, playlist_sync, expected_code, expected_result): sync_id = playlist_sync.id if playlist_sync is not None else NOT_EXISTING_ID event = { "path": f"/playlistsync/{application_instance.id}/playlists/{sync_id}", "httpMethod": "GET", "headers": {"Content-Type": "application/json", "Authorization": "test"}, } result = lambda_handler(event, aws_context) assert result["statusCode"] == expected_code, f"Something went wrong. Body: {result['body']}" if expected_result is not None: body = json.loads(result["body"]) assert body.pop("id") == playlist_sync.id assert body.pop("applicationId") == application_instance.id assert body.pop("toServiceAccountId") == service_account.id assert body.pop("lastUpdated") assert body.pop("createdAt") assert body == expected_result @pytest.mark.skip(reason="outdated") @pytest.mark.parametrize("playlist_sync, expected_code", ( (None, 404), (PLAYLIST_SYNC_FIXTURE, 202), ), indirect=("playlist_sync",)) def test_delete_playlist_sync(aws_context, playlist_sync_service, application_instance, service_account, playlist_sync, expected_code): sync_id = playlist_sync.id if playlist_sync is not None else NOT_EXISTING_ID event = { "path": f"/playlistsync/{application_instance.id}/playlists/{sync_id}", "httpMethod": "DELETE", "headers": {"Content-Type": "application/json", "Authorization": "test"}, } result = lambda_handler(event, aws_context) assert result["statusCode"] == expected_code, f"Something went wrong. Body: {result['body']}" assert not playlist_sync_service.exists_by_pk(sync_id) @pytest.mark.skip(reason="outdated") @pytest.mark.parametrize("playlist_sync, request_data, sync_manager_mock, expected_code, expected_result", ( (None, PLAYLIST_SYNC_CREATE_REQUEST, SYNC_MANAGER_MOCK, 200, PLAYLIST_SYNC_CREATE_RESPONSE), (None, PLAYLIST_SYNC_CREATE_SOURCE_PLAYLIST_DOES_NOT_EXISTS_REQUEST, Mock(get_playlist_info=Mock(return_value=None)), 400, None), (None, PLAYLIST_SYNC_IDENTICAL_FROM_TO_CREATE_REQUEST, SYNC_MANAGER_MOCK, 400, None), (PLAYLIST_SYNC_FIXTURE, PLAYLIST_SYNC_CREATE_REQUEST, SYNC_MANAGER_MOCK, 409, None), ), indirect=("playlist_sync", "sync_manager_mock")) def test_create_playlist_sync(aws_context, playlist_sync_service, application_instance, service_account, playlist_sync, request_data, sync_manager_mock, expected_code, expected_result): event = { "path": f"/playlistsync/{application_instance.id}/playlists", "httpMethod": "POST", "headers": {"Content-Type": "application/json", "Authorization": "test"}, "body": json.dumps(dict({ "toServiceAccountId": service_account.id, }, **request_data)) } result = lambda_handler(event, aws_context) assert result["statusCode"] == expected_code, f"Something went wrong. Body: {result['body']}" if expected_result is not None: body = json.loads(result["body"]) playlist_sync_id = body["id"] assert body.pop("id") == playlist_sync_id assert body.pop("applicationId") == application_instance.id assert body.pop("toServiceAccountId") == service_account.id assert body.pop("lastUpdated") assert body.pop("createdAt") assert body == expected_result assert playlist_sync_service.exists_by_pk(playlist_sync_id) sync_manager_mock.return_value.get_playlist_info.assert_called_once_with(request_data["fromPlaylistId"]) @pytest.mark.skip(reason="outdated") @pytest.mark.parametrize("playlist_sync, request_data, sync_manager_mock, expected_code, expected_result", ( (None, PLAYLIST_SYNC_UPDATE_REQUEST, SYNC_MANAGER_MOCK, 404, None), ((PLAYLIST_SYNC_FIXTURE,), PLAYLIST_SYNC_UPDATE_REQUEST, SYNC_MANAGER_MOCK, 200, PLAYLIST_SYNC_UPDATE_RESPONSE), ((PLAYLIST_SYNC_FIXTURE_2, PLAYLIST_SYNC_FIXTURE), PLAYLIST_SYNC_UPDATE_REQUEST, SYNC_MANAGER_MOCK, 409, None), ), indirect=("playlist_sync", "sync_manager_mock")) def test_update_playlist_sync(aws_context, playlist_sync_service, application_instance, service_account, playlist_sync, request_data, sync_manager_mock, expected_code, expected_result): sync_id = playlist_sync[0].id if playlist_sync is not None else NOT_EXISTING_ID event = { "path": f"/playlistsync/{application_instance.id}/playlists/{sync_id}", "httpMethod": "PUT", "headers": {"Content-Type": "application/json", "Authorization": "test"}, "body": json.dumps(dict({ "toServiceAccountId": service_account.id, }, **request_data)) } result = lambda_handler(event, aws_context) assert result["statusCode"] == expected_code, f"Something went wrong. Body: {result['body']}" if expected_result is not None: body = json.loads(result["body"]) assert body.pop("id") == sync_id assert body.pop("applicationId") == application_instance.id assert body.pop("toServiceAccountId") == service_account.id assert body.pop("lastUpdated") assert body.pop("createdAt") assert body == expected_result assert playlist_sync_service.select_by_pk(sync_id).active is False sync_manager_mock.return_value.get_playlist_info.assert_called_once_with(request_data["fromPlaylistId"]) # TODO: @pytest.mark.skip @pytest.mark.parametrize("playlist_sync, expected_code", ( (None, 404), (PLAYLIST_SYNC_FIXTURE, 200), ), indirect=("playlist_sync",)) def test_execute_playlist_sync(aws_context, playlist_sync_service, application_instance, service_account, playlist_sync, expected_code): sync_id = playlist_sync.id if playlist_sync is not None else NOT_EXISTING_ID event = { "path": f"/playlistsync/{application_instance.id}/playlists/{sync_id}/execute", "httpMethod": "POST", "headers": {"Content-Type": "application/json", "Authorization": "test"} } result = lambda_handler(event, aws_context) assert result["statusCode"] == expected_code, f"Something went wrong. Body: {result['body']}"