2013-03-14 20:43:59 -04:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2013-05-10 09:34:19 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2013-03-14 20:43:59 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncodeAuth(t *testing.T) {
|
2013-03-22 05:19:39 -04:00
|
|
|
newAuthConfig := &AuthConfig{Username: "ken", Password: "test", Email: "test@example.com"}
|
2013-06-17 14:29:02 -04:00
|
|
|
authStr := encodeAuth(newAuthConfig)
|
|
|
|
decAuthConfig, err := decodeAuth(authStr)
|
2013-03-14 20:43:59 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if newAuthConfig.Username != decAuthConfig.Username {
|
|
|
|
t.Fatal("Encode Username doesn't match decoded Username")
|
|
|
|
}
|
|
|
|
if newAuthConfig.Password != decAuthConfig.Password {
|
|
|
|
t.Fatal("Encode Password doesn't match decoded Password")
|
|
|
|
}
|
|
|
|
if authStr != "a2VuOnRlc3Q=" {
|
|
|
|
t.Fatal("AuthString encoding isn't correct.")
|
|
|
|
}
|
|
|
|
}
|
2013-05-10 09:34:19 -04:00
|
|
|
|
2013-05-10 11:52:25 -04:00
|
|
|
func TestLogin(t *testing.T) {
|
2013-05-10 09:34:19 -04:00
|
|
|
os.Setenv("DOCKER_INDEX_URL", "https://indexstaging-docker.dotcloud.com")
|
|
|
|
defer os.Setenv("DOCKER_INDEX_URL", "")
|
|
|
|
authConfig := NewAuthConfig("unittester", "surlautrerivejetattendrai", "noise+unittester@dotcloud.com", "/tmp")
|
2013-06-17 14:29:02 -04:00
|
|
|
status, err := Login(authConfig, false)
|
2013-05-10 09:34:19 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-06-21 05:20:57 -04:00
|
|
|
if status != "Login Succeeded" {
|
2013-05-10 09:34:19 -04:00
|
|
|
t.Fatalf("Expected status \"Login Succeeded\", found \"%s\" instead", status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-10 11:52:25 -04:00
|
|
|
func TestCreateAccount(t *testing.T) {
|
2013-05-10 09:34:19 -04:00
|
|
|
os.Setenv("DOCKER_INDEX_URL", "https://indexstaging-docker.dotcloud.com")
|
|
|
|
defer os.Setenv("DOCKER_INDEX_URL", "")
|
|
|
|
tokenBuffer := make([]byte, 16)
|
|
|
|
_, err := rand.Read(tokenBuffer)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := hex.EncodeToString(tokenBuffer)[:12]
|
|
|
|
username := "ut" + token
|
2013-05-10 11:52:25 -04:00
|
|
|
authConfig := NewAuthConfig(username, "test42", "docker-ut+"+token+"@example.com", "/tmp")
|
2013-06-17 14:29:02 -04:00
|
|
|
status, err := Login(authConfig, false)
|
2013-05-10 09:34:19 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expectedStatus := "Account created. Please use the confirmation link we sent" +
|
2013-06-21 05:20:57 -04:00
|
|
|
" to your e-mail to activate it."
|
2013-05-10 09:34:19 -04:00
|
|
|
if status != expectedStatus {
|
|
|
|
t.Fatalf("Expected status: \"%s\", found \"%s\" instead.", expectedStatus, status)
|
|
|
|
}
|
|
|
|
|
2013-06-17 14:29:02 -04:00
|
|
|
status, err = Login(authConfig, false)
|
2013-05-10 09:34:19 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error but found nil instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedError := "Login: Account is not Active"
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), expectedError) {
|
|
|
|
t.Fatalf("Expected message \"%s\" but found \"%s\" instead", expectedError, err.Error())
|
|
|
|
}
|
2013-05-10 11:52:25 -04:00
|
|
|
}
|