github.com/argoproj/argo-cd/v3@v3.2.1/util/notification/settings/settings_test.go (about) 1 package settings 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/argoproj/notifications-engine/pkg/api" 8 "github.com/argoproj/notifications-engine/pkg/services" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 corev1 "k8s.io/api/core/v1" 12 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 13 "k8s.io/client-go/kubernetes/fake" 14 15 "github.com/argoproj/argo-cd/v3/reposerver/apiclient/mocks" 16 service "github.com/argoproj/argo-cd/v3/util/notification/argocd" 17 ) 18 19 const ( 20 testNamespace = "default" 21 testContextKey = "test-context-key" 22 testContextKeyValue = "test-context-key-value" 23 ) 24 25 func TestInitGetVars(t *testing.T) { 26 notificationsCm := corev1.ConfigMap{ 27 ObjectMeta: metav1.ObjectMeta{ 28 Namespace: testNamespace, 29 Name: "argocd-notifications-cm", 30 }, 31 Data: map[string]string{ 32 "context": fmt.Sprintf("%s: %s", testContextKey, testContextKeyValue), 33 "service.webhook.test": "url: https://test.example.com", 34 "template.app-created": "email:\n subject: Application {{.app.metadata.name}} has been created.\nmessage: Application {{.app.metadata.name}} has been created.\nteams:\n title: Application {{.app.metadata.name}} has been created.\n", 35 "trigger.on-created": "- description: Application is created.\n oncePer: app.metadata.name\n send:\n - app-created\n when: \"true\"\n", 36 }, 37 } 38 notificationsSecret := corev1.Secret{ 39 ObjectMeta: metav1.ObjectMeta{ 40 Name: "argocd-notifications-secret", 41 Namespace: testNamespace, 42 }, 43 Data: map[string][]byte{ 44 "notification-secret": []byte("secret-value"), 45 }, 46 } 47 kubeclientset := fake.NewClientset(&corev1.ConfigMap{ 48 ObjectMeta: metav1.ObjectMeta{ 49 Namespace: testNamespace, 50 Name: "argocd-notifications-cm", 51 }, 52 Data: notificationsCm.Data, 53 }, 54 &corev1.Secret{ 55 ObjectMeta: metav1.ObjectMeta{ 56 Name: "argocd-notifications-secret", 57 Namespace: testNamespace, 58 }, 59 Data: notificationsSecret.Data, 60 }) 61 mockRepoClient := &mocks.Clientset{RepoServerServiceClient: &mocks.RepoServerServiceClient{}} 62 argocdService, err := service.NewArgoCDService(kubeclientset, testNamespace, mockRepoClient) 63 require.NoError(t, err) 64 defer argocdService.Close() 65 config := api.Config{} 66 testDestination := services.Destination{ 67 Service: "webhook", 68 } 69 emptyAppData := map[string]any{} 70 71 varsProvider, _ := initGetVars(argocdService, &config, ¬ificationsCm, ¬ificationsSecret) 72 73 t.Run("Vars provider serves Application data on app key", func(t *testing.T) { 74 appData := map[string]any{ 75 "name": "app-name", 76 } 77 result := varsProvider(appData, testDestination) 78 assert.NotNil(t, t, result["app"]) 79 assert.Equal(t, result["app"], appData) 80 }) 81 t.Run("Vars provider serves notification context data on context key", func(t *testing.T) { 82 expectedContext := map[string]string{ 83 testContextKey: testContextKeyValue, 84 "notificationType": testDestination.Service, 85 } 86 result := varsProvider(emptyAppData, testDestination) 87 assert.NotNil(t, result["context"]) 88 assert.Equal(t, expectedContext, result["context"]) 89 }) 90 t.Run("Vars provider serves notification secrets on secrets key", func(t *testing.T) { 91 result := varsProvider(emptyAppData, testDestination) 92 assert.NotNil(t, result["secrets"]) 93 assert.Equal(t, result["secrets"], notificationsSecret.Data) 94 }) 95 }