github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+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", "uaa", grantType) 35 }) 36 37 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 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, origin, passedGrantType := fakeUAAClient.AuthenticateArgsForCall(0) 56 Expect(ID).To(Equal("some-username")) 57 Expect(secret).To(Equal("some-password")) 58 Expect(origin).To(Equal("uaa")) 59 Expect(passedGrantType).To(Equal(constant.GrantTypePassword)) 60 61 Expect(fakeConfig.SetTokenInformationCallCount()).To(Equal(1)) 62 accessToken, refreshToken, sshOAuthClient := fakeConfig.SetTokenInformationArgsForCall(0) 63 Expect(accessToken).To(Equal("bearer some-access-token")) 64 Expect(refreshToken).To(Equal("some-refresh-token")) 65 Expect(sshOAuthClient).To(BeEmpty()) 66 67 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 68 Expect(fakeConfig.SetUAAGrantTypeCallCount()).To(Equal(0)) 69 }) 70 71 When("a previous user authenticated with a client grant type", func() { 72 BeforeEach(func() { 73 fakeConfig.UAAGrantTypeReturns("client_credentials") 74 }) 75 It("returns a PasswordGrantTypeLogoutRequiredError", func() { 76 Expect(actualErr).To(MatchError(actionerror.PasswordGrantTypeLogoutRequiredError{})) 77 Expect(fakeConfig.UAAGrantTypeCallCount()).To(Equal(1)) 78 }) 79 }) 80 }) 81 82 When("the grant type is not password", func() { 83 BeforeEach(func() { 84 grantType = constant.GrantTypeClientCredentials 85 }) 86 87 It("stores the grant type and the client credentials", func() { 88 Expect(fakeConfig.SetUAAClientCredentialsCallCount()).To(Equal(1)) 89 client, clientSecret := fakeConfig.SetUAAClientCredentialsArgsForCall(0) 90 Expect(client).To(Equal("some-username")) 91 Expect(clientSecret).To(Equal("some-password")) 92 Expect(fakeConfig.SetUAAGrantTypeCallCount()).To(Equal(1)) 93 Expect(fakeConfig.SetUAAGrantTypeArgsForCall(0)).To(Equal(string(constant.GrantTypeClientCredentials))) 94 }) 95 }) 96 }) 97 98 When("an API error occurs", func() { 99 var expectedErr error 100 101 BeforeEach(func() { 102 expectedErr = errors.New("some error") 103 fakeUAAClient.AuthenticateReturns( 104 "", 105 "", 106 expectedErr, 107 ) 108 }) 109 110 It("returns the error", func() { 111 Expect(actualErr).To(MatchError(expectedErr)) 112 113 Expect(fakeConfig.SetTokenInformationCallCount()).To(Equal(1)) 114 accessToken, refreshToken, sshOAuthClient := fakeConfig.SetTokenInformationArgsForCall(0) 115 Expect(accessToken).To(BeEmpty()) 116 Expect(refreshToken).To(BeEmpty()) 117 Expect(sshOAuthClient).To(BeEmpty()) 118 119 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 120 }) 121 }) 122 }) 123 })