github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/docker_password_getter_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/command/commandfakes"
     5  	. "code.cloudfoundry.org/cli/command/v7"
     6  	"code.cloudfoundry.org/cli/util/ui"
     7  	. "github.com/onsi/ginkgo"
     8  
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gbytes"
    11  )
    12  
    13  var _ = Describe("GetDockerPassword", func() {
    14  	var (
    15  		cmd        PushCommand
    16  		fakeConfig *commandfakes.FakeConfig
    17  		testUI     *ui.UI
    18  
    19  		dockerUsername        string
    20  		containsPrivateDocker bool
    21  
    22  		executeErr     error
    23  		dockerPassword string
    24  
    25  		input *Buffer
    26  	)
    27  
    28  	BeforeEach(func() {
    29  		input = NewBuffer()
    30  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    31  		fakeConfig = new(commandfakes.FakeConfig)
    32  
    33  		cmd = PushCommand{
    34  			BaseCommand: BaseCommand{
    35  				Config: fakeConfig,
    36  				UI:     testUI,
    37  			},
    38  		}
    39  	})
    40  
    41  	Describe("Get", func() {
    42  		JustBeforeEach(func() {
    43  			dockerPassword, executeErr = cmd.GetDockerPassword(dockerUsername, containsPrivateDocker)
    44  		})
    45  
    46  		When("docker image is private", func() {
    47  			When("there is a manifest", func() {
    48  				BeforeEach(func() {
    49  					dockerUsername = ""
    50  					containsPrivateDocker = true
    51  				})
    52  
    53  				When("a password is provided via environment variable", func() {
    54  					BeforeEach(func() {
    55  						fakeConfig.DockerPasswordReturns("some-docker-password")
    56  					})
    57  
    58  					It("takes the password from the environment", func() {
    59  						Expect(executeErr).ToNot(HaveOccurred())
    60  
    61  						Expect(testUI.Out).ToNot(Say("Environment variable CF_DOCKER_PASSWORD not set."))
    62  						Expect(testUI.Out).ToNot(Say("Docker password"))
    63  
    64  						Expect(testUI.Out).To(Say("Using docker repository password from environment variable CF_DOCKER_PASSWORD."))
    65  
    66  						Expect(dockerPassword).To(Equal("some-docker-password"))
    67  					})
    68  				})
    69  
    70  				When("no password is provided", func() {
    71  					BeforeEach(func() {
    72  						_, err := input.Write([]byte("some-docker-password\n"))
    73  						Expect(err).ToNot(HaveOccurred())
    74  					})
    75  
    76  					It("prompts for a password", func() {
    77  						Expect(executeErr).ToNot(HaveOccurred())
    78  
    79  						Expect(testUI.Out).To(Say("Environment variable CF_DOCKER_PASSWORD not set."))
    80  						Expect(testUI.Out).To(Say("Docker password"))
    81  
    82  						Expect(dockerPassword).To(Equal("some-docker-password"))
    83  					})
    84  				})
    85  			})
    86  
    87  			When("there is no manifest", func() {
    88  				BeforeEach(func() {
    89  					dockerUsername = "some-docker-username"
    90  					containsPrivateDocker = false
    91  				})
    92  
    93  				When("a password is provided via environment variable", func() {
    94  					BeforeEach(func() {
    95  						fakeConfig.DockerPasswordReturns("some-docker-password")
    96  					})
    97  
    98  					It("takes the password from the environment", func() {
    99  						Expect(executeErr).ToNot(HaveOccurred())
   100  
   101  						Expect(testUI.Out).ToNot(Say("Environment variable CF_DOCKER_PASSWORD not set."))
   102  						Expect(testUI.Out).ToNot(Say("Docker password"))
   103  
   104  						Expect(testUI.Out).To(Say("Using docker repository password from environment variable CF_DOCKER_PASSWORD."))
   105  
   106  						Expect(dockerPassword).To(Equal("some-docker-password"))
   107  					})
   108  				})
   109  
   110  				When("no password is provided", func() {
   111  					BeforeEach(func() {
   112  						_, err := input.Write([]byte("some-docker-password\n"))
   113  						Expect(err).ToNot(HaveOccurred())
   114  					})
   115  
   116  					It("prompts for a password", func() {
   117  						Expect(executeErr).ToNot(HaveOccurred())
   118  
   119  						Expect(testUI.Out).To(Say("Environment variable CF_DOCKER_PASSWORD not set."))
   120  						Expect(testUI.Out).To(Say("Docker password"))
   121  
   122  						Expect(dockerPassword).To(Equal("some-docker-password"))
   123  					})
   124  				})
   125  			})
   126  		})
   127  		When("docker image is public", func() {
   128  			BeforeEach(func() {
   129  				dockerUsername = ""
   130  				containsPrivateDocker = false
   131  			})
   132  
   133  			It("does not prompt for a password", func() {
   134  				Expect(testUI.Out).ToNot(Say("Environment variable CF_DOCKER_PASSWORD not set."))
   135  				Expect(testUI.Out).ToNot(Say("Docker password"))
   136  				Expect(testUI.Out).ToNot(Say("Using docker repository password from environment variable CF_DOCKER_PASSWORD."))
   137  			})
   138  
   139  			It("returns an empty password", func() {
   140  				Expect(executeErr).ToNot(HaveOccurred())
   141  				Expect(dockerPassword).To(Equal(""))
   142  			})
   143  		})
   144  	})
   145  })