2021-03-19 10:12:56 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
2023-04-25 07:02:29 +00:00
|
|
|
"strings"
|
2021-03-19 10:12:56 +00:00
|
|
|
"testing"
|
2023-04-25 07:02:29 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-03-19 10:12:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEmailValid(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
email *Email
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
result bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty email, invalid",
|
|
|
|
args: args{
|
|
|
|
email: &Email{},
|
|
|
|
},
|
|
|
|
result: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "only letters email, invalid",
|
|
|
|
args: args{
|
|
|
|
email: &Email{EmailAddress: "testemail"},
|
|
|
|
},
|
|
|
|
result: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nothing after @, invalid",
|
|
|
|
args: args{
|
|
|
|
email: &Email{EmailAddress: "testemail@"},
|
|
|
|
},
|
|
|
|
result: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "email, valid",
|
|
|
|
args: args{
|
|
|
|
email: &Email{EmailAddress: "testemail@gmail.com"},
|
|
|
|
},
|
|
|
|
result: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "email, valid",
|
|
|
|
args: args{
|
|
|
|
email: &Email{EmailAddress: "test.email@gmail.com"},
|
|
|
|
},
|
|
|
|
result: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "email, valid",
|
|
|
|
args: args{
|
|
|
|
email: &Email{EmailAddress: "test/email@gmail.com"},
|
|
|
|
},
|
|
|
|
result: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "email, valid",
|
|
|
|
args: args{
|
|
|
|
email: &Email{EmailAddress: "test/email@gmail.com"},
|
|
|
|
},
|
|
|
|
result: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-03-14 19:20:38 +00:00
|
|
|
result := tt.args.email.Validate() == nil
|
2021-03-19 10:12:56 +00:00
|
|
|
if result != tt.result {
|
|
|
|
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result, result)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-04-25 07:02:29 +00:00
|
|
|
|
|
|
|
func TestRenderConfirmURLTemplate(t *testing.T) {
|
|
|
|
type args struct {
|
2023-05-24 10:22:00 +00:00
|
|
|
tmpl string
|
|
|
|
userID string
|
|
|
|
code string
|
|
|
|
orgID string
|
2023-04-25 07:02:29 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "invalid template",
|
|
|
|
args: args{
|
2023-05-24 10:22:00 +00:00
|
|
|
tmpl: "{{",
|
|
|
|
userID: "user1",
|
|
|
|
code: "123",
|
|
|
|
orgID: "org1",
|
2023-04-25 07:02:29 +00:00
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "DOMAIN-oGh5e", "Errors.User.InvalidURLTemplate"),
|
2023-04-25 07:02:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "execution error",
|
|
|
|
args: args{
|
2023-05-24 10:22:00 +00:00
|
|
|
tmpl: "{{.Foo}}",
|
|
|
|
userID: "user1",
|
|
|
|
code: "123",
|
|
|
|
orgID: "org1",
|
2023-04-25 07:02:29 +00:00
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "DOMAIN-ieYa7", "Errors.User.InvalidURLTemplate"),
|
2023-04-25 07:02:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
args: args{
|
2023-05-24 10:22:00 +00:00
|
|
|
tmpl: "https://example.com/email/verify?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}",
|
|
|
|
userID: "user1",
|
|
|
|
code: "123",
|
|
|
|
orgID: "org1",
|
2023-04-25 07:02:29 +00:00
|
|
|
},
|
|
|
|
want: "https://example.com/email/verify?userID=user1&code=123&orgID=org1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
var w strings.Builder
|
2023-05-24 10:22:00 +00:00
|
|
|
err := RenderConfirmURLTemplate(&w, tt.args.tmpl, tt.args.userID, tt.args.code, tt.args.orgID)
|
2023-04-25 07:02:29 +00:00
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
assert.Equal(t, tt.want, w.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|