From 4b0f9873c30cfa4e00fb87a266e8e3e67dd4eedd Mon Sep 17 00:00:00 2001 From: Manish Dangi Date: Thu, 12 Sep 2024 09:16:54 +0530 Subject: [PATCH] fix: fix PostHookDBTransformConfigs for receiver configs validation (#84) --- plugins/receivers/lark/service.go | 19 +++++++++---------- plugins/receivers/larkchannel/service_test.go | 14 ++++++++------ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/plugins/receivers/lark/service.go b/plugins/receivers/lark/service.go index 8a7bce4d..8725e23b 100644 --- a/plugins/receivers/lark/service.go +++ b/plugins/receivers/lark/service.go @@ -77,29 +77,28 @@ func (s *PluginService) PreHookDBTransformConfigs(ctx context.Context, configura return receiverConfig.AsMap(), nil } -func (s *PluginService) PostHookDBTransformConfigs(ctx context.Context, notificationConfigMap map[string]any) (map[string]any, error) { - notificationConfig := &NotificationConfig{} - if err := mapstructure.Decode(notificationConfigMap, notificationConfig); err != nil { +func (s *PluginService) PostHookDBTransformConfigs(ctx context.Context, configurations map[string]any) (map[string]any, error) { + receiverConfig := &ReceiverConfig{} + if err := mapstructure.Decode(configurations, receiverConfig); err != nil { return nil, fmt.Errorf("failed to transform configurations to notification config: %w", err) } - - if err := notificationConfig.Validate(); err != nil { + if err := receiverConfig.Validate(); err != nil { return nil, err } - clientId, err := s.cryptoClient.Decrypt(notificationConfig.ClientID) + clientId, err := s.cryptoClient.Decrypt(receiverConfig.ClientID) if err != nil { return nil, fmt.Errorf("lark clientId decryption failed: %w", err) } - clientSecret, err := s.cryptoClient.Decrypt(notificationConfig.ClientSecret) + clientSecret, err := s.cryptoClient.Decrypt(receiverConfig.ClientSecret) if err != nil { return nil, fmt.Errorf("lark clientSecret decryption failed: %w", err) } - notificationConfig.ClientID = clientId - notificationConfig.ClientSecret = clientSecret + receiverConfig.ClientID = clientId + receiverConfig.ClientSecret = clientSecret - return notificationConfig.AsMap(), nil + return receiverConfig.AsMap(), nil } func (s *PluginService) PreHookQueueTransformConfigs(ctx context.Context, configurations map[string]any) (map[string]any, error) { diff --git a/plugins/receivers/larkchannel/service_test.go b/plugins/receivers/larkchannel/service_test.go index 549b3398..55fa88b8 100644 --- a/plugins/receivers/larkchannel/service_test.go +++ b/plugins/receivers/larkchannel/service_test.go @@ -97,9 +97,11 @@ func TestService_PostHookDBTransformConfigs(t *testing.T) { wantErr bool }{ { - name: "should return error if failed to parse configmap to notification config", - notificationConfigMap: nil, - wantErr: true, + name: "should return error if failed to parse configmap to notification config", + notificationConfigMap: map[string]any{ + "client_id": 123, + }, + wantErr: true, }, { name: "should return error if validate notification config failed", @@ -111,7 +113,7 @@ func TestService_PostHookDBTransformConfigs(t *testing.T) { { name: "should return error if lark token decryption failed", notificationConfigMap: map[string]any{ - "client_id": secret.MaskableString("a token"), + "client_secret": 123, }, setup: func(e *mocks.Encryptor) { e.EXPECT().Decrypt(mock.AnythingOfType("secret.MaskableString")).Return("", errors.New("some error")) @@ -151,11 +153,11 @@ func TestService_PostHookDBTransformConfigs(t *testing.T) { s := larkchannel.NewPluginService(lark.AppConfig{}, mockEncryptor) got, err := s.PostHookDBTransformConfigs(context.TODO(), tt.notificationConfigMap) if (err != nil) != tt.wantErr { - t.Errorf("Service.PostHookQueueTransformConfigs() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("Service.PostHookDBTransformConfigs() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Service.PostHookQueueTransformConfigs() = %v, want %v", got, tt.want) + t.Errorf("Service.PostHookDBTransformConfigs() got = %v, want %v", got, tt.want) } }) }