2013-03-14 20:43:59 -04:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2013-05-10 09:34:19 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2013-07-24 23:25:16 -04:00
|
|
|
"io/ioutil"
|
2013-05-10 09:34:19 -04:00
|
|
|
"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)
|
2013-07-24 08:28:22 -04:00
|
|
|
decAuthConfig := &AuthConfig{}
|
|
|
|
var err error
|
|
|
|
decAuthConfig.Username, decAuthConfig.Password, 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", "")
|
2013-07-24 08:28:22 -04:00
|
|
|
authConfig := &AuthConfig{Username: "unittester", Password: "surlautrerivejetattendrai", Email: "noise+unittester@dotcloud.com"}
|
2013-08-02 14:08:16 -04:00
|
|
|
status, err := Login(authConfig, nil)
|
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-07-24 23:25:16 -04:00
|
|
|
authConfig := &AuthConfig{Username: username, Password: "test42", Email: "docker-ut+" + token + "@example.com"}
|
2013-08-02 14:08:16 -04:00
|
|
|
status, err := Login(authConfig, nil)
|
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-08-02 14:08:16 -04:00
|
|
|
status, err = Login(authConfig, nil)
|
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) {
|
2013-07-04 17:28:49 -04:00
|
|
|
t.Fatalf("Expected message \"%s\" but found \"%s\" instead", expectedError, err)
|
2013-05-10 09:34:19 -04:00
|
|
|
}
|
2013-05-10 11:52:25 -04:00
|
|
|
}
|
2013-07-24 23:25:16 -04:00
|
|
|
|
2013-09-24 12:26:17 -04:00
|
|
|
func setupTempConfigFile() (*ConfigFile, error) {
|
2013-10-16 16:44:15 -04:00
|
|
|
root, err := ioutil.TempDir("", "docker-test-auth")
|
2013-07-24 23:25:16 -04:00
|
|
|
if err != nil {
|
2013-09-24 12:26:17 -04:00
|
|
|
return nil, err
|
2013-07-24 23:25:16 -04:00
|
|
|
}
|
|
|
|
configFile := &ConfigFile{
|
|
|
|
rootPath: root,
|
2013-09-24 12:26:17 -04:00
|
|
|
Configs: make(map[string]AuthConfig),
|
2013-07-24 23:25:16 -04:00
|
|
|
}
|
|
|
|
|
2013-09-24 12:26:17 -04:00
|
|
|
for _, registry := range []string{"testIndex", IndexServerAddress()} {
|
|
|
|
configFile.Configs[registry] = AuthConfig{
|
|
|
|
Username: "docker-user",
|
|
|
|
Password: "docker-pass",
|
|
|
|
Email: "docker@docker.io",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return configFile, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSameAuthDataPostSave(t *testing.T) {
|
|
|
|
configFile, err := setupTempConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2013-07-24 23:25:16 -04:00
|
|
|
}
|
2013-10-16 16:44:15 -04:00
|
|
|
defer os.RemoveAll(configFile.rootPath)
|
2013-07-24 23:25:16 -04:00
|
|
|
|
|
|
|
err = SaveConfig(configFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
authConfig := configFile.Configs["testIndex"]
|
|
|
|
if authConfig.Username != "docker-user" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if authConfig.Password != "docker-pass" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if authConfig.Email != "docker@docker.io" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if authConfig.Auth != "" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
2013-09-24 12:26:17 -04:00
|
|
|
|
|
|
|
func TestResolveAuthConfigIndexServer(t *testing.T) {
|
|
|
|
configFile, err := setupTempConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-16 16:44:15 -04:00
|
|
|
defer os.RemoveAll(configFile.rootPath)
|
2013-09-24 12:26:17 -04:00
|
|
|
|
|
|
|
for _, registry := range []string{"", IndexServerAddress()} {
|
|
|
|
resolved := configFile.ResolveAuthConfig(registry)
|
|
|
|
if resolved != configFile.Configs[IndexServerAddress()] {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolveAuthConfigFullURL(t *testing.T) {
|
|
|
|
configFile, err := setupTempConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-16 16:44:15 -04:00
|
|
|
defer os.RemoveAll(configFile.rootPath)
|
2013-09-24 12:26:17 -04:00
|
|
|
|
|
|
|
registryAuth := AuthConfig{
|
|
|
|
Username: "foo-user",
|
|
|
|
Password: "foo-pass",
|
|
|
|
Email: "foo@example.com",
|
|
|
|
}
|
|
|
|
localAuth := AuthConfig{
|
|
|
|
Username: "bar-user",
|
|
|
|
Password: "bar-pass",
|
|
|
|
Email: "bar@example.com",
|
|
|
|
}
|
|
|
|
configFile.Configs["https://registry.example.com/v1/"] = registryAuth
|
|
|
|
configFile.Configs["http://localhost:8000/v1/"] = localAuth
|
|
|
|
|
|
|
|
validRegistries := map[string][]string{
|
|
|
|
"https://registry.example.com/v1/": {
|
|
|
|
"https://registry.example.com/v1/",
|
|
|
|
"http://registry.example.com/v1/",
|
|
|
|
"registry.example.com",
|
|
|
|
"registry.example.com/v1/",
|
|
|
|
},
|
|
|
|
"http://localhost:8000/v1/": {
|
|
|
|
"https://localhost:8000/v1/",
|
|
|
|
"http://localhost:8000/v1/",
|
|
|
|
"localhost:8000",
|
|
|
|
"localhost:8000/v1/",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for configKey, registries := range validRegistries {
|
|
|
|
for _, registry := range registries {
|
|
|
|
var (
|
|
|
|
configured AuthConfig
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
resolved := configFile.ResolveAuthConfig(registry)
|
|
|
|
if configured, ok = configFile.Configs[configKey]; !ok {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if resolved.Email != configured.Email {
|
|
|
|
t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|