auth_test.go 2.1 KB

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