github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/user/space_users_test.go (about)

     1  package user_test
     2  
     3  import (
     4  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     5  	. "github.com/cloudfoundry/cli/cf/commands/user"
     6  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     7  	"github.com/cloudfoundry/cli/cf/models"
     8  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     9  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    10  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    11  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  
    15  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    16  )
    17  
    18  var _ = Describe("space-users command", func() {
    19  	var (
    20  		ui                  *testterm.FakeUI
    21  		requirementsFactory *testreq.FakeReqFactory
    22  		spaceRepo           *testapi.FakeSpaceRepository
    23  		userRepo            *testapi.FakeUserRepository
    24  		config              core_config.ReadWriter
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		config = testconfig.NewRepositoryWithDefaults()
    29  		ui = &testterm.FakeUI{}
    30  		requirementsFactory = &testreq.FakeReqFactory{}
    31  		spaceRepo = &testapi.FakeSpaceRepository{}
    32  		userRepo = &testapi.FakeUserRepository{}
    33  	})
    34  
    35  	runCommand := func(args ...string) bool {
    36  		return testcmd.RunCommand(NewSpaceUsers(ui, config, spaceRepo, userRepo), args, requirementsFactory)
    37  	}
    38  
    39  	Describe("requirements", func() {
    40  		It("fails when not logged in", func() {
    41  			Expect(runCommand("my-org", "my-space")).To(BeFalse())
    42  		})
    43  
    44  		It("succeeds when logged in", func() {
    45  			requirementsFactory.LoginSuccess = true
    46  			passed := runCommand("some-org", "whatever-space")
    47  
    48  			Expect(passed).To(BeTrue())
    49  			Expect("some-org").To(Equal(requirementsFactory.OrganizationName))
    50  		})
    51  	})
    52  
    53  	It("fails with usage when not invoked with exactly two args", func() {
    54  		runCommand("my-org")
    55  		Expect(ui.FailedWithUsage).To(BeTrue())
    56  	})
    57  
    58  	Context("when logged in and given some users in the org and space", func() {
    59  		BeforeEach(func() {
    60  			requirementsFactory.LoginSuccess = true
    61  
    62  			org := models.Organization{}
    63  			org.Name = "Org1"
    64  			org.Guid = "org1-guid"
    65  			space := models.Space{}
    66  			space.Name = "Space1"
    67  			space.Guid = "space1-guid"
    68  
    69  			requirementsFactory.Organization = org
    70  			spaceRepo.FindByNameInOrgSpace = space
    71  
    72  			user := models.UserFields{}
    73  			user.Username = "user1"
    74  			user2 := models.UserFields{}
    75  			user2.Username = "user2"
    76  			user3 := models.UserFields{}
    77  			user3.Username = "user3"
    78  			user4 := models.UserFields{}
    79  			user4.Username = "user4"
    80  			userRepo.ListUsersByRole = map[string][]models.UserFields{
    81  				models.SPACE_MANAGER:   []models.UserFields{user, user2},
    82  				models.SPACE_DEVELOPER: []models.UserFields{user4},
    83  				models.SPACE_AUDITOR:   []models.UserFields{user3},
    84  			}
    85  		})
    86  
    87  		It("tells you about the space users in the given space", func() {
    88  			runCommand("my-org", "my-space")
    89  
    90  			Expect(spaceRepo.FindByNameInOrgName).To(Equal("my-space"))
    91  			Expect(spaceRepo.FindByNameInOrgOrgGuid).To(Equal("org1-guid"))
    92  			Expect(userRepo.ListUsersSpaceGuid).To(Equal("space1-guid"))
    93  
    94  			Expect(ui.Outputs).To(ContainSubstrings(
    95  				[]string{"Getting users in org", "Org1", "Space1", "my-user"},
    96  				[]string{"SPACE MANAGER"},
    97  				[]string{"user1"},
    98  				[]string{"user2"},
    99  				[]string{"SPACE DEVELOPER"},
   100  				[]string{"user4"},
   101  				[]string{"SPACE AUDITOR"},
   102  				[]string{"user3"},
   103  			))
   104  		})
   105  	})
   106  })