This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin_test.go
116 lines (96 loc) · 2.98 KB
/
admin_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
package gapi
import (
"strings"
"testing"
"github.com/esnet/grafana-swagger-api-golang/goclient/client/admin"
"github.com/esnet/grafana-swagger-api-golang/goclient/client/admin_users"
"github.com/esnet/grafana-swagger-api-golang/goclient/models"
"github.com/gobs/pretty"
)
const (
createUserJSON = `{"id":1,"message":"User created"}`
deleteUserJSON = `{"message":"User deleted"}`
updateUserPasswordJSON = `{"message":"User password updated"}`
updateUserPermissionsJSON = `{"message":"User permissions updated"}`
pauseAllAlertsJSON = `{
"alertsAffected": 1,
"state": "Paused",
"message": "alert paused"
}`
)
func TestCreateUser(t *testing.T) {
mocksrv, client := gapiTestTools(t, 200, createUserJSON)
defer mocksrv.Close()
resp, err := client.AdminUsers.AdminCreateUser(admin_users.NewAdminCreateUserParams().WithBody(
&models.AdminCreateUserForm{
Email: "admin@localhost",
Login: "admin",
Name: "Administrator",
Password: "password",
},
), nil)
if err != nil {
t.Error(err)
}
if resp.Payload.ID != 1 {
t.Error("Not correctly parsing returned user message.")
}
}
func TestDeleteUser(t *testing.T) {
mocksrv, client := gapiTestTools(t, 200, deleteUserJSON)
defer mocksrv.Close()
_, err := client.AdminUsers.AdminDeleteUser(admin_users.NewAdminDeleteUserParams().WithUserID(1), nil)
if err != nil {
t.Error(err)
}
}
func TestUpdateUserPassword(t *testing.T) {
mocksrv, client := gapiTestTools(t, 200, updateUserPasswordJSON)
defer mocksrv.Close()
_, err := client.AdminUsers.AdminUpdateUserPassword(admin_users.NewAdminUpdateUserPasswordParams().
WithUserID(1).
WithBody(&models.AdminUpdateUserPasswordForm{
Password: "new-password",
},
), nil)
if err != nil {
t.Error(err)
}
}
func TestUpdateUserPermissions(t *testing.T) {
mocksrv, client := gapiTestTools(t, 200, updateUserPermissionsJSON)
defer mocksrv.Close()
_, err := client.AdminUsers.AdminUpdateUserPermissions(admin_users.NewAdminUpdateUserPermissionsParams().
WithUserID(1).
WithBody(&models.AdminUpdateUserPermissionsForm{
IsGrafanaAdmin: false,
},
), nil)
if err != nil {
t.Error(err)
}
}
func TestPauseAllAlerts(t *testing.T) {
mocksrv, client := gapiTestTools(t, 200, pauseAllAlertsJSON)
defer mocksrv.Close()
res, err := client.Admin.PauseAllAlerts(admin.NewPauseAllAlertsParams().WithBody(
&models.PauseAllAlertsCommand{
Paused: true,
},
), nil)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
if res.Payload.State != "Paused" {
t.Error("pause all alerts response should contain the correct response message")
}
}
func TestPauseAllAlerts_500(t *testing.T) {
server, client := gapiTestTools(t, 500, pauseAllAlertsJSON)
defer server.Close()
_, err := client.Admin.PauseAllAlerts(admin.NewPauseAllAlertsParams(), nil)
if !strings.Contains(*err.(*admin.PauseAllAlertsInternalServerError).Payload.Message, "alert paused") {
t.Errorf("expected error to contain 'status: 500'; got: %s", err.Error())
}
}