Skip to content

Commit

Permalink
test credentials storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Soulou committed Dec 23, 2014
1 parent 5cef637 commit fa9b644
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 20 deletions.
36 changes: 16 additions & 20 deletions config/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}
106 changes: 106 additions & 0 deletions config/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package config

import (
"reflect"
"testing"

"github.com/Scalingo/cli/users"
)

var (
u = &users.User{
Email: "[email protected]",
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()
}
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/Scalingo/envconfig"
Expand Down
18 changes: 18 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit fa9b644

Please sign in to comment.