Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into shield_provider_changes
Browse files Browse the repository at this point in the history
  • Loading branch information
utsav14nov committed Oct 10, 2024
2 parents d7e9e71 + 8ac4398 commit 42c9441
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 10 deletions.
21 changes: 11 additions & 10 deletions cli/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,19 @@ func runJobCmd() *cobra.Command {
logger := log.NewCtxLogger(config.LogLevel, []string{config.AuditLogTraceIDHeaderKey})
crypto := crypto.NewAES(config.EncryptionSecretKeyKey)
validator := validator.New()
var notifierMap map[string]interface{}
errr := json.Unmarshal([]byte(config.Notifiers), &notifierMap)
if errr != nil {
return fmt.Errorf("failed to parse notifier config: %w", errr)
}
var notifierConfigMap map[string]notifiers.Config
err = mapstructure.Decode(notifierMap, &notifierConfigMap)
if err != nil {
return fmt.Errorf("failed to parse notifier config: %w", err)
}

notifierConfig := []notifiers.Config{}
if config.Notifiers != "" {
var notifierMap map[string]interface{}
errr := json.Unmarshal([]byte(config.Notifiers), &notifierMap)
if errr != nil {
return fmt.Errorf("failed to parse notifier config: %w", errr)
}
var notifierConfigMap map[string]notifiers.Config
err = mapstructure.Decode(notifierMap, &notifierConfigMap)
if err != nil {
return fmt.Errorf("failed to parse notifier config: %w", err)
}
for _, val := range notifierConfigMap {
notifierConfig = append(notifierConfig, val)
}
Expand Down
3 changes: 3 additions & 0 deletions plugins/notifiers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type NotifyManager struct {

func (m *NotifyManager) Notify(ctx context.Context, notification []domain.Notification) []error {
var errs []error
if len(notification) == 0 {
return errs
}
for i, client := range m.clients {
// evaluate criteria
config := m.configs[i]
Expand Down
104 changes: 104 additions & 0 deletions plugins/notifiers/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package notifiers

import (
"context"
"fmt"
"reflect"
"testing"

Expand Down Expand Up @@ -215,3 +217,105 @@ func TestNewSlackLarkConfig(t *testing.T) {
})
}
}
func TestNotify(t *testing.T) {
var errs []error
type fields struct {
clients []Client
configs []Config
}
type args struct {
ctx context.Context
notification []domain.Notification
}
tests := []struct {
name string
fields fields
args args
want []error
}{
{
name: "should return no errors when notifications are empty",
fields: fields{
clients: []Client{},
configs: []Config{},
},
args: args{
ctx: context.Background(),
notification: []domain.Notification{},
},
want: errs,
},
{
name: "should return error when criteria does not evaluate to boolean",
fields: fields{
clients: []Client{&mockClient{}},
configs: []Config{
{
Criteria: "1 + 1",
},
},
},
args: args{
ctx: context.Background(),
notification: []domain.Notification{
{User: "[email protected]"},
},
},
want: []error{fmt.Errorf("notifier expression did not evaluate to a boolean: 1 + 1")},
},
{
name: "should notify client when criteria evaluates to true",
fields: fields{
clients: []Client{&mockClient{}},
configs: []Config{
{
Criteria: "1 == 1",
},
},
},
args: args{
ctx: context.Background(),
notification: []domain.Notification{
{User: "[email protected]"},
},
},
want: errs,
},
{
name: "should not notify client when criteria evaluates to false",
fields: fields{
clients: []Client{&mockClient{}},
configs: []Config{
{
Criteria: "1 == 2",
},
},
},
args: args{
ctx: context.Background(),
notification: []domain.Notification{
{User: "[email protected]"},
},
},
want: errs,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &NotifyManager{
clients: tt.fields.clients,
configs: tt.fields.configs,
}
fmt.Println()
if got := m.Notify(tt.args.ctx, tt.args.notification); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Notify() = %v, want %v", got, tt.want)
}
})
}
}

type mockClient struct{}

func (m *mockClient) Notify(ctx context.Context, notifications []domain.Notification) []error {
return nil
}

0 comments on commit 42c9441

Please sign in to comment.