-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathresin_test.go
104 lines (94 loc) · 2.09 KB
/
resin_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
package resingo
import (
"net/http"
"net/url"
"os"
"strings"
"testing"
)
var ENV *EnvVars
type EnvVars struct {
Username, Email, Password string
ID int64
Register struct {
Username, Email, Password string
}
}
func init() {
ENV = &EnvVars{
Username: os.Getenv("RESINTEST_USERNAME"),
Password: os.Getenv("RESINTEST_PASSWORD"),
Email: os.Getenv("RESINTEST_EMAIL"),
}
ENV.Register.Username = os.Getenv("RESINTEST_REGISTER_USERNAME")
ENV.Register.Password = os.Getenv("RESINTEST_REGISTER_PASSWORD")
ENV.Register.Email = os.Getenv("RESINTEST_REGISTER_EMAIL")
}
func TestResin(t *testing.T) {
config := &Config{
Username: ENV.Username,
Password: ENV.Password,
ResinEndpoint: apiEndpoint,
}
client := &http.Client{}
t.Run("Authenticate", func(ts *testing.T) {
cfg := *config
ctx := &Context{
Client: client,
Config: &cfg,
}
testAuthenticate(ctx, ts)
})
t.Run("Login", func(ts *testing.T) {
cfg := *config
ctx := &Context{
Client: client,
Config: &cfg,
}
testLogin(ctx, ts)
})
}
func testAuthenticate(ctx *Context, t *testing.T) {
token, err := Authenticate(ctx, Credentials)
if err != nil {
t.Fatal(err)
}
t.Run("ParseToken", func(ts *testing.T) {
claims, err := ParseToken(token)
if err != nil {
ts.Fatal(err)
}
if claims.Username != ctx.Config.Username {
ts.Errorf("expected username %s got %s", ctx.Config.Username, claims.Username)
}
})
}
func testLogin(ctx *Context, t *testing.T) {
err := Login(ctx, Credentials)
if err != nil {
t.Fatal(err)
}
if ctx.Config.tokenClain == nil {
t.Error("expected the token to be saved")
}
}
func TestEncode(t *testing.T) {
sample := []struct {
params []string
expect string
}{
{[]string{"filter,Name", "eq,Milk"}, "$filter=Name%20eq%20'Milk'"},
{[]string{"expand,device"}, "$expand=device"},
}
for _, v := range sample {
param := make(url.Values)
for _, p := range v.params {
s := strings.Split(p, ",")
param.Set(s[0], s[1])
}
e := Encode(param)
if e != v.expect {
t.Errorf("expectes %s got %s", v.expect, e)
}
}
}