-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tokens_test.go
121 lines (100 loc) · 2.93 KB
/
tokens_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package desec
import (
"context"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTokensService_Create(t *testing.T) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
client := New("token", NewDefaultClientOptions())
client.BaseURL = server.URL
mux.HandleFunc("/auth/tokens/", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(rw, "invalid method", http.StatusMethodNotAllowed)
return
}
rw.WriteHeader(http.StatusCreated)
file, err := os.Open("./fixtures/tokens_create.json")
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
defer func() { _ = file.Close() }()
_, err = io.Copy(rw, file)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
newToken, err := client.Tokens.Create(context.Background(), "my new token")
require.NoError(t, err)
expected := &Token{
ID: "3a6b94b5-d20e-40bd-a7cc-521f5c79fab3",
Name: "my new token",
Value: "4pnk7u-NHvrEkFzrhFDRTjGFyX_S",
Created: mustParseTime("2018-09-06T09:08:43.762697Z"),
}
assert.Equal(t, expected, newToken)
}
func TestTokensService_GetAll(t *testing.T) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
client := New("token", NewDefaultClientOptions())
client.BaseURL = server.URL
mux.HandleFunc("/auth/tokens/", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(rw, "invalid method", http.StatusMethodNotAllowed)
return
}
file, err := os.Open("./fixtures/tokens_getall.json")
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
defer func() { _ = file.Close() }()
_, err = io.Copy(rw, file)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
tokens, err := client.Tokens.GetAll(context.Background())
require.NoError(t, err)
expected := []Token{
{
ID: "3159e485-5499-46c0-ae2b-aeb84d627a8e",
Name: "login",
Created: mustParseTime("2018-09-06T07:05:54.080564Z"),
},
{
ID: "76d6e39d-65bc-4ab2-a1b7-6e94eee0a534",
Name: "sample",
Created: mustParseTime("2018-09-06T08:53:26.428396Z"),
},
}
assert.Equal(t, expected, tokens)
}
func TestTokensService_Delete(t *testing.T) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
client := New("token", NewDefaultClientOptions())
client.BaseURL = server.URL
mux.HandleFunc("/auth/tokens/aaa/", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodDelete {
http.Error(rw, "invalid method", http.StatusMethodNotAllowed)
return
}
rw.WriteHeader(http.StatusNoContent)
})
err := client.Tokens.Delete(context.Background(), "aaa")
require.NoError(t, err)
}