github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/actor/v2action/auth_test.go (about) 1 package v2action_test 2 3 import ( 4 "errors" 5 6 . "code.cloudfoundry.org/cli/actor/v2action" 7 "code.cloudfoundry.org/cli/actor/v2action/v2actionfakes" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 ) 11 12 var _ = Describe("Auth Actions", func() { 13 var ( 14 actor *Actor 15 fakeUAAClient *v2actionfakes.FakeUAAClient 16 fakeConfig *v2actionfakes.FakeConfig 17 ) 18 19 BeforeEach(func() { 20 fakeUAAClient = new(v2actionfakes.FakeUAAClient) 21 actor = NewActor(nil, fakeUAAClient) 22 fakeConfig = new(v2actionfakes.FakeConfig) 23 }) 24 25 Describe("Authenticate", func() { 26 var actualErr error 27 28 JustBeforeEach(func() { 29 actualErr = actor.Authenticate(fakeConfig, "some-username", "some-password") 30 }) 31 32 Context("when no API errors occur", func() { 33 BeforeEach(func() { 34 fakeUAAClient.AuthenticateReturns( 35 "some-access-token", 36 "some-refresh-token", 37 nil, 38 ) 39 }) 40 41 It("authenticates the user and returns access and refresh tokens", func() { 42 Expect(actualErr).NotTo(HaveOccurred()) 43 44 Expect(fakeUAAClient.AuthenticateCallCount()).To(Equal(1)) 45 username, password := fakeUAAClient.AuthenticateArgsForCall(0) 46 Expect(username).To(Equal("some-username")) 47 Expect(password).To(Equal("some-password")) 48 49 Expect(fakeConfig.SetTokenInformationCallCount()).To(Equal(1)) 50 accessToken, refreshToken, sshOAuthClient := fakeConfig.SetTokenInformationArgsForCall(0) 51 Expect(accessToken).To(Equal("bearer some-access-token")) 52 Expect(refreshToken).To(Equal("some-refresh-token")) 53 Expect(sshOAuthClient).To(BeEmpty()) 54 55 Expect(fakeConfig.UnsetOrganizationInformationCallCount()).To(Equal(1)) 56 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(1)) 57 }) 58 }) 59 60 Context("when an API error occurs", func() { 61 var expectedErr error 62 63 BeforeEach(func() { 64 expectedErr = errors.New("some error") 65 fakeUAAClient.AuthenticateReturns( 66 "", 67 "", 68 expectedErr, 69 ) 70 }) 71 72 It("returns the error", func() { 73 Expect(actualErr).To(MatchError(expectedErr)) 74 75 Expect(fakeUAAClient.AuthenticateCallCount()).To(Equal(1)) 76 username, password := fakeUAAClient.AuthenticateArgsForCall(0) 77 Expect(username).To(Equal("some-username")) 78 Expect(password).To(Equal("some-password")) 79 80 Expect(fakeConfig.SetTokenInformationCallCount()).To(Equal(1)) 81 accessToken, refreshToken, sshOAuthClient := fakeConfig.SetTokenInformationArgsForCall(0) 82 Expect(accessToken).To(BeEmpty()) 83 Expect(refreshToken).To(BeEmpty()) 84 Expect(sshOAuthClient).To(BeEmpty()) 85 86 Expect(fakeConfig.UnsetOrganizationInformationCallCount()).To(Equal(1)) 87 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(1)) 88 }) 89 }) 90 }) 91 })