auth_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package docker
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "fmt"
  6. "github.com/dotcloud/docker/registry"
  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. t.Skip("FIXME: please remove dependency on external services")
  17. os.Setenv("DOCKER_INDEX_URL", "https://indexstaging-docker.dotcloud.com")
  18. defer os.Setenv("DOCKER_INDEX_URL", "")
  19. authConfig := &registry.AuthConfig{
  20. Username: "unittester",
  21. Password: "surlautrerivejetattendrai",
  22. Email: "noise+unittester@docker.com",
  23. ServerAddress: "https://indexstaging-docker.dotcloud.com/v1/",
  24. }
  25. status, err := registry.Login(authConfig, nil)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if status != "Login Succeeded" {
  30. t.Fatalf("Expected status \"Login Succeeded\", found \"%s\" instead", status)
  31. }
  32. }
  33. func TestCreateAccount(t *testing.T) {
  34. t.Skip("FIXME: please remove dependency on external services")
  35. tokenBuffer := make([]byte, 16)
  36. _, err := rand.Read(tokenBuffer)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. token := hex.EncodeToString(tokenBuffer)[:12]
  41. username := "ut" + token
  42. authConfig := &registry.AuthConfig{
  43. Username: username,
  44. Password: "test42",
  45. Email: fmt.Sprintf("docker-ut+%s@example.com", token),
  46. ServerAddress: "https://indexstaging-docker.dotcloud.com/v1/",
  47. }
  48. status, err := registry.Login(authConfig, nil)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. expectedStatus := fmt.Sprintf(
  53. "Account created. Please see the documentation of the registry %s for instructions how to activate it.",
  54. authConfig.ServerAddress,
  55. )
  56. if status != expectedStatus {
  57. t.Fatalf("Expected status: \"%s\", found \"%s\" instead.", expectedStatus, status)
  58. }
  59. status, err = registry.Login(authConfig, nil)
  60. if err == nil {
  61. t.Fatalf("Expected error but found nil instead")
  62. }
  63. expectedError := "Login: Account is not Active"
  64. if !strings.Contains(err.Error(), expectedError) {
  65. t.Fatalf("Expected message \"%s\" but found \"%s\" instead", expectedError, err)
  66. }
  67. }