GitLabgitlab-org
cli
main
1//go:build !integration
3package delete
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"
19)
21func Test_DeleteSSHKey(t *testing.T) {
22 type testCase struct {
23 Name string
24 ExpectedMsg []string
25 wantErr bool
26 cli string
27 wantStderr string
28 setupMock func(tc *gitlabtesting.TestClient)
29 }
31 testCases := []testCase{
32 {
33 Name: "Delete SSH key by ID",
34 ExpectedMsg: []string{"SSH key deleted.\n"},
35 cli: "123",
36 wantErr: false,
37 setupMock: func(tc *gitlabtesting.TestClient) {
38 tc.MockUsers.EXPECT().DeleteSSHKey(int64(123)).Return(nil, nil)
39 },
40 },
41 {
42 Name: "Delete SSH key with non numeric ID",
43 cli: "abc",
44 wantErr: true,
45 wantStderr: "404",
46 setupMock: func(tc *gitlabtesting.TestClient) {
47 tc.MockUsers.EXPECT().DeleteSSHKey(int64(0)).Return(nil, errors.New("404 Not Found"))
48 },
49 },
50 {
51 Name: "Delete non existent SSH key",
52 cli: "999",
53 wantErr: true,
54 wantStderr: "404",
55 setupMock: func(tc *gitlabtesting.TestClient) {
56 tc.MockUsers.EXPECT().DeleteSSHKey(int64(999)).Return(nil, errors.New("404 Not found"))
57 },
58 },
59 {
60 Name: "Delete SSH key without ID",
61 cli: "",
62 wantErr: true,
63 wantStderr: "the <key-id> argument is required when prompts are disabled",
64 setupMock: func(tc *gitlabtesting.TestClient) {},
65 },
66 {
67 Name: "Delete SSH key with unauthorized error",
68 cli: "123",
69 wantErr: true,
70 wantStderr: "401",
71 setupMock: func(tc *gitlabtesting.TestClient) {
72 tc.MockUsers.EXPECT().DeleteSSHKey(int64(123)).Return(nil, errors.New("401 Unauthorized"))
73 },
74 },
75 {
76 Name: "Explicit zero ID returns not found",
77 cli: "0",
78 wantErr: true,
79 wantStderr: "404 Not found",
80 setupMock: func(tc *gitlabtesting.TestClient) {
81 tc.MockUsers.EXPECT().DeleteSSHKey(int64(0)).Return(nil, errors.New("404 Not found"))
82 },
83 },
84 }
86 for _, tc := range testCases {
87 t.Run(tc.Name, func(t *testing.T) {
88 // GIVEN
89 testClient := gitlabtesting.NewTestClient(t)
90 tc.setupMock(testClient)
91 exec := cmdtest.SetupCmdForTest(
92 t,
93 NewCmdDelete,
94 false,
95 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
96 )
98 // WHEN
99 out, err := exec(tc.cli)
101 // THEN
102 if tc.wantErr {
103 require.Error(t, err)
104 assert.Contains(t, err.Error(), tc.wantStderr)
105 return
106 }
107 require.NoError(t, err)
108 for _, msg := range tc.ExpectedMsg {
109 assert.Contains(t, out.OutBuf.String(), msg)
110 }
111 })
112 }
113}
115func TestSshKeyDelete_Completions(t *testing.T) {
116 t.Parallel()
118 testKey := &gitlab.SSHKey{ID: 123, Title: "mysshkey"}
120 testCases := []struct {
121 Name string
122 Args []string
123 setupMock func(tc *gitlabtesting.TestClient)
124 wantComps []cobra.Completion
125 wantDirective cobra.ShellCompDirective
126 }{
127 {
128 Name: "Completes key IDs as ID and title",
129 setupMock: func(tc *gitlabtesting.TestClient) {
130 tc.MockUsers.EXPECT().ListSSHKeys(gomock.Any(), gomock.Any()).
131 Return([]*gitlab.SSHKey{testKey}, &gitlab.Response{}, nil)
132 },
133 wantComps: []cobra.Completion{"123\tmysshkey"},
134 wantDirective: cobra.ShellCompDirectiveNoFileComp,
135 },
136 {
137 Name: "No completions when the second argument is completed",
138 Args: []string{"123", "456"},
139 setupMock: func(tc *gitlabtesting.TestClient) {},
140 wantDirective: cobra.ShellCompDirectiveNoFileComp,
141 },
142 {
143 Name: "API error returns no completions",
144 setupMock: func(tc *gitlabtesting.TestClient) {
145 tc.MockUsers.EXPECT().ListSSHKeys(gomock.Any(), gomock.Any()).
146 Return(nil, nil, errors.New("500 Internal Server Error"))
147 },
148 wantDirective: cobra.ShellCompDirectiveNoFileComp,
149 },
150 }
152 for _, tc := range testCases {
153 t.Run(tc.Name, func(t *testing.T) {
154 t.Parallel()
156 testClient := gitlabtesting.NewTestClient(t)
157 tc.setupMock(testClient)
158 f := cmdtest.NewTestFactory(nil,
159 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
160 )
162 cmd := NewCmdDelete(f)
163 cmd.SetContext(t.Context())
164 completions, directive := cmd.ValidArgsFunction(cmd, tc.Args, "")
166 assert.Equal(t, tc.wantComps, completions)
167 assert.Equal(t, tc.wantDirective, directive)
168 })
169 }
170}
Diagnostics8 git calls · 223 ms
CallStatusSizeTime0223 ms
discover200185 B3 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 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 · 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=4273 git.response.cache=HIT git.response.duration_ms=2 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=2 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 · 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=1857 git.response.cache=HIT git.response.duration_ms=2 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:0200289 B · hit3 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=289 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=1dd4ebf804b44e69b594244d57246af8b61f1ed3 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=289 git.pkt.payload.max=262 git.sideband.band1.bytes=262 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=262 git.pack.trailer=d2a3ec46393f5a5492eae3096186e74c059a8aafOpen 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=211 git.response.bytes=138 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=13515d04f5763fac4ae8f4bd881dd087aa1e9368 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=ccaf8c5a098822b834228ad82df07783df6d712bOpen pack map
fetch2001.4 KiB211 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1405 git.response.cache=MISS git.response.duration_ms=211 git.fetch.wants=a23da2d7ba9c1e7e978ca9c45eec147a277f4aaa 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=1405 git.pkt.payload.max=1378 git.sideband.band1.bytes=1378 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
- a5e2c60e4e964afc
- Request
- ee197915…
- 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