GitLabgitlab-org

cli

main
ply8 git calls233 ms99 KiB5 cachedprotocol v2
189 lines5.9 KiBRaw
1//go:build !integration
3package delete
5import (
6 "errors"
7 "testing"
9 "git.sr.ht/~timofurrer/ugh"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 "go.uber.org/mock/gomock"
14 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
16 "gitlab.com/gitlab-org/cli/internal/api"
17 "gitlab.com/gitlab-org/cli/internal/cmdutils"
18 "gitlab.com/gitlab-org/cli/internal/iostreams"
19 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
22func setupExec(t *testing.T, tc *gitlabtesting.TestClient, isTTY bool, opts ...cmdtest.FactoryOption) cmdtest.CmdExecFunc {
23 t.Helper()
25 opts = append(
26 []cmdtest.FactoryOption{
27 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(tc.Client))),
28 },
29 opts...,
30 )
32 return cmdtest.SetupCmdForTest(t, NewCmd, isTTY, opts...)
35func TestDelete_Project(t *testing.T) {
36 t.Parallel()
38 tc := gitlabtesting.NewTestClient(t)
39 tc.MockDeployTokens.EXPECT().
40 DeleteProjectDeployToken("OWNER/REPO", int64(42), gomock.Any()).
41 Return(nil, nil)
43 out, err := setupExec(t, tc, false)("42 --yes")
44 require.NoError(t, err)
46 assert.Equal(t, "✓ Deleted deploy token 42 of project OWNER/REPO.\n", out.OutBuf.String())
47 assert.Empty(t, out.ErrBuf.String())
50func TestDelete_Group(t *testing.T) {
51 t.Parallel()
53 tc := gitlabtesting.NewTestClient(t)
54 tc.MockDeployTokens.EXPECT().
55 DeleteGroupDeployToken("group/sub-group", int64(7), gomock.Any()).
56 Return(nil, nil)
58 out, err := setupExec(t, tc, false)("7 --yes --group group/sub-group")
59 require.NoError(t, err)
61 assert.Equal(t, "✓ Deleted deploy token 7 of group group/sub-group.\n", out.OutBuf.String())
64func TestDelete_RequiresYesWhenNotInteractive(t *testing.T) {
65 t.Parallel()
67 tc := gitlabtesting.NewTestClient(t)
69 _, err := setupExec(t, tc, false)("42")
70 require.Error(t, err)
71 assert.Equal(t, "--yes required when not running interactively", err.Error())
74func TestDelete_InvalidTokenID(t *testing.T) {
75 t.Parallel()
77 tc := gitlabtesting.NewTestClient(t)
79 _, err := setupExec(t, tc, false)("not-a-number --yes")
80 require.Error(t, err)
81 assert.Contains(t, err.Error(), `invalid deploy token ID: "not-a-number"`)
84func TestDelete_APIError(t *testing.T) {
85 t.Parallel()
87 tc := gitlabtesting.NewTestClient(t)
88 tc.MockDeployTokens.EXPECT().
89 DeleteProjectDeployToken("OWNER/REPO", int64(42), gomock.Any()).
90 Return(nil, errors.New("404 Not Found"))
92 _, err := setupExec(t, tc, false)("42 --yes")
93 require.Error(t, err)
95 var exitErr *cmdutils.ExitError
96 require.ErrorAs(t, err, &exitErr)
97 assert.Equal(t, "failed to delete deploy token", exitErr.Details)
98 assert.Contains(t, err.Error(), "404 Not Found")
101func TestDelete_RepoAndGroupAreMutuallyExclusive(t *testing.T) {
102 t.Parallel()
104 tc := gitlabtesting.NewTestClient(t)
106 _, err := setupExec(t, tc, false)("42 --yes --repo OWNER/REPO --group group/sub-group")
107 require.Error(t, err)
108 assert.Contains(t, err.Error(), "none of the others can be")
111// The project scope needs a base repository, and says so before any client is built.
112func TestDelete_OutsideRepositoryWithoutGroup(t *testing.T) {
113 t.Parallel()
115 tc := gitlabtesting.NewTestClient(t)
117 _, err := setupExec(t, tc, false, cmdtest.WithBaseRepoError(errors.New("not a git repository")))("42 --yes")
118 require.Error(t, err)
119 assert.Contains(t, err.Error(), "not a git repository")
122// The group scope does not need a base repository.
123func TestDelete_OutsideRepositoryWithGroup(t *testing.T) {
124 t.Parallel()
126 tc := gitlabtesting.NewTestClient(t)
127 tc.MockDeployTokens.EXPECT().
128 DeleteGroupDeployToken("group/sub-group", int64(7), gomock.Any()).
129 Return(nil, nil)
131 out, err := setupExec(t, tc, false, cmdtest.WithBaseRepoError(errors.New("not a git repository")))("7 --yes --group group/sub-group")
132 require.NoError(t, err)
134 assert.Contains(t, out.OutBuf.String(), "Deleted deploy token 7 of group group/sub-group.")
137func TestDelete_Prompt(t *testing.T) {
138 // NOTE: This test cannot run in parallel because the huh form library
139 // uses global state (charmbracelet/bubbles runeutil sanitizer).
141 t.Run("confirming deletes", func(t *testing.T) {
142 tc := gitlabtesting.NewTestClient(t)
143 tc.MockDeployTokens.EXPECT().
144 DeleteProjectDeployToken("OWNER/REPO", int64(42), gomock.Any()).
145 Return(nil, nil)
147 c := ugh.New(t)
148 c.Expect(ugh.Confirm("Are you ABSOLUTELY SURE you wish to delete deploy token 42?")).Do(ugh.Affirm)
150 out, err := setupExec(t, tc, true, cmdtest.WithConsole(t, c))("42")
151 require.NoError(t, err)
153 assert.Contains(t, out.ErrBuf.String(), "This permanently deletes deploy token 42 of project OWNER/REPO.")
154 assert.Contains(t, out.OutBuf.String(), "Deleted deploy token 42 of project OWNER/REPO.")
155 })
157 t.Run("declining deletes nothing", func(t *testing.T) {
158 tc := gitlabtesting.NewTestClient(t)
159 tc.MockDeployTokens.EXPECT().
160 DeleteProjectDeployToken(gomock.Any(), gomock.Any(), gomock.Any()).
161 Times(0)
163 c := ugh.New(t)
164 c.Expect(ugh.Confirm("Are you ABSOLUTELY SURE you wish to delete deploy token 42?")).Do(ugh.Reject)
166 _, err := setupExec(t, tc, true, cmdtest.WithConsole(t, c))("42")
167 require.ErrorIs(t, err, iostreams.ErrUserCancelled)
168 })
170 // GITLAB_GROUP redirects the command with no flag passed, so the advisory is
171 // the only place the caller sees which target loses a token.
172 t.Run("the advisory names a group from the environment", func(t *testing.T) {
173 t.Setenv("GITLAB_GROUP", "env-group")
175 tc := gitlabtesting.NewTestClient(t)
176 tc.MockDeployTokens.EXPECT().
177 DeleteGroupDeployToken("env-group", int64(7), gomock.Any()).
178 Return(nil, nil)
180 c := ugh.New(t)
181 c.Expect(ugh.Confirm("Are you ABSOLUTELY SURE you wish to delete deploy token 7?")).Do(ugh.Affirm)
183 out, err := setupExec(t, tc, true, cmdtest.WithConsole(t, c))("7")
184 require.NoError(t, err)
186 assert.Contains(t, out.ErrBuf.String(), "This permanently deletes deploy token 7 of group env-group.")
187 assert.Contains(t, out.OutBuf.String(), "Deleted deploy token 7 of group env-group.")
188 })
Diagnostics8 git calls · 233 ms
CallStatusSizeTime0233 ms
discover200185 B4 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 KiB3 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=UPDATING
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
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=3
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=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 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=3
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 · 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=1857
git.response.cache=HIT
git.response.duration_ms=4
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:0200263 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=263
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=aff3dd5052d75f0afe2383713d669f993c82b2f9
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=263
git.pkt.payload.max=236
git.sideband.band1.bytes=236
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=236
git.pack.trailer=4076aed8a003d312c41b730611843784ab33181f
Open pack map
fetchtree:0200139 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=214
git.response.bytes=139
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=d6784907a69994a056e6857dd1ae5d611178e54a
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=139
git.pkt.payload.max=112
git.sideband.band1.bytes=112
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=112
git.pack.trailer=5ea7b6af50711cdef8e8c010ded7a8ba292a8997
Open pack map
fetch2001.6 KiB214 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1630
git.response.cache=MISS
git.response.duration_ms=214
git.fetch.wants=25f822fb1c284c36f8a195e54cc3abc0b4e404c9
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=1630
git.pkt.payload.max=1603
git.sideband.band1.bytes=1603
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
a5e27c96ddd0c892
Request
51fdf455…
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