GitLabgitlab-org
cli
main
1//go:build !integration
3package revoke
5import (
6 "testing"
7 "time"
9 "github.com/spf13/cobra"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 "go.uber.org/mock/gomock"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
17 "gitlab.com/gitlab-org/cli/internal/api"
18 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
19)
21// noMorePages creates a response that indicates no more pages are available
22func noMorePages() *gitlab.Response {
23 return &gitlab.Response{NextPage: 0}
24}
26func TestRevokePersonalAccessToken(t *testing.T) {
27 type testCase struct {
28 name string
29 cli string
30 expectedOut string
31 wantErr bool
32 wantStderr string
33 setupMock func(tc *gitlabtesting.TestClient)
34 }
36 testUser := &gitlab.User{
37 ID: 1,
38 Username: "johndoe",
39 Name: "John Doe",
40 Email: "john.doe@acme.com",
41 }
43 testPAT := &gitlab.PersonalAccessToken{
44 ID: 10183862,
45 Name: "my-pat",
46 Revoked: false,
47 CreatedAt: parseTime("2024-07-08T01:23:04.311Z"),
48 Scopes: []string{"k8s_proxy"},
49 UserID: 926857,
50 Active: true,
51 ExpiresAt: new(gitlab.ISOTime(*parseTime("2024-08-07T00:00:00Z"))),
52 Token: "sometoken",
53 }
55 testCases := []testCase{
56 {
57 name: "revoke personal access token as text",
58 cli: "--user @me my-pat",
59 expectedOut: "revoked @me my-pat 10183862\n",
60 setupMock: func(tc *gitlabtesting.TestClient) {
61 tc.MockUsers.EXPECT().
62 CurrentUser(gomock.Any()).
63 Return(testUser, nil, nil)
64 tc.MockPersonalAccessTokens.EXPECT().
65 ListPersonalAccessTokens(gomock.Any(), gomock.Any()).
66 Return([]*gitlab.PersonalAccessToken{testPAT}, noMorePages(), nil)
67 tc.MockPersonalAccessTokens.EXPECT().
68 RevokePersonalAccessTokenByID(int64(10183862)).
69 Return(nil, nil)
70 },
71 },
72 }
74 for _, tc := range testCases {
75 t.Run(tc.name, func(t *testing.T) {
76 // GIVEN
77 testClient := gitlabtesting.NewTestClient(t)
78 tc.setupMock(testClient)
79 exec := cmdtest.SetupCmdForTest(
80 t,
81 NewCmdRevoke,
82 true,
83 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
84 )
86 // WHEN
87 out, err := exec(tc.cli)
89 // THEN
90 if tc.wantErr {
91 require.Error(t, err)
92 assert.Contains(t, err.Error(), tc.wantStderr)
93 return
94 }
95 require.NoError(t, err)
96 assert.Equal(t, tc.expectedOut, out.OutBuf.String())
97 assert.Empty(t, out.ErrBuf.String())
98 })
99 }
100}
102func TestRevokeGroupAccessToken(t *testing.T) {
103 type testCase struct {
104 name string
105 cli string
106 expectedOut string
107 wantErr bool
108 wantStderr string
109 setupMock func(tc *gitlabtesting.TestClient)
110 }
112 testGroupToken := &gitlab.GroupAccessToken{
113 PersonalAccessToken: gitlab.PersonalAccessToken{
114 ID: 10190772,
115 UserID: 21989300,
116 Name: "my-group-token",
117 Scopes: []string{"read_registry", "read_repository"},
118 CreatedAt: parseTime("2024-07-08T17:33:34.829Z"),
119 ExpiresAt: new(gitlab.ISOTime(*parseTime("2024-08-07T00:00:00Z"))),
120 Active: true,
121 Revoked: false,
122 Token: "glpat-yz2791KMU-xxxxxxxxx",
123 },
124 AccessLevel: gitlab.DeveloperPermissions,
125 }
127 testCases := []testCase{
128 {
129 name: "revoke group access token as text",
130 cli: "--group GROUP my-group-token",
131 expectedOut: "revoked my-group-token 10190772\n",
132 setupMock: func(tc *gitlabtesting.TestClient) {
133 tc.MockGroupAccessTokens.EXPECT().
134 ListGroupAccessTokens("GROUP", gomock.Any(), gomock.Any()).
135 Return([]*gitlab.GroupAccessToken{testGroupToken}, noMorePages(), nil)
136 tc.MockGroupAccessTokens.EXPECT().
137 RevokeGroupAccessToken("GROUP", int64(10190772)).
138 Return(nil, nil)
139 },
140 },
141 }
143 for _, tc := range testCases {
144 t.Run(tc.name, func(t *testing.T) {
145 // GIVEN
146 testClient := gitlabtesting.NewTestClient(t)
147 tc.setupMock(testClient)
148 exec := cmdtest.SetupCmdForTest(
149 t,
150 NewCmdRevoke,
151 true,
152 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
153 )
155 // WHEN
156 out, err := exec(tc.cli)
158 // THEN
159 if tc.wantErr {
160 require.Error(t, err)
161 assert.Contains(t, err.Error(), tc.wantStderr)
162 return
163 }
164 require.NoError(t, err)
165 assert.Equal(t, tc.expectedOut, out.OutBuf.String())
166 assert.Empty(t, out.ErrBuf.String())
167 })
168 }
169}
171func TestRevokeProjectAccessToken(t *testing.T) {
172 type testCase struct {
173 name string
174 cli string
175 expectedOut string
176 wantErr bool
177 wantStderr string
178 setupMock func(tc *gitlabtesting.TestClient)
179 }
181 testProjectToken := &gitlab.ProjectAccessToken{
182 PersonalAccessToken: gitlab.PersonalAccessToken{
183 ID: 10191548,
184 UserID: 21990679,
185 Name: "my-project-token",
186 Scopes: []string{"api", "read_repository"},
187 CreatedAt: parseTime("2024-07-08T19:47:14.727Z"),
188 ExpiresAt: new(gitlab.ISOTime(*parseTime("2024-08-07T00:00:00Z"))),
189 Active: true,
190 Revoked: false,
191 Token: "glpat-dfsdfjksjdfslkdfjsd",
192 },
193 AccessLevel: gitlab.DeveloperPermissions,
194 }
196 testCases := []testCase{
197 {
198 name: "revoke project access token as text",
199 cli: "my-project-token",
200 expectedOut: "revoked my-project-token 10191548\n",
201 setupMock: func(tc *gitlabtesting.TestClient) {
202 tc.MockProjectAccessTokens.EXPECT().
203 ListProjectAccessTokens("OWNER/REPO", gomock.Any(), gomock.Any()).
204 Return([]*gitlab.ProjectAccessToken{testProjectToken}, noMorePages(), nil)
205 tc.MockProjectAccessTokens.EXPECT().
206 RevokeProjectAccessToken("OWNER/REPO", int64(10191548)).
207 Return(nil, nil)
208 },
209 },
210 }
212 for _, tc := range testCases {
213 t.Run(tc.name, func(t *testing.T) {
214 // GIVEN
215 testClient := gitlabtesting.NewTestClient(t)
216 tc.setupMock(testClient)
217 exec := cmdtest.SetupCmdForTest(
218 t,
219 NewCmdRevoke,
220 true,
221 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
222 )
224 // WHEN
225 out, err := exec(tc.cli)
227 // THEN
228 if tc.wantErr {
229 require.Error(t, err)
230 assert.Contains(t, err.Error(), tc.wantStderr)
231 return
232 }
233 require.NoError(t, err)
234 assert.Equal(t, tc.expectedOut, out.OutBuf.String())
235 assert.Empty(t, out.ErrBuf.String())
236 })
237 }
238}
240func parseTime(s string) *time.Time {
241 t, _ := time.Parse(time.RFC3339, s)
242 return &t
243}
245func TestRevoke_Completions(t *testing.T) {
246 testClient := gitlabtesting.NewTestClient(t)
247 // No mock expectations: with an argument already present the completion
248 // must not call the API.
249 f := cmdtest.NewTestFactory(nil,
250 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
251 )
253 cmd := NewCmdRevoke(f)
254 cmd.SetContext(t.Context())
256 comps, directive := cmd.ValidArgsFunction(cmd, []string{"123"}, "")
257 assert.Empty(t, comps)
258 assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive)
260 testCases := map[string]struct {
261 wantComps []cobra.Completion
262 wantDirective cobra.ShellCompDirective
263 }{
264 "group": {wantDirective: cobra.ShellCompDirectiveNoFileComp},
265 "user": {wantComps: []cobra.Completion{cobra.CompletionWithDesc("@me", "Current user")}, wantDirective: cobra.ShellCompDirectiveNoFileComp},
266 }
268 for flag, tc := range testCases {
269 t.Run(flag, func(t *testing.T) {
270 compFn, ok := cmd.GetFlagCompletionFunc(flag)
271 require.True(t, ok, "--%s should have a completion function registered", flag)
273 comps, directive := compFn(cmd, nil, "")
274 assert.Equal(t, tc.wantComps, comps)
275 assert.Equal(t, tc.wantDirective, directive)
276 })
277 }
278}
Diagnostics8 git calls · 203 ms
CallStatusSizeTime0203 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchtree:02001.8 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200358 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=358 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=218b543e075f31e84c52193d193103408c52cb34 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=358 git.pkt.payload.max=331 git.sideband.band1.bytes=331 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=331 git.pack.trailer=fbb77d44047cd3e1ee682eb9483227c59489e50fOpen pack map
fetchtree:0200138 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=138 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=fb2a827af46071634b453729567d34a70b35ae11 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=138 git.pkt.payload.max=111 git.sideband.band1.bytes=111 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=111 git.pack.trailer=d6388980558ca093ef295f67582fc274896ef765Open pack map
fetch2001.9 KiB192 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=192 git.response.bytes=1930 git.response.cache=MISS git.response.duration_ms=192 git.fetch.wants=99000ea9b896051bc0c98d840b292fffa466cb1c git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1930 git.pkt.payload.max=1903 git.sideband.band1.bytes=1903 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e2e6a79a8becfa
- Request
- f38aba1c…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format