auth_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package docker
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "github.com/dotcloud/docker/auth"
  6. "os"
  7. "strings"
  8. "testing"
  9. )
  10. // FIXME: these tests have an external dependency on a staging index hosted
  11. // on the docker.io infrastructure. That dependency should be removed.
  12. // - Unit tests should have no side-effect dependencies.
  13. // - Integration tests should have side-effects limited to the host environment being tested.
  14. func TestLogin(t *testing.T) {
  15. os.Setenv("DOCKER_INDEX_URL", "https://indexstaging-docker.dotcloud.com")
  16. defer os.Setenv("DOCKER_INDEX_URL", "")
  17. authConfig := &auth.AuthConfig{Username: "unittester", Password: "surlautrerivejetattendrai", Email: "noise+unittester@dotcloud.com"}
  18. status, err := auth.Login(authConfig, nil)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. if status != "Login Succeeded" {
  23. t.Fatalf("Expected status \"Login Succeeded\", found \"%s\" instead", status)
  24. }
  25. }
  26. func TestCreateAccount(t *testing.T) {
  27. os.Setenv("DOCKER_INDEX_URL", "https://indexstaging-docker.dotcloud.com")
  28. defer os.Setenv("DOCKER_INDEX_URL", "")
  29. tokenBuffer := make([]byte, 16)
  30. _, err := rand.Read(tokenBuffer)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. token := hex.EncodeToString(tokenBuffer)[:12]
  35. username := "ut" + token
  36. authConfig := &auth.AuthConfig{Username: username, Password: "test42", Email: "docker-ut+" + token + "@example.com"}
  37. status, err := auth.Login(authConfig, nil)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. expectedStatus := "Account created. Please use the confirmation link we sent" +
  42. " to your e-mail to activate it."
  43. if status != expectedStatus {
  44. t.Fatalf("Expected status: \"%s\", found \"%s\" instead.", expectedStatus, status)
  45. }
  46. status, err = auth.Login(authConfig, nil)
  47. if err == nil {
  48. t.Fatalf("Expected error but found nil instead")
  49. }
  50. expectedError := "Login: Account is not Active"
  51. if !strings.Contains(err.Error(), expectedError) {
  52. t.Fatalf("Expected message \"%s\" but found \"%s\" instead", expectedError, err)
  53. }
  54. }