|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestParseRole(t *testing.T) { |
| 8 | + testCases := []struct { |
| 9 | + name string |
| 10 | + input string |
| 11 | + expected Role |
| 12 | + }{ |
| 13 | + // Single role tests |
| 14 | + {"Single shell role", "shell", Shell}, |
| 15 | + {"Single actions role", "actions", Actions}, |
| 16 | + {"Single download role", "download", Download}, |
| 17 | + {"None role", "none", None}, |
| 18 | + {"All role", "all", All}, |
| 19 | + |
| 20 | + // Case insensitive tests |
| 21 | + {"Shell uppercase", "SHELL", Shell}, |
| 22 | + {"Actions mixed case", "AcTiOnS", Actions}, |
| 23 | + {"Download with spaces", " download ", Download}, |
| 24 | + |
| 25 | + // Multiple roles with comma separator |
| 26 | + {"Shell and actions", "shell,actions", Shell | Actions}, |
| 27 | + {"All three roles", "shell,actions,download", Shell | Actions | Download}, |
| 28 | + {"Roles with spaces", "shell , actions , download", Shell | Actions | Download}, |
| 29 | + |
| 30 | + // Multiple roles with pipe separator |
| 31 | + {"Shell and actions with pipe", "shell|actions", Shell | Actions}, |
| 32 | + {"All three with pipe", "shell|actions|download", Shell | Actions | Download}, |
| 33 | + {"Mixed separators", "shell,actions|download", Shell | Actions | Download}, |
| 34 | + |
| 35 | + // JSON format tests |
| 36 | + {"JSON single role", `["shell"]`, Shell}, |
| 37 | + {"JSON multiple roles", `["shell", "actions"]`, Shell | Actions}, |
| 38 | + {"JSON all roles", `["shell", "actions", "download"]`, Shell | Actions | Download}, |
| 39 | + {"JSON with spaces", ` ["shell", "actions"] `, Shell | Actions}, |
| 40 | + |
| 41 | + // Edge cases |
| 42 | + {"Empty string", "", None}, |
| 43 | + {"Whitespace only", " ", None}, |
| 44 | + {"Invalid role", "invalid", None}, |
| 45 | + {"Mixed valid and invalid", "shell,invalid,actions", Shell | Actions}, |
| 46 | + {"None overrides others", "shell,none,actions", None}, |
| 47 | + {"All overrides others", "shell,all,actions", All}, |
| 48 | + |
| 49 | + // Invalid JSON |
| 50 | + {"Invalid JSON format", `["shell"`, None}, |
| 51 | + {"Malformed JSON", `{shell: "test"}`, None}, |
| 52 | + } |
| 53 | + |
| 54 | + for _, tc := range testCases { |
| 55 | + t.Run(tc.name, func(t *testing.T) { |
| 56 | + result := ParseRole(tc.input) |
| 57 | + if result != tc.expected { |
| 58 | + t.Errorf("ParseRole(%q) = %d, expected %d", tc.input, int(result), int(tc.expected)) |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
0 commit comments