|
1 | 1 | package azure |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "net/http" |
5 | 6 | "net/http/httptest" |
6 | 7 | "testing" |
| 8 | + "time" |
7 | 9 |
|
| 10 | + "github.com/google/uuid" |
8 | 11 | "github.com/sirupsen/logrus" |
9 | 12 | "github.com/stretchr/testify/assert" |
10 | 13 | "github.com/stretchr/testify/require" |
11 | 14 | "github.com/superplanehq/superplane/pkg/configuration" |
12 | 15 | "github.com/superplanehq/superplane/pkg/core" |
| 16 | + "github.com/superplanehq/superplane/pkg/oidc" |
13 | 17 | ) |
14 | 18 |
|
| 19 | +// mockOIDCProvider implements oidc.Provider for testing. |
| 20 | +type mockOIDCProvider struct{} |
| 21 | + |
| 22 | +func (m *mockOIDCProvider) Sign(subject string, duration time.Duration, audience string, additionalClaims map[string]any) (string, error) { |
| 23 | + return "mock-jwt-token", nil |
| 24 | +} |
| 25 | + |
| 26 | +func (m *mockOIDCProvider) PublicJWKs() []oidc.PublicJWK { |
| 27 | + return nil |
| 28 | +} |
| 29 | + |
| 30 | +// mockIntegrationContext implements core.IntegrationContext for testing. |
| 31 | +type mockIntegrationContext struct { |
| 32 | + id string |
| 33 | + config map[string]string |
| 34 | +} |
| 35 | + |
| 36 | +func (m *mockIntegrationContext) ID() uuid.UUID { |
| 37 | + id, _ := uuid.Parse(m.id) |
| 38 | + return id |
| 39 | +} |
| 40 | + |
| 41 | +func (m *mockIntegrationContext) GetConfig(name string) ([]byte, error) { |
| 42 | + if v, ok := m.config[name]; ok { |
| 43 | + return []byte(v), nil |
| 44 | + } |
| 45 | + return nil, fmt.Errorf("config %s not found", name) |
| 46 | +} |
| 47 | + |
| 48 | +func (m *mockIntegrationContext) GetMetadata() any { return nil } |
| 49 | +func (m *mockIntegrationContext) SetMetadata(any) {} |
| 50 | +func (m *mockIntegrationContext) Ready() {} |
| 51 | +func (m *mockIntegrationContext) Error(string) {} |
| 52 | +func (m *mockIntegrationContext) NewBrowserAction(core.BrowserAction) {} |
| 53 | +func (m *mockIntegrationContext) RemoveBrowserAction() {} |
| 54 | +func (m *mockIntegrationContext) SetSecret(string, []byte) error { return nil } |
| 55 | +func (m *mockIntegrationContext) GetSecrets() ([]core.IntegrationSecret, error) { return nil, nil } |
| 56 | +func (m *mockIntegrationContext) RequestWebhook(any) error { return nil } |
| 57 | +func (m *mockIntegrationContext) Subscribe(any) (*uuid.UUID, error) { return nil, nil } |
| 58 | +func (m *mockIntegrationContext) ScheduleResync(time.Duration) error { return nil } |
| 59 | +func (m *mockIntegrationContext) ScheduleActionCall(string, any, time.Duration) error { return nil } |
| 60 | +func (m *mockIntegrationContext) ListSubscriptions() ([]core.IntegrationSubscriptionContext, error) { |
| 61 | + return nil, nil |
| 62 | +} |
| 63 | + |
15 | 64 | func TestAzureIntegration_Name(t *testing.T) { |
16 | 65 | integration := &AzureIntegration{} |
17 | 66 | assert.Equal(t, "azure", integration.Name()) |
@@ -189,18 +238,39 @@ func TestAzureIntegration_HandleRequest_Unknown(t *testing.T) { |
189 | 238 | assert.Equal(t, http.StatusNotFound, rec.Code) |
190 | 239 | } |
191 | 240 |
|
192 | | -func TestAzureIntegration_GetProvider(t *testing.T) { |
| 241 | +func TestAzureIntegration_SetOIDCProvider(t *testing.T) { |
193 | 242 | integration := &AzureIntegration{} |
194 | 243 |
|
195 | | - // Initially nil |
196 | | - assert.Nil(t, integration.GetProvider()) |
| 244 | + assert.Nil(t, integration.oidcProvider) |
| 245 | + |
| 246 | + mockOIDC := &mockOIDCProvider{} |
| 247 | + integration.SetOIDCProvider(mockOIDC) |
197 | 248 |
|
198 | | - // Set a mock provider |
| 249 | + assert.NotNil(t, integration.oidcProvider) |
| 250 | + assert.Equal(t, mockOIDC, integration.oidcProvider) |
| 251 | +} |
| 252 | + |
| 253 | +func TestAzureIntegration_EnsureProvider_ReturnsCachedProvider(t *testing.T) { |
199 | 254 | provider := &AzureProvider{} |
200 | | - integration.provider = provider |
| 255 | + integration := &AzureIntegration{ |
| 256 | + provider: provider, |
| 257 | + integrationID: "test-id", |
| 258 | + } |
201 | 259 |
|
202 | | - assert.NotNil(t, integration.GetProvider()) |
203 | | - assert.Equal(t, provider, integration.GetProvider()) |
| 260 | + ctx := &mockIntegrationContext{id: "test-id"} |
| 261 | + result, err := integration.ensureProvider(ctx) |
| 262 | + assert.NoError(t, err) |
| 263 | + assert.Equal(t, provider, result) |
| 264 | +} |
| 265 | + |
| 266 | +func TestAzureIntegration_EnsureProvider_FailsWithoutOIDC(t *testing.T) { |
| 267 | + integration := &AzureIntegration{} |
| 268 | + |
| 269 | + ctx := &mockIntegrationContext{id: "test-id"} |
| 270 | + result, err := integration.ensureProvider(ctx) |
| 271 | + assert.Error(t, err) |
| 272 | + assert.Nil(t, result) |
| 273 | + assert.Contains(t, err.Error(), "OIDC provider not available") |
204 | 274 | } |
205 | 275 |
|
206 | 276 | func TestConfiguration_Struct(t *testing.T) { |
|
0 commit comments