|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestBuildSubscriptionPortalURL(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + tenantID string |
| 12 | + subscriptionID string |
| 13 | + want string |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "valid subscription URL", |
| 17 | + tenantID: "12345678-1234-1234-1234-123456789012", |
| 18 | + subscriptionID: "87654321-4321-4321-4321-210987654321", |
| 19 | + want: "https://portal.azure.com/#@12345678-1234-1234-1234-123456789012/resource/subscriptions/87654321-4321-4321-4321-210987654321/overview", |
| 20 | + }, |
| 21 | + } |
| 22 | + |
| 23 | + for _, tt := range tests { |
| 24 | + t.Run(tt.name, func(t *testing.T) { |
| 25 | + got := BuildSubscriptionPortalURL(tt.tenantID, tt.subscriptionID) |
| 26 | + if got != tt.want { |
| 27 | + t.Errorf("BuildSubscriptionPortalURL() = %v, want %v", got, tt.want) |
| 28 | + } |
| 29 | + // Verify no double slashes (except in protocol) |
| 30 | + if strings.Contains(got, "://") { |
| 31 | + path := strings.SplitN(got, "://", 2)[1] |
| 32 | + if strings.Contains(path, "//") { |
| 33 | + t.Errorf("URL contains double slashes: %v", got) |
| 34 | + } |
| 35 | + } |
| 36 | + }) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestBuildResourceGroupPortalURL(t *testing.T) { |
| 41 | + tests := []struct { |
| 42 | + name string |
| 43 | + tenantID string |
| 44 | + subscriptionID string |
| 45 | + resourceGroupName string |
| 46 | + want string |
| 47 | + }{ |
| 48 | + { |
| 49 | + name: "valid resource group URL", |
| 50 | + tenantID: "12345678-1234-1234-1234-123456789012", |
| 51 | + subscriptionID: "87654321-4321-4321-4321-210987654321", |
| 52 | + resourceGroupName: "my-resource-group", |
| 53 | + want: "https://portal.azure.com/#@12345678-1234-1234-1234-123456789012/resource/subscriptions/87654321-4321-4321-4321-210987654321/resourceGroups/my-resource-group/overview", |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + for _, tt := range tests { |
| 58 | + t.Run(tt.name, func(t *testing.T) { |
| 59 | + got := BuildResourceGroupPortalURL(tt.tenantID, tt.subscriptionID, tt.resourceGroupName) |
| 60 | + if got != tt.want { |
| 61 | + t.Errorf("BuildResourceGroupPortalURL() = %v, want %v", got, tt.want) |
| 62 | + } |
| 63 | + // Verify no double slashes (except in protocol) |
| 64 | + if strings.Contains(got, "://") { |
| 65 | + path := strings.SplitN(got, "://", 2)[1] |
| 66 | + if strings.Contains(path, "//") { |
| 67 | + t.Errorf("URL contains double slashes: %v", got) |
| 68 | + } |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestBuildResourcePortalURL(t *testing.T) { |
| 75 | + tests := []struct { |
| 76 | + name string |
| 77 | + tenantID string |
| 78 | + resourceID string |
| 79 | + want string |
| 80 | + }{ |
| 81 | + { |
| 82 | + name: "valid resource URL with leading slash", |
| 83 | + tenantID: "12345678-1234-1234-1234-123456789012", |
| 84 | + resourceID: "/subscriptions/87654321-4321-4321-4321-210987654321/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachines/my-vm", |
| 85 | + want: "https://portal.azure.com/#@12345678-1234-1234-1234-123456789012/resource/subscriptions/87654321-4321-4321-4321-210987654321/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachines/my-vm/overview", |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "resource URL should not have double slashes", |
| 89 | + tenantID: "tenant-123", |
| 90 | + resourceID: "/subscriptions/sub-123/resourceGroups/rg-1/providers/Microsoft.Storage/storageAccounts/account1", |
| 91 | + want: "https://portal.azure.com/#@tenant-123/resource/subscriptions/sub-123/resourceGroups/rg-1/providers/Microsoft.Storage/storageAccounts/account1/overview", |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + for _, tt := range tests { |
| 96 | + t.Run(tt.name, func(t *testing.T) { |
| 97 | + got := BuildResourcePortalURL(tt.tenantID, tt.resourceID) |
| 98 | + if got != tt.want { |
| 99 | + t.Errorf("BuildResourcePortalURL() = %v, want %v", got, tt.want) |
| 100 | + } |
| 101 | + // Verify no double slashes (except in protocol) |
| 102 | + if strings.Contains(got, "://") { |
| 103 | + path := strings.SplitN(got, "://", 2)[1] |
| 104 | + if strings.Contains(path, "//") { |
| 105 | + t.Errorf("URL contains double slashes: %v", got) |
| 106 | + } |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
0 commit comments