github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/webhook/fixtures_test.go (about) 1 package webhook_test 2 3 import ( 4 "database/sql" 5 "encoding/json" 6 "errors" 7 "testing" 8 "time" 9 10 "github.com/kyma-incubator/compass/components/director/pkg/str" 11 12 "github.com/kyma-incubator/compass/components/director/pkg/tenant" 13 14 "github.com/kyma-incubator/compass/components/director/internal/domain/webhook" 15 "github.com/kyma-incubator/compass/components/director/internal/repo" 16 "github.com/stretchr/testify/require" 17 18 "github.com/kyma-incubator/compass/components/director/internal/model" 19 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 20 ) 21 22 var fixColumns = []string{"id", "app_id", "app_template_id", "type", "url", "auth", "runtime_id", "integration_system_id", "mode", "correlation_id_key", "retry_interval", "timeout", "url_template", "input_template", "header_template", "output_template", "status_template", "created_at", "formation_template_id"} 23 24 var ( 25 emptyTemplate = `{}` 26 testURL = "testURL" 27 ) 28 29 func stringPtr(s string) *string { 30 return &s 31 } 32 33 func fixApplicationModelWebhook(id, appID, tenant, url string, createdAt time.Time) *model.Webhook { 34 appWebhook := fixGenericModelWebhook(id, appID, url) 35 appWebhook.ObjectType = model.ApplicationWebhookReference 36 appWebhook.CreatedAt = &createdAt 37 return appWebhook 38 } 39 40 func fixRuntimeModelWebhook(id, runtimeID, url string) *model.Webhook { 41 runtimeWebhook := fixGenericModelWebhook(id, runtimeID, url) 42 runtimeWebhook.ObjectType = model.RuntimeWebhookReference 43 return runtimeWebhook 44 } 45 46 func fixFormationTemplateModelWebhook(id, formationTemplateID, url string) *model.Webhook { 47 formationTmplWebhook := fixGenericModelWebhook(id, formationTemplateID, url) 48 formationTmplWebhook.ObjectType = model.FormationTemplateWebhookReference 49 formationTmplWebhook.Type = model.WebhookTypeFormationLifecycle 50 return formationTmplWebhook 51 } 52 53 func fixApplicationTemplateModelWebhook(id, appTemplateID, url string) *model.Webhook { 54 appTmplWebhook := fixGenericModelWebhook(id, appTemplateID, url) 55 appTmplWebhook.ObjectType = model.ApplicationTemplateWebhookReference 56 return appTmplWebhook 57 } 58 59 func fixIntegrationSystemModelWebhook(id, intSysID, url string) *model.Webhook { 60 intSysWebhook := fixGenericModelWebhook(id, intSysID, url) 61 intSysWebhook.ObjectType = model.IntegrationSystemWebhookReference 62 intSysWebhook.Type = "" 63 return intSysWebhook 64 } 65 66 func fixGenericModelWebhook(id, objectID, url string) *model.Webhook { 67 return &model.Webhook{ 68 ID: id, 69 ObjectID: objectID, 70 ObjectType: model.UnknownWebhookReference, 71 Type: model.WebhookTypeConfigurationChanged, 72 URL: &url, 73 Auth: fixBasicAuth(), 74 Mode: &modelWebhookMode, 75 URLTemplate: &emptyTemplate, 76 InputTemplate: &emptyTemplate, 77 HeaderTemplate: &emptyTemplate, 78 OutputTemplate: &emptyTemplate, 79 } 80 } 81 82 func fixApplicationGQLWebhook(id, appID, url string) *graphql.Webhook { 83 appWebhook := fixGenericGQLWebhook(id, url) 84 appWebhook.ApplicationID = &appID 85 appWebhook.CreatedAt = &graphql.Timestamp{} 86 return appWebhook 87 } 88 89 func fixRuntimeGQLWebhook(id, rtmID, url string) *graphql.Webhook { 90 rtmWebhook := fixGenericGQLWebhook(id, url) 91 rtmWebhook.RuntimeID = &rtmID 92 return rtmWebhook 93 } 94 95 func fixApplicationTemplateGQLWebhook(id, appTmplID, url string) *graphql.Webhook { 96 appTmplWebhook := fixGenericGQLWebhook(id, url) 97 appTmplWebhook.ApplicationTemplateID = &appTmplID 98 return appTmplWebhook 99 } 100 101 func fixFormationTemplateGQLWebhook(id, formationTmplID, url string) *graphql.Webhook { 102 formationTmplWebhook := fixGenericGQLWebhook(id, url) 103 formationTmplWebhook.FormationTemplateID = &formationTmplID 104 formationTmplWebhook.Type = graphql.WebhookTypeFormationLifecycle 105 return formationTmplWebhook 106 } 107 108 func fixIntegrationSystemGQLWebhook(id, intSysID, url string) *graphql.Webhook { 109 intSysWebhook := fixGenericGQLWebhook(id, url) 110 intSysWebhook.IntegrationSystemID = &intSysID 111 intSysWebhook.Type = "" 112 return intSysWebhook 113 } 114 115 func fixGenericGQLWebhook(id, url string) *graphql.Webhook { 116 return &graphql.Webhook{ 117 ID: id, 118 Type: graphql.WebhookTypeConfigurationChanged, 119 URL: &url, 120 Auth: &graphql.Auth{}, 121 Mode: &graphqlWebhookMode, 122 URLTemplate: &emptyTemplate, 123 InputTemplate: &emptyTemplate, 124 HeaderTemplate: &emptyTemplate, 125 OutputTemplate: &emptyTemplate, 126 } 127 } 128 129 func fixModelWebhookInput(url string) *model.WebhookInput { 130 return &model.WebhookInput{ 131 Type: model.WebhookTypeConfigurationChanged, 132 URL: &url, 133 Auth: &model.AuthInput{}, 134 Mode: &modelWebhookMode, 135 URLTemplate: &emptyTemplate, 136 InputTemplate: &emptyTemplate, 137 HeaderTemplate: &emptyTemplate, 138 OutputTemplate: &emptyTemplate, 139 } 140 } 141 142 func fixGQLWebhookInput(url string) *graphql.WebhookInput { 143 return &graphql.WebhookInput{ 144 Type: graphql.WebhookTypeConfigurationChanged, 145 URL: &url, 146 Auth: &graphql.AuthInput{}, 147 Mode: &graphqlWebhookMode, 148 URLTemplate: &emptyTemplate, 149 InputTemplate: &emptyTemplate, 150 HeaderTemplate: &emptyTemplate, 151 OutputTemplate: &emptyTemplate, 152 } 153 } 154 155 func fixApplicationModelWebhookWithType(id, appID, tenant, url string, webhookType model.WebhookType, createdAt time.Time) (w *model.Webhook) { 156 w = fixApplicationModelWebhook(id, appID, tenant, url, createdAt) 157 w.Type = webhookType 158 return 159 } 160 161 func fixApplicationTemplateModelWebhookWithType(id, appTemplateID, url string, webhookType model.WebhookType) (w *model.Webhook) { 162 w = fixApplicationTemplateModelWebhook(id, appTemplateID, url) 163 w.Type = webhookType 164 return 165 } 166 167 func fixApplicationTemplateModelWebhookWithTypeAndTimestamp(id, appTemplateID, url string, webhookType model.WebhookType, createdAt time.Time) (w *model.Webhook) { 168 w = fixApplicationTemplateModelWebhookWithType(id, appTemplateID, url, webhookType) 169 w.CreatedAt = &createdAt 170 return 171 } 172 173 func fixBasicAuth() *model.Auth { 174 return &model.Auth{ 175 Credential: model.CredentialData{ 176 Basic: &model.BasicCredentialData{ 177 Username: "aaa", 178 Password: "bbb", 179 }, 180 }, 181 } 182 } 183 184 func fixAuthAsAString(t *testing.T) string { 185 b, err := json.Marshal(fixBasicAuth()) 186 require.NoError(t, err) 187 return string(b) 188 } 189 190 func fixApplicationWebhookEntity(t *testing.T, createdAt time.Time) *webhook.Entity { 191 return fixApplicationWebhookEntityWithID(t, givenID(), createdAt) 192 } 193 194 func fixApplicationWebhookEntityWithID(t *testing.T, id string, createdAt time.Time) *webhook.Entity { 195 return fixApplicationWebhookEntityWithIDAndWebhookType(t, id, model.WebhookTypeConfigurationChanged, createdAt) 196 } 197 198 func fixApplicationWebhookEntityWithIDAndWebhookType(t *testing.T, id string, whType model.WebhookType, createdAt time.Time) *webhook.Entity { 199 return &webhook.Entity{ 200 ID: id, 201 ApplicationID: repo.NewValidNullableString(givenApplicationID()), 202 Type: string(whType), 203 URL: repo.NewValidNullableString("http://kyma.io"), 204 Mode: repo.NewValidNullableString(string(model.WebhookModeSync)), 205 Auth: sql.NullString{Valid: true, String: fixAuthAsAString(t)}, 206 URLTemplate: repo.NewValidNullableString(emptyTemplate), 207 InputTemplate: repo.NewValidNullableString(emptyTemplate), 208 HeaderTemplate: repo.NewValidNullableString(emptyTemplate), 209 OutputTemplate: repo.NewValidNullableString(emptyTemplate), 210 CreatedAt: &createdAt, 211 } 212 } 213 214 func fixRuntimeWebhookEntityWithID(t *testing.T, id string) *webhook.Entity { 215 return &webhook.Entity{ 216 ID: id, 217 RuntimeID: repo.NewValidNullableString(givenRuntimeID()), 218 Type: string(model.WebhookTypeConfigurationChanged), 219 URL: repo.NewValidNullableString("http://kyma.io"), 220 Mode: repo.NewValidNullableString(string(model.WebhookModeSync)), 221 Auth: sql.NullString{Valid: true, String: fixAuthAsAString(t)}, 222 URLTemplate: repo.NewValidNullableString(emptyTemplate), 223 InputTemplate: repo.NewValidNullableString(emptyTemplate), 224 HeaderTemplate: repo.NewValidNullableString(emptyTemplate), 225 OutputTemplate: repo.NewValidNullableString(emptyTemplate), 226 } 227 } 228 229 func fixFormationTemplateWebhookEntityWithID(t *testing.T, id string) *webhook.Entity { 230 return &webhook.Entity{ 231 ID: id, 232 FormationTemplateID: repo.NewValidNullableString(givenFormationTemplateID()), 233 Type: string(model.WebhookTypeFormationLifecycle), 234 URL: repo.NewValidNullableString("http://kyma.io"), 235 Mode: repo.NewValidNullableString(string(model.WebhookModeSync)), 236 Auth: sql.NullString{Valid: true, String: fixAuthAsAString(t)}, 237 URLTemplate: repo.NewValidNullableString(emptyTemplate), 238 InputTemplate: repo.NewValidNullableString(emptyTemplate), 239 HeaderTemplate: repo.NewValidNullableString(emptyTemplate), 240 OutputTemplate: repo.NewValidNullableString(emptyTemplate), 241 } 242 } 243 244 func fixApplicationTemplateWebhookEntity(t *testing.T) *webhook.Entity { 245 return &webhook.Entity{ 246 ID: givenID(), 247 ApplicationTemplateID: repo.NewValidNullableString(givenApplicationTemplateID()), 248 Type: string(model.WebhookTypeConfigurationChanged), 249 URL: repo.NewValidNullableString("http://kyma.io"), 250 Mode: repo.NewValidNullableString(string(model.WebhookModeSync)), 251 Auth: sql.NullString{Valid: true, String: fixAuthAsAString(t)}, 252 URLTemplate: repo.NewValidNullableString(emptyTemplate), 253 InputTemplate: repo.NewValidNullableString(emptyTemplate), 254 HeaderTemplate: repo.NewValidNullableString(emptyTemplate), 255 OutputTemplate: repo.NewValidNullableString(emptyTemplate), 256 CreatedAt: nil, 257 } 258 } 259 260 func fixApplicationTemplateWebhookEntityWithTimestamp(t *testing.T, createdAt time.Time) *webhook.Entity { 261 w := fixApplicationTemplateWebhookEntity(t) 262 w.CreatedAt = &createdAt 263 return w 264 } 265 266 func newModelBusinessTenantMappingWithType(tenantType tenant.Type) *model.BusinessTenantMapping { 267 return &model.BusinessTenantMapping{ 268 ID: givenTenant(), 269 Name: "name", 270 ExternalTenant: "external", 271 Parent: givenParentTenant(), 272 Type: tenantType, 273 Provider: "test-provider", 274 Status: tenant.Active, 275 } 276 } 277 278 func givenID() string { 279 return "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" 280 } 281 282 func anotherID() string { 283 return "dddddddd-dddd-dddd-dddd-dddddddddddd" 284 } 285 286 func givenTenant() string { 287 return "b91b59f7-2563-40b2-aba9-fef726037aa3" 288 } 289 290 func givenParentTenant() string { 291 return "b92b59f7-2563-40b2-aba9-fef726037aa3" 292 } 293 294 func givenExternalTenant() string { 295 return "eeeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee" 296 } 297 298 func givenApplicationID() string { 299 return "cccccccc-cccc-cccc-cccc-cccccccccccc" 300 } 301 302 func givenRuntimeID() string { 303 return "rrrrrrrr-rrrr-rrrr-rrrr-rrrrrrrrrrrr" 304 } 305 306 func givenFormationTemplateID() string { 307 return "rrrrrrrr-rrrr-rrrr-rrrr-rrrrrrrrrrrr" 308 } 309 310 func givenApplicationTemplateID() string { 311 return "ffffffff-ffff-ffff-ffff-ffffffffffff" 312 } 313 314 func givenError() error { 315 return errors.New("some error") 316 } 317 318 func fixEmptyTenantMappingConfig() map[string]interface{} { 319 tenantMappingJSON := "{}" 320 return GetTenantMappingConfig(tenantMappingJSON) 321 } 322 323 func fixTenantMappingConfig() map[string]interface{} { 324 tenantMappingJSON := "{\"SYNC\": {\"v1.0\": [{ \"type\": \"CONFIGURATION_CHANGED\",\"urlTemplate\": \"%s\",\"inputTemplate\": \"input template\",\"headerTemplate\": \"header template\",\"outputTemplate\": \"output template\"}]}}" 325 return GetTenantMappingConfig(tenantMappingJSON) 326 } 327 328 func fixTenantMappingConfigForAsyncCallback() map[string]interface{} { 329 tenantMappingJSON := "{\"ASYNC_CALLBACK\": {\"v1.0\": [{ \"type\": \"CONFIGURATION_CHANGED\",\"urlTemplate\": \"%s\",\"inputTemplate\": \"input template\",\"headerTemplate\": \"%s\",\"outputTemplate\": \"output template\"}]}}" 330 return GetTenantMappingConfig(tenantMappingJSON) 331 } 332 333 func fixInvalidTenantMappingConfig() map[string]interface{} { 334 tenantMappingJSON := "{\"SYNC\": []}" 335 return GetTenantMappingConfig(tenantMappingJSON) 336 } 337 338 func fixTenantMappedWebhooks() []*graphql.WebhookInput { 339 syncMode := graphql.WebhookModeSync 340 341 return []*graphql.WebhookInput{ 342 { 343 Type: graphql.WebhookTypeConfigurationChanged, 344 Auth: nil, 345 Mode: &syncMode, 346 URL: &testURL, 347 Version: str.Ptr("v1.0"), 348 }, 349 } 350 } 351 352 func fixTenantMappedWebhooksForAsyncCallbackMode() []*graphql.WebhookInput { 353 asyncMode := graphql.WebhookModeAsyncCallback 354 355 return []*graphql.WebhookInput{ 356 { 357 Type: graphql.WebhookTypeConfigurationChanged, 358 Auth: nil, 359 Mode: &asyncMode, 360 URL: &testURL, 361 Version: str.Ptr("v1.0"), 362 }, 363 } 364 } 365 366 func fixTenantMappedWebhooksWithInvalidVersion() []*graphql.WebhookInput { 367 syncMode := graphql.WebhookModeSync 368 369 return []*graphql.WebhookInput{ 370 { 371 Type: graphql.WebhookTypeConfigurationChanged, 372 Auth: nil, 373 Mode: &syncMode, 374 URL: &testURL, 375 Version: str.Ptr("notfound"), 376 }, 377 } 378 } 379 380 func fixEnrichedTenantMappedWebhooks() []*graphql.WebhookInput { 381 syncMode := graphql.WebhookModeSync 382 383 return []*graphql.WebhookInput{ 384 { 385 Type: graphql.WebhookTypeConfigurationChanged, 386 Auth: nil, 387 Mode: &syncMode, 388 URLTemplate: &testURL, 389 InputTemplate: str.Ptr("input template"), 390 HeaderTemplate: str.Ptr("header template"), 391 OutputTemplate: str.Ptr("output template"), 392 }, 393 } 394 } 395 396 func fixEnrichedTenantMappedWebhooksForAsyncCallbackMode(callbackURL string) []*graphql.WebhookInput { 397 asyncMode := graphql.WebhookModeAsyncCallback 398 399 return []*graphql.WebhookInput{ 400 { 401 Type: graphql.WebhookTypeConfigurationChanged, 402 Auth: nil, 403 Mode: &asyncMode, 404 URLTemplate: &testURL, 405 InputTemplate: str.Ptr("input template"), 406 HeaderTemplate: str.Ptr(callbackURL), 407 OutputTemplate: str.Ptr("output template"), 408 }, 409 } 410 }