-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
77 lines (67 loc) · 2.31 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package suspect
import (
"github.com/gavv/httpexpect/v2"
"net/http"
"strconv"
"testing"
)
type UserCredentials struct {
Email string `json:"email"`
Password string `json:"password"`
}
// Taking advantage of fixed key. This is likely to break in the future.
// https://github.com/supabase/cli/blob/7fa402cd5a95d6a83e32f82113de449656a080e2/internal/start/start.go#L77
const apiKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24ifQ.625_WdcF3KHqz5amU0x2X5WWHP-OEs_4qj0ssLNHzTs"
func newApiClient(t *testing.T, conf config) *httpexpect.Expect {
return httpexpect.WithConfig(httpexpect.Config{
BaseURL: "http://localhost:" + strconv.FormatUint(uint64(conf.Api.Port), 10),
Client: &http.Client{Jar: httpexpect.NewJar()},
Reporter: httpexpect.NewAssertReporter(t),
Printers: []httpexpect.Printer{httpexpect.NewCompactPrinter(t), httpexpect.NewDebugPrinter(t, true)},
}).Builder(func(r *httpexpect.Request) {
r.WithQuery("apikey", apiKey)
})
}
func (s *Suspect) Api(a func(*httpexpect.Expect) *httpexpect.Expect) *Suspect {
s.api = a(s.api)
return s
}
func AssertSignUp(credentials UserCredentials) func(api *httpexpect.Expect) *httpexpect.Expect {
return func(api *httpexpect.Expect) *httpexpect.Expect {
r := api.POST("/auth/v1/signup").
WithJSON(credentials).
Expect().
Status(http.StatusOK)
accessToken := r.JSON().Object().Value("access_token").String().Raw()
api = api.Builder(func(r *httpexpect.Request) {
r.WithHeader("Authorization", "Bearer "+accessToken)
})
return api
}
}
func AssertSignIn(credentials UserCredentials) func(api *httpexpect.Expect) *httpexpect.Expect {
return func(api *httpexpect.Expect) *httpexpect.Expect {
r := api.POST("/auth/v1/token").
WithQuery("grant_type", "password").
WithJSON(credentials).
Expect().
Status(http.StatusOK)
accessToken := r.JSON().Object().Value("access_token").String().Raw()
api = api.Builder(func(r *httpexpect.Request) {
r.WithHeader("Authorization", "Bearer "+accessToken)
})
return api
}
}
func AssertSignOut(api *httpexpect.Expect) *httpexpect.Expect {
api.POST("/auth/v1/logout").
Expect().
Status(http.StatusNoContent)
return api
}
func AssertUser(api *httpexpect.Expect) *httpexpect.Expect {
api.GET("/auth/v1/user").
Expect().
Status(http.StatusOK)
return api
}