github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/requirements/user_test.go (about)

     1  package requirements_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/models"
     7  	"code.cloudfoundry.org/cli/cf/requirements"
     8  
     9  	"code.cloudfoundry.org/cli/cf/api/apifakes"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("UserRequirement", func() {
    16  	var (
    17  		userRepo        *apifakes.FakeUserRepository
    18  		userRequirement requirements.UserRequirement
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		userRepo = new(apifakes.FakeUserRepository)
    23  	})
    24  
    25  	Describe("Execute", func() {
    26  		Context("when wantGUID is true", func() {
    27  			BeforeEach(func() {
    28  				userRequirement = requirements.NewUserRequirement("the-username", userRepo, true)
    29  			})
    30  
    31  			It("tries to find the user in CC", func() {
    32  				userRequirement.Execute()
    33  				Expect(userRepo.FindByUsernameCallCount()).To(Equal(1))
    34  				Expect(userRepo.FindByUsernameArgsForCall(0)).To(Equal("the-username"))
    35  			})
    36  
    37  			Context("when the call to find the user succeeds", func() {
    38  				var user models.UserFields
    39  				BeforeEach(func() {
    40  					user = models.UserFields{Username: "the-username", GUID: "the-guid"}
    41  					userRepo.FindByUsernameReturns(user, nil)
    42  				})
    43  
    44  				It("stores the user that was found", func() {
    45  					userRequirement.Execute()
    46  					Expect(userRequirement.GetUser()).To(Equal(user))
    47  				})
    48  
    49  				It("does not error", func() {
    50  					err := userRequirement.Execute()
    51  					Expect(err).NotTo(HaveOccurred())
    52  				})
    53  			})
    54  
    55  			Context("when the call to find the user fails", func() {
    56  				userError := errors.New("some error")
    57  				BeforeEach(func() {
    58  					userRepo.FindByUsernameReturns(models.UserFields{}, userError)
    59  				})
    60  
    61  				It("errors", func() {
    62  					err := userRequirement.Execute()
    63  
    64  					Expect(err).To(HaveOccurred())
    65  					Expect(err).To(Equal(userError))
    66  				})
    67  			})
    68  		})
    69  
    70  		Context("when wantGUID is false", func() {
    71  			BeforeEach(func() {
    72  				userRequirement = requirements.NewUserRequirement("the-username", userRepo, false)
    73  			})
    74  
    75  			It("does not try to find the user in CC", func() {
    76  				userRequirement.Execute()
    77  				Expect(userRepo.FindByUsernameCallCount()).To(Equal(0))
    78  			})
    79  
    80  			It("stores a user with just Username set", func() {
    81  				userRequirement.Execute()
    82  				expectedUser := models.UserFields{Username: "the-username"}
    83  				Expect(userRequirement.GetUser()).To(Equal(expectedUser))
    84  			})
    85  		})
    86  	})
    87  })