github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/user_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 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 9 "code.cloudfoundry.org/cli/api/uaa" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("User Actions", func() { 15 var ( 16 actor *Actor 17 fakeUAAClient *v2actionfakes.FakeUAAClient 18 fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient 19 ) 20 21 BeforeEach(func() { 22 fakeUAAClient = new(v2actionfakes.FakeUAAClient) 23 fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient) 24 actor = NewActor(fakeCloudControllerClient, fakeUAAClient, nil) 25 }) 26 27 Describe("CreateUser", func() { 28 var ( 29 actualUser User 30 actualWarnings Warnings 31 actualErr error 32 ) 33 34 JustBeforeEach(func() { 35 actualUser, actualWarnings, actualErr = actor.CreateUser("some-new-user", "some-password", "some-origin") 36 }) 37 38 When("no API errors occur", func() { 39 var createdUser ccv2.User 40 41 BeforeEach(func() { 42 createdUser = ccv2.User{ 43 GUID: "new-user-cc-guid", 44 } 45 fakeUAAClient.CreateUserReturns( 46 uaa.User{ 47 ID: "new-user-uaa-id", 48 }, 49 nil, 50 ) 51 fakeCloudControllerClient.CreateUserReturns( 52 createdUser, 53 ccv2.Warnings{ 54 "warning-1", 55 "warning-2", 56 }, 57 nil, 58 ) 59 }) 60 61 It("creates a new user and returns all warnings", func() { 62 Expect(actualErr).NotTo(HaveOccurred()) 63 64 Expect(actualUser).To(Equal(User(createdUser))) 65 Expect(actualWarnings).To(ConsistOf("warning-1", "warning-2")) 66 67 Expect(fakeUAAClient.CreateUserCallCount()).To(Equal(1)) 68 username, password, origin := fakeUAAClient.CreateUserArgsForCall(0) 69 Expect(username).To(Equal("some-new-user")) 70 Expect(password).To(Equal("some-password")) 71 Expect(origin).To(Equal("some-origin")) 72 73 Expect(fakeCloudControllerClient.CreateUserCallCount()).To(Equal(1)) 74 uaaUserID := fakeCloudControllerClient.CreateUserArgsForCall(0) 75 Expect(uaaUserID).To(Equal("new-user-uaa-id")) 76 }) 77 }) 78 79 When("a create user request to the UAA returns an error", func() { 80 var returnedErr error 81 82 BeforeEach(func() { 83 returnedErr = errors.New("some UAA error") 84 fakeUAAClient.CreateUserReturns( 85 uaa.User{ 86 ID: "new-user-uaa-id", 87 }, 88 returnedErr, 89 ) 90 }) 91 92 It("returns the same error", func() { 93 Expect(actualErr).To(MatchError(returnedErr)) 94 }) 95 }) 96 97 When("the CC API returns an error", func() { 98 var returnedErr error 99 100 BeforeEach(func() { 101 returnedErr = errors.New("CC error") 102 fakeUAAClient.CreateUserReturns( 103 uaa.User{ 104 ID: "new-user-uaa-id", 105 }, 106 nil, 107 ) 108 fakeCloudControllerClient.CreateUserReturns( 109 ccv2.User{}, 110 ccv2.Warnings{ 111 "warning-1", 112 "warning-2", 113 }, 114 returnedErr, 115 ) 116 }) 117 118 It("returns the same error and all warnings", func() { 119 Expect(actualErr).To(MatchError(returnedErr)) 120 Expect(actualWarnings).To(ConsistOf("warning-1", "warning-2")) 121 }) 122 }) 123 }) 124 })