-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
141 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"net/url" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/Scalingo/envconfig" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |