github.com/koron/hk@v0.0.0-20150303213137-b8aeaa3ab34c/util_test.go (about) 1 package main 2 3 import ( 4 "log" 5 "os" 6 "path/filepath" 7 "testing" 8 ) 9 10 func setupFakeNetrc() { 11 nrc = nil 12 wd, err := os.Getwd() 13 if err != nil { 14 log.Fatal(err) 15 } 16 err = os.Setenv("NETRC_PATH", filepath.Join(wd, "fakenetrc")) 17 if err != nil { 18 log.Fatal(err) 19 } 20 } 21 22 func cleanupNetrc() { 23 nrc = nil 24 os.Setenv("NETRC_PATH", "") 25 } 26 27 func TestGetCreds(t *testing.T) { 28 setupFakeNetrc() 29 30 u, p := getCreds("https://omg:wtf@api.heroku.com") 31 if u != "omg" { 32 t.Errorf("expected user=omg, got %s", u) 33 } 34 if p != "wtf" { 35 t.Errorf("expected password=wtf, got %s", p) 36 } 37 u, p = getCreds("https://api.heroku.com") 38 if u != "user@test.com" { 39 t.Errorf("expected user=user@test.com, got %s", u) 40 } 41 if p != "faketestpassword" { 42 t.Errorf("expected password=faketestpassword, got %s", p) 43 } 44 45 // test with a nil machine 46 u, p = getCreds("https://someotherapi.heroku.com") 47 if u != "" || p != "" { 48 t.Errorf("expected empty user and pass, got u=%q p=%q", u, p) 49 } 50 51 cleanupNetrc() 52 } 53 54 func TestNetrcPath(t *testing.T) { 55 fakepath := "/fake/net/rc" 56 os.Setenv("NETRC_PATH", fakepath) 57 if p := netrcPath(); p != fakepath { 58 t.Errorf("NETRC_PATH override expected %q, got %q", fakepath, p) 59 } 60 os.Setenv("NETRC_PATH", "") 61 } 62 63 func TestLoadNetrc(t *testing.T) { 64 setupFakeNetrc() 65 66 loadNetrc() 67 m := nrc.FindMachine("api.heroku.com") 68 if m == nil { 69 t.Errorf("machine api.heroku.com not found") 70 } else if m.Login != "user@test.com" { 71 t.Errorf("expected user=user@test.com, got %s", m.Login) 72 } 73 74 nrc = nil 75 fakepath := "/fake/net/rc" 76 os.Setenv("NETRC_PATH", fakepath) 77 78 loadNetrc() 79 if nrc == nil { 80 t.Fatalf("expected non-nil netrc") 81 } 82 m = nrc.FindMachine("api.heroku.com") 83 if m != nil { 84 t.Errorf("unexpected machine api.heroku.com found") 85 } 86 87 cleanupNetrc() 88 }