2020-07-08 11:56:37 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2023-11-13 10:41:29 +00:00
|
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
2020-07-08 11:56:37 +00:00
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-07-08 11:56:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidator_Healthz(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
validations map[string]ValidationFunction
|
|
|
|
}
|
|
|
|
type res struct {
|
2023-11-13 10:41:29 +00:00
|
|
|
want *emptypb.Empty
|
2020-07-08 11:56:37 +00:00
|
|
|
hasErr bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"ok",
|
|
|
|
fields{},
|
|
|
|
res{
|
2023-11-13 10:41:29 +00:00
|
|
|
&emptypb.Empty{},
|
2020-07-08 11:56:37 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
v := &Validator{
|
|
|
|
validations: tt.fields.validations,
|
|
|
|
}
|
2023-11-13 10:41:29 +00:00
|
|
|
got, err := v.Healthz(context.Background(), &emptypb.Empty{})
|
2020-07-08 11:56:37 +00:00
|
|
|
if (err != nil) != tt.res.hasErr {
|
|
|
|
t.Errorf("Healthz() error = %v, wantErr %v", err, tt.res.hasErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.res.want) {
|
|
|
|
t.Errorf("Healthz() got = %v, want %v", got, tt.res.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidator_Ready(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
validations map[string]ValidationFunction
|
|
|
|
}
|
|
|
|
type res struct {
|
2023-11-13 10:41:29 +00:00
|
|
|
want *emptypb.Empty
|
2020-07-08 11:56:37 +00:00
|
|
|
hasErr bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"unready error",
|
|
|
|
fields{validations: map[string]ValidationFunction{
|
|
|
|
"error": func(_ context.Context) error {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowInternal(nil, "id", "message")
|
2020-07-08 11:56:37 +00:00
|
|
|
},
|
|
|
|
}},
|
|
|
|
res{
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ready ok",
|
|
|
|
fields{validations: map[string]ValidationFunction{
|
|
|
|
"ok": func(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
res{
|
2023-11-13 10:41:29 +00:00
|
|
|
&emptypb.Empty{},
|
2020-07-08 11:56:37 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
v := &Validator{
|
|
|
|
validations: tt.fields.validations,
|
|
|
|
}
|
2023-11-13 10:41:29 +00:00
|
|
|
got, err := v.Ready(context.Background(), &emptypb.Empty{})
|
2020-07-08 11:56:37 +00:00
|
|
|
if (err != nil) != tt.res.hasErr {
|
|
|
|
t.Errorf("Ready() error = %v, wantErr %v", err, tt.res.hasErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.res.want) {
|
|
|
|
t.Errorf("Ready() got = %v, want %v", got, tt.res.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_validate(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
validations map[string]ValidationFunction
|
|
|
|
}
|
|
|
|
type res struct {
|
2023-11-13 10:41:29 +00:00
|
|
|
want map[string]any
|
2020-07-08 11:56:37 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"no error empty",
|
|
|
|
args{
|
|
|
|
validations: map[string]ValidationFunction{
|
|
|
|
"ok": func(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res{
|
2023-11-13 10:41:29 +00:00
|
|
|
map[string]any{},
|
2020-07-08 11:56:37 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"error in list",
|
|
|
|
args{
|
|
|
|
validations: map[string]ValidationFunction{
|
|
|
|
"ok": func(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"error": func(_ context.Context) error {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowInternal(nil, "id", "message")
|
2020-07-08 11:56:37 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res{
|
2023-11-13 10:41:29 +00:00
|
|
|
map[string]any{
|
2023-12-08 14:30:55 +00:00
|
|
|
"error": zerrors.ThrowInternal(nil, "id", "message"),
|
2020-07-08 11:56:37 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := validate(context.Background(), tt.args.validations); !reflect.DeepEqual(got, tt.res.want) {
|
|
|
|
t.Errorf("validate() = %v, want %v", got, tt.res.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|