-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(migration): add webhook secretKey to authType migration in v1130 #27438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+326
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
225 changes: 225 additions & 0 deletions
225
...rvice/src/test/java/org/openmetadata/service/migration/utils/v1130/MigrationUtilTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| package org.openmetadata.service.migration.utils.v1130; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.mockito.Answers.RETURNS_DEEP_STUBS; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
| import org.jdbi.v3.core.Handle; | ||
| import org.jdbi.v3.core.statement.Update; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.MockedStatic; | ||
| import org.openmetadata.service.resources.databases.DatasourceConfig; | ||
|
|
||
| class MigrationUtilTest { | ||
|
|
||
| private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
|
||
| private static final String WEBHOOK_WITH_SECRET_KEY = | ||
| """ | ||
| { | ||
| "destinations": [ | ||
| { | ||
| "type": "Webhook", | ||
| "config": { | ||
| "endpoint": "https://example.com/hook", | ||
| "secretKey": "mysecret" | ||
| } | ||
| } | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
| ] | ||
| } | ||
| """; | ||
|
|
||
| private static final String WEBHOOK_WITHOUT_SECRET_KEY = | ||
| """ | ||
| { | ||
| "destinations": [ | ||
| { | ||
| "type": "Webhook", | ||
| "config": { | ||
| "endpoint": "https://example.com/hook" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| """; | ||
|
|
||
| private static final String WEBHOOK_WITH_EMPTY_SECRET_KEY = | ||
| """ | ||
| { | ||
| "destinations": [ | ||
| { | ||
| "type": "Webhook", | ||
| "config": { | ||
| "endpoint": "https://example.com/hook", | ||
| "secretKey": "" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| """; | ||
|
|
||
| private static final String WEBHOOK_ALREADY_MIGRATED = | ||
| """ | ||
| { | ||
| "destinations": [ | ||
| { | ||
| "type": "Webhook", | ||
| "config": { | ||
| "endpoint": "https://example.com/hook", | ||
| "authType": { "type": "bearer", "secretKey": "mysecret" } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| """; | ||
|
|
||
| private static final String NON_WEBHOOK_DESTINATION = | ||
| """ | ||
| { | ||
| "destinations": [ | ||
| { | ||
| "type": "Slack", | ||
| "config": { | ||
| "secretKey": "mysecret" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| """; | ||
|
|
||
| private Map<String, Object> row(String json) { | ||
| return Map.of("id", UUID.randomUUID().toString(), "json", json); | ||
| } | ||
|
|
||
| private Handle handleReturningRows(List<Map<String, Object>> rows) { | ||
| Handle handle = mock(Handle.class, RETURNS_DEEP_STUBS); | ||
| when(handle.createQuery(any(String.class)).mapToMap().list()).thenReturn(rows); | ||
| return handle; | ||
| } | ||
|
|
||
| private Handle handleWithUpdateCapture(List<Map<String, Object>> rows, Update mockUpdate) { | ||
| Handle handle = mock(Handle.class, RETURNS_DEEP_STUBS); | ||
| when(handle.createQuery(any(String.class)).mapToMap().list()).thenReturn(rows); | ||
| when(handle.createUpdate(any(String.class))).thenReturn(mockUpdate); | ||
| return handle; | ||
| } | ||
|
|
||
| @Test | ||
| void migrateWebhookSecretKeyToAuthTypeIsNoOpWhenNoRows() { | ||
| Handle handle = handleReturningRows(List.of()); | ||
|
|
||
| assertDoesNotThrow(() -> MigrationUtil.migrateWebhookSecretKeyToAuthType(handle)); | ||
|
|
||
| verify(handle, never()).createUpdate(any()); | ||
| } | ||
|
|
||
| @Test | ||
| void migrateWebhookSecretKeyToAuthTypeIsNoOpWhenNoSecretKey() { | ||
| Handle handle = handleReturningRows(List.of(row(WEBHOOK_WITHOUT_SECRET_KEY))); | ||
|
|
||
| assertDoesNotThrow(() -> MigrationUtil.migrateWebhookSecretKeyToAuthType(handle)); | ||
|
|
||
| verify(handle, never()).createUpdate(any()); | ||
| } | ||
|
|
||
| @Test | ||
| void migrateWebhookSecretKeyToAuthTypeIsNoOpWhenEmptySecretKey() { | ||
| Handle handle = handleReturningRows(List.of(row(WEBHOOK_WITH_EMPTY_SECRET_KEY))); | ||
|
|
||
| assertDoesNotThrow(() -> MigrationUtil.migrateWebhookSecretKeyToAuthType(handle)); | ||
|
|
||
| verify(handle, never()).createUpdate(any()); | ||
| } | ||
|
|
||
| @Test | ||
| void migrateWebhookSecretKeyToAuthTypeIsNoOpWhenAlreadyMigrated() { | ||
| Handle handle = handleReturningRows(List.of(row(WEBHOOK_ALREADY_MIGRATED))); | ||
|
|
||
| assertDoesNotThrow(() -> MigrationUtil.migrateWebhookSecretKeyToAuthType(handle)); | ||
|
|
||
| verify(handle, never()).createUpdate(any()); | ||
| } | ||
|
|
||
| @Test | ||
| void migrateWebhookSecretKeyToAuthTypeIsNoOpForNonWebhookDestinations() { | ||
| Handle handle = handleReturningRows(List.of(row(NON_WEBHOOK_DESTINATION))); | ||
|
|
||
| assertDoesNotThrow(() -> MigrationUtil.migrateWebhookSecretKeyToAuthType(handle)); | ||
|
|
||
| verify(handle, never()).createUpdate(any()); | ||
| } | ||
|
|
||
| @Test | ||
| void migrateWebhookSecretKeyToAuthTypeMigratesMysql() throws Exception { | ||
| Update mockUpdate = mock(Update.class, RETURNS_DEEP_STUBS); | ||
| Handle handle = handleWithUpdateCapture(List.of(row(WEBHOOK_WITH_SECRET_KEY)), mockUpdate); | ||
|
|
||
| try (MockedStatic<DatasourceConfig> ds = mockStatic(DatasourceConfig.class)) { | ||
| DatasourceConfig mockConfig = mock(DatasourceConfig.class); | ||
| ds.when(DatasourceConfig::getInstance).thenReturn(mockConfig); | ||
| when(mockConfig.isMySQL()).thenReturn(true); | ||
|
|
||
| assertDoesNotThrow(() -> MigrationUtil.migrateWebhookSecretKeyToAuthType(handle)); | ||
|
|
||
| ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class); | ||
| verify(handle).createUpdate(sqlCaptor.capture()); | ||
| assertEquals( | ||
| "UPDATE event_subscription_entity SET json = :json WHERE id = :id", sqlCaptor.getValue()); | ||
|
|
||
| ArgumentCaptor<String> jsonCaptor = ArgumentCaptor.forClass(String.class); | ||
| verify(mockUpdate).bind(eq("json"), jsonCaptor.capture()); | ||
|
|
||
| JsonNode config = | ||
| MAPPER.readTree(jsonCaptor.getValue()).get("destinations").get(0).get("config"); | ||
| assertNull(config.get("secretKey")); | ||
| assertNotNull(config.get("authType")); | ||
| assertEquals("bearer", config.get("authType").get("type").asText()); | ||
| assertEquals("mysecret", config.get("authType").get("secretKey").asText()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void migrateWebhookSecretKeyToAuthTypeMigratesPostgres() throws Exception { | ||
| Update mockUpdate = mock(Update.class, RETURNS_DEEP_STUBS); | ||
| Handle handle = handleWithUpdateCapture(List.of(row(WEBHOOK_WITH_SECRET_KEY)), mockUpdate); | ||
|
|
||
| try (MockedStatic<DatasourceConfig> ds = mockStatic(DatasourceConfig.class)) { | ||
| DatasourceConfig mockConfig = mock(DatasourceConfig.class); | ||
| ds.when(DatasourceConfig::getInstance).thenReturn(mockConfig); | ||
| when(mockConfig.isMySQL()).thenReturn(false); | ||
|
|
||
| assertDoesNotThrow(() -> MigrationUtil.migrateWebhookSecretKeyToAuthType(handle)); | ||
|
|
||
| ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class); | ||
| verify(handle).createUpdate(sqlCaptor.capture()); | ||
| assertEquals( | ||
| "UPDATE event_subscription_entity SET json = :json::jsonb WHERE id = :id", | ||
| sqlCaptor.getValue()); | ||
|
|
||
| ArgumentCaptor<String> jsonCaptor = ArgumentCaptor.forClass(String.class); | ||
| verify(mockUpdate).bind(eq("json"), jsonCaptor.capture()); | ||
|
|
||
| JsonNode config = | ||
| MAPPER.readTree(jsonCaptor.getValue()).get("destinations").get(0).get("config"); | ||
| assertNull(config.get("secretKey")); | ||
| assertNotNull(config.get("authType")); | ||
| assertEquals("bearer", config.get("authType").get("type").asText()); | ||
| assertEquals("mysecret", config.get("authType").get("secretKey").asText()); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.