mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-31 19:42:15 +00:00
# Which Problems Are Solved - Some mail providers (e.g.: MS Exchange) have deprecated plain auth # How the Problems Are Solved Adds XOauth2 auth option to SMPT - SMTP config now can contain XOAuth2 auth config - Proto files are updated to have a oneof for configuration selection. Old fields for configuring plain auth are still available not to create a breaking change. These can be removed in the future. - Columns are added to the database to persist the config # Additional Changes none # Additional Context Replace this example with links to related issues, discussions, discord threads, or other sources with more context. Use the Closing #issue syntax for issues that are resolved with this PR. - Closes #8042 --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Tim Möhlmann <[email protected]>
154 lines
4.3 KiB
Go
154 lines
4.3 KiB
Go
package handlers
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"go.uber.org/mock/gomock"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/notification/channels/email"
|
|
"github.com/zitadel/zitadel/internal/notification/channels/smtp"
|
|
"github.com/zitadel/zitadel/internal/notification/handlers/mock"
|
|
"github.com/zitadel/zitadel/internal/query"
|
|
)
|
|
|
|
func TestNotificationQueries_GetActiveEmailConfig(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
const instId = "instance-1"
|
|
const cryptAlg = "alg1"
|
|
const keyId = "key1"
|
|
const pwd = "password"
|
|
|
|
encryptedPwd := []byte("password-encrypted")
|
|
cryptAlgMock := crypto.NewMockEncryptionAlgorithm(ctrl)
|
|
cryptAlgMock.EXPECT().DecryptString(gomock.Any(), gomock.Any()).Return(pwd, nil)
|
|
cryptAlgMock.EXPECT().Algorithm().Return(cryptAlg)
|
|
cryptAlgMock.EXPECT().DecryptionKeyIDs().Return([]string{keyId})
|
|
|
|
tt := []struct {
|
|
smtpConfig *query.SMTPConfig
|
|
expected *email.Config
|
|
}{
|
|
{
|
|
smtpConfig: &query.SMTPConfig{
|
|
ID: "smtp1",
|
|
Description: "smtp config with authentication (user and password)",
|
|
SMTPConfig: &query.SMTP{
|
|
TLS: true,
|
|
SenderAddress: "[email protected]",
|
|
SenderName: "sender",
|
|
ReplyToAddress: "[email protected]",
|
|
Host: "mail.com",
|
|
User: "mail-user",
|
|
PlainAuth: &query.PlainAuth{
|
|
Password: &crypto.CryptoValue{
|
|
CryptoType: 0,
|
|
Algorithm: cryptAlg,
|
|
KeyID: keyId,
|
|
Crypted: encryptedPwd,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expected: &email.Config{
|
|
ProviderConfig: &email.Provider{
|
|
ID: "smtp1",
|
|
Description: "smtp config with authentication (user and password)",
|
|
},
|
|
SMTPConfig: &smtp.Config{
|
|
SMTP: smtp.SMTP{
|
|
Host: "mail.com",
|
|
PlainAuth: &smtp.PlainAuthConfig{
|
|
User: "mail-user",
|
|
Password: pwd,
|
|
},
|
|
},
|
|
Tls: true,
|
|
From: "[email protected]",
|
|
FromName: "sender",
|
|
ReplyToAddress: "[email protected]",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
smtpConfig: &query.SMTPConfig{
|
|
ID: "smtp2",
|
|
Description: "smtp config with authentication (user and no password)",
|
|
SMTPConfig: &query.SMTP{
|
|
TLS: true,
|
|
SenderAddress: "[email protected]",
|
|
SenderName: "sender",
|
|
ReplyToAddress: "[email protected]",
|
|
Host: "mail.com",
|
|
User: "mail-user",
|
|
},
|
|
},
|
|
expected: &email.Config{
|
|
ProviderConfig: &email.Provider{
|
|
ID: "smtp2",
|
|
Description: "smtp config with authentication (user and no password)",
|
|
},
|
|
SMTPConfig: &smtp.Config{
|
|
SMTP: smtp.SMTP{
|
|
Host: "mail.com",
|
|
PlainAuth: &smtp.PlainAuthConfig{
|
|
User: "mail-user",
|
|
},
|
|
},
|
|
Tls: true,
|
|
From: "[email protected]",
|
|
FromName: "sender",
|
|
ReplyToAddress: "[email protected]",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
smtpConfig: &query.SMTPConfig{
|
|
ID: "smtp3",
|
|
Description: "smtp config without authentication",
|
|
SMTPConfig: &query.SMTP{
|
|
TLS: true,
|
|
SenderAddress: "[email protected]",
|
|
SenderName: "sender",
|
|
ReplyToAddress: "[email protected]",
|
|
Host: "mail.com",
|
|
},
|
|
},
|
|
expected: &email.Config{
|
|
ProviderConfig: &email.Provider{
|
|
ID: "smtp3",
|
|
Description: "smtp config without authentication",
|
|
},
|
|
SMTPConfig: &smtp.Config{
|
|
SMTP: smtp.SMTP{
|
|
Host: "mail.com",
|
|
},
|
|
Tls: true,
|
|
From: "[email protected]",
|
|
FromName: "sender",
|
|
ReplyToAddress: "[email protected]",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range tt {
|
|
t.Run(tc.smtpConfig.Description, func(t *testing.T) {
|
|
ctx := authz.NewMockContext(instId, "org-1", "user-1")
|
|
|
|
queryMock := mock.NewMockQueries(ctrl)
|
|
queryMock.EXPECT().SMTPConfigActive(gomock.Any(), instId).Return(tc.smtpConfig, nil)
|
|
|
|
notificationQueries := NewNotificationQueries(queryMock, &eventstore.Eventstore{}, "ext domain", uint16(1234), false, "filepath", nil, cryptAlgMock, nil)
|
|
cfg, err := notificationQueries.GetActiveEmailConfig(ctx)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, tc.expected, cfg)
|
|
})
|
|
}
|
|
}
|