2024-04-09 17:21:21 +00:00
|
|
|
package action
|
2024-02-15 05:39:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/muhlemmer/gu"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"google.golang.org/protobuf/types/known/durationpb"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2024-04-09 17:21:21 +00:00
|
|
|
action "github.com/zitadel/zitadel/pkg/grpc/action/v3alpha"
|
2024-02-15 05:39:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_createTargetToCommand(t *testing.T) {
|
|
|
|
type args struct {
|
2024-04-09 17:21:21 +00:00
|
|
|
req *action.CreateTargetRequest
|
2024-02-15 05:39:10 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *command.AddTarget
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "nil",
|
|
|
|
args: args{nil},
|
|
|
|
want: &command.AddTarget{
|
|
|
|
Name: "",
|
2024-05-04 09:55:57 +00:00
|
|
|
Endpoint: "",
|
2024-02-15 05:39:10 +00:00
|
|
|
Timeout: 0,
|
|
|
|
InterruptOnError: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-05-04 09:55:57 +00:00
|
|
|
name: "all fields (webhook)",
|
2024-04-09 17:21:21 +00:00
|
|
|
args: args{&action.CreateTargetRequest{
|
2024-05-04 09:55:57 +00:00
|
|
|
Name: "target 1",
|
|
|
|
Endpoint: "https://example.com/hooks/1",
|
2024-04-09 17:21:21 +00:00
|
|
|
TargetType: &action.CreateTargetRequest_RestWebhook{
|
2024-05-04 09:55:57 +00:00
|
|
|
RestWebhook: &action.SetRESTWebhook{},
|
2024-02-15 05:39:10 +00:00
|
|
|
},
|
|
|
|
Timeout: durationpb.New(10 * time.Second),
|
|
|
|
}},
|
|
|
|
want: &command.AddTarget{
|
|
|
|
Name: "target 1",
|
|
|
|
TargetType: domain.TargetTypeWebhook,
|
2024-05-04 09:55:57 +00:00
|
|
|
Endpoint: "https://example.com/hooks/1",
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
InterruptOnError: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all fields (async)",
|
|
|
|
args: args{&action.CreateTargetRequest{
|
|
|
|
Name: "target 1",
|
|
|
|
Endpoint: "https://example.com/hooks/1",
|
|
|
|
TargetType: &action.CreateTargetRequest_RestAsync{
|
|
|
|
RestAsync: &action.SetRESTAsync{},
|
|
|
|
},
|
|
|
|
Timeout: durationpb.New(10 * time.Second),
|
|
|
|
}},
|
|
|
|
want: &command.AddTarget{
|
|
|
|
Name: "target 1",
|
|
|
|
TargetType: domain.TargetTypeAsync,
|
|
|
|
Endpoint: "https://example.com/hooks/1",
|
2024-02-15 05:39:10 +00:00
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
InterruptOnError: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all fields (interrupting response)",
|
2024-04-09 17:21:21 +00:00
|
|
|
args: args{&action.CreateTargetRequest{
|
2024-05-04 09:55:57 +00:00
|
|
|
Name: "target 1",
|
|
|
|
Endpoint: "https://example.com/hooks/1",
|
|
|
|
TargetType: &action.CreateTargetRequest_RestCall{
|
|
|
|
RestCall: &action.SetRESTCall{
|
|
|
|
InterruptOnError: true,
|
2024-02-15 05:39:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Timeout: durationpb.New(10 * time.Second),
|
|
|
|
}},
|
|
|
|
want: &command.AddTarget{
|
|
|
|
Name: "target 1",
|
2024-05-04 09:55:57 +00:00
|
|
|
TargetType: domain.TargetTypeCall,
|
|
|
|
Endpoint: "https://example.com/hooks/1",
|
2024-02-15 05:39:10 +00:00
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
InterruptOnError: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := createTargetToCommand(tt.args.req)
|
|
|
|
assert.Equal(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_updateTargetToCommand(t *testing.T) {
|
|
|
|
type args struct {
|
2024-04-09 17:21:21 +00:00
|
|
|
req *action.UpdateTargetRequest
|
2024-02-15 05:39:10 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *command.ChangeTarget
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "nil",
|
|
|
|
args: args{nil},
|
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all fields nil",
|
2024-04-09 17:21:21 +00:00
|
|
|
args: args{&action.UpdateTargetRequest{
|
2024-05-04 09:55:57 +00:00
|
|
|
Name: nil,
|
|
|
|
TargetType: nil,
|
|
|
|
Timeout: nil,
|
2024-02-15 05:39:10 +00:00
|
|
|
}},
|
|
|
|
want: &command.ChangeTarget{
|
|
|
|
Name: nil,
|
|
|
|
TargetType: nil,
|
2024-05-04 09:55:57 +00:00
|
|
|
Endpoint: nil,
|
2024-02-15 05:39:10 +00:00
|
|
|
Timeout: nil,
|
|
|
|
InterruptOnError: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all fields empty",
|
2024-04-09 17:21:21 +00:00
|
|
|
args: args{&action.UpdateTargetRequest{
|
2024-05-04 09:55:57 +00:00
|
|
|
Name: gu.Ptr(""),
|
|
|
|
TargetType: nil,
|
|
|
|
Timeout: durationpb.New(0),
|
2024-02-15 05:39:10 +00:00
|
|
|
}},
|
|
|
|
want: &command.ChangeTarget{
|
|
|
|
Name: gu.Ptr(""),
|
|
|
|
TargetType: nil,
|
2024-05-04 09:55:57 +00:00
|
|
|
Endpoint: nil,
|
2024-02-15 05:39:10 +00:00
|
|
|
Timeout: gu.Ptr(0 * time.Second),
|
|
|
|
InterruptOnError: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-05-04 09:55:57 +00:00
|
|
|
name: "all fields (webhook)",
|
2024-04-09 17:21:21 +00:00
|
|
|
args: args{&action.UpdateTargetRequest{
|
2024-05-04 09:55:57 +00:00
|
|
|
Name: gu.Ptr("target 1"),
|
|
|
|
Endpoint: gu.Ptr("https://example.com/hooks/1"),
|
2024-04-09 17:21:21 +00:00
|
|
|
TargetType: &action.UpdateTargetRequest_RestWebhook{
|
|
|
|
RestWebhook: &action.SetRESTWebhook{
|
2024-05-04 09:55:57 +00:00
|
|
|
InterruptOnError: false,
|
2024-02-15 05:39:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Timeout: durationpb.New(10 * time.Second),
|
2024-05-04 09:55:57 +00:00
|
|
|
}},
|
|
|
|
want: &command.ChangeTarget{
|
|
|
|
Name: gu.Ptr("target 1"),
|
|
|
|
TargetType: gu.Ptr(domain.TargetTypeWebhook),
|
|
|
|
Endpoint: gu.Ptr("https://example.com/hooks/1"),
|
|
|
|
Timeout: gu.Ptr(10 * time.Second),
|
|
|
|
InterruptOnError: gu.Ptr(false),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all fields (webhook interrupt)",
|
|
|
|
args: args{&action.UpdateTargetRequest{
|
|
|
|
Name: gu.Ptr("target 1"),
|
|
|
|
Endpoint: gu.Ptr("https://example.com/hooks/1"),
|
|
|
|
TargetType: &action.UpdateTargetRequest_RestWebhook{
|
|
|
|
RestWebhook: &action.SetRESTWebhook{
|
|
|
|
InterruptOnError: true,
|
|
|
|
},
|
2024-02-15 05:39:10 +00:00
|
|
|
},
|
2024-05-04 09:55:57 +00:00
|
|
|
Timeout: durationpb.New(10 * time.Second),
|
2024-02-15 05:39:10 +00:00
|
|
|
}},
|
|
|
|
want: &command.ChangeTarget{
|
|
|
|
Name: gu.Ptr("target 1"),
|
|
|
|
TargetType: gu.Ptr(domain.TargetTypeWebhook),
|
2024-05-04 09:55:57 +00:00
|
|
|
Endpoint: gu.Ptr("https://example.com/hooks/1"),
|
|
|
|
Timeout: gu.Ptr(10 * time.Second),
|
|
|
|
InterruptOnError: gu.Ptr(true),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all fields (async)",
|
|
|
|
args: args{&action.UpdateTargetRequest{
|
|
|
|
Name: gu.Ptr("target 1"),
|
|
|
|
Endpoint: gu.Ptr("https://example.com/hooks/1"),
|
|
|
|
TargetType: &action.UpdateTargetRequest_RestAsync{
|
|
|
|
RestAsync: &action.SetRESTAsync{},
|
|
|
|
},
|
|
|
|
Timeout: durationpb.New(10 * time.Second),
|
|
|
|
}},
|
|
|
|
want: &command.ChangeTarget{
|
|
|
|
Name: gu.Ptr("target 1"),
|
|
|
|
TargetType: gu.Ptr(domain.TargetTypeAsync),
|
|
|
|
Endpoint: gu.Ptr("https://example.com/hooks/1"),
|
2024-02-15 05:39:10 +00:00
|
|
|
Timeout: gu.Ptr(10 * time.Second),
|
|
|
|
InterruptOnError: gu.Ptr(false),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all fields (interrupting response)",
|
2024-04-09 17:21:21 +00:00
|
|
|
args: args{&action.UpdateTargetRequest{
|
2024-05-04 09:55:57 +00:00
|
|
|
Name: gu.Ptr("target 1"),
|
|
|
|
Endpoint: gu.Ptr("https://example.com/hooks/1"),
|
|
|
|
TargetType: &action.UpdateTargetRequest_RestCall{
|
|
|
|
RestCall: &action.SetRESTCall{
|
|
|
|
InterruptOnError: true,
|
2024-02-15 05:39:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Timeout: durationpb.New(10 * time.Second),
|
|
|
|
}},
|
|
|
|
want: &command.ChangeTarget{
|
|
|
|
Name: gu.Ptr("target 1"),
|
2024-05-04 09:55:57 +00:00
|
|
|
TargetType: gu.Ptr(domain.TargetTypeCall),
|
|
|
|
Endpoint: gu.Ptr("https://example.com/hooks/1"),
|
2024-02-15 05:39:10 +00:00
|
|
|
Timeout: gu.Ptr(10 * time.Second),
|
|
|
|
InterruptOnError: gu.Ptr(true),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := updateTargetToCommand(tt.args.req)
|
|
|
|
assert.Equal(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|