@@ -485,3 +485,88 @@ func expectEquals(t *testing.T, a interface{}, b interface{}) {
485485 t .Errorf ("Expected %+v to equal %+v" , a , b )
486486 }
487487}
488+
489+ func TestTokenIdentity (t * testing.T ) {
490+ // Note: These tests verify that the connector returns groups based on its configuration.
491+ // The actual inclusion of groups in the final Dex token depends on the 'groups' scope
492+ // in the token exchange request, which is handled by the Dex server, not the connector.
493+ tests := []struct {
494+ name string
495+ userInfo userInfo
496+ groups []string
497+ getGroupsPermission bool
498+ useLoginAsID bool
499+ expectUserID string
500+ expectGroups []string
501+ }{
502+ {
503+ name : "without groups config" ,
504+ expectUserID : "12345678" ,
505+ expectGroups : nil ,
506+ },
507+ {
508+ name : "with groups filter" ,
509+ userInfo : userInfo {
510+ Groups : []string {"team-1" , "team-2" },
511+ },
512+ groups : []string {"team-1" },
513+ expectUserID : "12345678" ,
514+ expectGroups : []string {"team-1" },
515+ },
516+ {
517+ name : "with groups permission" ,
518+ userInfo : userInfo {
519+ Groups : []string {"ops" , "dev" },
520+ OwnerPermission : []string {"ops" },
521+ DeveloperPermission : []string {"dev" },
522+ MaintainerPermission : []string {},
523+ },
524+ getGroupsPermission : true ,
525+ expectUserID : "12345678" ,
526+ expectGroups : []string {"ops" , "dev" , "ops:owner" , "dev:developer" },
527+ },
528+ {
529+ name : "with useLoginAsID" ,
530+ useLoginAsID : true ,
531+ expectUserID : "joebloggs" ,
532+ expectGroups : nil ,
533+ },
534+ }
535+
536+ for _ , tc := range tests {
537+ t .Run (tc .name , func (t * testing.T ) {
538+ responses := map [string ]interface {}{
539+ "/api/v4/user" : gitlabUser {
540+ Email : "some@email.com" ,
541+ ID : 12345678 ,
542+ Name : "Joe Bloggs" ,
543+ Username : "joebloggs" ,
544+ },
545+ "/oauth/userinfo" : tc .userInfo ,
546+ }
547+
548+ s := newTestServer (responses )
549+ defer s .Close ()
550+
551+ c := gitlabConnector {
552+ baseURL : s .URL ,
553+ httpClient : newClient (),
554+ groups : tc .groups ,
555+ getGroupsPermission : tc .getGroupsPermission ,
556+ useLoginAsID : tc .useLoginAsID ,
557+ }
558+
559+ accessToken := "test-access-token"
560+ ctx := context .Background ()
561+ identity , err := c .TokenIdentity (ctx , "urn:ietf:params:oauth:token-type:access_token" , accessToken )
562+
563+ expectNil (t , err )
564+ expectEquals (t , identity .UserID , tc .expectUserID )
565+ expectEquals (t , identity .Username , "Joe Bloggs" )
566+ expectEquals (t , identity .PreferredUsername , "joebloggs" )
567+ expectEquals (t , identity .Email , "some@email.com" )
568+ expectEquals (t , identity .EmailVerified , true )
569+ expectEquals (t , identity .Groups , tc .expectGroups )
570+ })
571+ }
572+ }
0 commit comments