github.com/BarDweller/libpak@v0.0.0-20230630201634-8dd5cfc15ec9/carton/netrc_test.go (about) 1 /* 2 * Copyright 2018-2020 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package carton_test 18 19 import ( 20 "net/http" 21 "os" 22 "os/user" 23 "path/filepath" 24 "testing" 25 26 . "github.com/onsi/gomega" 27 "github.com/sclevine/spec" 28 29 "github.com/BarDweller/libpak/carton" 30 ) 31 32 func testNetrc(t *testing.T, context spec.G, it spec.S) { 33 var ( 34 Expect = NewWithT(t).Expect 35 36 path string 37 ) 38 39 it.Before(func() { 40 var err error 41 42 f, err := os.CreateTemp("", "netrc") 43 Expect(err).NotTo(HaveOccurred()) 44 Expect(f.Close()).To(Succeed()) 45 path = f.Name() 46 }) 47 48 it.After(func() { 49 Expect(os.RemoveAll(path)).To(Succeed()) 50 }) 51 52 context("path", func() { 53 context("$NETRC", func() { 54 it.Before(func() { 55 Expect(os.Setenv("NETRC", "test-value")).To(Succeed()) 56 }) 57 58 it.After(func() { 59 Expect(os.Unsetenv("NETRC")).To(Succeed()) 60 }) 61 62 it("returns value from env var", func() { 63 Expect(carton.NetrcPath()).To(Equal("test-value")) 64 }) 65 }) 66 67 it("returns default", func() { 68 u, err := user.Current() 69 Expect(err).NotTo(HaveOccurred()) 70 71 Expect(carton.NetrcPath()).To(Equal(filepath.Join(u.HomeDir, ".netrc"))) 72 }) 73 }) 74 75 context("parse", func() { 76 it("parses one-liner", func() { 77 Expect(os.WriteFile(path, []byte(`machine test-machine login test-login password test-password`), 0644)).To(Succeed()) 78 79 Expect(carton.ParseNetrc(path)).To(Equal(carton.Netrc{ 80 { 81 Machine: "test-machine", 82 Login: "test-login", 83 Password: "test-password", 84 }, 85 })) 86 }) 87 88 it("parses multi-liner", func() { 89 Expect(os.WriteFile(path, []byte(` 90 machine test-machine 91 login test-login 92 password test-password 93 `), 0644)).To(Succeed()) 94 95 Expect(carton.ParseNetrc(path)).To(Equal(carton.Netrc{ 96 { 97 Machine: "test-machine", 98 Login: "test-login", 99 Password: "test-password", 100 }, 101 })) 102 }) 103 104 it("ignores macdef", func() { 105 Expect(os.WriteFile(path, []byte(` 106 macdef uploadtest 107 cd /pub/tests 108 bin 109 put filename.tar.gz 110 quit 111 112 machine test-machine login test-login password test-password 113 `), 0644)).To(Succeed()) 114 115 Expect(carton.ParseNetrc(path)).To(Equal(carton.Netrc{ 116 { 117 Machine: "test-machine", 118 Login: "test-login", 119 Password: "test-password", 120 }, 121 })) 122 }) 123 124 it("ignores all after default", func() { 125 Expect(os.WriteFile(path, []byte(` 126 machine test-machine-1 login test-login-1 password test-password-1 127 128 default 129 login test-login-2 130 password test-password-2 131 132 machine test-machine-3 login test-login-3 password test-password-3 133 `), 0644)).To(Succeed()) 134 135 Expect(carton.ParseNetrc(path)).To(Equal(carton.Netrc{ 136 { 137 Machine: "test-machine-1", 138 Login: "test-login-1", 139 Password: "test-password-1", 140 }, 141 { 142 Machine: "default", 143 Login: "test-login-2", 144 Password: "test-password-2", 145 }, 146 })) 147 }) 148 }) 149 150 context("basic auth", func() { 151 it("does not apply auth if no candidates", func() { 152 n := carton.Netrc{ 153 { 154 Machine: "test-machine", 155 Login: "test-login", 156 Password: "test-password", 157 }, 158 } 159 160 req, err := http.NewRequest("GET", "http://another-machine", nil) 161 Expect(err).NotTo(HaveOccurred()) 162 163 req, err = n.BasicAuth(req) 164 Expect(err).NotTo(HaveOccurred()) 165 166 _, _, ok := req.BasicAuth() 167 Expect(ok).To(BeFalse()) 168 }) 169 170 it("applies basic auth for match", func() { 171 n := carton.Netrc{ 172 { 173 Machine: "test-machine", 174 Login: "test-login", 175 Password: "test-password", 176 }, 177 } 178 179 req, err := http.NewRequest("GET", "http://test-machine", nil) 180 Expect(err).NotTo(HaveOccurred()) 181 182 req, err = n.BasicAuth(req) 183 Expect(err).NotTo(HaveOccurred()) 184 185 u, p, ok := req.BasicAuth() 186 Expect(ok).To(BeTrue()) 187 Expect(u).To(Equal("test-login")) 188 Expect(p).To(Equal("test-password")) 189 }) 190 191 it("applies basic auth for default", func() { 192 n := carton.Netrc{ 193 { 194 Machine: "test-machine", 195 Login: "test-login", 196 Password: "test-password", 197 }, 198 { 199 Machine: "default", 200 Login: "default-login", 201 Password: "default-password", 202 }, 203 } 204 205 req, err := http.NewRequest("GET", "http://another-machine", nil) 206 Expect(err).NotTo(HaveOccurred()) 207 208 req, err = n.BasicAuth(req) 209 Expect(err).NotTo(HaveOccurred()) 210 211 u, p, ok := req.BasicAuth() 212 Expect(ok).To(BeTrue()) 213 Expect(u).To(Equal("default-login")) 214 Expect(p).To(Equal("default-password")) 215 }) 216 }) 217 }