mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-15 04:18:01 +00:00
7d2d85f57c
# Which Problems Are Solved The v2beta services are stable but not GA. # How the Problems Are Solved The v2beta services are copied to v2. The corresponding v1 and v2beta services are deprecated. # Additional Context Closes #7236 --------- Co-authored-by: Elio Bischof <elio@zitadel.com>
233 lines
5.5 KiB
Go
233 lines
5.5 KiB
Go
//go:build integration
|
|
|
|
package user_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/muhlemmer/gu"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
"github.com/zitadel/zitadel/internal/integration"
|
|
object "github.com/zitadel/zitadel/pkg/grpc/object/v2beta"
|
|
user "github.com/zitadel/zitadel/pkg/grpc/user/v2beta"
|
|
)
|
|
|
|
func TestServer_RequestPasswordReset(t *testing.T) {
|
|
userID := Tester.CreateHumanUser(CTX).GetUserId()
|
|
|
|
tests := []struct {
|
|
name string
|
|
req *user.PasswordResetRequest
|
|
want *user.PasswordResetResponse
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "default medium",
|
|
req: &user.PasswordResetRequest{
|
|
UserId: userID,
|
|
},
|
|
want: &user.PasswordResetResponse{
|
|
Details: &object.Details{
|
|
Sequence: 1,
|
|
ChangeDate: timestamppb.Now(),
|
|
ResourceOwner: Tester.Organisation.ID,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "custom url template",
|
|
req: &user.PasswordResetRequest{
|
|
UserId: userID,
|
|
Medium: &user.PasswordResetRequest_SendLink{
|
|
SendLink: &user.SendPasswordResetLink{
|
|
NotificationType: user.NotificationType_NOTIFICATION_TYPE_Email,
|
|
UrlTemplate: gu.Ptr("https://example.com/password/change?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}"),
|
|
},
|
|
},
|
|
},
|
|
want: &user.PasswordResetResponse{
|
|
Details: &object.Details{
|
|
Sequence: 1,
|
|
ChangeDate: timestamppb.Now(),
|
|
ResourceOwner: Tester.Organisation.ID,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "template error",
|
|
req: &user.PasswordResetRequest{
|
|
UserId: userID,
|
|
Medium: &user.PasswordResetRequest_SendLink{
|
|
SendLink: &user.SendPasswordResetLink{
|
|
UrlTemplate: gu.Ptr("{{"),
|
|
},
|
|
},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "return code",
|
|
req: &user.PasswordResetRequest{
|
|
UserId: userID,
|
|
Medium: &user.PasswordResetRequest_ReturnCode{
|
|
ReturnCode: &user.ReturnPasswordResetCode{},
|
|
},
|
|
},
|
|
want: &user.PasswordResetResponse{
|
|
Details: &object.Details{
|
|
Sequence: 1,
|
|
ChangeDate: timestamppb.Now(),
|
|
ResourceOwner: Tester.Organisation.ID,
|
|
},
|
|
VerificationCode: gu.Ptr("xxx"),
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := Client.PasswordReset(CTX, tt.req)
|
|
if tt.wantErr {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
integration.AssertDetails(t, tt.want, got)
|
|
if tt.want.GetVerificationCode() != "" {
|
|
assert.NotEmpty(t, got.GetVerificationCode())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestServer_SetPassword(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
req *user.SetPasswordRequest
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
prepare func(request *user.SetPasswordRequest) error
|
|
args args
|
|
want *user.SetPasswordResponse
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "missing user id",
|
|
prepare: func(request *user.SetPasswordRequest) error {
|
|
return nil
|
|
},
|
|
args: args{
|
|
ctx: CTX,
|
|
req: &user.SetPasswordRequest{},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "set successful",
|
|
prepare: func(request *user.SetPasswordRequest) error {
|
|
userID := Tester.CreateHumanUser(CTX).GetUserId()
|
|
request.UserId = userID
|
|
return nil
|
|
},
|
|
args: args{
|
|
ctx: CTX,
|
|
req: &user.SetPasswordRequest{
|
|
NewPassword: &user.Password{
|
|
Password: "Secr3tP4ssw0rd!",
|
|
},
|
|
},
|
|
},
|
|
want: &user.SetPasswordResponse{
|
|
Details: &object.Details{
|
|
ChangeDate: timestamppb.Now(),
|
|
ResourceOwner: Tester.Organisation.ID,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "change successful",
|
|
prepare: func(request *user.SetPasswordRequest) error {
|
|
userID := Tester.CreateHumanUser(CTX).GetUserId()
|
|
request.UserId = userID
|
|
_, err := Client.SetPassword(CTX, &user.SetPasswordRequest{
|
|
UserId: userID,
|
|
NewPassword: &user.Password{
|
|
Password: "InitialPassw0rd!",
|
|
},
|
|
})
|
|
return err
|
|
},
|
|
args: args{
|
|
ctx: CTX,
|
|
req: &user.SetPasswordRequest{
|
|
NewPassword: &user.Password{
|
|
Password: "Secr3tP4ssw0rd!",
|
|
},
|
|
Verification: &user.SetPasswordRequest_CurrentPassword{
|
|
CurrentPassword: "InitialPassw0rd!",
|
|
},
|
|
},
|
|
},
|
|
want: &user.SetPasswordResponse{
|
|
Details: &object.Details{
|
|
ChangeDate: timestamppb.Now(),
|
|
ResourceOwner: Tester.Organisation.ID,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "set with code successful",
|
|
prepare: func(request *user.SetPasswordRequest) error {
|
|
userID := Tester.CreateHumanUser(CTX).GetUserId()
|
|
request.UserId = userID
|
|
resp, err := Client.PasswordReset(CTX, &user.PasswordResetRequest{
|
|
UserId: userID,
|
|
Medium: &user.PasswordResetRequest_ReturnCode{
|
|
ReturnCode: &user.ReturnPasswordResetCode{},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
request.Verification = &user.SetPasswordRequest_VerificationCode{
|
|
VerificationCode: resp.GetVerificationCode(),
|
|
}
|
|
return nil
|
|
},
|
|
args: args{
|
|
ctx: CTX,
|
|
req: &user.SetPasswordRequest{
|
|
NewPassword: &user.Password{
|
|
Password: "Secr3tP4ssw0rd!",
|
|
},
|
|
},
|
|
},
|
|
want: &user.SetPasswordResponse{
|
|
Details: &object.Details{
|
|
ChangeDate: timestamppb.Now(),
|
|
ResourceOwner: Tester.Organisation.ID,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := tt.prepare(tt.args.req)
|
|
require.NoError(t, err)
|
|
|
|
got, err := Client.SetPassword(tt.args.ctx, tt.args.req)
|
|
if tt.wantErr {
|
|
require.Error(t, err)
|
|
return
|
|
}
|
|
require.NoError(t, err)
|
|
require.NotNil(t, got)
|
|
integration.AssertDetails(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|