Skip to content

Commit b66e943

Browse files
committed
update tests to allow acting as a wallet with sig headers
1 parent d61fbe4 commit b66e943

3 files changed

Lines changed: 75 additions & 8 deletions

File tree

api/server_test.go

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"testing"
1111

12+
"bridgerton.audius.co/api/testdata"
1213
"bridgerton.audius.co/config"
1314
"github.com/jackc/pgx/v5"
1415
"github.com/stretchr/testify/assert"
@@ -87,12 +88,10 @@ func TestHome(t *testing.T) {
8788
assert.True(t, strings.Contains(string(body), "uptime"))
8889
}
8990

90-
func Test200(t *testing.T) {
91+
func Test200UnAuthed(t *testing.T) {
9192
urls := []string{
9293
"/v1/full/users?id=7eP5n&id=_some_invalid_hash_id",
9394

94-
"/v1/full/users/account/0x7d273271690538cf855e5b3002a0dd8c154bb060",
95-
9695
"/v1/full/users/7eP5n",
9796
"/v1/full/users/7eP5n/followers",
9897
"/v1/full/users/7eP5n/following",
@@ -148,6 +147,28 @@ func Test200(t *testing.T) {
148147
}
149148
}
150149

150+
func Test200Authed(t *testing.T) {
151+
urls := []string{
152+
"/v1/full/users/account/0x7d273271690538cf855e5b3002a0dd8c154bb060",
153+
}
154+
155+
for _, u := range urls {
156+
status, body := testGetWithWallet(t, u, "0x7d273271690538cf855e5b3002a0dd8c154bb060")
157+
require.Equal(t, 200, status, u+" "+string(body))
158+
159+
// also test as a user
160+
if strings.Contains(u, "?") {
161+
u += "&user_id=7eP5n"
162+
} else {
163+
u += "?user_id=7eP5n"
164+
}
165+
166+
status, _ = testGetWithWallet(t, u, "0x7d273271690538cf855e5b3002a0dd8c154bb060")
167+
require.Equal(t, 200, status, u+" "+string(body))
168+
}
169+
170+
}
171+
151172
func testGet(t *testing.T, path string, dest ...any) (int, []byte) {
152173
req := httptest.NewRequest("GET", path, nil)
153174
res, err := app.Test(req, -1)
@@ -161,3 +182,28 @@ func testGet(t *testing.T, path string, dest ...any) (int, []byte) {
161182

162183
return res.StatusCode, body
163184
}
185+
186+
// testGetWithWallet makes a GET request with authentication headers for the given wallet address
187+
func testGetWithWallet(t *testing.T, path string, walletAddress string, dest ...any) (int, []byte) {
188+
req := httptest.NewRequest("GET", path, nil)
189+
190+
// Add signature headers if wallet address is provided
191+
if walletAddress != "" {
192+
sigData, exists := testdata.GetSignatureData(walletAddress)
193+
if exists {
194+
req.Header.Set("Encoded-Data-Message", sigData.Message)
195+
req.Header.Set("Encoded-Data-Signature", sigData.Signature)
196+
}
197+
}
198+
199+
res, err := app.Test(req, -1)
200+
assert.NoError(t, err)
201+
body, _ := io.ReadAll(res.Body)
202+
203+
if len(dest) > 0 {
204+
err = json.Unmarshal(body, &dest[0])
205+
assert.NoError(t, err)
206+
}
207+
208+
return res.StatusCode, body
209+
}

api/testdata/signatures.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package testdata
2+
3+
// SignatureData represents a message and its corresponding signature for a wallet
4+
type SignatureData struct {
5+
Message string
6+
Signature string
7+
}
8+
9+
// TestSignatures contains a mapping of wallet addresses to their corresponding message and signature pairs
10+
var TestSignatures = map[string]SignatureData{
11+
"0x7d273271690538cf855e5b3002a0dd8c154bb060": {
12+
Message: "signature:1744763856446",
13+
Signature: "0xbb202be3a7f3a0aa22c1458ef6a3f2f8360fb86791c7b137e8562df0707825c11fa1db01096efd2abc5e6613c4d1e8d4ae1e2b993abdd555fe270c1b17bff0d21c",
14+
},
15+
}
16+
17+
// GetSignatureData returns the message and signature for a given test wallet address
18+
func GetSignatureData(walletAddress string) (SignatureData, bool) {
19+
data, exists := TestSignatures[walletAddress]
20+
return data, exists
21+
}

api/testdata/user_fixtures.csv

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
user_id,handle,handle_lc,is_deactivated,wallet
2-
1,rayjacobson,rayjacobson,f,0x7d273271690538cf855e5b3002a0dd8c154bb060
3-
2,stereosteve,stereosteve,f,
4-
3,someseller,someseller,f,
5-
91,badguy,badguy,t,
1+
user_id,handle,handle_lc,is_deactivated,wallet,playlist_library
2+
1,rayjacobson,rayjacobson,f,0x7d273271690538cf855e5b3002a0dd8c154bb060,"{""contents"":[{""type"":""playlist123"",""playlist_id"":""123""},{""type"":""explore_playlist"",""playlist_id"":""Audio NFTs""},{""type"":""folder"",""id"":""bbcae31a-7cd2-4a1a-8b54-fdc979a34435"",""name"":""My Nested Playlists"",""contents"":[{""type"":""playlist"",""playlist_id"":""345""}]}]}"
3+
2,stereosteve,stereosteve,f,0x1234567890abcdef,
4+
3,someseller,someseller,f,0x234567890abcdef1,
5+
91,badguy,badguy,t,0x4567890abcdef123,

0 commit comments

Comments
 (0)