github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/auth_command_test.go (about)

     1  package v2_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/api/uaa"
     7  	"github.com/liamawhite/cli-with-i18n/command/commandfakes"
     8  	"github.com/liamawhite/cli-with-i18n/command/translatableerror"
     9  	. "github.com/liamawhite/cli-with-i18n/command/v2"
    10  	"github.com/liamawhite/cli-with-i18n/command/v2/v2fakes"
    11  	"github.com/liamawhite/cli-with-i18n/integration/helpers"
    12  	"github.com/liamawhite/cli-with-i18n/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("auth Command", func() {
    19  	var (
    20  		cmd        AuthCommand
    21  		testUI     *ui.UI
    22  		fakeActor  *v2fakes.FakeAuthActor
    23  		fakeConfig *commandfakes.FakeConfig
    24  		binaryName string
    25  		err        error
    26  	)
    27  
    28  	BeforeEach(func() {
    29  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    30  		fakeActor = new(v2fakes.FakeAuthActor)
    31  		fakeConfig = new(commandfakes.FakeConfig)
    32  		binaryName = "faceman"
    33  		fakeConfig.BinaryNameReturns(binaryName)
    34  
    35  		cmd = AuthCommand{
    36  			UI:     testUI,
    37  			Config: fakeConfig,
    38  			Actor:  fakeActor,
    39  		}
    40  	})
    41  
    42  	JustBeforeEach(func() {
    43  		err = cmd.Execute(nil)
    44  	})
    45  
    46  	Context("when there are no errors", func() {
    47  		var (
    48  			testUsername string
    49  			testPassword string
    50  		)
    51  
    52  		BeforeEach(func() {
    53  			testUsername = helpers.NewUsername()
    54  			testPassword = helpers.NewPassword()
    55  			cmd.RequiredArgs.Username = testUsername
    56  			cmd.RequiredArgs.Password = testPassword
    57  
    58  			fakeConfig.TargetReturns("some-api-target")
    59  
    60  			fakeActor.AuthenticateReturns(nil)
    61  		})
    62  
    63  		It("outputs API target information and clears the targeted org and space", func() {
    64  			Expect(err).ToNot(HaveOccurred())
    65  
    66  			Expect(testUI.Out).To(Say("API endpoint: %s", fakeConfig.Target()))
    67  			Expect(testUI.Out).To(Say("Authenticating\\.\\.\\."))
    68  			Expect(testUI.Out).To(Say("OK"))
    69  			Expect(testUI.Out).To(Say("Use '%s target' to view or set your target org and space", binaryName))
    70  
    71  			Expect(fakeActor.AuthenticateCallCount()).To(Equal(1))
    72  			config, username, password := fakeActor.AuthenticateArgsForCall(0)
    73  			Expect(config).To(Equal(fakeConfig))
    74  			Expect(username).To(Equal(testUsername))
    75  			Expect(password).To(Equal(testPassword))
    76  		})
    77  	})
    78  
    79  	Context("when there is an auth error", func() {
    80  		BeforeEach(func() {
    81  			cmd.RequiredArgs.Username = "foo"
    82  			cmd.RequiredArgs.Password = "bar"
    83  
    84  			fakeConfig.TargetReturns("some-api-target")
    85  
    86  			fakeActor.AuthenticateReturns(uaa.BadCredentialsError{Message: "some message"})
    87  		})
    88  
    89  		It("returns a BadCredentialsError", func() {
    90  			Expect(err).To(MatchError(translatableerror.BadCredentialsError{}))
    91  		})
    92  	})
    93  
    94  	Context("when there is a non-auth error", func() {
    95  		var expectedError error
    96  
    97  		BeforeEach(func() {
    98  			cmd.RequiredArgs.Username = "foo"
    99  			cmd.RequiredArgs.Password = "bar"
   100  
   101  			fakeConfig.TargetReturns("some-api-target")
   102  			expectedError = errors.New("my humps")
   103  
   104  			fakeActor.AuthenticateReturns(expectedError)
   105  		})
   106  
   107  		It("returns the error", func() {
   108  			Expect(err).To(MatchError(expectedError))
   109  		})
   110  	})
   111  
   112  	Describe("it checks the CLI version", func() {
   113  		var (
   114  			apiVersion    string
   115  			minCLIVersion string
   116  			binaryVersion string
   117  		)
   118  
   119  		BeforeEach(func() {
   120  			apiVersion = "1.2.3"
   121  			fakeConfig.APIVersionReturns(apiVersion)
   122  			minCLIVersion = "1.0.0"
   123  			fakeConfig.MinCLIVersionReturns(minCLIVersion)
   124  		})
   125  
   126  		Context("the CLI version is older than the minimum version required by the API", func() {
   127  			BeforeEach(func() {
   128  				binaryVersion = "0.0.0"
   129  				fakeConfig.BinaryVersionReturns(binaryVersion)
   130  			})
   131  
   132  			It("displays a recommendation to update the CLI version", func() {
   133  				Expect(err).ToNot(HaveOccurred())
   134  				Expect(testUI.Err).To(Say("Cloud Foundry API version %s requires CLI version %s. You are currently on version %s. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads", apiVersion, minCLIVersion, binaryVersion))
   135  			})
   136  		})
   137  
   138  		Context("the CLI version satisfies the API's minimum version requirements", func() {
   139  			BeforeEach(func() {
   140  				binaryVersion = "1.0.0"
   141  				fakeConfig.BinaryVersionReturns(binaryVersion)
   142  			})
   143  
   144  			It("does not display a recommendation to update the CLI version", func() {
   145  				Expect(err).ToNot(HaveOccurred())
   146  				Expect(testUI.Err).ToNot(Say("Cloud Foundry API version %s requires CLI version %s. You are currently on version %s. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads", apiVersion, minCLIVersion, binaryVersion))
   147  			})
   148  		})
   149  
   150  		Context("when the CLI version is invalid", func() {
   151  			BeforeEach(func() {
   152  				fakeConfig.BinaryVersionReturns("&#%")
   153  			})
   154  
   155  			It("returns an error", func() {
   156  				Expect(err).To(HaveOccurred())
   157  				Expect(err.Error()).To(Equal("No Major.Minor.Patch elements found"))
   158  			})
   159  		})
   160  	})
   161  })