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
/
service_account_test.go
174 lines (146 loc) · 3.56 KB
/
service_account_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package gapi
import (
"net/http"
"testing"
"github.com/gobs/pretty"
)
const (
serviceAccountJSON = `{
"id": 8,
"name": "newSA",
"login": "sa-newsa",
"orgId": 1,
"isDisabled": false,
"role": "",
"tokens": 0,
"avatarUrl": ""
}`
searchServiceAccountsJSON = `{
"totalCount": 2,
"serviceAccounts": [
{
"id": 8,
"name": "newSA",
"login": "sa-newsa",
"orgId": 1,
"isDisabled": false,
"role": "",
"tokens": 1,
"avatarUrl": "/avatar/0e94f33c929884a5163d953582f27fec"
},
{
"id": 9,
"name": "newnewSA",
"login": "sa-newnewsa",
"orgId": 1,
"isDisabled": true,
"role": "Admin",
"tokens": 2,
"avatarUrl": "/avatar/0e29f33c929824a5163d953582e83abe"
}
],
"page": 1,
"perPage": 1000
}`
createServiceAccountTokenJSON = `{"name":"key-name", "key":"mock-api-key"}` //#nosec
deleteServiceAccountTokenJSON = `{"message":"Service account token deleted"}` //#nosec
deleteServiceAccountJSON = `{"message":"service account deleted"}`
getServiceAccountTokensJSON = `[
{
"id": 4,
"name": "testToken",
"created": "2022-06-15T15:19:00+02:00",
"expiration": "2022-06-15T16:17:20+02:00",
"secondsUntilExpiration": 3412.443626017,
"hasExpired": false
},
{
"id": 1,
"name": "testToken2",
"created": "2022-01-15T15:19:00+02:00",
"expiration": "2022-02-15T16:17:20+02:00",
"secondsUntilExpiration": 0,
"hasExpired": true
},
{
"id": 6,
"name": "testTokenzx",
"created": "2022-06-15T15:39:54+02:00",
"expiration": null,
"secondsUntilExpiration": 0,
"hasExpired": false
}
]` //#nosec
)
func TestCreateServiceAccountToken(t *testing.T) {
client := gapiTestTools(t, http.StatusOK, createServiceAccountTokenJSON)
req := CreateServiceAccountTokenRequest{
Name: "key-name",
SecondsToLive: 0,
}
res, err := client.CreateServiceAccountToken(req)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
}
func TestCreateServiceAccount(t *testing.T) {
client := gapiTestTools(t, http.StatusOK, serviceAccountJSON)
isDisabled := true
req := CreateServiceAccountRequest{
Name: "newSA",
Role: "Admin",
IsDisabled: &isDisabled,
}
res, err := client.CreateServiceAccount(req)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
}
func TestUpdateServiceAccount(t *testing.T) {
client := gapiTestTools(t, http.StatusOK, serviceAccountJSON)
isDisabled := false
req := UpdateServiceAccountRequest{
Name: "",
Role: "Admin",
IsDisabled: &isDisabled,
}
res, err := client.UpdateServiceAccount(5, req)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
}
func TestDeleteServiceAccount(t *testing.T) {
client := gapiTestTools(t, http.StatusOK, deleteServiceAccountJSON)
res, err := client.DeleteServiceAccount(int64(1))
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
}
func TestDeleteServiceAccountToken(t *testing.T) {
client := gapiTestTools(t, http.StatusOK, deleteServiceAccountTokenJSON)
res, err := client.DeleteServiceAccountToken(int64(1), int64(1))
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
}
func TestGetServiceAccounts(t *testing.T) {
client := gapiTestTools(t, http.StatusOK, searchServiceAccountsJSON)
res, err := client.GetServiceAccounts()
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
}
func TestGetServiceAccountTokens(t *testing.T) {
client := gapiTestTools(t, http.StatusOK, getServiceAccountTokensJSON)
res, err := client.GetServiceAccountTokens(5)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
}