|
| 1 | +// MIT License |
| 2 | +// |
| 3 | +// Copyright (c) Microsoft Corporation. |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE |
| 22 | + |
| 23 | +package main |
| 24 | + |
| 25 | +import ( |
| 26 | + "os" |
| 27 | + "path/filepath" |
| 28 | + "reflect" |
| 29 | + "testing" |
| 30 | +) |
| 31 | + |
| 32 | +func TestParseInitialPermissions(t *testing.T) { |
| 33 | + tests := []struct { |
| 34 | + name string |
| 35 | + input string |
| 36 | + want []string |
| 37 | + wantErr bool |
| 38 | + setupFile bool |
| 39 | + fileContent string |
| 40 | + }{ |
| 41 | + { |
| 42 | + name: "empty string returns nil", |
| 43 | + input: "", |
| 44 | + want: nil, |
| 45 | + wantErr: false, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "single permission", |
| 49 | + input: "Microsoft.Storage/storageAccounts/read", |
| 50 | + want: []string{"Microsoft.Storage/storageAccounts/read"}, |
| 51 | + wantErr: false, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "comma-separated permissions", |
| 55 | + input: "Microsoft.Storage/storageAccounts/read,Microsoft.Storage/storageAccounts/write", |
| 56 | + want: []string{ |
| 57 | + "Microsoft.Storage/storageAccounts/read", |
| 58 | + "Microsoft.Storage/storageAccounts/write", |
| 59 | + }, |
| 60 | + wantErr: false, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "comma-separated permissions with spaces", |
| 64 | + input: "Microsoft.Storage/storageAccounts/read, Microsoft.Storage/storageAccounts/write , Microsoft.Storage/storageAccounts/listKeys/action", |
| 65 | + want: []string{ |
| 66 | + "Microsoft.Storage/storageAccounts/read", |
| 67 | + "Microsoft.Storage/storageAccounts/write", |
| 68 | + "Microsoft.Storage/storageAccounts/listKeys/action", |
| 69 | + }, |
| 70 | + wantErr: false, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "file reference with valid JSON", |
| 74 | + input: "@testperms.json", |
| 75 | + setupFile: true, |
| 76 | + fileContent: `{ |
| 77 | + "RequiredPermissions": { |
| 78 | + "": [ |
| 79 | + "Microsoft.Storage/storageAccounts/read", |
| 80 | + "Microsoft.Storage/storageAccounts/write" |
| 81 | + ] |
| 82 | + } |
| 83 | + }`, |
| 84 | + want: []string{ |
| 85 | + "Microsoft.Storage/storageAccounts/read", |
| 86 | + "Microsoft.Storage/storageAccounts/write", |
| 87 | + }, |
| 88 | + wantErr: false, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "file reference to non-existent file", |
| 92 | + input: "@nonexistent.json", |
| 93 | + want: nil, |
| 94 | + wantErr: true, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "file reference with invalid JSON", |
| 98 | + input: "@invalid.json", |
| 99 | + setupFile: true, |
| 100 | + fileContent: `not valid json`, |
| 101 | + want: nil, |
| 102 | + wantErr: true, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "file reference with empty permissions array", |
| 106 | + input: "@empty.json", |
| 107 | + setupFile: true, |
| 108 | + fileContent: `{ |
| 109 | + "RequiredPermissions": { |
| 110 | + "": [] |
| 111 | + } |
| 112 | + }`, |
| 113 | + want: []string{}, |
| 114 | + wantErr: false, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + for _, tt := range tests { |
| 119 | + t.Run(tt.name, func(t *testing.T) { |
| 120 | + // Setup file if needed |
| 121 | + if tt.setupFile { |
| 122 | + filename := tt.input[1:] // Remove @ prefix |
| 123 | + tmpDir := t.TempDir() |
| 124 | + filePath := filepath.Join(tmpDir, filename) |
| 125 | + err := os.WriteFile(filePath, []byte(tt.fileContent), 0644) |
| 126 | + if err != nil { |
| 127 | + t.Fatalf("failed to create test file: %v", err) |
| 128 | + } |
| 129 | + // Update input to use absolute path |
| 130 | + tt.input = "@" + filePath |
| 131 | + } |
| 132 | + |
| 133 | + got, err := parseInitialPermissions(tt.input) |
| 134 | + if (err != nil) != tt.wantErr { |
| 135 | + t.Errorf("parseInitialPermissions() error = %v, wantErr %v", err, tt.wantErr) |
| 136 | + return |
| 137 | + } |
| 138 | + if !reflect.DeepEqual(got, tt.want) { |
| 139 | + t.Errorf("parseInitialPermissions() = %v, want %v", got, tt.want) |
| 140 | + } |
| 141 | + }) |
| 142 | + } |
| 143 | +} |
0 commit comments