forked from resend/resend-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_keys_test.go
85 lines (72 loc) · 1.87 KB
/
api_keys_test.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
78
79
80
81
82
83
84
85
package resend
import (
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCreateApiKey(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/api-keys", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
ret := `
{
"id": "dacf4072-4119-4d88-932f-6202748ac7c8",
"token": "re_c1tpEyD8_NKFusih9vKVQknRAQfmFcWCv"
}`
fmt.Fprintf(w, ret)
})
req := &CreateApiKeyRequest{
Name: "new api key",
}
resp, err := client.ApiKeys.Create(req)
if err != nil {
t.Errorf("ApiKeys.Create returned error: %v", err)
}
assert.Equal(t, resp.Id, "dacf4072-4119-4d88-932f-6202748ac7c8")
assert.Equal(t, resp.Token, "re_c1tpEyD8_NKFusih9vKVQknRAQfmFcWCv")
}
func TestListApiKeys(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/api-keys", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
ret := `
{
"data": [
{
"id": "91f3200a-df72-4654-b0cd-f202395f5354",
"name": "Production",
"created_at": "2023-04-08T00:11:13.110779+00:00"
}
]
}`
fmt.Fprintf(w, ret)
})
resp, err := client.ApiKeys.List()
if err != nil {
t.Errorf("ApiKeys.List returned error: %v", err)
}
assert.Equal(t, len(resp.Data), 1)
assert.Equal(t, resp.Data[0].Name, "Production")
}
func TestRemoveApiKey(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/api-keys/keyid", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Length", "0")
fmt.Fprint(w, nil)
})
deleted, err := client.ApiKeys.Remove("keyid")
if err != nil {
t.Errorf("ApiKeys.Remove returned error: %v", err)
}
assert.True(t, deleted)
}