GitLabgitlab-org

cli

main
ply8 git calls262 ms99 KiB6 cachedprotocol v2
213 lines7.6 KiBRaw
1//go:build !integration
3package tokenutils
5import (
6 "errors"
7 "testing"
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"
21func TestNameOrIDCompletions(t *testing.T) {
22 t.Parallel()
24 testProjectTokens := []*gitlab.ProjectAccessToken{
25 {ID: 123, Name: "my-project-token", Active: true},
26 {ID: 456, Name: "expired-project-token", Active: false},
27 {ID: 789, Name: "other-project-token", Active: true},
28 }
29 testGroupTokens := []*gitlab.GroupAccessToken{
30 {ID: 321, Name: "my-group-token", Active: true},
31 {ID: 654, Name: "revoked-group-token", Active: false},
32 }
33 testPersonalTokens := []*gitlab.PersonalAccessToken{
34 {ID: 111, Name: "my-personal-token", Active: true},
35 {ID: 222, Name: "revoked-personal-token", Active: false},
36 }
37 testUser := &gitlab.User{ID: 926857, Username: "@me"}
39 testCases := []struct {
40 name string
41 args []string
42 flags map[string]string
43 baseRepoErr error
44 setupMock func(tc *gitlabtesting.TestClient)
45 wantComps []cobra.Completion
46 wantDirective cobra.ShellCompDirective
47 }{
48 {
49 name: "Completes project token IDs and names by default",
50 setupMock: func(tc *gitlabtesting.TestClient) {
51 tc.MockProjectAccessTokens.EXPECT().
52 ListProjectAccessTokens("owner/repo", gomock.Any(), gomock.Any()).
53 Return(testProjectTokens, &gitlab.Response{}, nil)
54 },
55 wantComps: []cobra.Completion{
56 cobra.CompletionWithDesc("123", "my-project-token"), cobra.CompletionWithDesc("my-project-token", "123"),
57 cobra.CompletionWithDesc("789", "other-project-token"), cobra.CompletionWithDesc("other-project-token", "789"),
58 },
59 wantDirective: cobra.ShellCompDirectiveNoFileComp,
60 },
61 {
62 name: "Completes project token IDs and names with --repo",
63 flags: map[string]string{"repo": "other/proj"},
64 setupMock: func(tc *gitlabtesting.TestClient) {
65 tc.MockProjectAccessTokens.EXPECT().
66 ListProjectAccessTokens("other/proj", gomock.Any(), gomock.Any()).
67 Return(testProjectTokens, &gitlab.Response{}, nil)
68 },
69 wantComps: []cobra.Completion{
70 cobra.CompletionWithDesc("123", "my-project-token"), cobra.CompletionWithDesc("my-project-token", "123"),
71 cobra.CompletionWithDesc("789", "other-project-token"), cobra.CompletionWithDesc("other-project-token", "789"),
72 },
73 wantDirective: cobra.ShellCompDirectiveNoFileComp,
74 },
75 {
76 name: "Uses --repo over --group",
77 flags: map[string]string{"repo": "other/proj", "group": "group/sub-group"},
78 setupMock: func(tc *gitlabtesting.TestClient) {
79 tc.MockProjectAccessTokens.EXPECT().
80 ListProjectAccessTokens("other/proj", gomock.Any(), gomock.Any()).
81 Return(testProjectTokens, &gitlab.Response{}, nil)
82 },
83 wantComps: []cobra.Completion{
84 cobra.CompletionWithDesc("123", "my-project-token"), cobra.CompletionWithDesc("my-project-token", "123"),
85 cobra.CompletionWithDesc("789", "other-project-token"), cobra.CompletionWithDesc("other-project-token", "789"),
86 },
87 wantDirective: cobra.ShellCompDirectiveNoFileComp,
88 },
89 {
90 name: "Completes group token IDs and names with --group",
91 flags: map[string]string{"group": "group/sub-group"},
92 setupMock: func(tc *gitlabtesting.TestClient) {
93 tc.MockGroupAccessTokens.EXPECT().
94 ListGroupAccessTokens("group/sub-group", gomock.Any(), gomock.Any()).
95 Return(testGroupTokens, &gitlab.Response{}, nil)
96 },
97 wantComps: []cobra.Completion{
98 cobra.CompletionWithDesc("321", "my-group-token"), cobra.CompletionWithDesc("my-group-token", "321"),
99 },
100 wantDirective: cobra.ShellCompDirectiveNoFileComp,
101 },
102 {
103 name: "Completes own personal token IDs and names with --user @me",
104 flags: map[string]string{"user": "@me"},
105 setupMock: func(tc *gitlabtesting.TestClient) {
106 tc.MockUsers.EXPECT().
107 CurrentUser(gomock.Any()).
108 Return(testUser, nil, nil)
109 tc.MockPersonalAccessTokens.EXPECT().
110 ListPersonalAccessTokens(gomock.Any(), gomock.Any()).
111 Return(testPersonalTokens, &gitlab.Response{}, nil)
112 },
113 wantComps: []cobra.Completion{
114 cobra.CompletionWithDesc("111", "my-personal-token"), cobra.CompletionWithDesc("my-personal-token", "111"),
115 },
116 wantDirective: cobra.ShellCompDirectiveNoFileComp,
117 },
118 {
119 name: "No completions when an argument is already given",
120 args: []string{"123"},
121 setupMock: func(tc *gitlabtesting.TestClient) {},
122 wantComps: nil,
123 wantDirective: cobra.ShellCompDirectiveNoFileComp,
124 },
125 {
126 name: "No project completions without a base repository",
127 baseRepoErr: errors.New("no git remotes found"),
128 setupMock: func(tc *gitlabtesting.TestClient) {},
129 wantComps: nil,
130 wantDirective: cobra.ShellCompDirectiveNoFileComp,
131 },
132 {
133 name: "No completions when listing project tokens fails",
134 setupMock: func(tc *gitlabtesting.TestClient) {
135 tc.MockProjectAccessTokens.EXPECT().
136 ListProjectAccessTokens(gomock.Any(), gomock.Any(), gomock.Any()).
137 Return(nil, nil, errors.New("500 Internal Server Error"))
138 },
139 wantComps: nil,
140 wantDirective: cobra.ShellCompDirectiveNoFileComp,
141 },
142 {
143 name: "No completions when listing group tokens fails",
144 flags: map[string]string{"group": "group/sub-group"},
145 setupMock: func(tc *gitlabtesting.TestClient) {
146 tc.MockGroupAccessTokens.EXPECT().
147 ListGroupAccessTokens(gomock.Any(), gomock.Any(), gomock.Any()).
148 Return(nil, nil, errors.New("500 Internal Server Error"))
149 },
150 wantComps: nil,
151 wantDirective: cobra.ShellCompDirectiveNoFileComp,
152 },
153 {
154 name: "No completions when the user cannot be resolved",
155 flags: map[string]string{"user": "nonexistent-user"},
156 setupMock: func(tc *gitlabtesting.TestClient) {
157 tc.MockUsers.EXPECT().
158 ListUsers(gomock.Any(), gomock.Any()).
159 Return([]*gitlab.User{}, &gitlab.Response{}, nil)
160 },
161 wantComps: nil,
162 wantDirective: cobra.ShellCompDirectiveNoFileComp,
163 },
164 }
166 for _, tc := range testCases {
167 t.Run(tc.name, func(t *testing.T) {
168 t.Parallel()
170 testClient := gitlabtesting.NewTestClient(t)
171 tc.setupMock(testClient)
172 options := []cmdtest.FactoryOption{
173 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
174 }
175 if tc.baseRepoErr != nil {
176 options = append(options, cmdtest.WithBaseRepoError(tc.baseRepoErr))
177 } else {
178 options = append(options, cmdtest.WithBaseRepo("owner", "repo", "gitlab.com"))
179 }
180 f := cmdtest.NewTestFactory(nil, options...)
182 cmd := &cobra.Command{Use: "rotate [<token-name|token-id>]"}
183 cmd.Flags().StringP("user", "U", "", "")
184 cmd.Flags().StringP("group", "g", "", "")
185 cmd.Flags().StringP("repo", "R", "", "")
186 for flag, value := range tc.flags {
187 require.NoError(t, cmd.Flags().Set(flag, value))
188 }
189 cmd.SetContext(t.Context())
191 completions, directive := NameOrIDCompletions(cmd.Context(), f, cmd, tc.args, "")
193 assert.Equal(t, tc.wantComps, completions)
194 assert.Equal(t, tc.wantDirective, directive)
195 })
196 }
199func TestGroupCompletions(t *testing.T) {
200 t.Parallel()
202 comps, directive := GroupCompletions(&cobra.Command{}, nil, "")
203 assert.Empty(t, comps)
204 assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive)
207func TestUserCompletions(t *testing.T) {
208 t.Parallel()
210 comps, directive := UserCompletions(&cobra.Command{}, nil, "")
211 assert.Equal(t, []cobra.Completion{cobra.CompletionWithDesc("@me", "Current user")}, comps)
212 assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive)
Diagnostics8 git calls · 262 ms
CallStatusSizeTime0262 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 · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=2
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
fetchtree:0 · deepen 1020032 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=32926
git.response.cache=HIT
git.response.duration_ms=3
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=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=4
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:02001.8 KiB · hit9 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=9
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=9
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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200358 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=358
git.response.cache=HIT
git.response.duration_ms=4
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=fbb77d44047cd3e1ee682eb9483227c59489e50f
Open pack map
fetchtree:0200143 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=143
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=92a7d66497c7f31e5826eef28aeb6841773b0b04
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=143
git.pkt.payload.max=116
git.sideband.band1.bytes=116
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=116
git.pack.trailer=feb17a6171e412d7f30efc3ef7678e87ec969ecc
Open pack map
fetch2001.7 KiB241 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=241
git.response.bytes=1716
git.response.cache=MISS
git.response.duration_ms=241
git.fetch.wants=a11f4b921e2055f182ee49b7c18192629f2bd011
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=1716
git.pkt.payload.max=1689
git.sideband.band1.bytes=1689
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
a5e2dc2c180ec125
Request
b7f34fef…
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