diff --git a/cache_test.go b/cache_test.go index f12d95d..1b24d6e 100644 --- a/cache_test.go +++ b/cache_test.go @@ -24,6 +24,8 @@ func (m mockSchlosser) GetKey(u string) ([]string, error) { switch u { case "alice": return []string{"ssh-rsa ABC"}, nil + case "bob": + return nil, nil default: return nil, fmt.Errorf("Error") } @@ -44,6 +46,7 @@ func TestCacheGet(t *testing.T) { {"alice", 0, "alice.key-1", false}, {"alice", 1, "alice.key-1", false}, {"alice", 2, "", true}, + {"bob", 1, "", true}, } cache := &cache{dir} gk := mockSchlosser{} diff --git a/getkey.go b/getkey.go index da4de2f..6fd182a 100644 --- a/getkey.go +++ b/getkey.go @@ -9,22 +9,21 @@ import ( "strings" ) -// GITHUB https://github.com/.keys -const GITHUB = "https://github.com" - // Schlosser interface type Schlosser interface { GetKey(string) ([]string, error) } // Locksmith implements Schlosser -type Locksmith struct{} +type Locksmith struct { + Github string +} // GetKey fetches ssh-key from url func (l Locksmith) GetKey(u string) ([]string, error) { url := u if !isURL.MatchString(u) { - url = fmt.Sprintf("%s/%s.keys", GITHUB, u) + url = fmt.Sprintf("%s/%s.keys", l.Github, u) } client := &http.Client{} // create a new request diff --git a/getkey_test.go b/getkey_test.go index 68dc41f..53ba199 100644 --- a/getkey_test.go +++ b/getkey_test.go @@ -7,7 +7,7 @@ import ( "testing" ) -func TestGetKeyFound(t *testing.T) { +func TestGetKeyFoundURL(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { expect(t, "ssh-vault", r.Header.Get("User-agent")) fmt.Fprintln(w, "ssh-rsa ABC") @@ -22,6 +22,21 @@ func TestGetKeyFound(t *testing.T) { expect(t, 1, len(s)) } +func TestGetKeyFoundUser(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + expect(t, "ssh-vault", r.Header.Get("User-agent")) + fmt.Fprintln(w, "ssh-rsa ABC") + })) + defer ts.Close() + + l := Locksmith{ts.URL} + s, err := l.GetKey("bob") + if err != nil { + t.Error(err) + } + expect(t, 1, len(s)) +} + func TestGetKeyNotFound(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { expect(t, "ssh-vault", r.Header.Get("User-agent")) diff --git a/vault.go b/vault.go index 43a0352..d415102 100644 --- a/vault.go +++ b/vault.go @@ -22,6 +22,10 @@ type vault struct { Password []byte } +// GITHUB https://github.com/.keys +const GITHUB = "https://github.com" + +// isURL regex to match if user is an URL var isURL = regexp.MustCompile(`^https?://`) // New initialize vault parameters @@ -31,7 +35,7 @@ func New(k, u, o, v string) (*vault, error) { keyPath string = k ) cache := Cache() - s := Locksmith{} + s := Locksmith{GITHUB} if u != "" { // use -k N where N is the index to use when multiple keys // are available