|
| 1 | +package visualize |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestInferAuthTypeFromProxyLegacyFields(t *testing.T) { |
| 11 | + cases := []struct { |
| 12 | + name string |
| 13 | + userKey string |
| 14 | + appID string |
| 15 | + appKey string |
| 16 | + oidcURL string |
| 17 | + want string |
| 18 | + }{ |
| 19 | + {name: "api key booleans", userKey: "true", appID: "false", want: "api_key"}, |
| 20 | + {name: "app id booleans", userKey: "false", appID: "true", appKey: "app_key", want: "app_id_and_app_key"}, |
| 21 | + {name: "copec param names", userKey: "user_key", appID: "app_id", appKey: "app_key", want: "app_id_and_app_key"}, |
| 22 | + {name: "oidc issuer", oidcURL: "https://sso.example.com/realm/demo", want: "oidc"}, |
| 23 | + {name: "explicit auth type", userKey: "true", appID: "false", want: "api_key"}, |
| 24 | + } |
| 25 | + for _, tc := range cases { |
| 26 | + t.Run(tc.name, func(t *testing.T) { |
| 27 | + got := inferAuthTypeFromProxy("", tc.userKey, tc.appID, tc.appKey, "keycloak", tc.oidcURL, nil) |
| 28 | + if got != tc.want { |
| 29 | + t.Fatalf("inferAuthTypeFromProxy() = %q, want %q", got, tc.want) |
| 30 | + } |
| 31 | + }) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestInferAuthTypeFromProxyDefaultCredentialsPolicy(t *testing.T) { |
| 36 | + raw := []byte(`[ |
| 37 | + {"name":"apicast","enabled":true,"configuration":{}}, |
| 38 | + {"name":"default_credentials","enabled":true,"configuration":{"auth_type":"user_key"}} |
| 39 | + ]`) |
| 40 | + got := inferAuthTypeFromProxy("", "app_id", "app_key", "user_key", "", "", raw) |
| 41 | + if got != "api_key" { |
| 42 | + t.Fatalf("got %q, want api_key", got) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestReadAuthTypeFromYAMLListProduct(t *testing.T) { |
| 47 | + dir := t.TempDir() |
| 48 | + path := filepath.Join(dir, "B2B-IS.yaml") |
| 49 | + content := `apiVersion: v1 |
| 50 | +kind: List |
| 51 | +items: |
| 52 | +- apiVersion: capabilities.3scale.net/v1beta1 |
| 53 | + kind: Product |
| 54 | + spec: |
| 55 | + deployment: |
| 56 | + apicastHosted: |
| 57 | + authentication: |
| 58 | + appKeyAppID: |
| 59 | + appID: app_id |
| 60 | + appKey: app_key |
| 61 | +` |
| 62 | + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 63 | + t.Fatal(err) |
| 64 | + } |
| 65 | + got, err := readAuthTypeFromYAML(path) |
| 66 | + if err != nil { |
| 67 | + t.Fatal(err) |
| 68 | + } |
| 69 | + if got != "app_id_and_app_key" { |
| 70 | + t.Fatalf("got %q, want app_id_and_app_key", got) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestReadAuthTypeFromYAMLUserKey(t *testing.T) { |
| 75 | + dir := t.TempDir() |
| 76 | + path := filepath.Join(dir, "Comisiones.yaml") |
| 77 | + content := `apiVersion: v1 |
| 78 | +kind: List |
| 79 | +items: |
| 80 | +- kind: Product |
| 81 | + spec: |
| 82 | + deployment: |
| 83 | + apicastHosted: |
| 84 | + authentication: |
| 85 | + userkey: |
| 86 | + authUserKey: user_key |
| 87 | +` |
| 88 | + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 89 | + t.Fatal(err) |
| 90 | + } |
| 91 | + got, err := readAuthTypeFromYAML(path) |
| 92 | + if err != nil { |
| 93 | + t.Fatal(err) |
| 94 | + } |
| 95 | + if got != "api_key" { |
| 96 | + t.Fatalf("got %q, want api_key", got) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestLoadExportCopecStyleProxy(t *testing.T) { |
| 101 | + dir := t.TempDir() |
| 102 | + writeMinimalManifest(t, dir, false) |
| 103 | + writeJSON(t, filepath.Join(dir, "backends", "billing.json"), map[string]any{ |
| 104 | + "id": 1, "system_name": "billing", "name": "billing", |
| 105 | + }) |
| 106 | + productDir := filepath.Join(dir, "products", "demo") |
| 107 | + if err := os.MkdirAll(productDir, 0o755); err != nil { |
| 108 | + t.Fatal(err) |
| 109 | + } |
| 110 | + writeJSON(t, filepath.Join(productDir, "proxy.json"), map[string]any{ |
| 111 | + "proxy": map[string]any{ |
| 112 | + "service_id": 10, |
| 113 | + "auth_app_id": "app_id", |
| 114 | + "auth_app_key": "app_key", |
| 115 | + "auth_user_key": "user_key", |
| 116 | + "endpoint": "https://demo.example.com", |
| 117 | + }, |
| 118 | + }) |
| 119 | + yaml := `apiVersion: v1 |
| 120 | +kind: List |
| 121 | +items: |
| 122 | +- kind: Product |
| 123 | + spec: |
| 124 | + name: Demo Product |
| 125 | + systemName: demo |
| 126 | + deployment: |
| 127 | + apicastHosted: |
| 128 | + authentication: |
| 129 | + userkey: |
| 130 | + authUserKey: user_key |
| 131 | +` |
| 132 | + if err := os.WriteFile(filepath.Join(dir, "products", "demo.yaml"), []byte(yaml), 0o644); err != nil { |
| 133 | + t.Fatal(err) |
| 134 | + } |
| 135 | + |
| 136 | + tenant, err := LoadExport(dir) |
| 137 | + if err != nil { |
| 138 | + t.Fatal(err) |
| 139 | + } |
| 140 | + product := findProduct(t, tenant, "demo") |
| 141 | + if product.AuthType != "api_key" { |
| 142 | + t.Fatalf("AuthType = %q, want api_key", product.AuthType) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func writeMinimalManifest(t *testing.T, dir string, includeApps bool) { |
| 147 | + t.Helper() |
| 148 | + if err := os.MkdirAll(filepath.Join(dir, "backends"), 0o755); err != nil { |
| 149 | + t.Fatal(err) |
| 150 | + } |
| 151 | + if err := os.MkdirAll(filepath.Join(dir, "products"), 0o755); err != nil { |
| 152 | + t.Fatal(err) |
| 153 | + } |
| 154 | + manifest := map[string]any{ |
| 155 | + "schema_version": "1.0", |
| 156 | + "exported_at": "2026-06-05T12:00:00Z", |
| 157 | + "admin_url": "https://tenant-admin.example.com", |
| 158 | + "product_count": 1, |
| 159 | + "backend_count": 1, |
| 160 | + "include_applications": includeApps, |
| 161 | + "incomplete": false, |
| 162 | + } |
| 163 | + writeJSON(t, filepath.Join(dir, "manifest.json"), manifest) |
| 164 | +} |
| 165 | + |
| 166 | +func writeJSON(t *testing.T, path string, payload any) { |
| 167 | + t.Helper() |
| 168 | + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 169 | + t.Fatal(err) |
| 170 | + } |
| 171 | + data, err := json.Marshal(payload) |
| 172 | + if err != nil { |
| 173 | + t.Fatal(err) |
| 174 | + } |
| 175 | + if err := os.WriteFile(path, data, 0o644); err != nil { |
| 176 | + t.Fatal(err) |
| 177 | + } |
| 178 | +} |
0 commit comments