Skip to content

Commit e70017c

Browse files
Copilotrickyrombo
andcommitted
Add test coverage for redirect_uris in developer app responses
Agent-Logs-Url: https://github.com/AudiusProject/api/sessions/a6bf18a2-f093-43e3-b3bd-fe551aef12f8 Co-authored-by: rickyrombo <3690498+rickyrombo@users.noreply.github.com>
1 parent a9b6236 commit e70017c

3 files changed

Lines changed: 65 additions & 1 deletion

File tree

api/v1_developer_apps_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ func TestGetDeveloperAppsQueries(t *testing.T) {
1616
assert.NoError(t, err)
1717
assert.Len(t, developerApps, 1)
1818
assert.Equal(t, "0x7d7b6b7a97d1deefe3a1ccc5a13c48e8f055e0b6", developerApps[0].Address)
19+
// redirect_uris must be an empty slice (not nil) when no URIs are registered
20+
assert.Equal(t, []string{}, developerApps[0].RedirectUris)
1921
}
2022

2123
func TestGetDeveloperApp(t *testing.T) {
@@ -29,4 +31,6 @@ func TestGetDeveloperApp(t *testing.T) {
2931
assert.True(t, strings.Contains(string(body), `"user_id":"7eP5n"`))
3032
assert.True(t, strings.Contains(string(body), `"name":"cool app"`))
3133
assert.Equal(t, "cool app", resp.Data.Name)
34+
// redirect_uris must be an empty slice (not nil) when no URIs are registered
35+
assert.Equal(t, []string{}, resp.Data.RedirectUris)
3236
}

api/v1_users_developer_apps_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"api.audius.co/database"
88
"api.audius.co/trashid"
99
"github.com/stretchr/testify/assert"
10+
"github.com/tidwall/gjson"
1011
)
1112

1213
func TestV1UsersDeveloperApps(t *testing.T) {
@@ -68,6 +69,11 @@ func TestV1UsersDeveloperApps(t *testing.T) {
6869
"data.1.name": "app_name_2",
6970
"data.1.description": "app_description_2",
7071
})
72+
// redirect_uris must be an empty array (not null) when no URIs are registered
73+
assert.True(t, gjson.GetBytes(body, "data.0.redirect_uris").IsArray())
74+
assert.Equal(t, 0, len(gjson.GetBytes(body, "data.0.redirect_uris").Array()))
75+
assert.True(t, gjson.GetBytes(body, "data.1.redirect_uris").IsArray())
76+
assert.Equal(t, 0, len(gjson.GetBytes(body, "data.1.redirect_uris").Array()))
7177
}
7278

7379
{
@@ -125,3 +131,57 @@ func TestV1UsersDeveloperAppsIncludeMetrics(t *testing.T) {
125131
"data.0.api_access_keys.#": 0,
126132
})
127133
}
134+
135+
// TestV1UsersDeveloperAppsRedirectUrisOrdering verifies that redirect_uris are returned
136+
// in deterministic order (by oauth_redirect_uris.id) rather than alphabetically,
137+
// and that apps without registered URIs return an empty array rather than null.
138+
func TestV1UsersDeveloperAppsRedirectUrisOrdering(t *testing.T) {
139+
app := emptyTestApp(t)
140+
141+
fixtures := database.FixtureMap{
142+
"users": []map[string]any{
143+
{"user_id": 1, "handle": "user1"},
144+
},
145+
"developer_apps": []map[string]any{
146+
{
147+
"address": "app_address_1",
148+
"user_id": 1,
149+
"name": "app_name_1",
150+
"is_current": true,
151+
"is_delete": false,
152+
"created_at": time.Now(),
153+
},
154+
{
155+
"address": "app_address_2",
156+
"user_id": 1,
157+
"name": "app_name_2",
158+
"is_current": true,
159+
"is_delete": false,
160+
"created_at": time.Now().Add(-time.Second),
161+
},
162+
},
163+
// Insert z before a: if ordering were alphabetical, a would come first.
164+
// With id ordering, z comes first because it was inserted with a lower id.
165+
"oauth_redirect_uris": []map[string]any{
166+
{"client_id": "app_address_1", "redirect_uri": "https://z.example.com/callback"},
167+
{"client_id": "app_address_1", "redirect_uri": "https://a.example.com/callback"},
168+
},
169+
}
170+
171+
database.Seed(app.pool.Replicas[0], fixtures)
172+
173+
status, body := testGet(t, app, "/v1/users/"+trashid.MustEncodeHashID(1)+"/developer-apps")
174+
assert.Equal(t, 200, status)
175+
// app_address_1 has 2 redirect URIs; z was inserted first (lower id) so it must come first
176+
jsonAssert(t, body, map[string]any{
177+
"data.#": 2,
178+
"data.0.address": "app_address_1",
179+
"data.0.redirect_uris.#": 2,
180+
"data.0.redirect_uris.0": "https://z.example.com/callback",
181+
"data.0.redirect_uris.1": "https://a.example.com/callback",
182+
"data.1.address": "app_address_2",
183+
})
184+
// app_address_2 has no redirect URIs; must be an empty array, not null
185+
assert.True(t, gjson.GetBytes(body, "data.1.redirect_uris").IsArray())
186+
assert.Equal(t, 0, len(gjson.GetBytes(body, "data.1.redirect_uris").Array()))
187+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/gofiber/contrib/fiberzap/v2 v2.1.5
2222
github.com/gofiber/contrib/websocket v1.3.4
2323
github.com/gofiber/fiber/v2 v2.52.9
24+
github.com/google/uuid v1.6.0
2425
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
2526
github.com/jackc/pgx/v5 v5.7.4
2627
github.com/jackc/pgxlisten v0.0.0-20241106001234-1d6f6656415c
@@ -122,7 +123,6 @@ require (
122123
github.com/google/go-cmp v0.7.0 // indirect
123124
github.com/google/go-querystring v1.1.0 // indirect
124125
github.com/google/orderedcode v0.0.1 // indirect
125-
github.com/google/uuid v1.6.0 // indirect
126126
github.com/gorilla/rpc v1.2.0 // indirect
127127
github.com/gorilla/websocket v1.5.3 // indirect
128128
github.com/gowebpki/jcs v1.0.0 // indirect

0 commit comments

Comments
 (0)