diff --git a/config/auth.go b/config/auth.go index 9a07d41ec..2c0e8b9af 100644 --- a/config/auth.go +++ b/config/auth.go @@ -16,26 +16,8 @@ type AuthConfigData struct { } func StoreAuth(user *users.User) error { - // Check ~/.config/scalingo - if _, err := os.Stat(C.ConfigDir); err != nil { - if err, ok := err.(*os.PathError); ok { - if err := os.MkdirAll(C.ConfigDir, 0755); err != nil { - return errgo.Mask(err, errgo.Any) - } - } else { - return errgo.Notef(err, "error reaching config directory") - } - } - - var authConfig AuthConfigData - authConfig.AuthConfigPerHost = make(map[string]*users.User) - content, err := ioutil.ReadFile(C.AuthFile) - if err == nil { - err = json.Unmarshal(content, &authConfig) - if err != nil { - return errgo.Mask(err) - } - } else if err != nil && !os.IsNotExist(err) { + authConfig, err := existingAuth() + if err != nil { return errgo.Mask(err) } @@ -76,3 +58,17 @@ func LoadAuth() (*users.User, error) { return user, nil } } + +func existingAuth() (*AuthConfigData, error) { + authConfig := &AuthConfigData{} + content, err := ioutil.ReadFile(C.AuthFile) + if err == nil { + // We don't care of the error + json.Unmarshal(content, &authConfig) + } else if os.IsNotExist(err) { + authConfig.AuthConfigPerHost = make(map[string]*users.User) + } else { + return nil, errgo.Mask(err) + } + return authConfig, nil +} diff --git a/config/auth_test.go b/config/auth_test.go new file mode 100644 index 000000000..ab56818c3 --- /dev/null +++ b/config/auth_test.go @@ -0,0 +1,106 @@ +package config + +import ( + "reflect" + "testing" + + "github.com/Scalingo/cli/users" +) + +var ( + u = &users.User{ + Email: "test@example.com", + Username: "test", + AuthToken: "0123456789", + } +) + +func TestStoreAuth(t *testing.T) { + // First creation + err := StoreAuth(u) + if err != nil { + t.Error("%v should be nil", err) + } + clean() + + // Rewrite over an existing file + err = StoreAuth(u) + if err != nil { + t.Error("%v should be nil", err) + } + err = StoreAuth(u) + if err != nil { + t.Error("%v should be nil", err) + } + clean() + + // Add an additional api url + err = StoreAuth(u) + if err != nil { + t.Error("%v should be nil", err) + } + C.apiHost = "scalingo2.dev" + err = StoreAuth(u) + if err != nil { + t.Error("%v should be nil", err) + } + clean() +} + +func TestLoadAuth(t *testing.T) { + // Load without Store should return nil User + u, err := LoadAuth() + if err != nil { + t.Error("%v should be nil", err) + } + if u != nil { + t.Error("%v should be nil", u) + } + + // Load after storage of credentials + err = StoreAuth(u) + if err != nil { + t.Error("%v should be nil", err) + } + uLoad, err := LoadAuth() + if err != nil { + t.Error("%v should be nil", err) + } + if !reflect.DeepEqual(uLoad, u) { + t.Error("want %v, got %v", u, uLoad) + } + + clean() +} + +func TestExistingAuth(t *testing.T) { + // Before any auth + auth, err := existingAuth() + if err != nil { + t.Error("%v should be nil", err) + } + if len(auth.AuthConfigPerHost) > 0 { + t.Error("want auth.AuthConfigPerHost = [], got %v", auth.AuthConfigPerHost) + } + if !auth.LastUpdate.IsZero() { + t.Error("auth should never have been updated: %v", auth.LastUpdate) + } + + // After one auth + err = StoreAuth(u) + if err != nil { + t.Error("%v should be nil", err) + } + auth, err = existingAuth() + if err != nil { + t.Error("%v should be nil", err) + } + if len(auth.AuthConfigPerHost) != 1 { + t.Error("want len(auth.AuthConfigPerHost) = 1, got %v", auth.AuthConfigPerHost) + } + if auth.LastUpdate.IsZero() { + t.Error("auth should have been updated: %v", auth.LastUpdate) + } + + clean() +} diff --git a/config/config.go b/config/config.go index a70582327..664a7398c 100644 --- a/config/config.go +++ b/config/config.go @@ -7,6 +7,7 @@ import ( "net/url" "os" "path/filepath" + "runtime" "strings" "github.com/Scalingo/envconfig" diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 000000000..bdb424758 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,18 @@ +package config + +import "os" + +var ( + testConfig = Config{ + apiHost: "scalingo.dev", + AuthFile: "/tmp/test-scalingo-auth", + } +) + +func init() { + C = testConfig +} + +func clean() { + os.Remove(testConfig.AuthFile) +}