diff --git a/roles/roles.go b/roles/roles.go index 60eeb1c..b962cb5 100644 --- a/roles/roles.go +++ b/roles/roles.go @@ -5,6 +5,8 @@ import ( "strings" "github.com/sourcegraph/sourcegraph-accounts-sdk-go/services" + "golang.org/x/text/cases" + "golang.org/x/text/language" ) // Role is always the full qualified role name, e.g. "dotcom::site_admin". @@ -47,7 +49,7 @@ const ( // Service type is a special type used for service level roles. Service ResourceType = "service" // Subscription resources for Enterprise Portal. - Subscription ResourceType = "subscription" + EnterpriseSubscription ResourceType = "enterprise_subscription" ) // IsService returns true if the resource type is a service. @@ -56,6 +58,12 @@ func (r ResourceType) IsService() bool { return r == Service } +// Display returns the display name of the resource type. +func (r ResourceType) Display() string { + s := strings.ReplaceAll(string(r), "_", " ") + return cases.Title(language.English).String(s) +} + // roleInfo is the sdk internal representation of a role. type roleInfo struct { // id is the fully qualified role name. e.g. "dotcom::site_admin" @@ -103,7 +111,7 @@ var ( { id: RoleEnterprisePortalCustomerAdmin, service: services.EnterprisePortal, - resourceType: Subscription, + resourceType: EnterpriseSubscription, }, } ) diff --git a/roles/roles_test.go b/roles/roles_test.go index 4f7f228..8543517 100644 --- a/roles/roles_test.go +++ b/roles/roles_test.go @@ -89,8 +89,8 @@ func TestRolesByResourceType(t *testing.T) { }), }, { - name: "subscription", - resource: Subscription, + name: "enterprise_subscription", + resource: EnterpriseSubscription, expected: autogold.Expect([]Role{ Role("enterprise_portal::customer_admin"), }), @@ -124,3 +124,28 @@ func TestToService(t *testing.T) { }) } } + +func TestDisplay(t *testing.T) { + tests := []struct { + name string + resource ResourceType + want string + }{ + { + name: "service", + resource: Service, + want: "Service", + }, + { + name: "enterprise_subscription", + resource: EnterpriseSubscription, + want: "Enterprise Subscription", + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := test.resource.Display() + assert.Equal(t, test.want, got) + }) + } +}