github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/actor/v2action/auth_test.go (about) 1 package v2action_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 . "code.cloudfoundry.org/cli/actor/v2action" 8 "code.cloudfoundry.org/cli/actor/v2action/v2actionfakes" 9 "code.cloudfoundry.org/cli/api/uaa/constant" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("Auth Actions", func() { 15 var ( 16 actor *Actor 17 fakeUAAClient *v2actionfakes.FakeUAAClient 18 fakeConfig *v2actionfakes.FakeConfig 19 ) 20 21 BeforeEach(func() { 22 fakeUAAClient = new(v2actionfakes.FakeUAAClient) 23 fakeConfig = new(v2actionfakes.FakeConfig) 24 actor = NewActor(nil, fakeUAAClient, fakeConfig) 25 }) 26 27 Describe("Authenticate", func() { 28 var ( 29 grantType constant.GrantType 30 actualErr error 31 ) 32 33 JustBeforeEach(func() { 34 actualErr = actor.Authenticate("some-username", "some-password", grantType) 35 }) 36 37 Context("when no API errors occur", func() { 38 BeforeEach(func() { 39 fakeUAAClient.AuthenticateReturns( 40 "some-access-token", 41 "some-refresh-token", 42 nil, 43 ) 44 }) 45 46 Context("when the grant type is a password grant", func() { 47 BeforeEach(func() { 48 grantType = constant.GrantTypePassword 49 }) 50 51 It("authenticates the user and returns access and refresh tokens", func() { 52 Expect(actualErr).NotTo(HaveOccurred()) 53 54 Expect(fakeUAAClient.AuthenticateCallCount()).To(Equal(1)) 55 ID, secret, passedGrantType := fakeUAAClient.AuthenticateArgsForCall(0) 56 Expect(ID).To(Equal("some-username")) 57 Expect(secret).To(Equal("some-password")) 58 Expect(passedGrantType).To(Equal(constant.GrantTypePassword)) 59 60 Expect(fakeConfig.SetTokenInformationCallCount()).To(Equal(1)) 61 accessToken, refreshToken, sshOAuthClient := fakeConfig.SetTokenInformationArgsForCall(0) 62 Expect(accessToken).To(Equal("bearer some-access-token")) 63 Expect(refreshToken).To(Equal("some-refresh-token")) 64 Expect(sshOAuthClient).To(BeEmpty()) 65 66 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 67 Expect(fakeConfig.SetUAAGrantTypeCallCount()).To(Equal(0)) 68 }) 69 70 Context("when a previous user authenticated with a client grant type", func() { 71 BeforeEach(func() { 72 fakeConfig.UAAGrantTypeReturns("client_credentials") 73 }) 74 It("returns a PasswordGrantTypeLogoutRequiredError", func() { 75 Expect(actualErr).To(MatchError(actionerror.PasswordGrantTypeLogoutRequiredError{})) 76 Expect(fakeConfig.UAAGrantTypeCallCount()).To(Equal(1)) 77 }) 78 }) 79 }) 80 81 Context("when the grant type is not password", func() { 82 BeforeEach(func() { 83 grantType = constant.GrantTypeClientCredentials 84 }) 85 86 It("stores the grant type and the client credentials", func() { 87 Expect(fakeConfig.SetUAAClientCredentialsCallCount()).To(Equal(1)) 88 client, clientSecret := fakeConfig.SetUAAClientCredentialsArgsForCall(0) 89 Expect(client).To(Equal("some-username")) 90 Expect(clientSecret).To(Equal("some-password")) 91 Expect(fakeConfig.SetUAAGrantTypeCallCount()).To(Equal(1)) 92 Expect(fakeConfig.SetUAAGrantTypeArgsForCall(0)).To(Equal(string(constant.GrantTypeClientCredentials))) 93 }) 94 }) 95 }) 96 97 Context("when an API error occurs", func() { 98 var expectedErr error 99 100 BeforeEach(func() { 101 expectedErr = errors.New("some error") 102 fakeUAAClient.AuthenticateReturns( 103 "", 104 "", 105 expectedErr, 106 ) 107 }) 108 109 It("returns the error", func() { 110 Expect(actualErr).To(MatchError(expectedErr)) 111 112 Expect(fakeConfig.SetTokenInformationCallCount()).To(Equal(1)) 113 accessToken, refreshToken, sshOAuthClient := fakeConfig.SetTokenInformationArgsForCall(0) 114 Expect(accessToken).To(BeEmpty()) 115 Expect(refreshToken).To(BeEmpty()) 116 Expect(sshOAuthClient).To(BeEmpty()) 117 118 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 119 }) 120 }) 121 }) 122 })