auth/auth_test.go
be20f3c5
 package auth
 
 import (
20a57f15
 	"crypto/rand"
 	"encoding/hex"
 	"os"
 	"strings"
be20f3c5
 	"testing"
 )
 
 func TestEncodeAuth(t *testing.T) {
c72ff318
 	newAuthConfig := &AuthConfig{Username: "ken", Password: "test", Email: "test@example.com"}
13e03a69
 	authStr := encodeAuth(newAuthConfig)
 	decAuthConfig, err := decodeAuth(authStr)
be20f3c5
 	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.")
 	}
 }
20a57f15
 
17ad00a3
 func TestLogin(t *testing.T) {
20a57f15
 	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")
13e03a69
 	status, err := Login(authConfig, false)
20a57f15
 	if err != nil {
 		t.Fatal(err)
 	}
 	if status != "Login Succeeded\n" {
 		t.Fatalf("Expected status \"Login Succeeded\", found \"%s\" instead", status)
 	}
 }
 
17ad00a3
 func TestCreateAccount(t *testing.T) {
20a57f15
 	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
17ad00a3
 	authConfig := NewAuthConfig(username, "test42", "docker-ut+"+token+"@example.com", "/tmp")
13e03a69
 	status, err := Login(authConfig, false)
20a57f15
 	if err != nil {
 		t.Fatal(err)
 	}
 	expectedStatus := "Account created. Please use the confirmation link we sent" +
 		" to your e-mail to activate it.\n"
 	if status != expectedStatus {
 		t.Fatalf("Expected status: \"%s\", found \"%s\" instead.", expectedStatus, status)
 	}
 
13e03a69
 	status, err = Login(authConfig, false)
20a57f15
 	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())
 	}
17ad00a3
 }