Skip to content

Commit

Permalink
fix: fix PostHookDBTransformConfigs for receiver configs validation (#84
Browse files Browse the repository at this point in the history
)
  • Loading branch information
manishdangi98 authored Sep 12, 2024
1 parent a05a252 commit 4b0f987
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
19 changes: 9 additions & 10 deletions plugins/receivers/lark/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 8 additions & 6 deletions plugins/receivers/larkchannel/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"))
Expand Down Expand Up @@ -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)
}
})
}
Expand Down

0 comments on commit 4b0f987

Please sign in to comment.