Skip to content

Commit

Permalink
Merge remote-tracking branch 'sykesm/all-org-users'
Browse files Browse the repository at this point in the history
Conflicts:
	src/cf/api/users_test.go
	src/cf/commands/user/org_users_test.go
  • Loading branch information
tjarratt committed Feb 5, 2014
2 parents cd153fe + 728941b commit c197cd5
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/cf/api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type UserEntity struct {
}

var orgRoleToPathMap = map[string]string{
cf.ORG_USER: "users",
cf.ORG_MANAGER: "managers",
cf.BILLING_MANAGER: "billing_managers",
cf.ORG_AUDITOR: "auditors",
Expand Down
83 changes: 54 additions & 29 deletions src/cf/api/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func createUsersByRoleEndpoints(rolePath string) (ccReqs []testnet.TestRequest,
}

func testSetOrgRoleWithValidRole(t mr.TestingT, role string, path string) {

req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
Method: "PUT",
Path: path,
Expand Down Expand Up @@ -177,7 +176,7 @@ func createUsersRepo(t mr.TestingT, ccReqs []testnet.TestRequest, uaaReqs []test
return
}
func init() {
Describe("Testing with ginkgo", func() {
Describe("UserRepository", func() {
It("TestListUsersInOrgForRole", func() {
ccReqs, uaaReqs := createUsersByRoleEndpoints("/v2/organizations/my-org-guid/managers")

Expand Down Expand Up @@ -205,8 +204,8 @@ func init() {
assert.Equal(mr.T(), users[1].Guid, "user-2-guid")
assert.Equal(mr.T(), users[1].Username, "Super user 2")
})
It("TestListUsersInSpaceForRole", func() {

It("TestListUsersInSpaceForRole", func() {
ccReqs, uaaReqs := createUsersByRoleEndpoints("/v2/spaces/my-space-guid/managers")

cc, ccHandler, uaa, uaaHandler, repo := createUsersRepo(mr.T(), ccReqs, uaaReqs)
Expand All @@ -233,12 +232,9 @@ func init() {
assert.Equal(mr.T(), users[1].Guid, "user-2-guid")
assert.Equal(mr.T(), users[1].Username, "Super user 2")
})
It("TestFindByUsername", func() {

usersResponse := `{ "resources": [
{ "id": "my-guid", "userName": "my-full-username" }
]}`

It("TestFindByUsername", func() {
usersResponse := `{ "resources": [{ "id": "my-guid", "userName": "my-full-username" }]}`
uaaReq := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
Method: "GET",
Path: "/Users?attributes=id,userName&filter=userName+Eq+%22damien%2Buser1%40pivotallabs.com%22",
Expand All @@ -257,8 +253,8 @@ func init() {
expectedUserFields.Guid = "my-guid"
assert.Equal(mr.T(), user, expectedUserFields)
})
It("TestFindByUsernameWhenNotFound", func() {

It("TestFindByUsernameWhenNotFound", func() {
uaaReq := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
Method: "GET",
Path: "/Users?attributes=id,userName&filter=userName+Eq+%22my-user%22",
Expand All @@ -273,8 +269,8 @@ func init() {
assert.False(mr.T(), apiResponse.IsError())
assert.True(mr.T(), apiResponse.IsNotFound())
})
It("TestCreateUser", func() {

It("TestCreateUser", func() {
ccReq := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
Method: "POST",
Path: "/v2/users",
Expand Down Expand Up @@ -308,8 +304,8 @@ func init() {
assert.True(mr.T(), uaaHandler.AllRequestsCalled())
assert.False(mr.T(), apiResponse.IsNotSuccessful())
})
It("TestDeleteUser", func() {

It("TestDeleteUser", func() {
ccReq := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
Method: "DELETE",
Path: "/v2/users/my-user-guid",
Expand All @@ -331,14 +327,12 @@ func init() {
assert.True(mr.T(), uaaHandler.AllRequestsCalled())
assert.True(mr.T(), apiResponse.IsSuccessful())
})
It("TestDeleteUserWhenNotFoundOnTheCloudController", func() {

It("TestDeleteUserWhenNotFoundOnTheCloudController", func() {
ccReq := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
Method: "DELETE",
Path: "/v2/users/my-user-guid",
Response: testnet.TestResponse{Status: http.StatusNotFound, Body: `{
"code": 20003, "description": "The user could not be found"
}`},
Method: "DELETE",
Path: "/v2/users/my-user-guid",
Response: testnet.TestResponse{Status: http.StatusNotFound, Body: `{"code": 20003, "description": "The user could not be found"}`},
})

uaaReq := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
Expand All @@ -356,65 +350,96 @@ func init() {
assert.True(mr.T(), uaaHandler.AllRequestsCalled())
assert.True(mr.T(), apiResponse.IsSuccessful())
})
It("TestSetOrgRoleToOrgManager", func() {

It("TestSetOrgRoleToOrgManager", func() {
testSetOrgRoleWithValidRole(mr.T(), "OrgManager", "/v2/organizations/my-org-guid/managers/my-user-guid")
})
It("TestSetOrgRoleToBillingManager", func() {

It("TestSetOrgRoleToBillingManager", func() {
testSetOrgRoleWithValidRole(mr.T(), "BillingManager", "/v2/organizations/my-org-guid/billing_managers/my-user-guid")
})
It("TestSetOrgRoleToOrgAuditor", func() {

It("TestSetOrgRoleToOrgAuditor", func() {
testSetOrgRoleWithValidRole(mr.T(), "OrgAuditor", "/v2/organizations/my-org-guid/auditors/my-user-guid")
})
It("TestSetOrgRoleWithInvalidRole", func() {

It("TestSetOrgRoleWithInvalidRole", func() {
repo := createUsersRepoWithoutEndpoints()
apiResponse := repo.SetOrgRole("user-guid", "org-guid", "foo")

assert.False(mr.T(), apiResponse.IsSuccessful())
assert.Contains(mr.T(), apiResponse.Message, "Invalid Role")
})
It("TestUnsetOrgRoleFromOrgManager", func() {

It("TestUnsetOrgRoleFromOrgManager", func() {
testUnsetOrgRoleWithValidRole(mr.T(), "OrgManager", "/v2/organizations/my-org-guid/managers/my-user-guid")
})
It("TestUnsetOrgRoleFromBillingManager", func() {

It("TestUnsetOrgRoleFromBillingManager", func() {
testUnsetOrgRoleWithValidRole(mr.T(), "BillingManager", "/v2/organizations/my-org-guid/billing_managers/my-user-guid")
})
It("TestUnsetOrgRoleFromOrgAuditor", func() {

It("TestUnsetOrgRoleFromOrgAuditor", func() {
testUnsetOrgRoleWithValidRole(mr.T(), "OrgAuditor", "/v2/organizations/my-org-guid/auditors/my-user-guid")
})
It("TestUnsetOrgRoleWithInvalidRole", func() {

It("TestUnsetOrgRoleWithInvalidRole", func() {
repo := createUsersRepoWithoutEndpoints()
apiResponse := repo.UnsetOrgRole("user-guid", "org-guid", "foo")

assert.False(mr.T(), apiResponse.IsSuccessful())
assert.Contains(mr.T(), apiResponse.Message, "Invalid Role")
})
It("TestSetSpaceRoleToSpaceManager", func() {

It("TestSetSpaceRoleToSpaceManager", func() {
testSetSpaceRoleWithValidRole(mr.T(), "SpaceManager", "/v2/spaces/my-space-guid/managers/my-user-guid")
})
It("TestSetSpaceRoleToSpaceDeveloper", func() {

It("TestSetSpaceRoleToSpaceDeveloper", func() {
testSetSpaceRoleWithValidRole(mr.T(), "SpaceDeveloper", "/v2/spaces/my-space-guid/developers/my-user-guid")
})
It("TestSetSpaceRoleToSpaceAuditor", func() {

It("TestSetSpaceRoleToSpaceAuditor", func() {
testSetSpaceRoleWithValidRole(mr.T(), "SpaceAuditor", "/v2/spaces/my-space-guid/auditors/my-user-guid")
})
It("TestSetSpaceRoleWithInvalidRole", func() {

It("TestSetSpaceRoleWithInvalidRole", func() {
repo := createUsersRepoWithoutEndpoints()
apiResponse := repo.SetSpaceRole("user-guid", "space-guid", "org-guid", "foo")

assert.False(mr.T(), apiResponse.IsSuccessful())
assert.Contains(mr.T(), apiResponse.Message, "Invalid Role")
})

It("lists all users in the org", func() {
t := mr.T()
ccReqs, uaaReqs := createUsersByRoleEndpoints("/v2/organizations/my-org-guid/users")

cc, ccHandler, uaa, uaaHandler, repo := createUsersRepo(t, ccReqs, uaaReqs)
defer cc.Close()
defer uaa.Close()

stopChan := make(chan bool)
defer close(stopChan)
usersChan, statusChan := repo.ListUsersInOrgForRole("my-org-guid", cf.ORG_USER, stopChan)

users := []cf.UserFields{}
for chunk := range usersChan {
users = append(users, chunk...)
}
apiResponse := <-statusChan

assert.True(t, ccHandler.AllRequestsCalled())
assert.True(t, uaaHandler.AllRequestsCalled())
assert.True(t, apiResponse.IsSuccessful())

assert.Equal(t, len(users), 3)
assert.Equal(t, users[0].Guid, "user-1-guid")
assert.Equal(t, users[0].Username, "Super user 1")
assert.Equal(t, users[1].Guid, "user-2-guid")
assert.Equal(t, users[1].Username, "Super user 2")
assert.Equal(t, users[2].Guid, "user-3-guid")
assert.Equal(t, users[2].Username, "Super user 3")
})
})
}
3 changes: 3 additions & 0 deletions src/cf/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ func NewApp(cmdRunner commands.Runner) (app *cli.App, err error) {
Name: "org-users",
Description: "Show org users by role",
Usage: fmt.Sprintf("%s org-users ORG", cf.Name()),
Flags: []cli.Flag{
cli.BoolFlag{Name: "a", Usage: "List all users in the org"},
},
Action: func(c *cli.Context) {
cmdRunner.RunCmdByName("org-users", c)
},
Expand Down
9 changes: 8 additions & 1 deletion src/cf/commands/user/org_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
var orgRoles = []string{cf.ORG_MANAGER, cf.BILLING_MANAGER, cf.ORG_AUDITOR}

var orgRoleToDisplayName = map[string]string{
cf.ORG_USER: "USERS",
cf.ORG_MANAGER: "ORG MANAGER",
cf.BILLING_MANAGER: "BILLING MANAGER",
cf.ORG_AUDITOR: "ORG AUDITOR",
Expand Down Expand Up @@ -49,13 +50,19 @@ func (cmd *OrgUsers) GetRequirements(reqFactory requirements.Factory, c *cli.Con

func (cmd *OrgUsers) Run(c *cli.Context) {
org := cmd.orgReq.GetOrganization()
all := c.Bool("a")

cmd.ui.Say("Getting users in org %s as %s...",
terminal.EntityNameColor(org.Name),
terminal.EntityNameColor(cmd.config.Username()),
)

for _, role := range orgRoles {
roles := orgRoles
if all {
roles = []string{cf.ORG_USER}
}

for _, role := range roles {
stopChan := make(chan bool)
defer close(stopChan)

Expand Down
52 changes: 42 additions & 10 deletions src/cf/commands/user/org_users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
testterm "testhelpers/terminal"
)

func callOrgUsers(t mr.TestingT, args []string, reqFactory *testreq.FakeReqFactory, userRepo *testapi.FakeUserRepository) (ui *testterm.FakeUI) {
func callOrgUsers(args []string, reqFactory *testreq.FakeReqFactory, userRepo *testapi.FakeUserRepository) (ui *testterm.FakeUI) {
ui = &testterm.FakeUI{}

token, err := testconfig.CreateAccessTokenWithTokenInfo(configuration.TokenInfo{
Username: "my-user",
})
assert.NoError(t, err)
assert.NoError(mr.T(), err)
org3 := cf.OrganizationFields{}
org3.Name = "my-org"
space := cf.SpaceFields{}
Expand All @@ -38,35 +38,36 @@ func callOrgUsers(t mr.TestingT, args []string, reqFactory *testreq.FakeReqFacto
testcmd.RunCommand(cmd, ctxt, reqFactory)
return
}

func init() {
Describe("Testing with ginkgo", func() {
Describe("Listing users in an org", func() {
It("TestOrgUsersFailsWithUsage", func() {
reqFactory := &testreq.FakeReqFactory{}
userRepo := &testapi.FakeUserRepository{}
ui := callOrgUsers(mr.T(), []string{}, reqFactory, userRepo)
ui := callOrgUsers([]string{}, reqFactory, userRepo)
assert.True(mr.T(), ui.FailedWithUsage)

ui = callOrgUsers(mr.T(), []string{"Org1"}, reqFactory, userRepo)
ui = callOrgUsers([]string{"Org1"}, reqFactory, userRepo)
assert.False(mr.T(), ui.FailedWithUsage)
})
It("TestOrgUsersRequirements", func() {

It("TestOrgUsersRequirements", func() {
reqFactory := &testreq.FakeReqFactory{}
userRepo := &testapi.FakeUserRepository{}
args := []string{"Org1"}

reqFactory.LoginSuccess = false
callOrgUsers(mr.T(), args, reqFactory, userRepo)
callOrgUsers(args, reqFactory, userRepo)
assert.False(mr.T(), testcmd.CommandDidPassRequirements)

reqFactory.LoginSuccess = true
callOrgUsers(mr.T(), args, reqFactory, userRepo)
callOrgUsers(args, reqFactory, userRepo)
assert.True(mr.T(), testcmd.CommandDidPassRequirements)

assert.Equal(mr.T(), "Org1", reqFactory.OrganizationName)
})
It("TestOrgUsers", func() {

It("TestOrgUsers", func() {
org := cf.Organization{}
org.Name = "Found Org"
org.Guid = "found-org-guid"
Expand All @@ -91,7 +92,7 @@ func init() {
Organization: org,
}

ui := callOrgUsers(mr.T(), []string{"Org1"}, reqFactory, userRepo)
ui := callOrgUsers([]string{"Org1"}, reqFactory, userRepo)

assert.Equal(mr.T(), userRepo.ListUsersOrganizationGuid, "found-org-guid")
testassert.SliceContains(mr.T(), ui.Outputs, testassert.Lines{
Expand All @@ -105,5 +106,36 @@ func init() {
{"user3"},
})
})

It("lists all org users", func() {
t := mr.T()
org := cf.Organization{}
org.Name = "Found Org"
org.Guid = "found-org-guid"

userRepo := &testapi.FakeUserRepository{}
user := cf.UserFields{}
user.Username = "user1"
user2 := cf.UserFields{}
user2.Username = "user2"
userRepo.ListUsersByRole = map[string][]cf.UserFields{
cf.ORG_USER: []cf.UserFields{user, user2},
}

reqFactory := &testreq.FakeReqFactory{
LoginSuccess: true,
Organization: org,
}

ui := callOrgUsers([]string{"-a", "Org1"}, reqFactory, userRepo)

assert.Equal(t, userRepo.ListUsersOrganizationGuid, "found-org-guid")
testassert.SliceContains(t, ui.Outputs, testassert.Lines{
{"Getting users in org", "Found Org", "my-user"},
{"USERS"},
{"user1"},
{"user2"},
})
})
})
}
1 change: 1 addition & 0 deletions src/cf/user_roles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cf

const (
ORG_USER = "OrgUser"
ORG_MANAGER = "OrgManager"
BILLING_MANAGER = "BillingManager"
ORG_AUDITOR = "OrgAuditor"
Expand Down

0 comments on commit c197cd5

Please sign in to comment.