|
| 1 | +package token_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + |
| 8 | + "github.com/ilijamt/vault-plugin-secrets-gitlab/internal/token" |
| 9 | +) |
| 10 | + |
| 11 | +func TestIsValidPath(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + path string |
| 15 | + tokenType token.Type |
| 16 | + valid bool |
| 17 | + }{ |
| 18 | + // Test cases |
| 19 | + {"personal access token - dynamic path", "admin-user", token.TypePersonal, true}, |
| 20 | + {"project access token - dynamic path", "example/example", token.TypeProject, true}, |
| 21 | + {"group access token - dynamic path", "example", token.TypeGroup, true}, |
| 22 | + |
| 23 | + // TypePersonal and TypeUserServiceAccount: single segment |
| 24 | + {"single valid - letters", "userone", token.TypePersonal, true}, |
| 25 | + {"single valid - underscore", "user_one", token.TypeUserServiceAccount, true}, |
| 26 | + {"single valid - hyphen+dot", "user.one-two", token.TypePersonal, true}, |
| 27 | + {"single valid - digits", "user2024", token.TypePersonal, true}, |
| 28 | + {"starts with invalid prefix '-'", "-user", token.TypePersonal, false}, |
| 29 | + {"starts with invalid prefix '_'", "_user", token.TypeUserServiceAccount, false}, |
| 30 | + {"starts with invalid prefix '.'", ".user", token.TypePersonal, false}, |
| 31 | + {"ends with invalid suffix '-'", "user-", token.TypePersonal, false}, |
| 32 | + {"ends with invalid suffix '_'", "user_", token.TypeUserServiceAccount, false}, |
| 33 | + {"ends with invalid suffix '.'", "user.", token.TypePersonal, false}, |
| 34 | + {"ends with invalid suffix '.git'", "user.git", token.TypeUserServiceAccount, false}, |
| 35 | + {"ends with invalid suffix '.atom'", "user.atom", token.TypePersonal, false}, |
| 36 | + {"ends with invalid suffix (mixed case)", "user.Atom", token.TypePersonal, true}, // Only lower ".atom" is invalid |
| 37 | + {"too many segments", "user/one", token.TypePersonal, false}, |
| 38 | + {"empty path", "", token.TypePersonal, false}, |
| 39 | + {"whitespace path", " ", token.TypeUserServiceAccount, false}, |
| 40 | + |
| 41 | + // TypeGroupServiceAccount: two segments |
| 42 | + {"group SA valid", "group1/account2", token.TypeGroupServiceAccount, true}, |
| 43 | + {"group SA valid underscore", "group1/_account", token.TypeGroupServiceAccount, false}, |
| 44 | + {"group SA valid, dot middle", "team.service/acct-2", token.TypeGroupServiceAccount, true}, |
| 45 | + {"group SA too few segments", "group1", token.TypeGroupServiceAccount, false}, |
| 46 | + {"group SA too many segments", "g/a/too/many", token.TypeGroupServiceAccount, false}, |
| 47 | + {"group SA segment starts with invalid", "-group/acct", token.TypeGroupServiceAccount, false}, |
| 48 | + {"group SA segment ends with invalid", "group/acct-", token.TypeGroupServiceAccount, false}, |
| 49 | + |
| 50 | + // TypeProject, TypeGroup, TypeProjectDeploy, TypeGroupDeploy, TypePipelineProjectTrigger types |
| 51 | + {"one segment", "myproj", token.TypeProject, true}, |
| 52 | + {"two segments", "group/proj", token.TypeGroup, true}, |
| 53 | + {"segments invalid", "grp-1/pro.j2/_b", token.TypeProjectDeploy, false}, |
| 54 | + {"segments valid", "grp1/pro.j2/b_c", token.TypeGroup, true}, |
| 55 | + {"forbidden prefix", "-group/project", token.TypeGroupDeploy, false}, |
| 56 | + {"forbidden suffix", "g1/proj.git", token.TypeProject, false}, |
| 57 | + {"trailing slash", "g1/", token.TypeProject, false}, |
| 58 | + {"leading slash", "/g1", token.TypeProjectDeploy, false}, |
| 59 | + {"double slash (empty segment)", "g1//p2", token.TypeProjectDeploy, false}, |
| 60 | + {"ends with forbidden segment edge", "g1/g2.", token.TypeProject, false}, |
| 61 | + {"dots and hyphens", "g1.part-2/project_3.one", token.TypeGroup, true}, |
| 62 | + |
| 63 | + // Empty segment from double slash |
| 64 | + {"double slash", "foo//bar", token.TypeProject, false}, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tt := range tests { |
| 68 | + assert.Equal(t, tt.valid, token.IsValidPath(tt.path, tt.tokenType)) |
| 69 | + } |
| 70 | +} |
0 commit comments