2023-08-02 16:57:53 +00:00
|
|
|
//go:build integration
|
|
|
|
|
|
|
|
package user_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-02-28 10:21:11 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2023-08-02 16:57:53 +00:00
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/integration"
|
2024-07-26 20:39:55 +00:00
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/object/v2"
|
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/user/v2"
|
2023-08-02 16:57:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServer_AddOTPSMS(t *testing.T) {
|
2024-09-06 12:47:57 +00:00
|
|
|
userID := Instance.CreateHumanUser(CTX).GetUserId()
|
|
|
|
Instance.RegisterUserPasskey(CTX, userID)
|
|
|
|
_, sessionToken, _, _ := Instance.CreateVerifiedWebAuthNSession(t, CTX, userID)
|
2023-08-02 16:57:53 +00:00
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
otherUser := Instance.CreateHumanUser(CTX).GetUserId()
|
|
|
|
Instance.RegisterUserPasskey(CTX, otherUser)
|
|
|
|
_, sessionTokenOtherUser, _, _ := Instance.CreateVerifiedWebAuthNSession(t, CTX, otherUser)
|
2024-05-07 05:38:26 +00:00
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
userVerified := Instance.CreateHumanUser(CTX)
|
|
|
|
_, err := Instance.Client.UserV2.VerifyPhone(CTX, &user.VerifyPhoneRequest{
|
2024-05-07 05:38:26 +00:00
|
|
|
UserId: userVerified.GetUserId(),
|
|
|
|
VerificationCode: userVerified.GetPhoneCode(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-09-06 12:47:57 +00:00
|
|
|
Instance.RegisterUserPasskey(CTX, userVerified.GetUserId())
|
|
|
|
_, sessionTokenVerified, _, _ := Instance.CreateVerifiedWebAuthNSession(t, CTX, userVerified.GetUserId())
|
2024-05-07 05:38:26 +00:00
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
userVerified2 := Instance.CreateHumanUser(CTX)
|
|
|
|
_, err = Instance.Client.UserV2.VerifyPhone(CTX, &user.VerifyPhoneRequest{
|
2024-05-07 05:38:26 +00:00
|
|
|
UserId: userVerified2.GetUserId(),
|
|
|
|
VerificationCode: userVerified2.GetPhoneCode(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-02 16:57:53 +00:00
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
req *user.AddOTPSMSRequest
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *user.AddOTPSMSResponse
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing user id",
|
|
|
|
args: args{
|
|
|
|
ctx: CTX,
|
|
|
|
req: &user.AddOTPSMSRequest{},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user mismatch",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: integration.WithAuthorizationToken(context.Background(), sessionTokenOtherUser),
|
2023-08-02 16:57:53 +00:00
|
|
|
req: &user.AddOTPSMSRequest{
|
2024-05-07 05:38:26 +00:00
|
|
|
UserId: userID,
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "phone not verified",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: integration.WithAuthorizationToken(context.Background(), sessionToken),
|
2023-08-02 16:57:53 +00:00
|
|
|
req: &user.AddOTPSMSRequest{
|
|
|
|
UserId: userID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
{
|
|
|
|
name: "add success",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: integration.WithAuthorizationToken(context.Background(), sessionTokenVerified),
|
2024-05-07 05:38:26 +00:00
|
|
|
req: &user.AddOTPSMSRequest{
|
|
|
|
UserId: userVerified.GetUserId(),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
},
|
|
|
|
want: &user.AddOTPSMSResponse{
|
|
|
|
Details: &object.Details{
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: Instance.DefaultOrg.Id,
|
2024-05-07 05:38:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "add success, admin",
|
|
|
|
args: args{
|
|
|
|
ctx: CTX,
|
|
|
|
req: &user.AddOTPSMSRequest{
|
|
|
|
UserId: userVerified2.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &user.AddOTPSMSResponse{
|
|
|
|
Details: &object.Details{
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: Instance.DefaultOrg.Id,
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
},
|
2023-08-02 16:57:53 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := Client.AddOTPSMS(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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_RemoveOTPSMS(t *testing.T) {
|
2024-09-06 12:47:57 +00:00
|
|
|
userID := Instance.CreateHumanUser(CTX).GetUserId()
|
|
|
|
Instance.RegisterUserPasskey(CTX, userID)
|
|
|
|
_, sessionToken, _, _ := Instance.CreateVerifiedWebAuthNSession(t, CTX, userID)
|
2024-05-07 05:38:26 +00:00
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
userVerified := Instance.CreateHumanUser(CTX)
|
|
|
|
Instance.RegisterUserPasskey(CTX, userVerified.GetUserId())
|
|
|
|
_, sessionTokenVerified, _, _ := Instance.CreateVerifiedWebAuthNSession(t, CTX, userVerified.GetUserId())
|
|
|
|
userVerifiedCtx := integration.WithAuthorizationToken(context.Background(), sessionTokenVerified)
|
|
|
|
_, err := Instance.Client.UserV2.VerifyPhone(userVerifiedCtx, &user.VerifyPhoneRequest{
|
2024-05-07 05:38:26 +00:00
|
|
|
UserId: userVerified.GetUserId(),
|
|
|
|
VerificationCode: userVerified.GetPhoneCode(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-09-06 12:47:57 +00:00
|
|
|
_, err = Instance.Client.UserV2.AddOTPSMS(userVerifiedCtx, &user.AddOTPSMSRequest{UserId: userVerified.GetUserId()})
|
2024-05-07 05:38:26 +00:00
|
|
|
require.NoError(t, err)
|
2023-08-02 16:57:53 +00:00
|
|
|
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
req *user.RemoveOTPSMSRequest
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *user.RemoveOTPSMSResponse
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "not added",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: integration.WithAuthorizationToken(context.Background(), sessionToken),
|
2023-08-02 16:57:53 +00:00
|
|
|
req: &user.RemoveOTPSMSRequest{
|
2024-05-07 05:38:26 +00:00
|
|
|
UserId: userID,
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
args: args{
|
|
|
|
ctx: userVerifiedCtx,
|
|
|
|
req: &user.RemoveOTPSMSRequest{
|
|
|
|
UserId: userVerified.GetUserId(),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
},
|
|
|
|
want: &user.RemoveOTPSMSResponse{
|
|
|
|
Details: &object.Details{
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: Instance.DefaultOrg.Details.ResourceOwner,
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
},
|
2023-08-02 16:57:53 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := Client.RemoveOTPSMS(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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_AddOTPEmail(t *testing.T) {
|
2024-09-06 12:47:57 +00:00
|
|
|
userID := Instance.CreateHumanUser(CTX).GetUserId()
|
|
|
|
Instance.RegisterUserPasskey(CTX, userID)
|
|
|
|
_, sessionToken, _, _ := Instance.CreateVerifiedWebAuthNSession(t, CTX, userID)
|
2023-08-02 16:57:53 +00:00
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
otherUser := Instance.CreateHumanUser(CTX).GetUserId()
|
|
|
|
Instance.RegisterUserPasskey(CTX, otherUser)
|
|
|
|
_, sessionTokenOtherUser, _, _ := Instance.CreateVerifiedWebAuthNSession(t, CTX, otherUser)
|
2024-05-07 05:38:26 +00:00
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
userVerified := Instance.CreateHumanUser(CTX)
|
|
|
|
_, err := Instance.Client.UserV2.VerifyEmail(CTX, &user.VerifyEmailRequest{
|
2023-08-02 16:57:53 +00:00
|
|
|
UserId: userVerified.GetUserId(),
|
|
|
|
VerificationCode: userVerified.GetEmailCode(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-09-06 12:47:57 +00:00
|
|
|
Instance.RegisterUserPasskey(CTX, userVerified.GetUserId())
|
|
|
|
_, sessionTokenVerified, _, _ := Instance.CreateVerifiedWebAuthNSession(t, CTX, userVerified.GetUserId())
|
2023-08-02 16:57:53 +00:00
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
userVerified2 := Instance.CreateHumanUser(CTX)
|
|
|
|
_, err = Instance.Client.UserV2.VerifyEmail(CTX, &user.VerifyEmailRequest{
|
2024-05-07 05:38:26 +00:00
|
|
|
UserId: userVerified2.GetUserId(),
|
|
|
|
VerificationCode: userVerified2.GetEmailCode(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-02 16:57:53 +00:00
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
req *user.AddOTPEmailRequest
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *user.AddOTPEmailResponse
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing user id",
|
|
|
|
args: args{
|
|
|
|
ctx: CTX,
|
|
|
|
req: &user.AddOTPEmailRequest{},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user mismatch",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: integration.WithAuthorizationToken(context.Background(), sessionTokenOtherUser),
|
2023-08-02 16:57:53 +00:00
|
|
|
req: &user.AddOTPEmailRequest{
|
2024-05-07 05:38:26 +00:00
|
|
|
UserId: userID,
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "email not verified",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: integration.WithAuthorizationToken(context.Background(), sessionToken),
|
2023-08-02 16:57:53 +00:00
|
|
|
req: &user.AddOTPEmailRequest{
|
|
|
|
UserId: userID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "add success",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: integration.WithAuthorizationToken(context.Background(), sessionTokenVerified),
|
2023-08-02 16:57:53 +00:00
|
|
|
req: &user.AddOTPEmailRequest{
|
|
|
|
UserId: userVerified.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &user.AddOTPEmailResponse{
|
|
|
|
Details: &object.Details{
|
2024-02-28 10:21:11 +00:00
|
|
|
ChangeDate: timestamppb.Now(),
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: Instance.DefaultOrg.Id,
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
{
|
|
|
|
name: "add success, admin",
|
|
|
|
args: args{
|
|
|
|
ctx: CTX,
|
|
|
|
req: &user.AddOTPEmailRequest{
|
|
|
|
UserId: userVerified2.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &user.AddOTPEmailResponse{
|
|
|
|
Details: &object.Details{
|
|
|
|
ChangeDate: timestamppb.Now(),
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: Instance.DefaultOrg.Id,
|
2024-05-07 05:38:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-02 16:57:53 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := Client.AddOTPEmail(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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_RemoveOTPEmail(t *testing.T) {
|
2024-09-06 12:47:57 +00:00
|
|
|
userID := Instance.CreateHumanUser(CTX).GetUserId()
|
|
|
|
Instance.RegisterUserPasskey(CTX, userID)
|
|
|
|
_, sessionToken, _, _ := Instance.CreateVerifiedWebAuthNSession(t, CTX, userID)
|
2023-08-02 16:57:53 +00:00
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
userVerified := Instance.CreateHumanUser(CTX)
|
|
|
|
Instance.RegisterUserPasskey(CTX, userVerified.GetUserId())
|
|
|
|
_, sessionTokenVerified, _, _ := Instance.CreateVerifiedWebAuthNSession(t, CTX, userVerified.GetUserId())
|
|
|
|
userVerifiedCtx := integration.WithAuthorizationToken(context.Background(), sessionTokenVerified)
|
|
|
|
_, err := Instance.Client.UserV2.VerifyEmail(userVerifiedCtx, &user.VerifyEmailRequest{
|
2023-08-02 16:57:53 +00:00
|
|
|
UserId: userVerified.GetUserId(),
|
|
|
|
VerificationCode: userVerified.GetEmailCode(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-09-06 12:47:57 +00:00
|
|
|
_, err = Instance.Client.UserV2.AddOTPEmail(userVerifiedCtx, &user.AddOTPEmailRequest{UserId: userVerified.GetUserId()})
|
2023-08-02 16:57:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
req *user.RemoveOTPEmailRequest
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *user.RemoveOTPEmailResponse
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "not added",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: integration.WithAuthorizationToken(context.Background(), sessionToken),
|
2023-08-02 16:57:53 +00:00
|
|
|
req: &user.RemoveOTPEmailRequest{
|
|
|
|
UserId: userID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
args: args{
|
|
|
|
ctx: userVerifiedCtx,
|
|
|
|
req: &user.RemoveOTPEmailRequest{
|
|
|
|
UserId: userVerified.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &user.RemoveOTPEmailResponse{
|
|
|
|
Details: &object.Details{
|
2024-02-28 10:21:11 +00:00
|
|
|
ChangeDate: timestamppb.Now(),
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: Instance.DefaultOrg.Details.ResourceOwner,
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := Client.RemoveOTPEmail(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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|