|
1 | 1 | package api |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "api.audius.co/api/dbv1" |
| 8 | + "api.audius.co/trashid" |
7 | 9 | "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
8 | 11 | ) |
9 | 12 |
|
10 | 13 | func TestPlaylistsEndpoint(t *testing.T) { |
@@ -60,3 +63,79 @@ func TestPlaylistsEndpointWithAlbumPermalink(t *testing.T) { |
60 | 63 | "data.0.playlist_name": "album by permalink", |
61 | 64 | }) |
62 | 65 | } |
| 66 | + |
| 67 | +// A permalink-based lookup of a private playlist works for anonymous callers. |
| 68 | +func TestPlaylistsEndpointPrivatePermalinkAnonymous(t *testing.T) { |
| 69 | + app := testAppWithFixtures(t) |
| 70 | + ctx := context.Background() |
| 71 | + require.NotNil(t, app.writePool, "test requires write pool") |
| 72 | + |
| 73 | + _, err := app.writePool.Exec(ctx, `UPDATE playlists SET is_private = true WHERE playlist_id = 500 AND is_current = true`) |
| 74 | + require.NoError(t, err) |
| 75 | + |
| 76 | + var resp struct { |
| 77 | + Data []dbv1.Playlist |
| 78 | + } |
| 79 | + status, body := testGet(t, app, "/v1/full/playlists?permalink=/PlaylistsByPermalink/playlist/playlist-by-permalink", &resp) |
| 80 | + assert.Equal(t, 200, status) |
| 81 | + assert.Len(t, resp.Data, 1, "permalink lookup must return private playlist even without auth") |
| 82 | + |
| 83 | + jsonAssert(t, body, map[string]any{ |
| 84 | + "data.0.id": "eYake", |
| 85 | + "data.0.playlist_name": "playlist by permalink", |
| 86 | + "data.0.is_private": true, |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +// An ID-based lookup must NOT return private playlists to anonymous callers. |
| 91 | +func TestPlaylistsEndpointPrivateByIdHiddenFromAnonymous(t *testing.T) { |
| 92 | + app := testAppWithFixtures(t) |
| 93 | + ctx := context.Background() |
| 94 | + require.NotNil(t, app.writePool, "test requires write pool") |
| 95 | + |
| 96 | + _, err := app.writePool.Exec(ctx, `UPDATE playlists SET is_private = true WHERE playlist_id = 500 AND is_current = true`) |
| 97 | + require.NoError(t, err) |
| 98 | + |
| 99 | + var resp struct { |
| 100 | + Data []dbv1.Playlist |
| 101 | + } |
| 102 | + status, _ := testGet(t, app, "/v1/full/playlists?id=eYake", &resp) |
| 103 | + assert.Equal(t, 200, status) |
| 104 | + assert.Len(t, resp.Data, 0, "private playlist must not be returned for ID-based anonymous lookup") |
| 105 | +} |
| 106 | + |
| 107 | +// The single playlist endpoint must also hide private playlists from anonymous callers. |
| 108 | +func TestGetPlaylistPrivateAnonymous404(t *testing.T) { |
| 109 | + app := testAppWithFixtures(t) |
| 110 | + ctx := context.Background() |
| 111 | + require.NotNil(t, app.writePool, "test requires write pool") |
| 112 | + |
| 113 | + _, err := app.writePool.Exec(ctx, `UPDATE playlists SET is_private = true WHERE playlist_id = 500 AND is_current = true`) |
| 114 | + require.NoError(t, err) |
| 115 | + |
| 116 | + status, _ := testGet(t, app, "/v1/full/playlists/eYake") |
| 117 | + assert.Equal(t, 404, status, "private playlist must 404 for anonymous ID-based fetch") |
| 118 | +} |
| 119 | + |
| 120 | +// The single playlist endpoint must return private playlists to their owner. |
| 121 | +func TestGetPlaylistPrivateOwnerAllowed(t *testing.T) { |
| 122 | + app := testAppWithFixtures(t) |
| 123 | + // user 7's fixture wallet has no test signature, so bypass the auth |
| 124 | + // middleware and let user_id alone identify the owner for this test. |
| 125 | + app.skipAuthCheck = true |
| 126 | + ctx := context.Background() |
| 127 | + require.NotNil(t, app.writePool, "test requires write pool") |
| 128 | + |
| 129 | + // playlist 500 is owned by user 7 |
| 130 | + _, err := app.writePool.Exec(ctx, `UPDATE playlists SET is_private = true WHERE playlist_id = 500 AND is_current = true`) |
| 131 | + require.NoError(t, err) |
| 132 | + |
| 133 | + ownerId := trashid.MustEncodeHashID(7) |
| 134 | + status, body := testGet(t, app, "/v1/full/playlists/eYake?user_id="+ownerId) |
| 135 | + assert.Equal(t, 200, status, "owner must be able to view their own private playlist by ID") |
| 136 | + jsonAssert(t, body, map[string]any{ |
| 137 | + "data.0.id": "eYake", |
| 138 | + "data.0.playlist_name": "playlist by permalink", |
| 139 | + "data.0.is_private": true, |
| 140 | + }) |
| 141 | +} |
0 commit comments