From 6477b4a51badbc1e1fa6f2aa1ad7321f3d48e625 Mon Sep 17 00:00:00 2001
From: jlandowner <jlandowner8@gmail.com>
Date: Thu, 3 Aug 2023 02:39:55 +0900
Subject: [PATCH] Support "group" can be used for role

---
 api/v1alpha1/user_types.go      |  9 ++++++++-
 api/v1alpha1/user_types_test.go | 24 ++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/api/v1alpha1/user_types.go b/api/v1alpha1/user_types.go
index 2a7cc735..543181b1 100644
--- a/api/v1alpha1/user_types.go
+++ b/api/v1alpha1/user_types.go
@@ -105,7 +105,14 @@ type UserRole struct {
 func (r UserRole) GetGroupAndRole() (group string, role string) {
 	v := strings.Split(r.Name, "-")
 	if len(v) > 0 {
-		return strings.Join(v[:len(v)-1], "-"), v[len(v)-1]
+		group := strings.Join(v[:len(v)-1], "-")
+		role := v[len(v)-1]
+
+		if group == "" {
+			return r.Name, ""
+		}
+
+		return group, role
 	}
 	return r.Name, ""
 }
diff --git a/api/v1alpha1/user_types_test.go b/api/v1alpha1/user_types_test.go
index 87d4b1f3..34f3cc1a 100644
--- a/api/v1alpha1/user_types_test.go
+++ b/api/v1alpha1/user_types_test.go
@@ -94,6 +94,30 @@ func TestUserRole_GetGroupAndRole(t *testing.T) {
 			wantGroup: "cosmo-admin",
 			wantRole:  "developer",
 		},
+		{
+			name: "non-admin: no hyphen",
+			fields: fields{
+				Name: "チームX",
+			},
+			wantGroup: "チームX",
+			wantRole:  "",
+		},
+		{
+			name: "non-admin: prefix",
+			fields: fields{
+				Name: "-XXX",
+			},
+			wantGroup: "-XXX",
+			wantRole:  "",
+		},
+		{
+			name: "admin: prefix",
+			fields: fields{
+				Name: "-XXX-admin",
+			},
+			wantGroup: "-XXX",
+			wantRole:  "admin",
+		},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {