@@ -145,6 +145,84 @@ func TestOIDCManagerGetProvider(t *testing.T) {
145145 }
146146}
147147
148+ func TestMapClaimsToRole (t * testing.T ) {
149+ tests := []struct {
150+ name string
151+ mapping RoleMappingRules
152+ email string
153+ groups []string
154+ expectedRole string
155+ }{
156+ {
157+ name : "Exact email match" ,
158+ mapping : RoleMappingRules {
159+ DomainMappings : map [string ]string {"admin@company.com" : "admin" },
160+ DefaultRole : "user" ,
161+ },
162+ email : "admin@company.com" ,
163+ expectedRole : "admin" ,
164+ },
165+ {
166+ name : "Domain match" ,
167+ mapping : RoleMappingRules {
168+ DomainMappings : map [string ]string {"@company.com" : "user" },
169+ DefaultRole : "none" ,
170+ },
171+ email : "someone@company.com" ,
172+ expectedRole : "user" ,
173+ },
174+ {
175+ name : "Group match" ,
176+ mapping : RoleMappingRules {
177+ GroupMappings : map [string ]string {"devs" : "developer" },
178+ DefaultRole : "none" ,
179+ },
180+ email : "user@other.com" ,
181+ groups : []string {"devs" },
182+ expectedRole : "developer" ,
183+ },
184+ {
185+ name : "No match, default_role is none" ,
186+ mapping : RoleMappingRules {
187+ DomainMappings : map [string ]string {"@company.com" : "user" },
188+ DefaultRole : "none" ,
189+ },
190+ email : "outsider@other.com" ,
191+ expectedRole : "none" ,
192+ },
193+ {
194+ name : "No match, default_role is empty" ,
195+ mapping : RoleMappingRules {
196+ DomainMappings : map [string ]string {"@company.com" : "user" },
197+ },
198+ email : "outsider@other.com" ,
199+ expectedRole : "" ,
200+ },
201+ {
202+ name : "No match, falls back to default role" ,
203+ mapping : RoleMappingRules {
204+ DomainMappings : map [string ]string {"@company.com" : "user" },
205+ DefaultRole : "guest" ,
206+ },
207+ email : "outsider@other.com" ,
208+ expectedRole : "guest" ,
209+ },
210+ }
211+
212+ for _ , tt := range tests {
213+ t .Run (tt .name , func (t * testing.T ) {
214+ provider := & Provider {
215+ Name : "test" ,
216+ RoleMapping : & tt .mapping ,
217+ }
218+ role := provider .MapClaimsToRole (tt .email , tt .groups )
219+ if role != tt .expectedRole {
220+ t .Errorf ("Expected role %q, got %q" , tt .expectedRole , role )
221+ }
222+ })
223+ }
224+ }
225+
148226func TestProviderConfiguration (t * testing.T ) {
149227 ctx := context .Background ()
150228 roleMappingJSON := `{
0 commit comments