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
/
api_key_test.go
74 lines (61 loc) · 1.55 KB
/
api_key_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
package gapi
import (
"context"
"testing"
"time"
"github.com/esnet/grafana-swagger-api-golang/goclient/client/api_keys"
"github.com/esnet/grafana-swagger-api-golang/goclient/models"
"github.com/gobs/pretty"
)
const (
createAPIKeyJSON = `{"name":"key-name", "key":"mock-api-key"}` //#nosec
deleteAPIKeyJSON = `{"message":"API key deleted"}` //#nosec
getAPIKeysJSON = `[
{
"id": 1,
"name": "key-name-2",
"role": "Viewer"
},
{
"id": 2,
"name": "key-name-2",
"role": "Admin",
"expiration": "2021-10-30T10:52:03+03:00"
}
]` //#nosec
)
func TestCreateAPIKey(t *testing.T) {
mocksrv, client := gapiTestTools(t, 200, createAPIKeyJSON)
defer mocksrv.Close()
params := api_keys.AddAPIkeyParams{
Body: &models.AddCommand{
Name: "key-name",
Role: "Viewer",
SecondsToLive: int64(time.Hour),
},
Context: context.Background(),
}
resp, err := client.APIKeys.AddAPIkey(¶ms, nil)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(resp))
}
func TestDeleteAPIKey(t *testing.T) {
mocksrv, client := gapiTestTools(t, 200, deleteAPIKeyJSON)
defer mocksrv.Close()
res, err := client.APIKeys.DeleteAPIkey(api_keys.NewDeleteAPIkeyParams().WithID(1), nil)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
}
func TestGetAPIKeys(t *testing.T) {
mocksrv, client := gapiTestTools(t, 200, getAPIKeysJSON)
defer mocksrv.Close()
res, err := client.APIKeys.GetAPIkeys(api_keys.NewGetAPIkeysParams(), nil)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
}