GitLabgitlab-org
cli
main
1//go:build !integration
3package oauth2
5import (
6 "bytes"
7 "io"
8 "testing"
10 "git.sr.ht/~timofurrer/ugh"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
14 "gitlab.com/gitlab-org/cli/internal/iostreams"
15 "gitlab.com/gitlab-org/cli/internal/testing/consoletest"
16)
18// newNonInteractiveIOStreams returns a real, non-nil *iostreams.IOStreams
19// that reports itself as non-interactive, for callers that must never be
20// handed a nil io (see StartFlow/StartDeviceFlow's doc comments).
21func newNonInteractiveIOStreams() *iostreams.IOStreams {
22 return iostreams.New(
23 iostreams.WithStdin(io.NopCloser(&bytes.Buffer{}), false),
24 iostreams.WithStdout(&bytes.Buffer{}, false),
25 iostreams.WithStderr(&bytes.Buffer{}, false),
26 )
27}
29func TestResolveClientID_NonInteractiveSelfManagedFallsBackToError(t *testing.T) {
30 cfg := stubConfig{
31 hosts: map[string]map[string]string{
32 "salsa.debian.org": {},
33 },
34 }
36 clientID, persist, err := resolveClientID(t.Context(), cfg, newNonInteractiveIOStreams(), "salsa.debian.org")
37 require.Error(t, err)
38 assert.Empty(t, clientID)
39 assert.Nil(t, persist)
40 assert.ErrorContains(t, err, "set 'client_id' first")
41}
43func TestResolveClientID_WhitespaceOnlyConfigNonInteractiveFallsBackToError(t *testing.T) {
44 cfg := stubConfig{
45 hosts: map[string]map[string]string{
46 "salsa.debian.org": {
47 "client_id": " \t \n ",
48 },
49 },
50 }
52 clientID, persist, err := resolveClientID(t.Context(), cfg, newNonInteractiveIOStreams(), "salsa.debian.org")
53 require.Error(t, err)
54 assert.Empty(t, clientID)
55 assert.Nil(t, persist)
56 assert.ErrorContains(t, err, "set 'client_id' first")
57}
59const validTestClientID = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
61func TestResolveClientID_WhitespaceOnlyConfigFallsThroughToPrompt(t *testing.T) {
62 console := ugh.New(t)
63 console.Expect(ugh.Select("How do you want to continue?")).
64 Do(ugh.SelectIndex(0)) // clientIDChoicePaste
65 console.Expect(ugh.Input("Paste the Application ID here:")).
66 Do(ugh.Type(validTestClientID))
68 io, cleanup := consoletest.IOStreamsWithConsole(t, console)
69 t.Cleanup(cleanup)
71 cfg := stubConfig{
72 hosts: map[string]map[string]string{
73 "salsa.debian.org": {
74 "client_id": " \t \n ",
75 },
76 },
77 }
79 clientID, persist, err := resolveClientID(t.Context(), cfg, io, "salsa.debian.org")
80 require.NoError(t, err)
81 assert.Equal(t, validTestClientID, clientID)
82 assert.NotNil(t, persist)
83}
85func TestResolveClientID_InteractivePasteChoiceDefersPersistUntilCalled(t *testing.T) {
86 console := ugh.New(t)
87 console.Expect(ugh.Select("How do you want to continue?")).
88 Do(ugh.SelectIndex(0)) // clientIDChoicePaste
89 console.Expect(ugh.Input("Paste the Application ID here:")).
90 Do(ugh.Type(validTestClientID))
92 io, cleanup := consoletest.IOStreamsWithConsole(t, console)
93 t.Cleanup(cleanup)
95 writes := 0
96 cfg := stubConfig{
97 hosts: map[string]map[string]string{
98 "salsa.debian.org": {},
99 },
100 writes: &writes,
101 }
103 clientID, persist, err := resolveClientID(t.Context(), cfg, io, "salsa.debian.org")
104 require.NoError(t, err)
105 assert.Equal(t, validTestClientID, clientID)
107 // A pasted value must not be written until the caller proves it works
108 // (a completed OAuth round trip) by calling persist.
109 persisted, err := cfg.Get("salsa.debian.org", "client_id")
110 require.NoError(t, err)
111 assert.Empty(t, persisted, "resolveClientID must not persist a pasted value on its own")
112 assert.Zero(t, writes)
114 require.NoError(t, persist())
116 persisted, err = cfg.Get("salsa.debian.org", "client_id")
117 require.NoError(t, err)
118 assert.Equal(t, validTestClientID, persisted)
119 assert.Equal(t, 1, writes, "expected persist() to flush the pasted client_id to disk")
120}
122func TestResolveClientID_InteractiveShowMeChoiceDoesNotPersist(t *testing.T) {
123 console := ugh.New(t)
124 console.Expect(ugh.Select("How do you want to continue?")).
125 Do(ugh.SelectIndex(1)) // clientIDChoiceShow
127 io, cleanup := consoletest.IOStreamsWithConsole(t, console)
128 t.Cleanup(cleanup)
130 writes := 0
131 cfg := stubConfig{
132 hosts: map[string]map[string]string{
133 "salsa.debian.org": {},
134 },
135 writes: &writes,
136 }
138 clientID, persist, err := resolveClientID(t.Context(), cfg, io, "salsa.debian.org")
139 require.ErrorContains(t, err, "set 'client_id' first")
140 assert.Empty(t, clientID)
141 assert.Nil(t, persist)
143 persisted, err := cfg.Get("salsa.debian.org", "client_id")
144 require.NoError(t, err)
145 assert.Empty(t, persisted, "showing the registration instructions must not persist anything")
146 assert.Zero(t, writes)
147}
149func TestResolveClientID_InteractiveCancelChoiceReturnsStandardError(t *testing.T) {
150 console := ugh.New(t)
151 console.Expect(ugh.Select("How do you want to continue?")).
152 Do(ugh.SelectIndex(2)) // clientIDChoiceCancel
154 io, cleanup := consoletest.IOStreamsWithConsole(t, console)
155 t.Cleanup(cleanup)
157 cfg := stubConfig{
158 hosts: map[string]map[string]string{
159 "salsa.debian.org": {},
160 },
161 }
163 clientID, persist, err := resolveClientID(t.Context(), cfg, io, "salsa.debian.org")
164 require.Error(t, err)
165 assert.Empty(t, clientID)
166 assert.Nil(t, persist)
167 assert.ErrorContains(t, err, "set 'client_id' first")
168}
170func TestResolveClientID_CtrlCDuringChoiceReturnsStandardError(t *testing.T) {
171 console := ugh.New(t)
172 console.Expect(ugh.Select("How do you want to continue?")).
173 Do(func(r *ugh.Response) { r.Send(ugh.CtrlC) })
175 io, cleanup := consoletest.IOStreamsWithConsole(t, console)
176 t.Cleanup(cleanup)
178 cfg := stubConfig{
179 hosts: map[string]map[string]string{
180 "salsa.debian.org": {},
181 },
182 }
184 clientID, persist, err := resolveClientID(t.Context(), cfg, io, "salsa.debian.org")
185 require.Error(t, err)
186 assert.Empty(t, clientID)
187 assert.Nil(t, persist)
188 assert.ErrorContains(t, err, "set 'client_id' first",
189 "a cancelled prompt must leave the same next step as a non-interactive session, not a bare 'user cancelled' error")
190}
192func TestResolveClientID_CtrlCDuringPasteReturnsStandardError(t *testing.T) {
193 console := ugh.New(t)
194 console.Expect(ugh.Select("How do you want to continue?")).
195 Do(ugh.SelectIndex(0)) // clientIDChoicePaste
196 console.Expect(ugh.Input("Paste the Application ID here:")).
197 Do(func(r *ugh.Response) { r.Send(ugh.CtrlC) })
199 io, cleanup := consoletest.IOStreamsWithConsole(t, console)
200 t.Cleanup(cleanup)
202 writes := 0
203 cfg := stubConfig{
204 hosts: map[string]map[string]string{
205 "salsa.debian.org": {},
206 },
207 writes: &writes,
208 }
210 clientID, persist, err := resolveClientID(t.Context(), cfg, io, "salsa.debian.org")
211 require.ErrorContains(t, err, "set 'client_id' first")
212 assert.Empty(t, clientID)
213 assert.Nil(t, persist)
214 assert.Zero(t, writes, "a cancelled paste must not persist a partial value")
215}
Diagnostics6 git calls · 248 ms
CallStatusSizeTime0248 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs 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 KiB8 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=8 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 · 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=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: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=44Open pack map
fetchtree:0200468 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=468 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=c498f09dbd0cebbc59f9eff3b3ef8be66186fac6 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=468 git.pkt.payload.max=441 git.sideband.band1.bytes=441 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=441 git.pack.trailer=6420a98b3b2e923a6b61c550c00de326d620e479Open pack map
fetch2001.4 KiB235 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=235 git.response.bytes=1388 git.response.cache=MISS git.response.duration_ms=235 git.fetch.wants=dcdb191f2dcd4b08494e87d8cb54f1ee610c7b43 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=1388 git.pkt.payload.max=1361 git.sideband.band1.bytes=1361 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
- a5e155d94b3a90ce
- Request
- 052e5bbc…
- 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