|
| 1 | +package terraform |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestResourceBlueprint_ValidateServiceAccess(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + usableBy []string |
| 13 | + serviceSubtype string |
| 14 | + resourceName string |
| 15 | + resourceType string |
| 16 | + expectError bool |
| 17 | + errorContains string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "empty usable_by allows all service types", |
| 21 | + usableBy: []string{}, |
| 22 | + serviceSubtype: "lambda", |
| 23 | + resourceName: "my_bucket", |
| 24 | + resourceType: "bucket", |
| 25 | + expectError: false, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "nil usable_by allows all service types", |
| 29 | + usableBy: nil, |
| 30 | + serviceSubtype: "fargate", |
| 31 | + resourceName: "my_database", |
| 32 | + resourceType: "database", |
| 33 | + expectError: false, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "service subtype in allowed list succeeds", |
| 37 | + usableBy: []string{"web", "worker", "api"}, |
| 38 | + serviceSubtype: "web", |
| 39 | + resourceName: "users_db", |
| 40 | + resourceType: "database", |
| 41 | + expectError: false, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "service subtype not in allowed list fails", |
| 45 | + usableBy: []string{"web", "worker"}, |
| 46 | + serviceSubtype: "lambda", |
| 47 | + resourceName: "users_db", |
| 48 | + resourceType: "database", |
| 49 | + expectError: true, |
| 50 | + errorContains: "database 'users_db' cannot be accessed by service subtype 'lambda'", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "single allowed service type succeeds", |
| 54 | + usableBy: []string{"fargate"}, |
| 55 | + serviceSubtype: "fargate", |
| 56 | + resourceName: "assets", |
| 57 | + resourceType: "bucket", |
| 58 | + expectError: false, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "single allowed service type with different subtype fails", |
| 62 | + usableBy: []string{"fargate"}, |
| 63 | + serviceSubtype: "lambda", |
| 64 | + resourceName: "assets", |
| 65 | + resourceType: "bucket", |
| 66 | + expectError: true, |
| 67 | + errorContains: "bucket 'assets' cannot be accessed by service subtype 'lambda'", |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "error message includes allowed types list", |
| 71 | + usableBy: []string{"web", "worker", "cron"}, |
| 72 | + serviceSubtype: "lambda", |
| 73 | + resourceName: "cache_db", |
| 74 | + resourceType: "database", |
| 75 | + expectError: true, |
| 76 | + errorContains: "only usable by service types: [web worker cron]", |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for _, tt := range tests { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + blueprint := &ResourceBlueprint{ |
| 83 | + UsableBy: tt.usableBy, |
| 84 | + } |
| 85 | + |
| 86 | + err := blueprint.ValidateServiceAccess(tt.serviceSubtype, tt.resourceName, tt.resourceType) |
| 87 | + |
| 88 | + if tt.expectError { |
| 89 | + assert.Error(t, err) |
| 90 | + if tt.errorContains != "" { |
| 91 | + assert.Contains(t, err.Error(), tt.errorContains) |
| 92 | + } |
| 93 | + } else { |
| 94 | + assert.NoError(t, err) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments