import pytest from datetime import datetime from http import HTTPStatus as http_status from server.db.models.messages import FeedMessage from server.db.session import db_session as session_scope from tests.factories import AccountFactory, ApplicationFactory, FeedMessageFactory async def set_db(session): session.add_all([ ApplicationFactory.create(slug="app_1", settings=None, types=None), ]) await session.commit() session.add_all([ AccountFactory.create(app_slug="app_1", user_id="u_1"), AccountFactory.create(app_slug="app_1", user_id="u_2"), ]) await session.commit() session.add_all([ FeedMessageFactory.create( id=1, app_slug="app_1", public=False, ttl=30 * 60, message_id=1, event_id=None, account_id=1, status=True, created_at=datetime(2022, 6, 10, 12, 0, 0, 342380), updated_at=datetime(2022, 6, 12, 12, 0, 0, 342380), meta={ "domain": "feed_message", "dsp": "APPLE", "country_code": "_gl", "type": "starred_playlist_tracklist_update", "subject": "starred_track_in_playlist" }, data={ "content": { "track": { "id": 1538003843, "isrc": "GBAHT1901299", "name": "Levitating", "artists": [{"id": None, "name": "Dua Lipa"}] }, "chart": { "name": "Some name", }, "body": "Some body", "title": "Added to a Top 250 Playlist", "current_position": 3, "previous_position": 6, } }, ), FeedMessageFactory.create( id=2, app_slug="app_1", public=False, ttl=30 * 60, message_id=2, event_id=None, account_id=2, status=True, created_at=datetime(2022, 6, 10, 12, 0, 0, 342380), updated_at=datetime(2022, 6, 16, 12, 0, 0, 342380), meta={ "domain": "feed_message", "dsp": "SPOTIFY", "country_code": "ar", "type": "starred_track_starred_playlist_entry", "subject": "starred_track_in_playlist" }, data={ "content": { "track": { "id": 1538003847, "isrc": "QM3DF2141958", "name": "KALEB DI MASI | DJ TAO Turreo Sessions #5", "artists": [{"id": None, "name": "Kaleb Di Masi, DJ Tao"}] }, "chart": { "name": "Some name", }, "body": "⚠️ Kaleb Di Masi, DJ Tao - KALEB DI MASI | DJ TAO Turreo Sessions #5 exited Spotify Daily Top 200 Chart ", "title": "Some title", "current_position": 15, "previous_position": 10, } }, ), FeedMessageFactory.create( id=3, app_slug="app_1", public=False, ttl=30 * 60, message_id=3, event_id=None, account_id=1, status=True, created_at=datetime(2022, 6, 10, 12, 0, 0, 342380), updated_at=datetime(2022, 6, 16, 12, 0, 0, 342380), meta={ "domain": "feed_message", "dsp": "SPOTIFY", "country_code": "ar", "type": "starred_track_starred_playlist_entry", "subject": "starred_track_in_playlist" }, data={ "content": { "track": { "id": 1538003849, "isrc": "QM3DF2141958", "name": "KALEB DI MASI | DJ TAO Turreo Sessions #5", "artists": [{"id": None, "name": "Kaleb Di Masi, DJ Tao"}] }, "chart": { "name": "Some name", }, "body": "⚠️ Kaleb Di Masi, DJ Tao - KALEB DI MASI | DJ TAO Turreo Sessions #5 exited Spotify Daily Top 200 Chart ", "title": "Some title", "current_position": 20, "previous_position": 30, } }, ), ]) await session.commit() async def test_feed_messages(patch_datetime_now, db_session, client, auth): async with session_scope() as session: await set_db(session) headers = {"X-App-Slug": "app_1", "X-User-Id": "u_1"} headers.update(auth) expected_items = [ { "target": "Some name", "artist_name": "Dua Lipa", "country_code": "_gl", "date": "2022-06-10T12:00:00", "vendor": "apple", "time_delta": 856800, "isrc": "GBAHT1901299", "is_new": True, "title": "Added to a Top 250 Playlist", "position": 3, "topic": "playlist_update", "id": 1, "message": "Some body", "track_name": "Levitating", "track_id": "1538003843" }, { "target": "Some name", "artist_name": "Kaleb Di Masi, DJ Tao", "country_code": "ar", "date": "2022-06-10T12:00:00", "vendor": "spotify", "time_delta": 856800, "isrc": "QM3DF2141958", "is_new": True, "title": "Some title", "position": 20, "topic": "starred_playlist_additions", "id": 3, "message": "⚠️ Kaleb Di Masi, DJ Tao - KALEB DI MASI | DJ TAO Turreo Sessions #5 exited Spotify Daily Top 200 Chart ", "track_name": "KALEB DI MASI | DJ TAO Turreo Sessions #5", "track_id": "1538003849" } ] response = await client.get("/api/users/messages/feed/", headers=headers) result = await response.json() result = sorted(result["items"], key=lambda i: i["id"]) assert result == expected_items @pytest.mark.parametrize( "message_id,messages_updated", ( (3, 2), (8, 0), ), ) async def test_put_feed(message_id, messages_updated, db_session, client, auth): async with session_scope() as session: await set_db(session) headers = {"X-App-Slug": "app_1", "X-User-Id": "u_1"} headers.update(auth) response = await client.post("/api/users/messages/feed/view/", headers=headers, json={"message_id": message_id}) assert response.status == http_status.OK async with session_scope(): messages, _ = await FeedMessage.list(account_id=1, status=False) assert len(messages) == messages_updated async def set_type_filter_and_search_db(session): session.add_all([ ApplicationFactory.create(slug="app_10", settings=None, types=None), ]) await session.commit() session.add_all([ AccountFactory.create(app_slug="app_10", user_id="u_10"), AccountFactory.create(app_slug="app_10", user_id="u_20"), ]) await session.commit() await session.commit() session.add_all([ FeedMessageFactory.create( id=10, app_slug="app_10", public=False, ttl=30 * 60, message_id=1, event_id=None, account_id=1, status=True, created_at=datetime(2022, 6, 10, 12, 0, 0, 342380), updated_at=datetime(2022, 6, 12, 12, 0, 0, 342380), meta={ "domain": "feed_message", "dsp": "apple", "country_code": "ca", "type": "starred_track_top_playlist_exit", "subject": "starred_track_in_playlist" }, data={ "content": { "body": "dev  Tom Grennan - Don't Break the Heart has been added to Pure Motivation at position 15" " on Apple Music ", "title": "", "track": { "id": "1579080417", "isrc": "GBARL2101166", "name": "Don't Break the Heart", "artists": [ { "id": "1089156574", "name": "Tom Grennan" }, ], "artists_names": "Tom Grennan", "image_url": "https://is4-ssl.mzstatic.com/image/thumb/886449289103.jpg/3000x3000bb.jpg" }, "playlist": { "id": "pl.047294ae14a24e5993d1f7ab2b127188", "name": "Apple Playlist Exit", "image_url": "http://image/pl.047294ae14a24e5993d1f7ab2b127188?resolution=medium&storefront=ca", "updated_at": "2023-07-16T02:06:47.798000" }, "current_position": None, "previous_position": 15 } }, ), FeedMessageFactory.create( id=11, app_slug="app_10", public=False, ttl=30 * 60, message_id=1, event_id=None, account_id=1, status=True, created_at=datetime(2022, 6, 10, 12, 0, 0, 342380), updated_at=datetime(2022, 6, 12, 12, 0, 0, 342380), meta={ "domain": "feed_message", "dsp": "spotify", "country_code": "ca", "type": "starred_track_top_playlist_entry", "subject": "starred_track_in_playlist" }, data={ "content": { "body": "dev  Tom Grennan - Don't Break the Heart has been added to Pure Motivation at position 15" " on Spotify ", "title": "", "track": { "id": "1579080417", "isrc": "GBARL2101166", "name": "Don't Break the Heart", "artists": [ { "id": "1089156574", "name": "Tom Grennan, Max Developer" } ], "artists_names": "Tom Grennan, Max Developer", "image_url": "https://is4-ssl.mzstatic.com/image/thumb/886449289103.jpg/3000x3000bb.jpg" }, "playlist": { "id": "pl.047294ae14a24e5993d1f7ab2b127188", "name": "Pure Motivation", "image_url": "http://image/pl.047294ae14a24e5993d1f7ab2b127188?resolution=medium&storefront=ca", "updated_at": "2023-07-16T02:06:47.798000" }, "current_position": 15, "previous_position": None } }, ), FeedMessageFactory.create( id=12, app_slug="app_10", public=False, ttl=30 * 60, message_id=1, event_id=None, account_id=1, status=True, created_at=datetime(2022, 6, 10, 12, 0, 0, 342380), updated_at=datetime(2022, 6, 12, 12, 0, 0, 342380), meta={ "domain": "feed_message", "dsp": "apple", "country_code": "ca", "type": "starred_track_top_playlist_major_move", "subject": "starred_track_in_playlist" }, data={ "content": { "body": "dev  Tom Grennan - Don't Break the Heart has been added to Pure Motivation at position 15" " on Apple Music ", "title": "", "track": { "id": "1579080417", "isrc": "GBARL2101166", "name": "Don't Break the Heart", "artists": [ { "id": "1089156574", "name": "Sixx A.M." } ], "artists_names": "Sixx A.M.", "image_url": "https://is4-ssl.mzstatic.com/image/thumb/886449289103.jpg/3000x3000bb.jpg" }, "playlist": { "id": "pl.047294ae14a24e5993d1f7ab2b127188", "name": "Pure Motivation Top", "image_url": "http://image/pl.047294ae14a24e5993d1f7ab2b127188?resolution=medium&storefront=ca", "updated_at": "2023-07-16T02:06:47.798000" }, "current_position": 5, "previous_position": 15 } }, ), FeedMessageFactory.create( id=13, app_slug="app_10", public=False, ttl=30 * 60, message_id=2, event_id=None, account_id=1, status=True, created_at=datetime(2022, 6, 10, 12, 0, 0, 342380), updated_at=datetime(2022, 6, 16, 12, 0, 0, 342380), meta={ "domain": "feed_message", "dsp": "spotify", "country_code": "ar", "type": "starred_track_top_chart_entry", "subject": "starred_track_in_chart" }, data={ "content": { "track": { "id": 1538003847, "isrc": "QM3DF2141958", "name": "KALEB DI MASI | DJ TAO Turreo Sessions #5", "artists": [{"id": None, "name": "Kaleb Di Masi, DJ Tao"}] }, "chart": { "name": "Spotify Top 200", }, "body": "⚠️ Kaleb Di Masi, DJ Tao - KALEB DI MASI | DJ TAO Turreo Sessions #5 exited Spotify Daily" " Top 200 Chart ", "title": "Some title", "current_position": 15, "previous_position": None, } }, ), FeedMessageFactory.create( id=14, app_slug="app_10", public=False, ttl=30 * 60, message_id=2, event_id=None, account_id=1, status=True, created_at=datetime(2022, 6, 10, 12, 0, 0, 342380), updated_at=datetime(2022, 6, 16, 12, 0, 0, 342380), meta={ "domain": "feed_message", "dsp": "spotify", "country_code": "ar", "type": "starred_track_top_chart_exit", "subject": "starred_track_in_chart" }, data={ "content": { "track": { "id": 1538003847, "isrc": "QM3DF2141958", "name": "KALEB DI MASI | DJ TAO Turreo Sessions #5", "artists": [{"id": None, "name": "Kaleb Di Masi, DJ Tao"}] }, "chart": { "name": "Spotify Top 200", }, "body": "⚠️ Kaleb Di Masi, DJ Tao - KALEB DI MASI | DJ TAO Turreo Sessions #5 exited Spotify Daily" " Top 200 Chart ", "title": "Some title", "current_position": None, "previous_position": 10, } }, ), FeedMessageFactory.create( id=15, app_slug="app_10", public=False, ttl=30 * 60, message_id=2, event_id=None, account_id=1, status=True, created_at=datetime(2022, 6, 10, 12, 0, 0, 342380), updated_at=datetime(2022, 6, 16, 12, 0, 0, 342380), meta={ "domain": "feed_message", "dsp": "spotify", "country_code": "ar", "type": "starred_track_top_chart_move", "subject": "starred_track_in_chart" }, data={ "content": { "track": { "id": 1538003847, "isrc": "QM3DF2141958", "name": "KALEB DI MASI | DJ TAO Turreo Sessions #5", "artists": [{"id": None, "name": "Kaleb Di Masi, DJ Tao"}] }, "chart": { "name": "Spotify Top 200", }, "body": "⚠️ Kaleb Di Masi, DJ Tao - KALEB DI MASI | DJ TAO Turreo Sessions #5 exited Spotify Daily" " Top 200 Chart ", "title": "Some title", "current_position": 15, "previous_position": 10, } }, ), FeedMessageFactory.create( id=16, app_slug="app_10", public=False, ttl=30 * 60, message_id=3, event_id=None, account_id=1, status=True, created_at=datetime(2022, 6, 10, 12, 0, 0, 342380), updated_at=datetime(2022, 6, 16, 12, 0, 0, 342380), meta={ "domain": "feed_message", "dsp": "spotify", "country_code": "ar", "type": "starred_playlist_tracklist_update", "subject": "starred_playlist", }, data={ "content": { "playlist": { "id": "67NHHyDFF7CKrIHqBbYqdV", "name": "Playlist tracklist update", "image_url": "https://images-api.atlas.stream/v2/playlists/by_spotify_id/67NHHyDFF7CKrIHqBbYqdV?resolution=medium", "updated_at": "2023-07-16T03:25:55.739000" }, "body": "dev ⭐ FIP : Les morceaux diffusés en playlist tracklist has been updated on Spotify 🏴󠁮󠁵󠁬󠁬󠁿", "title": "", } }, ), FeedMessageFactory.create( id=17, app_slug="app_10", public=False, ttl=30 * 60, message_id=3, event_id=None, account_id=1, status=True, created_at=datetime(2022, 6, 10, 12, 0, 0, 342380), updated_at=datetime(2022, 6, 16, 12, 0, 0, 342380), meta={ "domain": "feed_message", "dsp": "apple", "country_code": "us", "type": "top_chart_tracklist_update", "subject": "top_chart" }, data={ "content": { "body": "dev The Apple Music Top 100 Chart  has been updated", "chart": { "name": "Apple Music Top 100 Chart" }, "title": "" } }, ), ]) @pytest.mark.parametrize( "params,expected_result_len", ( ({}, 8), ({"type": ["starred_track_in_playlist"]}, 3), ({"type": ["starred_track_in_chart"]}, 3), ({"type": ["starred_playlist"]}, 1), ({"type": ["top_chart"]}, 1), ({"type": ["starred_track_in_playlist", "starred_playlist"]}, 4), ({"type": ["starred_track_in_playlist", "top_chart"]}, 4), ({"type": ["starred_track_in_playlist", "starred_track_in_chart"]}, 6), ({"type": ["starred_track_in_chart", "starred_playlist"]}, 4), ({"type": ["starred_track_in_chart", "top_chart"]}, 4), ({"type": ["starred_track_in_playlist", "starred_playlist", "top_chart"]}, 5), ({"type": ["starred_track_in_chart", "starred_playlist", "top_chart"]}, 5), ({"search": "%%%"}, 8), ({"search": "the Heart"}, 3), ({"search": "200 Top Spotify"}, 3), ({"search": "Grennan Developer Max Tom"}, 1), ({"search": "Grennan"}, 2), ({"search": "Pure Motivation"}, 2), ({"search": "Top"}, 5), ({ "type": ["top_chart", "starred_track_in_chart"], "search": "Top", }, 4), ({ "type": ["starred_track_in_playlist", "starred_playlist"], "search": "Top", }, 1), ), ) async def test_feed_messages_type_filter_and_search( patch_datetime_now, db_session, client, auth, params, expected_result_len ): async with session_scope() as session: await set_type_filter_and_search_db(session) headers = {"X-App-Slug": "app_10", "X-User-Id": "u_10"} headers.update(auth) response = await client.get("/api/users/messages/feed/", headers=headers, params=params) result = await response.json() assert len(result["items"]) == expected_result_len