@@ -14,6 +14,7 @@ import (
1414
1515 gitlab "github.com/ilijamt/vault-plugin-secrets-gitlab"
1616 gitlab2 "github.com/ilijamt/vault-plugin-secrets-gitlab/internal/gitlab"
17+ modelToken "github.com/ilijamt/vault-plugin-secrets-gitlab/internal/model/token"
1718 "github.com/ilijamt/vault-plugin-secrets-gitlab/internal/token"
1819)
1920
@@ -37,8 +38,8 @@ func TestPathTokenRoles(t *testing.T) {
3738 require .ErrorIs (t , err , gitlab .ErrRoleNotFound )
3839 })
3940
40- var generalTokenCreation = func (t * testing.T , tokenType token.Type , level token.AccessLevel , gitlabRevokesToken bool ) {
41- t .Logf ("token creation, token type: %s, level: %s, gitlab revokes token: %t" , tokenType , level , gitlabRevokesToken )
41+ var generalTokenCreation = func (t * testing.T , tokenType token.Type , level token.AccessLevel , gitlabRevokesToken bool , path string , pathOverride string ) {
42+ t .Logf ("token creation, token type: %s, level: %s, gitlab revokes token: %t, path: %s, path override: %s " , tokenType , level , gitlabRevokesToken , path , pathOverride )
4243 ctx := getCtxGitlabClient (t , "unit" )
4344 client := newInMemoryClient (true )
4445 ctx = gitlab2 .ClientNewContext (ctx , client )
@@ -56,16 +57,6 @@ func TestPathTokenRoles(t *testing.T) {
5657 ttl = "48h"
5758 }
5859
59- var path string
60- switch tokenType {
61- case token .TypeProject :
62- path = "example/example"
63- case token .TypePersonal :
64- path = "admin-user"
65- case token .TypeGroup :
66- path = "example"
67- }
68-
6960 // create a role
7061 resp , err := b .HandleRequest (ctx , & logical.Request {
7162 Operation : logical .CreateOperation ,
@@ -84,10 +75,18 @@ func TestPathTokenRoles(t *testing.T) {
8475 require .NoError (t , resp .Error ())
8576
8677 // read an access token
87- resp , err = b .HandleRequest (ctx , & logical.Request {
78+ reqPath := fmt .Sprintf ("%s/test" , gitlab .PathTokenRoleStorage )
79+ if pathOverride != "" {
80+ reqPath += fmt .Sprintf ("/%s" , pathOverride )
81+ }
82+
83+ req := & logical.Request {
8884 Operation : logical .ReadOperation ,
89- Path : fmt .Sprintf ("%s/test" , gitlab .PathTokenRoleStorage ), Storage : l ,
90- })
85+ Path : reqPath ,
86+ Storage : l ,
87+ }
88+
89+ resp , err = b .HandleRequest (ctx , req )
9190 require .NoError (t , err )
9291 require .NotNil (t , resp )
9392 require .NotNil (t , resp .Secret )
@@ -99,6 +98,24 @@ func TestPathTokenRoles(t *testing.T) {
9998
10099 require .Contains (t , client .accessTokens , fmt .Sprintf ("%s_%v" , tokenType .String (), tokenId ))
101100
101+ // Check path correctness
102+ expectedPath := path
103+ if path == "*" && pathOverride != "" {
104+ expectedPath = pathOverride
105+ }
106+
107+ var createdToken = client .accessTokens [fmt .Sprintf ("%s_%v" , tokenType .String (), tokenId )]
108+ var actualPath string
109+ switch v := createdToken .(type ) {
110+ case * modelToken.TokenProject :
111+ actualPath = v .Path
112+ case * modelToken.TokenPersonal :
113+ actualPath = v .Path
114+ case * modelToken.TokenGroup :
115+ actualPath = v .Path
116+ }
117+ require .Equal (t , expectedPath , actualPath , "Token path mismatch" )
118+
102119 // revoke the access token
103120 resp , err = b .HandleRequest (ctx , & logical.Request {
104121 Operation : logical .RevokeOperation ,
@@ -152,17 +169,18 @@ func TestPathTokenRoles(t *testing.T) {
152169 }
153170
154171 t .Run ("personal access token" , func (t * testing.T ) {
155- generalTokenCreation (t , token .TypePersonal , token .AccessLevelUnknown , false )
156- generalTokenCreation (t , token .TypePersonal , token .AccessLevelUnknown , true )
172+ generalTokenCreation (t , token .TypePersonal , token .AccessLevelUnknown , false , "admin-user" , "" )
173+ generalTokenCreation (t , token .TypePersonal , token .AccessLevelUnknown , true , "admin-user" , "" )
174+ generalTokenCreation (t , token .TypeProject , token .AccessLevelGuestPermissions , false , "*" , "some-user" )
157175 })
158176
159177 t .Run ("project access token" , func (t * testing.T ) {
160- generalTokenCreation (t , token .TypeProject , token .AccessLevelGuestPermissions , false )
161- generalTokenCreation (t , token .TypeProject , token .AccessLevelGuestPermissions , true )
178+ generalTokenCreation (t , token .TypeProject , token .AccessLevelGuestPermissions , false , "example/example" , "" )
179+ generalTokenCreation (t , token .TypeProject , token .AccessLevelGuestPermissions , true , "example/example" , "" )
162180 })
163181
164182 t .Run ("group access token" , func (t * testing.T ) {
165- generalTokenCreation (t , token .TypeGroup , token .AccessLevelGuestPermissions , false )
166- generalTokenCreation (t , token .TypeGroup , token .AccessLevelGuestPermissions , true )
183+ generalTokenCreation (t , token .TypeGroup , token .AccessLevelGuestPermissions , false , "example" , "" )
184+ generalTokenCreation (t , token .TypeGroup , token .AccessLevelGuestPermissions , true , "example" , "" )
167185 })
168186}
0 commit comments