github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/model/webhook_test.go (about) 1 package model_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/kyma-incubator/compass/components/director/internal/model" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestWebhookInput_ToApplicationWebhook(t *testing.T) { 12 // GIVEN 13 applicationID := "foo" 14 id := "bar" 15 template := `{}` 16 webhookMode := model.WebhookModeSync 17 webhookURL := "foourl" 18 testCases := []struct { 19 Name string 20 Input *model.WebhookInput 21 Expected *model.Webhook 22 }{ 23 { 24 Name: "All properties given", 25 Input: &model.WebhookInput{ 26 Type: model.WebhookTypeConfigurationChanged, 27 URL: &webhookURL, 28 Auth: &model.AuthInput{ 29 AdditionalHeaders: map[string][]string{ 30 "foo": {"foo", "bar"}, 31 "bar": {"bar", "foo"}, 32 }, 33 }, 34 Mode: &webhookMode, 35 URLTemplate: &template, 36 InputTemplate: &template, 37 HeaderTemplate: &template, 38 OutputTemplate: &template, 39 }, 40 Expected: &model.Webhook{ 41 ObjectID: applicationID, 42 ObjectType: model.ApplicationWebhookReference, 43 ID: id, 44 Type: model.WebhookTypeConfigurationChanged, 45 URL: &webhookURL, 46 Auth: &model.Auth{ 47 AdditionalHeaders: map[string][]string{ 48 "foo": {"foo", "bar"}, 49 "bar": {"bar", "foo"}, 50 }, 51 }, 52 Mode: &webhookMode, 53 URLTemplate: &template, 54 InputTemplate: &template, 55 HeaderTemplate: &template, 56 OutputTemplate: &template, 57 }, 58 }, 59 { 60 Name: "Empty", 61 Input: &model.WebhookInput{}, 62 Expected: &model.Webhook{ 63 ObjectID: applicationID, 64 ObjectType: model.ApplicationWebhookReference, 65 ID: id, 66 }, 67 }, 68 } 69 70 for i, testCase := range testCases { 71 t.Run(fmt.Sprintf("%d: %s", i, testCase.Name), func(t *testing.T) { 72 // WHEN 73 result := testCase.Input.ToWebhook(id, applicationID, model.ApplicationWebhookReference) 74 75 // THEN 76 assert.Equal(t, testCase.Expected, result) 77 }) 78 } 79 } 80 81 func TestWebhookInput_ToApplicationTemplateWebhook(t *testing.T) { 82 // GIVEN 83 applicationTemplateID := "foo" 84 id := "bar" 85 template := `{}` 86 webhookMode := model.WebhookModeSync 87 webhookURL := "foourl" 88 testCases := []struct { 89 Name string 90 Input *model.WebhookInput 91 Expected *model.Webhook 92 }{ 93 { 94 Name: "All properties given", 95 Input: &model.WebhookInput{ 96 Type: model.WebhookTypeConfigurationChanged, 97 URL: &webhookURL, 98 Auth: &model.AuthInput{ 99 AdditionalHeaders: map[string][]string{ 100 "foo": {"foo", "bar"}, 101 "bar": {"bar", "foo"}, 102 }, 103 }, 104 Mode: &webhookMode, 105 URLTemplate: &template, 106 InputTemplate: &template, 107 HeaderTemplate: &template, 108 OutputTemplate: &template, 109 }, 110 Expected: &model.Webhook{ 111 ObjectID: applicationTemplateID, 112 ObjectType: model.ApplicationTemplateWebhookReference, 113 ID: id, 114 Type: model.WebhookTypeConfigurationChanged, 115 URL: &webhookURL, 116 Auth: &model.Auth{ 117 AdditionalHeaders: map[string][]string{ 118 "foo": {"foo", "bar"}, 119 "bar": {"bar", "foo"}, 120 }, 121 }, 122 Mode: &webhookMode, 123 URLTemplate: &template, 124 InputTemplate: &template, 125 HeaderTemplate: &template, 126 OutputTemplate: &template, 127 }, 128 }, 129 { 130 Name: "Empty", 131 Input: &model.WebhookInput{}, 132 Expected: &model.Webhook{ 133 ObjectID: applicationTemplateID, 134 ObjectType: model.ApplicationTemplateWebhookReference, 135 ID: id, 136 }, 137 }, 138 } 139 140 for i, testCase := range testCases { 141 t.Run(fmt.Sprintf("%d: %s", i, testCase.Name), func(t *testing.T) { 142 // WHEN 143 result := testCase.Input.ToWebhook(id, applicationTemplateID, model.ApplicationTemplateWebhookReference) 144 145 // THEN 146 assert.Equal(t, testCase.Expected, result) 147 }) 148 } 149 }