github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/rights_test.go (about) 1 //go:build functional || openapi || role || ALL 2 3 /* 4 * Copyright 2021 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. 5 */ 6 7 package govcd 8 9 import ( 10 "fmt" 11 12 . "gopkg.in/check.v1" 13 ) 14 15 func (vcd *TestVCD) Test_RoleTenantContext(check *C) { 16 adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name) 17 check.Assert(err, IsNil) 18 check.Assert(adminOrg, NotNil) 19 20 clientRoles, err := adminOrg.client.GetAllRoles(nil) 21 check.Assert(err, IsNil) 22 check.Assert(clientRoles, NotNil) 23 check.Assert(len(clientRoles), Not(Equals), 0) 24 25 if testVerbose { 26 fmt.Println("Client roles") 27 for _, role := range clientRoles { 28 fmt.Printf("%-40s %s\n", role.Role.Name, role.Role.Description) 29 rights, err := role.GetRights(nil) 30 check.Assert(err, IsNil) 31 for i, right := range rights { 32 fmt.Printf("\t%3d %s\n", i+1, right.Name) 33 } 34 fmt.Println() 35 } 36 } 37 38 orgRoles, err := adminOrg.GetAllRoles(nil) 39 check.Assert(err, IsNil) 40 check.Assert(orgRoles, NotNil) 41 check.Assert(len(orgRoles), Not(Equals), 0) 42 43 if testVerbose { 44 fmt.Println("ORG roles") 45 for _, role := range orgRoles { 46 fmt.Printf("%-40s %s\n", role.Role.Name, role.Role.Description) 47 rights, err := role.GetRights(nil) 48 check.Assert(err, IsNil) 49 for i, right := range rights { 50 fmt.Printf("\t%3d %s\n", i+1, right.Name) 51 } 52 fmt.Println() 53 } 54 } 55 56 } 57 58 func (vcd *TestVCD) Test_Rights(check *C) { 59 adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name) 60 check.Assert(err, IsNil) 61 check.Assert(adminOrg, NotNil) 62 63 allOrgRights, err := adminOrg.GetAllRights(nil) 64 check.Assert(err, IsNil) 65 check.Assert(allOrgRights, NotNil) 66 67 if testVerbose { 68 fmt.Printf("(org) how many rights: %d\n", len(allOrgRights)) 69 for i, oneRight := range allOrgRights { 70 fmt.Printf("%3d %-20s %-53s %s\n", i, oneRight.Name, oneRight.ID, oneRight.Category) 71 } 72 } 73 allExistingRights, err := adminOrg.client.GetAllRights(nil) 74 check.Assert(err, IsNil) 75 check.Assert(allExistingRights, NotNil) 76 77 if testVerbose { 78 fmt.Printf("(global) how many rights: %d\n", len(allExistingRights)) 79 for i, oneRight := range allExistingRights { 80 fmt.Printf("%3d %-20s %-53s %s\n", i, oneRight.Name, oneRight.ID, oneRight.Category) 81 } 82 } 83 // Test a sample of rights 84 maxItems := 10 85 86 for i, right := range allExistingRights { 87 searchRight(adminOrg.client, right.Name, right.ID, check) 88 if i > maxItems { 89 break 90 } 91 } 92 var rigthNamesWithCommas = []string{ 93 "vApp: VM Migrate, Force Undeploy, Relocate, Consolidate", 94 "Role: Create, Edit, Delete, or Copy", 95 "Task: Resume, Abort, or Fail", 96 "VCD Extension: Register, Unregister, Refresh, Associate or Disassociate", 97 "UI Plugins: Define, Upload, Modify, Delete, Associate or Disassociate", 98 } 99 if vcd.client.Client.IsSysAdmin { 100 for _, name := range rigthNamesWithCommas { 101 searchRight(adminOrg.client, name, "", check) 102 } 103 } 104 105 rightsCategories, err := adminOrg.client.GetAllRightsCategories(nil) 106 check.Assert(err, IsNil) 107 check.Assert(len(rightsCategories) > 0, Equals, true) 108 } 109 110 func searchRight(client *Client, name, id string, check *C) { 111 fullRightByName, err := client.GetRightByName(name) 112 check.Assert(err, IsNil) 113 check.Assert(fullRightByName, NotNil) 114 if id != "" { 115 fullRightById, err := client.GetRightById(id) 116 check.Assert(err, IsNil) 117 check.Assert(fullRightById, NotNil) 118 category, err := client.GetRightsCategoryById(fullRightById.Category) 119 check.Assert(err, IsNil) 120 check.Assert(fullRightById.Category, Equals, category.Id) 121 } 122 }