forked from grafana-tools/sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rest-user_integration_test.go
130 lines (105 loc) · 2.96 KB
/
rest-user_integration_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
122
123
124
125
126
127
128
129
130
package sdk_test
import (
"context"
"testing"
"github.com/grafana-tools/sdk"
)
func Test_User_SmokeTests(t *testing.T) {
shouldSkip(t)
client := getClient(t)
ctx := context.Background()
actualUser, err := client.GetActualUser(ctx)
if err != nil {
t.Fatalf("failed to get actual user: %s", err.Error())
}
retrievedUser, err := client.GetUser(ctx, actualUser.ID)
if err != nil {
t.Fatalf("failed to get user with ID %d: %s", actualUser.ID, err.Error())
}
if actualUser.Name != retrievedUser.Name {
t.Fatalf("retrieved a different user: %s vs. %s", actualUser.Name, retrievedUser.Name)
}
allUsers, err := client.GetAllUsers(ctx)
if err != nil {
t.Fatalf("failed to get all users: %s", err.Error())
}
var found bool
for _, u := range allUsers {
if u.ID == retrievedUser.ID && u.Name == retrievedUser.Name {
found = true
break
}
}
if found == false {
t.Fatalf("failed to find an user with ID %d", actualUser.ID)
}
}
// Test_User_SearchUsers searches for the actual user
// and plays around with pagination.
func Test_User_SearchUsers(t *testing.T) {
shouldSkip(t)
client := getClient(t)
ctx := context.Background()
actualUser, err := client.GetActualUser(ctx)
if err != nil {
t.Fatalf("failed to get actual user: %s", err.Error())
}
q := actualUser.Login
var currInd int = -1
if pgUsers, err := client.SearchUsersWithPaging(ctx, nil, nil, nil); err == nil {
for i, u := range pgUsers.Users {
if u.Login == q {
currInd = i
}
}
} else {
t.Fatalf("failed to search for users with paging: %s", err.Error())
}
if currInd == -1 {
t.Fatal("failed to find the actual user")
}
// TODO(GiedriusS): add test case for index < currInd but we need to add more users first.
// TODO(GiedriusS): add test case for querying with `q'.
// Test that we cannot in general find that user.
perPage := currInd + 1000
numPage := 1
nonExistentUser := "foobar"
afterUsers, err := client.SearchUsersWithPaging(ctx, &nonExistentUser, &perPage, &numPage)
if err != nil {
t.Fatal(err)
}
var afterInd int = -1
for i, u := range afterUsers.Users {
if u.Login == q {
afterInd = i
}
}
if afterInd != -1 {
t.Fatal("actually found the user when we were not supposed to")
}
}
func Test_User_SwitchActualUserContext(t *testing.T) {
shouldSkip(t)
client := getClient(t)
ctx := context.Background()
const orgName = "Test Organization"
org := sdk.Org{
Name: orgName,
}
status, err := client.CreateOrg(ctx, org)
if err != nil {
t.Fatalf("failed to create organization '%s': %s", orgName, err.Error())
}
status, err = client.SwitchActualUserContext(ctx, *status.OrgID)
if err != nil {
t.Fatalf("failed to switch user context to new organization: %s", err.Error())
}
actualOrg, err := client.GetActualOrg(ctx)
if err != nil {
t.Fatalf("failed to get current organization: %s", err.Error())
}
if actualOrg.Name != orgName {
t.Fatalf("current organization is not the expected one. got: %s, want: %s",
actualOrg.Name, orgName)
}
}