Skip to content

Commit 15e0756

Browse files
Add test
1 parent 651fa90 commit 15e0756

4 files changed

Lines changed: 47 additions & 10 deletions

File tree

api/auth_middleware.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const (
1818
func (app *ApiServer) recoverAuthorityFromSignatureHeaders(c *fiber.Ctx) (int32, string) {
1919
message := c.Get(messageHeader)
2020
signature := c.Get(signatureHeader)
21-
21+
fmt.Println("message", message)
22+
fmt.Println("signature", signature)
2223
if message == "" || signature == "" {
2324
return 0, ""
2425
}
@@ -60,19 +61,24 @@ func (app *ApiServer) recoverAuthorityFromSignatureHeaders(c *fiber.Ctx) (int32,
6061
return userId, walletLower
6162
}
6263

64+
func (app *ApiServer) getAuthedUserId(c *fiber.Ctx) int32 {
65+
return int32(c.Locals("authedUserId").(int32))
66+
}
67+
68+
func (app *ApiServer) getAuthedWallet(c *fiber.Ctx) string {
69+
return c.Locals("authedWallet").(string)
70+
}
71+
6372
// Middleware to set authedUserId and authedWallet in context
6473
func (app *ApiServer) authMiddleware(c *fiber.Ctx) error {
74+
fmt.Println("authMiddleware")
6575
userId, wallet := app.recoverAuthorityFromSignatureHeaders(c)
6676
c.Locals("authedUserId", userId)
6777
c.Locals("authedWallet", wallet)
6878

6979
return c.Next()
7080
}
7181

72-
func (app *ApiServer) getAuthedUserId(c *fiber.Ctx) int32 {
73-
return int32(c.Locals("authedUserId").(int32))
74-
}
75-
7682
// Middleware that asserts authedUserId is valid
7783
func (app *ApiServer) requiresAuthMiddleware(c *fiber.Ctx) error {
7884
authedUserId := app.getAuthedUserId(c)

api/auth_middleware_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package api
2+
3+
import (
4+
"net/http/httptest"
5+
"testing"
6+
7+
"github.com/gofiber/fiber/v2"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestRecoverAuthorityFromSignatureHeaders(t *testing.T) {
12+
var userId int32
13+
var wallet string
14+
15+
// Create a dummy endpoint to test the authMiddleware
16+
testApp := fiber.New()
17+
testApp.Get("/", app.authMiddleware, func(c *fiber.Ctx) error {
18+
userId, wallet = app.getAuthedUserId(c), app.getAuthedWallet(c)
19+
return c.SendStatus(fiber.StatusOK)
20+
})
21+
22+
req := httptest.NewRequest("GET", "/", nil)
23+
req.Header.Set("Encoded-Data-Message", "signature:1744763856446")
24+
req.Header.Set("Encoded-Data-Signature", "0xbb202be3a7f3a0aa22c1458ef6a3f2f8360fb86791c7b137e8562df0707825c11fa1db01096efd2abc5e6613c4d1e8d4ae1e2b993abdd555fe270c1b17bff0d21c")
25+
26+
_, err := testApp.Test(req, -1)
27+
assert.NoError(t, err)
28+
assert.Equal(t, int32(1), userId)
29+
assert.Equal(t, "0x7d273271690538cf855e5b3002a0dd8c154bb060", wallet)
30+
}

api/fixture_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var (
1515
"user_id": nil,
1616
"handle": nil,
1717
"handle_lc": nil,
18+
"wallet": nil,
1819
"is_current": true,
1920
"is_verified": false,
2021
"created_at": time.Now(),

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
2-
1,rayjacobson,rayjacobson,f
3-
2,stereosteve,stereosteve,f
4-
3,someseller,someseller,f
5-
91,badguy,badguy,t
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,

0 commit comments

Comments
 (0)