Merge commit from fork

This commit is contained in:
Marco A.
2026-06-08 16:41:08 +02:00
committed by GitHub
parent 25e263394e
commit ed09b3df7f
6 changed files with 125 additions and 197 deletions
+31 -57
View File
@@ -5,12 +5,12 @@ import (
"testing"
"time"
"github.com/muhlemmer/gu"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"golang.org/x/text/language"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/crypto"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/eventstore"
@@ -1165,6 +1165,7 @@ func TestCommands_changeUserEmailWithGenerator(t *testing.T) {
loginPaths func(*testing.T) LoginPaths
}
type args struct {
ctx context.Context
userID string
email string
returnCode bool
@@ -1184,6 +1185,7 @@ func TestCommands_changeUserEmailWithGenerator(t *testing.T) {
loginPaths: expectLoginPathsNoCall,
},
args: args{
ctx: t.Context(),
userID: "",
email: "[email protected]",
returnCode: false,
@@ -1216,6 +1218,7 @@ func TestCommands_changeUserEmailWithGenerator(t *testing.T) {
loginPaths: expectLoginPathsNoCall,
},
args: args{
ctx: t.Context(),
userID: "user1",
email: "[email protected]",
returnCode: false,
@@ -1248,6 +1251,7 @@ func TestCommands_changeUserEmailWithGenerator(t *testing.T) {
loginPaths: expectLoginPathsNoCall,
},
args: args{
ctx: t.Context(),
userID: "user1",
email: "",
returnCode: false,
@@ -1280,6 +1284,7 @@ func TestCommands_changeUserEmailWithGenerator(t *testing.T) {
loginPaths: expectLoginPathsNoCall,
},
args: args{
ctx: t.Context(),
userID: "user1",
email: "[email protected]",
returnCode: false,
@@ -1330,6 +1335,7 @@ func TestCommands_changeUserEmailWithGenerator(t *testing.T) {
loginPaths: expectLoginPathsDefaultEmailCodeURLTemplate("http://example.com/{{.user}}/email/{{.code}}"),
},
args: args{
ctx: t.Context(),
userID: "user1",
email: "[email protected]",
returnCode: false,
@@ -1345,7 +1351,9 @@ func TestCommands_changeUserEmailWithGenerator(t *testing.T) {
},
},
{
name: "email changed, return code",
// returnCode=true returns the plaintext code to the caller, bypassing email delivery.
// Self-management must not be sufficient — explicit write permission is required.
name: "email change not allowed when return code=true for self-management",
fields: fields{
eventstore: expectEventstore(
expectFilter(
@@ -1364,42 +1372,18 @@ func TestCommands_changeUserEmailWithGenerator(t *testing.T) {
),
),
),
expectPush(
user.NewHumanEmailChangedEvent(context.Background(),
&user.NewAggregate("user1", "org1").Aggregate,
"[email protected]",
),
user.NewHumanEmailCodeAddedEvent(context.Background(),
&user.NewAggregate("user1", "org1").Aggregate,
&crypto.CryptoValue{
CryptoType: crypto.TypeEncryption,
Algorithm: "enc",
KeyID: "id",
Crypted: []byte("a"),
},
time.Hour*1,
"http://example.com/{{.user}}/email/{{.code2}}", true, "",
),
),
),
checkPermission: newMockPermissionCheckAllowed(),
loginPaths: expectLoginPathsDefaultEmailCodeURLTemplate("http://example.com/{{.user}}/email/{{.code2}}"),
checkPermission: newMockPermissionCheckNotAllowed(),
loginPaths: expectLoginPathsNoCall,
},
args: args{
ctx: authz.SetCtxData(t.Context(), authz.CtxData{UserID: "user1"}),
userID: "user1",
email: "[email protected]",
returnCode: true,
urlTmpl: "",
},
want: &domain.Email{
ObjectRoot: models.ObjectRoot{
AggregateID: "user1",
ResourceOwner: "org1",
},
EmailAddress: "[email protected]",
IsEmailVerified: false,
PlainCode: gu.Ptr("a"),
},
wantErr: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
},
{
name: "email changed, URL template",
@@ -1443,6 +1427,7 @@ func TestCommands_changeUserEmailWithGenerator(t *testing.T) {
loginPaths: expectLoginPathsNoCall,
},
args: args{
ctx: t.Context(),
userID: "user1",
email: "[email protected]",
returnCode: false,
@@ -1465,7 +1450,7 @@ func TestCommands_changeUserEmailWithGenerator(t *testing.T) {
checkPermission: tt.fields.checkPermission,
loginPaths: tt.fields.loginPaths(t),
}
got, err := c.changeUserEmailWithGenerator(context.Background(), tt.args.userID, tt.args.email, GetMockSecretGenerator(t), tt.args.returnCode, tt.args.urlTmpl)
got, err := c.changeUserEmailWithGenerator(tt.args.ctx, tt.args.userID, tt.args.email, GetMockSecretGenerator(t), tt.args.returnCode, tt.args.urlTmpl)
require.ErrorIs(t, tt.wantErr, err)
assert.Equal(t, tt.want, got)
})
@@ -1479,6 +1464,7 @@ func TestCommands_sendUserEmailCodeWithGeneratorEvents(t *testing.T) {
loginPaths func(*testing.T) LoginPaths
}
type args struct {
ctx context.Context
userID string
returnCode bool
urlTmpl string
@@ -1498,6 +1484,7 @@ func TestCommands_sendUserEmailCodeWithGeneratorEvents(t *testing.T) {
loginPaths: expectLoginPathsNoCall,
},
args: args{
ctx: t.Context(),
userID: "",
returnCode: false,
urlTmpl: "",
@@ -1543,6 +1530,7 @@ func TestCommands_sendUserEmailCodeWithGeneratorEvents(t *testing.T) {
loginPaths: expectLoginPathsNoCall,
},
args: args{
ctx: t.Context(),
userID: "user1",
returnCode: false,
urlTmpl: "",
@@ -1588,6 +1576,7 @@ func TestCommands_sendUserEmailCodeWithGeneratorEvents(t *testing.T) {
loginPaths: expectLoginPathsDefaultEmailCodeURLTemplate("http://example.com/{{.user}}/email/{{.code}}"),
},
args: args{
ctx: t.Context(),
userID: "user1",
returnCode: false,
urlTmpl: "",
@@ -1654,6 +1643,7 @@ func TestCommands_sendUserEmailCodeWithGeneratorEvents(t *testing.T) {
loginPaths: expectLoginPathsDefaultEmailCodeURLTemplate("http://example.com/{{.user}}/email/{{.code2}}"),
},
args: args{
ctx: t.Context(),
userID: "user1",
returnCode: false,
urlTmpl: "",
@@ -1694,6 +1684,7 @@ func TestCommands_sendUserEmailCodeWithGeneratorEvents(t *testing.T) {
loginPaths: expectLoginPathsNoCall,
},
args: args{
ctx: t.Context(),
userID: "user1",
returnCode: false,
urlTmpl: "",
@@ -1702,7 +1693,9 @@ func TestCommands_sendUserEmailCodeWithGeneratorEvents(t *testing.T) {
wantErr: zerrors.ThrowPreconditionFailed(nil, "EMAIL-5w5ilin4yt", "Errors.User.Code.Empty"),
},
{
name: "send code, return code",
// returnCode=true returns the plaintext code to the caller, bypassing email delivery.
// Self-management must not be sufficient — explicit write permission is required.
name: "send code not allowed when return code=true for self-management",
fields: fields{
eventstore: eventstoreExpect(
t,
@@ -1722,38 +1715,18 @@ func TestCommands_sendUserEmailCodeWithGeneratorEvents(t *testing.T) {
),
),
),
expectPush(
user.NewHumanEmailCodeAddedEvent(context.Background(),
&user.NewAggregate("user1", "org1").Aggregate,
&crypto.CryptoValue{
CryptoType: crypto.TypeEncryption,
Algorithm: "enc",
KeyID: "id",
Crypted: []byte("a"),
},
time.Hour*1,
"http://example.com/{{.user}}/email/{{.code}}", true, "",
),
),
),
checkPermission: newMockPermissionCheckAllowed(),
loginPaths: expectLoginPathsDefaultEmailCodeURLTemplate("http://example.com/{{.user}}/email/{{.code}}"),
checkPermission: newMockPermissionCheckNotAllowed(),
loginPaths: expectLoginPathsNoCall,
},
args: args{
ctx: authz.SetCtxData(t.Context(), authz.CtxData{UserID: "user1"}),
userID: "user1",
returnCode: true,
urlTmpl: "",
checkExisting: false,
},
want: &domain.Email{
ObjectRoot: models.ObjectRoot{
AggregateID: "user1",
ResourceOwner: "org1",
},
EmailAddress: "[email protected]",
IsEmailVerified: false,
PlainCode: gu.Ptr("a"),
},
wantErr: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
},
{
name: "send code, URL template",
@@ -1794,6 +1767,7 @@ func TestCommands_sendUserEmailCodeWithGeneratorEvents(t *testing.T) {
loginPaths: expectLoginPathsNoCall,
},
args: args{
ctx: t.Context(),
userID: "user1",
returnCode: false,
urlTmpl: "https://example.com/email/verify?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}",
@@ -1816,7 +1790,7 @@ func TestCommands_sendUserEmailCodeWithGeneratorEvents(t *testing.T) {
checkPermission: tt.fields.checkPermission,
loginPaths: tt.fields.loginPaths(t),
}
got, err := c.sendUserEmailCodeWithGenerator(context.Background(), tt.args.userID, GetMockSecretGenerator(t), tt.args.returnCode, tt.args.urlTmpl, tt.args.checkExisting)
got, err := c.sendUserEmailCodeWithGenerator(tt.args.ctx, tt.args.userID, GetMockSecretGenerator(t), tt.args.returnCode, tt.args.urlTmpl, tt.args.checkExisting)
require.ErrorIs(t, err, tt.wantErr)
assert.Equal(t, tt.want, got)
})