forked from xanzy/go-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_access_tokens_test.go
125 lines (109 loc) · 3.79 KB
/
group_access_tokens_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
//
// Copyright 2022, Masahiro Yoshida
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package gitlab
import (
"net/http"
"reflect"
"testing"
"time"
)
func TestListGroupAccessTokens(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
mux.HandleFunc("/api/v4/groups/1/access_tokens", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
mustWriteHTTPResponse(t, w, "testdata/list_group_access_tokens.json")
})
groupAccessTokens, _, err := client.GroupAccessTokens.ListGroupAccessTokens(1, &ListGroupAccessTokensOptions{Page: 1, PerPage: 20})
if err != nil {
t.Errorf("GroupAccessTokens.ListGroupAccessTokens returned error: %v", err)
}
time1, err := time.Parse(time.RFC3339, "2021-03-09T21:11:47.271Z")
if err != nil {
t.Errorf("GroupAccessTokens.ListGroupAccessTokens returned error: %v", err)
}
time2, err := time.Parse(time.RFC3339, "2021-03-09T21:11:47.340Z")
if err != nil {
t.Errorf("GroupAccessTokens.ListGroupAccessTokens returned error: %v", err)
}
want := []*GroupAccessToken{
{
ID: 1876,
UserID: 2453,
Name: "token 10",
Scopes: []string{"api", "read_api", "read_repository", "write_repository"},
CreatedAt: &time1,
Active: true,
Revoked: false,
AccessLevel: AccessLevelValue(40),
},
{
ID: 1877,
UserID: 2456,
Name: "token 8",
Scopes: []string{"api", "read_api", "read_repository", "write_repository"},
CreatedAt: &time2,
Active: true,
Revoked: false,
AccessLevel: AccessLevelValue(30),
},
}
if !reflect.DeepEqual(want, groupAccessTokens) {
t.Errorf("GroupAccessTokens.ListGroupAccessTokens returned %+v, want %+v", groupAccessTokens, want)
}
}
func TestCreateGroupAccessToken(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
mux.HandleFunc("/api/v4/groups/1/access_tokens", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
mustWriteHTTPResponse(t, w, "testdata/create_group_access_token.json")
})
groupAccessToken, _, err := client.GroupAccessTokens.CreateGroupAccessToken(1, nil)
if err != nil {
t.Errorf("GroupAccessTokens.CreateGroupAccessToken returned error: %v", err)
}
time1, err := time.Parse(time.RFC3339, "2021-03-09T21:11:47.271Z")
if err != nil {
t.Errorf("GroupAccessTokens.CreateGroupAccessToken returned error: %v", err)
}
want := &GroupAccessToken{
ID: 1876,
UserID: 2453,
Name: "token 10",
Scopes: []string{"api", "read_api", "read_repository", "write_repository"},
ExpiresAt: nil,
CreatedAt: &time1,
Active: true,
Revoked: false,
Token: "2UsevZE1x1ZdFZW4MNzH",
AccessLevel: AccessLevelValue(40),
}
if !reflect.DeepEqual(want, groupAccessToken) {
t.Errorf("GroupAccessTokens.CreateGroupAccessToken returned %+v, want %+v", groupAccessToken, want)
}
}
func TestDeleteGroupAccessToken(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
mux.HandleFunc("/api/v4/groups/1/access_tokens/1234", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.GroupAccessTokens.DeleteGroupAccessToken("1", 1234)
if err != nil {
t.Errorf("GroupAccessTokens.DeleteGroupAccessToken returned error: %v", err)
}
}