GitLabgitlab-org
cli
main
1//go:build !integration
3package set
5import (
6 "errors"
7 "testing"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
12 "gitlab.com/gitlab-org/cli/internal/config"
13 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
14)
16type configStub map[string]string
18func (c configStub) Local() (*config.LocalConfig, error) {
19 return nil, nil
20}
22func (c configStub) WriteAll() error {
23 c["_written"] = "true"
24 return nil
25}
27func (c configStub) Reload() (config.Config, error) {
28 return c, nil
29}
31func genKey(host, key string) string {
32 if host != "" {
33 return host + ":" + key
34 }
35 return key
36}
38func (c configStub) Get(host, key string) (string, error) {
39 val, _, err := c.GetWithSource(host, key, true)
40 return val, err
41}
43func (c configStub) GetWithSource(host, key string, searchENVVars bool) (string, string, error) {
44 if v, found := c[genKey(host, key)]; found {
45 return v, "(memory)", nil
46 }
47 return "", "", errors.New("not found")
48}
50func (c configStub) Set(host, key, value string) error {
51 c[genKey(host, key)] = value
52 return nil
53}
55func (c configStub) Aliases() (*config.AliasConfig, error) {
56 return nil, nil
57}
59func (c configStub) Hosts() ([]string, error) {
60 return nil, nil
61}
63func (c configStub) Write() error {
64 c["_written"] = "true"
65 return nil
66}
68func TestConfigSet(t *testing.T) {
69 t.Parallel()
71 tests := []struct {
72 name string
73 cli string
74 expectKey string
75 }{
76 {
77 name: "set key",
78 cli: "editor vim -g",
79 expectKey: "editor",
80 },
81 {
82 name: "set key scoped by host",
83 cli: "editor vim --host gitlab.com -g",
84 expectKey: "gitlab.com:editor",
85 },
86 {
87 name: "set key by alias",
88 cli: "visual vim -g",
89 expectKey: "visual",
90 },
91 {
92 name: "set glab_pager",
93 cli: "glab_pager vim -g",
94 expectKey: "glab_pager",
95 },
96 }
97 for _, tt := range tests {
98 t.Run(tt.name, func(t *testing.T) {
99 t.Parallel()
101 cfg := configStub{}
102 exec := cmdtest.SetupCmdForTest(t, NewCmdSet, true, cmdtest.WithConfig(cfg))
104 out, err := exec(tt.cli)
105 require.NoError(t, err)
107 assert.Empty(t, out.String())
108 assert.Empty(t, out.Stderr())
109 assert.Equal(t, "vim", cfg[tt.expectKey])
110 assert.Equal(t, "true", cfg["_written"])
111 })
112 }
113}
115func TestConfigSet_TokenClearsStaleOAuth2Config(t *testing.T) {
116 t.Parallel()
118 cfg := configStub{"gitlab.com:is_oauth2": "true", "gitlab.com:oauth2_refresh_token": "stale-refresh", "gitlab.com:oauth2_expiry_date": "2026-01-01T00:00:00Z"}
119 exec := cmdtest.SetupCmdForTest(t, NewCmdSet, true, cmdtest.WithConfig(cfg))
121 out, err := exec("token glpat-nOtARealToken --host gitlab.com")
122 require.NoError(t, err)
124 assert.Equal(t, "glpat-nOtARealToken", cfg["gitlab.com:token"])
125 assert.Empty(t, cfg["gitlab.com:is_oauth2"])
126 assert.Empty(t, cfg["gitlab.com:oauth2_refresh_token"])
127 assert.Empty(t, cfg["gitlab.com:oauth2_expiry_date"])
128 assert.Empty(t, out.String(), "the notice belongs on stderr, not in command output")
129 assert.Contains(t, out.Stderr(), "Cleared the OAuth configuration for gitlab.com")
130}
132func TestConfigSet_TokenLeavesNonOAuth2HostAlone(t *testing.T) {
133 t.Parallel()
135 cfg := configStub{"gitlab.com:is_oauth2": "false", "gitlab.com:job_token": "some-job-token"}
136 exec := cmdtest.SetupCmdForTest(t, NewCmdSet, true, cmdtest.WithConfig(cfg))
138 out, err := exec("token glpat-nOtARealToken --host gitlab.com")
139 require.NoError(t, err)
141 assert.Equal(t, "glpat-nOtARealToken", cfg["gitlab.com:token"])
142 assert.Equal(t, "some-job-token", cfg["gitlab.com:job_token"], "clearing is scoped to the OAuth fields")
143 assert.Empty(t, out.Stderr())
144}
146func TestConfigSet_NonTokenKeyLeavesOAuth2ConfigAlone(t *testing.T) {
147 t.Parallel()
149 cfg := configStub{"gitlab.com:is_oauth2": "true", "gitlab.com:oauth2_refresh_token": "live-refresh"}
150 exec := cmdtest.SetupCmdForTest(t, NewCmdSet, true, cmdtest.WithConfig(cfg))
152 out, err := exec("git_protocol ssh --host gitlab.com")
153 require.NoError(t, err)
155 assert.Equal(t, "true", cfg["gitlab.com:is_oauth2"])
156 assert.Equal(t, "live-refresh", cfg["gitlab.com:oauth2_refresh_token"])
157 assert.Empty(t, out.Stderr())
158}
160// failingSetConfig is a configStub whose Set always fails, so a test can
161// assert on the error `config set` reports.
162type failingSetConfig struct {
163 configStub
164 err error
165}
167func (c failingSetConfig) Set(host, key, value string) error {
168 return c.err
169}
171func TestConfigSet_ErrorOmitsValue(t *testing.T) {
172 t.Parallel()
174 const secret = "glpat-nOtARealToken"
175 cfg := failingSetConfig{configStub: configStub{}, err: errors.New("keyring is locked")}
176 exec := cmdtest.SetupCmdForTest(t, NewCmdSet, true, cmdtest.WithConfig(cfg))
178 _, err := exec("token " + secret + " --host gitlab.com")
179 require.Error(t, err)
181 assert.NotContains(t, err.Error(), secret, "credential must not be echoed into the error")
182 assert.Contains(t, err.Error(), `failed to set "token"`)
183 assert.Contains(t, err.Error(), "keyring is locked", "underlying cause must be preserved")
184}
186func TestConfigSet_RejectsUnknownKey(t *testing.T) {
187 t.Parallel()
189 cfg := configStub{}
190 exec := cmdtest.SetupCmdForTest(t, NewCmdSet, true, cmdtest.WithConfig(cfg))
192 _, err := exec(`oauth_scopes "openid profile" -g`)
193 require.Error(t, err)
194 assert.Contains(t, err.Error(), `"oauth_scopes" is not a recognized glab config key`)
196 _, present := cfg["oauth_scopes"]
197 assert.False(t, present, "unknown key should not be stored")
198 _, written := cfg["_written"]
199 assert.False(t, written, "config should not be written when key is rejected")
200}
202// State glab maintains for itself is read from the global file only, so
203// `config set` must not put it in the repository's local file where nothing
204// would read it again. configStub.Local returns nil, so a local write would not
205// reach the config at all and the assertions below would not hold.
206func TestConfigSet_GlobalOnlyKeyWritesGloballyWithoutTheFlag(t *testing.T) {
207 t.Parallel()
209 cfg := configStub{}
210 exec := cmdtest.SetupCmdForTest(t, NewCmdSet, true, cmdtest.WithConfig(cfg))
212 out, err := exec("duo_cli_binary_version 9.26.0")
213 require.NoError(t, err)
215 assert.Contains(t, out.Stderr(), "always stored in the global configuration",
216 "the user must be told the write was routed to the global config")
217 assert.Equal(t, "9.26.0", cfg["duo_cli_binary_version"],
218 "the value must land in the global config, which is the one glab reads")
219 assert.Equal(t, "true", cfg["_written"])
220}
222func TestConfigSet_ClientIDValidation(t *testing.T) {
223 t.Parallel()
225 validID := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
227 t.Run("accepts valid 64 hex characters", func(t *testing.T) {
228 t.Parallel()
230 cfg := configStub{}
231 exec := cmdtest.SetupCmdForTest(t, NewCmdSet, true, cmdtest.WithConfig(cfg))
233 out, err := exec("client_id " + validID + " --host salsa.debian.org -g")
234 require.NoError(t, err)
235 assert.Empty(t, out.String())
236 assert.Equal(t, validID, cfg["salsa.debian.org:client_id"])
237 assert.Equal(t, "true", cfg["_written"])
238 })
240 t.Run("allows clearing client_id with empty string", func(t *testing.T) {
241 t.Parallel()
243 cfg := configStub{"salsa.debian.org:client_id": validID}
244 exec := cmdtest.SetupCmdForTest(t, NewCmdSet, true, cmdtest.WithConfig(cfg))
246 out, err := exec(`client_id "" --host salsa.debian.org -g`)
247 require.NoError(t, err)
248 assert.Empty(t, out.String())
249 assert.Empty(t, cfg["salsa.debian.org:client_id"])
250 assert.Equal(t, "true", cfg["_written"])
251 })
253 t.Run("allows clearing client_id with whitespace string", func(t *testing.T) {
254 t.Parallel()
256 cfg := configStub{"salsa.debian.org:client_id": validID}
257 exec := cmdtest.SetupCmdForTest(t, NewCmdSet, true, cmdtest.WithConfig(cfg))
259 out, err := exec(`client_id " " --host salsa.debian.org -g`)
260 require.NoError(t, err)
261 assert.Empty(t, out.String())
262 assert.Empty(t, cfg["salsa.debian.org:client_id"])
263 assert.Equal(t, "true", cfg["_written"])
264 })
266 t.Run("trims surrounding whitespace before storing", func(t *testing.T) {
267 t.Parallel()
269 cfg := configStub{}
270 exec := cmdtest.SetupCmdForTest(t, NewCmdSet, true, cmdtest.WithConfig(cfg))
272 out, err := exec("client_id ' " + validID + " ' --host salsa.debian.org -g")
273 require.NoError(t, err)
274 assert.Empty(t, out.String())
275 assert.Equal(t, validID, cfg["salsa.debian.org:client_id"])
276 assert.Equal(t, "true", cfg["_written"])
277 })
279 t.Run("rejects personal access token", func(t *testing.T) {
280 t.Parallel()
282 cfg := configStub{}
283 exec := cmdtest.SetupCmdForTest(t, NewCmdSet, true, cmdtest.WithConfig(cfg))
285 _, err := exec("client_id glpat-1234567890abcdef1234567890abcdef1234567890abcdef1234567890 --host salsa.debian.org -g")
286 require.Error(t, err)
287 assert.Contains(t, err.Error(), `invalid value for "client_id": value is a personal access token (glpat-)`)
288 assert.NotEqual(t, "true", cfg["_written"])
289 })
291 t.Run("rejects application secret", func(t *testing.T) {
292 t.Parallel()
294 cfg := configStub{}
295 exec := cmdtest.SetupCmdForTest(t, NewCmdSet, true, cmdtest.WithConfig(cfg))
297 _, err := exec("client_id gloas-1234567890abcdef1234567890abcdef1234567890abcdef1234567890 --host salsa.debian.org -g")
298 require.Error(t, err)
299 assert.Contains(t, err.Error(), `invalid value for "client_id": value is an OAuth application secret (gloas-)`)
300 assert.NotEqual(t, "true", cfg["_written"])
301 })
303 t.Run("rejects short client_id", func(t *testing.T) {
304 t.Parallel()
306 cfg := configStub{}
307 exec := cmdtest.SetupCmdForTest(t, NewCmdSet, true, cmdtest.WithConfig(cfg))
309 _, err := exec("client_id 12345 --host salsa.debian.org -g")
310 require.Error(t, err)
311 assert.Contains(t, err.Error(), `invalid value for "client_id": OAuth application ID must be 64 hexadecimal characters`)
312 assert.NotEqual(t, "true", cfg["_written"])
313 })
314}
Diagnostics8 git calls · 198 ms
CallStatusSizeTime0198 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=3 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 · hit3 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=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=44Open pack map
fetchtree:02001.8 KiB · hit1 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=1 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:0200222 B · 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=222 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=4892d06f6f71f04d3c94318a90a62d9487e287c2 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=222 git.pkt.payload.max=195 git.sideband.band1.bytes=195 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=195 git.pack.trailer=18940a1fb32891f80983c75dd5bb2c39d512d714Open pack map
fetchtree:0200141 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=184 git.response.bytes=141 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=5fdde2eba5db1cd641ff80fdd1d56e9727f74ec5 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=141 git.pkt.payload.max=114 git.sideband.band1.bytes=114 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=114 git.pack.trailer=fd3cd57456629891dd81421d72165b117cd056a2Open pack map
fetch2002.2 KiB184 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2266 git.response.cache=MISS git.response.duration_ms=184 git.fetch.wants=9227dc32e163c062e70bb1d73eb85e97d088121a 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=2266 git.pkt.payload.max=2239 git.sideband.band1.bytes=2239 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
- a5e2dda64d412308
- Request
- d2bb8451…
- 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