GitLabgitlab-org

cli

main
ply8 git calls347 ms100 KiB5 cachedprotocol v2
308 lines11 KiBRaw
1//go:build !integration
3package login
5import (
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
15func npmrcPath(home string) string {
16 return filepath.Join(home, ".npmrc")
19func TestLoginNpm_CreatesFileWhenAbsent(t *testing.T) {
20 home := t.TempDir()
21 setHome(t, home)
23 require.NoError(t, loginNpm(mustURL(t, "https://registry.example.com/"), "tok1"))
25 want := "//registry.example.com/:_authToken=tok1\n"
27 got, err := os.ReadFile(npmrcPath(home))
28 require.NoError(t, err)
29 assert.Equal(t, want, string(got))
31 fileInfo, err := os.Stat(npmrcPath(home))
32 require.NoError(t, err)
33 assert.Equal(t, os.FileMode(0o600), fileInfo.Mode().Perm())
36// TestLoginNpm_CreatesFileWhenAbsent_BareHost guards against building the
37// line as u.Host+u.Path+":_authToken=" with no separator inserted when the
38// registry URL has no trailing slash (and thus no path): that produces
39// "//host:_authToken=...", missing the "/" npm requires before
40// ":_authToken=".
41func TestLoginNpm_CreatesFileWhenAbsent_BareHost(t *testing.T) {
42 home := t.TempDir()
43 setHome(t, home)
45 require.NoError(t, loginNpm(mustURL(t, "https://registry.example.com"), "tok1"))
47 want := "//registry.example.com/:_authToken=tok1\n"
49 got, err := os.ReadFile(npmrcPath(home))
50 require.NoError(t, err)
51 assert.Equal(t, want, string(got))
54// TestLoginNpm_CreatesFileWhenAbsent_PathWithoutTrailingSlash guards
55// against the same missing-separator bug for a registry URL that has a
56// path but no trailing slash.
57func TestLoginNpm_CreatesFileWhenAbsent_PathWithoutTrailingSlash(t *testing.T) {
58 home := t.TempDir()
59 setHome(t, home)
61 require.NoError(t, loginNpm(mustURL(t, "https://registry.example.com/api/v4/projects/1/packages/npm"), "tok1"))
63 want := "//registry.example.com/api/v4/projects/1/packages/npm/:_authToken=tok1\n"
65 got, err := os.ReadFile(npmrcPath(home))
66 require.NoError(t, err)
67 assert.Equal(t, want, string(got))
70func TestLoginNpm_CreatesFileWhenAbsent_WithPath(t *testing.T) {
71 home := t.TempDir()
72 setHome(t, home)
74 require.NoError(t, loginNpm(mustURL(t, "https://registry.example.com/api/v4/projects/1/packages/npm/"), "tok1"))
76 want := "//registry.example.com/api/v4/projects/1/packages/npm/:_authToken=tok1\n"
78 got, err := os.ReadFile(npmrcPath(home))
79 require.NoError(t, err)
80 assert.Equal(t, want, string(got))
83// TestLoginNpm_KeepsThePort pins that, unlike sbt, npm's key retains the
84// registry's port: npm's .npmrc keys are the full registry URL including it,
85// and npm matches on that. A registry on a non-standard port must not be
86// normalized to a port-less key the way sbt's host is.
87func TestLoginNpm_KeepsThePort(t *testing.T) {
88 home := t.TempDir()
89 setHome(t, home)
91 require.NoError(t, loginNpm(mustURL(t, "https://registry.example.com:8443/"), "tok1"))
93 want := "//registry.example.com:8443/:_authToken=tok1\n"
95 got, err := os.ReadFile(npmrcPath(home))
96 require.NoError(t, err)
97 assert.Equal(t, want, string(got))
100// TestLoginNpm_DropsTheSchemeDefaultPort pins the other half of that rule: npm
101// builds its key with the WHATWG URL parser, which omits the scheme's default
102// port, so writing ":443" (or ":80" on http) leaves a key npm never looks up
103// and installs go out anonymous after a green login.
104func TestLoginNpm_DropsTheSchemeDefaultPort(t *testing.T) {
105 for registry, want := range map[string]string{
106 "https://ar.example.com:443/api/v4/": "//ar.example.com/api/v4/:_authToken=tok1\n",
107 "http://ar.example.com:80/api/v4/": "//ar.example.com/api/v4/:_authToken=tok1\n",
108 "https://ar.example.com:80/api/v4/": "//ar.example.com:80/api/v4/:_authToken=tok1\n",
109 } {
110 t.Run(registry, func(t *testing.T) {
111 home := t.TempDir()
112 setHome(t, home)
114 require.NoError(t, loginNpm(mustURL(t, registry), "tok1"))
116 got, err := os.ReadFile(npmrcPath(home))
117 require.NoError(t, err)
118 assert.Equal(t, want, string(got))
119 })
120 }
123// TestLoginNpm_LowerCasesTheHost pins that the key is written with a
124// lower-cased host. npm derives this key by parsing the registry URL, which
125// normalizes the host to lower case, so a key holding what the user typed would
126// never be found: a 401 at install time with nothing wrong at login time.
127func TestLoginNpm_LowerCasesTheHost(t *testing.T) {
128 home := t.TempDir()
129 setHome(t, home)
131 require.NoError(t, loginNpm(mustURL(t, "https://AR.Example.COM:8443/API/v4/"), "tok1"))
133 // The path keeps its case, since URL paths are case-sensitive.
134 want := "//ar.example.com:8443/API/v4/:_authToken=tok1\n"
136 got, err := os.ReadFile(npmrcPath(home))
137 require.NoError(t, err)
138 assert.Equal(t, want, string(got))
141// TestLoginNpm_RefreshMatchesDifferentlyCasedRegistry covers the upsert side of
142// lower-casing: the same registry typed two ways must land on one line.
143func TestLoginNpm_RefreshMatchesDifferentlyCasedRegistry(t *testing.T) {
144 home := t.TempDir()
145 setHome(t, home)
147 require.NoError(t, loginNpm(mustURL(t, "https://AR.Example.com/"), "tok1"))
148 require.NoError(t, loginNpm(mustURL(t, "https://ar.example.com/"), "tok2"))
150 got, err := os.ReadFile(npmrcPath(home))
151 require.NoError(t, err)
152 content := string(got)
154 assert.Equal(t, 1, strings.Count(content, "_authToken="))
155 assert.Contains(t, content, "//ar.example.com/:_authToken=tok2")
158func TestLoginNpm_PreservesUnrelatedContent(t *testing.T) {
159 home := t.TempDir()
160 setHome(t, home)
162 fixture := "registry=https://registry.npmjs.org/\n"
163 require.NoError(t, os.WriteFile(npmrcPath(home), []byte(fixture), 0o600))
165 require.NoError(t, loginNpm(mustURL(t, "https://registry.example.com/"), "tok1"))
167 got, err := os.ReadFile(npmrcPath(home))
168 require.NoError(t, err)
169 content := string(got)
171 assert.Contains(t, content, "registry=https://registry.npmjs.org/")
172 assert.Contains(t, content, "//registry.example.com/:_authToken=tok1")
175func TestLoginNpm_UpdatesExistingEntryInPlace(t *testing.T) {
176 home := t.TempDir()
177 setHome(t, home)
179 require.NoError(t, loginNpm(mustURL(t, "https://registry.example.com/"), "tok1"))
180 require.NoError(t, loginNpm(mustURL(t, "https://registry.example.com/"), "tok2"))
182 got, err := os.ReadFile(npmrcPath(home))
183 require.NoError(t, err)
184 content := string(got)
186 assert.Equal(t, 1, strings.Count(content, "//registry.example.com/:_authToken="), "must not duplicate the auth token line")
187 assert.Contains(t, content, "//registry.example.com/:_authToken=tok2")
188 assert.NotContains(t, content, "//registry.example.com/:_authToken=tok1")
191func TestLoginNpm_DifferentRegistryAddsSeparateEntry_NoPrefixCrossMatch(t *testing.T) {
192 home := t.TempDir()
193 setHome(t, home)
195 // "registry.example.com" is a string-prefix of
196 // "registry.example.com.other" -- they must be treated as distinct
197 // hosts, not matched against each other.
198 require.NoError(t, loginNpm(mustURL(t, "https://registry.example.com/"), "tok1"))
199 require.NoError(t, loginNpm(mustURL(t, "https://registry.example.com.other/"), "tok2"))
201 got, err := os.ReadFile(npmrcPath(home))
202 require.NoError(t, err)
203 content := string(got)
205 assert.Equal(t, 1, strings.Count(content, "//registry.example.com/:_authToken="))
206 assert.Equal(t, 1, strings.Count(content, "//registry.example.com.other/:_authToken="))
207 assert.Contains(t, content, "//registry.example.com/:_authToken=tok1")
208 assert.Contains(t, content, "//registry.example.com.other/:_authToken=tok2")
210 // Updating the first registry's token must not disturb the second.
211 require.NoError(t, loginNpm(mustURL(t, "https://registry.example.com/"), "tok1-updated"))
213 got, err = os.ReadFile(npmrcPath(home))
214 require.NoError(t, err)
215 content = string(got)
217 assert.Equal(t, 1, strings.Count(content, "//registry.example.com/:_authToken="))
218 assert.Contains(t, content, "//registry.example.com/:_authToken=tok1-updated")
219 assert.Contains(t, content, "//registry.example.com.other/:_authToken=tok2")
222// TestLoginNpm_LeavesACommentedEntryAlone pins that a commented-out entry is
223// not refreshed in place. npm reads ";" and "#" as comment markers, so writing
224// the token there would leave npm with no credential after a login that
225// reported success. The match is a line prefix, which already excludes both,
226// and this keeps it that way.
227func TestLoginNpm_LeavesACommentedEntryAlone(t *testing.T) {
228 home := t.TempDir()
229 setHome(t, home)
231 fixture := ";//registry.example.com/:_authToken=commented-out\n" +
232 "#//registry.example.com/:_authToken=also-commented-out\n"
233 require.NoError(t, os.WriteFile(npmrcPath(home), []byte(fixture), 0o600))
235 require.NoError(t, loginNpm(mustURL(t, "https://registry.example.com/"), "tok1"))
237 got, err := os.ReadFile(npmrcPath(home))
238 require.NoError(t, err)
239 content := string(got)
241 assert.Contains(t, content, ";//registry.example.com/:_authToken=commented-out")
242 assert.Contains(t, content, "#//registry.example.com/:_authToken=also-commented-out")
244 lines := strings.Split(strings.TrimSuffix(content, "\n"), "\n")
245 assert.Equal(t, "//registry.example.com/:_authToken=tok1", lines[len(lines)-1], "the live entry is appended instead")
248// TestLoginNpm_KeepsThePathPercentEncoded pins that the key keeps the path's
249// percent-encoding. npm builds its lookup key from the URL's pathname, which is
250// not decoded, and walks it up one whole segment at a time, so a decoded
251// spelling of an encoded project path is a key npm never looks for: the request
252// goes out anonymous and 401s at install time, with a green check at login
253// time.
254func TestLoginNpm_KeepsThePathPercentEncoded(t *testing.T) {
255 home := t.TempDir()
256 setHome(t, home)
258 require.NoError(t, loginNpm(mustURL(t, "https://gl.example.com/api/v4/projects/group%2Fproj/packages/npm/"), "tok1"))
260 got, err := os.ReadFile(npmrcPath(home))
261 require.NoError(t, err)
263 assert.Equal(t, "//gl.example.com/api/v4/projects/group%2Fproj/packages/npm/:_authToken=tok1\n", string(got))
266// TestLoginNpm_RefreshMatchesAnIndentedEntry pins that an indented entry is
267// refreshed rather than duplicated. npm's ini parser trims every key before
268// matching it, so the indented line is live: appending next to it would leave a
269// superseded token in the file and add a line per login.
270func TestLoginNpm_RefreshMatchesAnIndentedEntry(t *testing.T) {
271 home := t.TempDir()
272 setHome(t, home)
274 fixture := " //registry.example.com/:_authToken=old\n"
275 require.NoError(t, os.WriteFile(npmrcPath(home), []byte(fixture), 0o600))
277 require.NoError(t, loginNpm(mustURL(t, "https://registry.example.com/"), "tok1"))
279 got, err := os.ReadFile(npmrcPath(home))
280 require.NoError(t, err)
282 assert.Equal(t, " //registry.example.com/:_authToken=tok1\n", string(got), "the entry is refreshed in place, keeping its indentation")
285// TestLoginNpm_HonoursNpmConfigUserconfig pins that the token lands where npm
286// reads it: npm_config_userconfig overrides ~/.npmrc, so writing the default
287// path on a machine that sets it leaves the credential in a file npm never
288// opens.
289func TestLoginNpm_HonoursNpmConfigUserconfig(t *testing.T) {
290 for _, name := range []string{"npm_config_userconfig", "NPM_CONFIG_USERCONFIG"} {
291 t.Run(name, func(t *testing.T) {
292 home := t.TempDir()
293 setHome(t, home)
295 userconfig := filepath.Join(t.TempDir(), "npm", "config")
296 t.Setenv(name, userconfig)
298 require.NoError(t, loginNpm(mustURL(t, "https://registry.example.com/"), "tok1"))
300 got, err := os.ReadFile(userconfig)
301 require.NoError(t, err)
302 assert.Equal(t, "//registry.example.com/:_authToken=tok1\n", string(got))
304 _, err = os.Stat(npmrcPath(home))
305 assert.ErrorIs(t, err, os.ErrNotExist, "nothing may be written to ~/.npmrc when the variable is set")
306 })
307 }
Diagnostics8 git calls · 347 ms
CallStatusSizeTime0347 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 KiB6 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=UPDATING
git.response.duration_ms=6
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=44
Open pack map
fetchtree:02001.8 KiB · hit0 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=0
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=0
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:0200242 B · 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=242
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=5da85e254ab77f5f6e7e1419be57d524a9f8208e
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=242
git.pkt.payload.max=215
git.sideband.band1.bytes=215
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=215
git.pack.trailer=bd426fb1ae09d9fdcf755902c6903cd2eea819b2
Open pack map
fetchtree:0200494 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=494
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=400358373377bb8e4922d8b821520ebdf8130c4d
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=494
git.pkt.payload.max=467
git.sideband.band1.bytes=467
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=467
git.pack.trailer=363b27e4d94209a9331c11b44fe1ce6e5f4e6e81
Open pack map
fetch2002.6 KiB334 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=334
git.response.bytes=2700
git.response.cache=MISS
git.response.duration_ms=334
git.fetch.wants=4f263f2aa1bccde570f0a4304d501c54c65deb6b
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=2700
git.pkt.payload.max=2673
git.sideband.band1.bytes=2673
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
a5e2fcdbac91cf81
Request
aaa591d5…
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