This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
user_test.go
91 lines (72 loc) · 1.79 KB
/
user_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
package gapi
import (
"testing"
"github.com/gobs/pretty"
)
const (
getUsersJSON = `[{"id":1,"email":"users@localhost","isAdmin":true}]`
getUserJSON = `{"id":2,"email":"user@localhost","isGrafanaAdmin":false}`
getUserByEmailJSON = `{"id":3,"email":"userByEmail@localhost","isGrafanaAdmin":true}`
getUserUpdateJSON = `{"id":4,"email":"userUpdate@localhost","isGrafanaAdmin":false}`
)
func TestUsers(t *testing.T) {
client := gapiTestToolsFromCalls(t, []mockServerCall{
{200, getUsersJSON},
{200, "null"},
})
resp, err := client.Users()
if err != nil {
t.Fatal(err)
}
t.Log(pretty.PrettyFormat(resp))
if len(resp) != 1 {
t.Fatal("No users were returned.")
}
user := resp[0]
if user.Email != "users@localhost" ||
user.ID != 1 ||
user.IsAdmin != true {
t.Error("Not correctly parsing returned users.")
}
}
func TestUser(t *testing.T) {
client := gapiTestTools(t, 200, getUserJSON)
user, err := client.User(1)
if err != nil {
t.Fatal(err)
}
t.Log(pretty.PrettyFormat(user))
if user.Email != "user@localhost" ||
user.ID != 2 ||
user.IsAdmin != false {
t.Error("Not correctly parsing returned user.")
}
}
func TestUserByEmail(t *testing.T) {
client := gapiTestTools(t, 200, getUserByEmailJSON)
user, err := client.UserByEmail("admin@localhost")
if err != nil {
t.Fatal(err)
}
t.Log(pretty.PrettyFormat(user))
if user.Email != "userByEmail@localhost" ||
user.ID != 3 ||
user.IsAdmin != true {
t.Error("Not correctly parsing returned user.")
}
}
func TestUserUpdate(t *testing.T) {
client := gapiTestToolsFromCalls(t, []mockServerCall{
{200, getUserJSON},
{200, getUserUpdateJSON},
})
user, err := client.User(4)
if err != nil {
t.Fatal(err)
}
user.IsAdmin = true
err = client.UserUpdate(user)
if err != nil {
t.Error(err)
}
}