github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/user/org_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("org-users command", func() {
    19  	var (
    20  		ui                  *testterm.FakeUI
    21  		requirementsFactory *testreq.FakeReqFactory
    22  		configRepo          core_config.ReadWriter
    23  		userRepo            *testapi.FakeUserRepository
    24  	)
    25  
    26  	BeforeEach(func() {
    27  		ui = &testterm.FakeUI{}
    28  		userRepo = &testapi.FakeUserRepository{}
    29  		configRepo = testconfig.NewRepositoryWithDefaults()
    30  		requirementsFactory = &testreq.FakeReqFactory{}
    31  	})
    32  
    33  	runCommand := func(args ...string) bool {
    34  		return testcmd.RunCommand(NewOrgUsers(ui, configRepo, userRepo), args, requirementsFactory)
    35  	}
    36  
    37  	Describe("requirements", func() {
    38  		It("fails with usage when invoked without an org name", func() {
    39  			requirementsFactory.LoginSuccess = true
    40  
    41  			runCommand()
    42  			Expect(ui.FailedWithUsage).To(BeTrue())
    43  		})
    44  
    45  		It("fails when not logged in", func() {
    46  			Expect(runCommand("say-hello-to-my-little-org")).To(BeFalse())
    47  		})
    48  	})
    49  
    50  	Context("when logged in and given an org with users", func() {
    51  		BeforeEach(func() {
    52  			org := models.Organization{}
    53  			org.Name = "the-org"
    54  			org.Guid = "the-org-guid"
    55  
    56  			user := models.UserFields{}
    57  			user.Username = "user1"
    58  			user2 := models.UserFields{}
    59  			user2.Username = "user2"
    60  			user3 := models.UserFields{}
    61  			user3.Username = "user3"
    62  			user4 := models.UserFields{}
    63  			user4.Username = "user4"
    64  			userRepo.ListUsersByRole = map[string][]models.UserFields{
    65  				models.ORG_MANAGER:     []models.UserFields{user, user2},
    66  				models.BILLING_MANAGER: []models.UserFields{user4},
    67  				models.ORG_AUDITOR:     []models.UserFields{user3},
    68  			}
    69  
    70  			requirementsFactory.LoginSuccess = true
    71  			requirementsFactory.Organization = org
    72  		})
    73  
    74  		It("shows the special users in the given org", func() {
    75  			runCommand("the-org")
    76  
    77  			Expect(userRepo.ListUsersOrganizationGuid).To(Equal("the-org-guid"))
    78  			Expect(ui.Outputs).To(ContainSubstrings(
    79  				[]string{"Getting users in org", "the-org", "my-user"},
    80  				[]string{"ORG MANAGER"},
    81  				[]string{"user1"},
    82  				[]string{"user2"},
    83  				[]string{"BILLING MANAGER"},
    84  				[]string{"user4"},
    85  				[]string{"ORG AUDITOR"},
    86  				[]string{"user3"},
    87  			))
    88  		})
    89  
    90  		Context("when the -a flag is provided", func() {
    91  			BeforeEach(func() {
    92  				user := models.UserFields{}
    93  				user.Username = "user1"
    94  				user2 := models.UserFields{}
    95  				user2.Username = "user2"
    96  				userRepo.ListUsersByRole = map[string][]models.UserFields{
    97  					models.ORG_USER: []models.UserFields{user, user2},
    98  				}
    99  			})
   100  
   101  			It("lists all org users, regardless of role", func() {
   102  				runCommand("-a", "the-org")
   103  
   104  				Expect(userRepo.ListUsersOrganizationGuid).To(Equal("the-org-guid"))
   105  				Expect(ui.Outputs).To(ContainSubstrings(
   106  					[]string{"Getting users in org", "the-org", "my-user"},
   107  					[]string{"USERS"},
   108  					[]string{"user1"},
   109  					[]string{"user2"},
   110  				))
   111  			})
   112  		})
   113  	})
   114  })