mirror of
https://github.com/hanzoai/team.git
synced 2026-09-30 01:57:35 +00:00
* Add EasyLogin configuration (#34217) * add easy login config * add easy login to the invite modal * add to the query parameters * Add an API to get login method for the login id (#34223) * add an api to get login method for the login id * do not return errors if user is not found * Add support for Easy Login invitation link sending (#34224) This generates Easy Login token types when requested. The server doesn't do anything with these tokens, yet - that will come in a future change. * Add support for logging in with easy login (#34236) * Fix E2E tests (#34240) * Prevent easy login accounts to reset their password (#34262) * Add easy login support to login api and limit token to 5 min (#34259) * webapp easy login ui mods (#34237) * webapp easy login ui mods * easy login i18n * lint issues * getUserLoginType * using the real API * easylogin proper redirect * remove unneeded functions and files * duplicated localization * remove easylogin * using EnableEasyLogin setting * localization fix * fix lint issue * remove excessive setIsWaiting * changed logic to make it more readable * renaming component to make easier editable * password will disappear when username change * login test * text for easy login password * Add app links to emails * Update templates and always land in the landing screen * Update svg image, improve checks on server, fix linking page and show deactivated on login type * Update naming * Fix mocks and imports * Remove all sessions on disable and forbid user promotion * Fix layer and tests * Address feedback * Fix tests * Fix missing string * Fix texts * Fix tests * Fix constant name * Fix tests * Fix test * Address feedback * Fix lint * Fix test * Address feedback * Fix test --------- Co-authored-by: Ibrahim Serdar Acikgoz <[email protected]> Co-authored-by: David Krauser <[email protected]> Co-authored-by: Daniel Espino <[email protected]> Co-authored-by: Mattermost Build <[email protected]>
360 lines
12 KiB
Go
360 lines
12 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package email
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/v8/platform/shared/mail"
|
|
)
|
|
|
|
func TestCondenseSiteURL(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
|
|
require.Equal(t, "", condenseSiteURL(""))
|
|
require.Equal(t, "mattermost.com", condenseSiteURL("mattermost.com"))
|
|
require.Equal(t, "mattermost.com", condenseSiteURL("mattermost.com/"))
|
|
require.Equal(t, "chat.mattermost.com", condenseSiteURL("chat.mattermost.com"))
|
|
require.Equal(t, "chat.mattermost.com", condenseSiteURL("chat.mattermost.com/"))
|
|
require.Equal(t, "mattermost.com/subpath", condenseSiteURL("mattermost.com/subpath"))
|
|
require.Equal(t, "mattermost.com/subpath", condenseSiteURL("mattermost.com/subpath/"))
|
|
require.Equal(t, "chat.mattermost.com/subpath", condenseSiteURL("chat.mattermost.com/subpath"))
|
|
require.Equal(t, "chat.mattermost.com/subpath", condenseSiteURL("chat.mattermost.com/subpath/"))
|
|
|
|
require.Equal(t, "mattermost.com:8080", condenseSiteURL("http://mattermost.com:8080"))
|
|
require.Equal(t, "mattermost.com:8080", condenseSiteURL("http://mattermost.com:8080/"))
|
|
require.Equal(t, "chat.mattermost.com:8080", condenseSiteURL("http://chat.mattermost.com:8080"))
|
|
require.Equal(t, "chat.mattermost.com:8080", condenseSiteURL("http://chat.mattermost.com:8080/"))
|
|
require.Equal(t, "mattermost.com:8080/subpath", condenseSiteURL("http://mattermost.com:8080/subpath"))
|
|
require.Equal(t, "mattermost.com:8080/subpath", condenseSiteURL("http://mattermost.com:8080/subpath/"))
|
|
require.Equal(t, "chat.mattermost.com:8080/subpath", condenseSiteURL("http://chat.mattermost.com:8080/subpath"))
|
|
require.Equal(t, "chat.mattermost.com:8080/subpath", condenseSiteURL("http://chat.mattermost.com:8080/subpath/"))
|
|
}
|
|
|
|
func TestSendInviteEmails(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t).InitBasic(t)
|
|
th.ConfigureInbucketMail(t)
|
|
|
|
emailTo := "[email protected]"
|
|
|
|
retrieveEmail := func(t *testing.T) mail.JSONMessageInbucket {
|
|
t.Helper()
|
|
var resultsMailbox mail.JSONMessageHeaderInbucket
|
|
err2 := mail.RetryInbucket(5, func() error {
|
|
var err error
|
|
resultsMailbox, err = mail.GetMailBox(emailTo)
|
|
return err
|
|
})
|
|
if err2 != nil {
|
|
t.Skipf("No email was received, maybe due load on the server: %v", err2)
|
|
}
|
|
require.Len(t, resultsMailbox, 1)
|
|
require.Contains(t, resultsMailbox[0].To[0], emailTo, "Wrong To: recipient")
|
|
resultsEmail, err := mail.GetMessageFromMailbox(emailTo, resultsMailbox[0].ID)
|
|
require.NoError(t, err, "Could not get message from mailbox")
|
|
return resultsEmail
|
|
}
|
|
|
|
verifyMailbox := func(t *testing.T) {
|
|
t.Helper()
|
|
email := retrieveEmail(t)
|
|
require.Contains(t, email.Body.HTML, "http://testserver", "Wrong received message %s", email.Body.Text)
|
|
require.Contains(t, email.Body.HTML, "test-user", "Wrong received message %s", email.Body.Text)
|
|
require.Contains(t, email.Body.Text, "http://testserver", "Wrong received message %s", email.Body.Text)
|
|
require.Contains(t, email.Body.Text, "test-user", "Wrong received message %s", email.Body.Text)
|
|
}
|
|
|
|
th.UpdateConfig(t, func(cfg *model.Config) {
|
|
*cfg.ServiceSettings.EnableEmailInvitations = true
|
|
*cfg.EmailSettings.SendEmailNotifications = false
|
|
})
|
|
t.Run("SendInviteEmails", func(t *testing.T) {
|
|
err := mail.DeleteMailBox(emailTo)
|
|
require.NoError(t, err, "Failed to delete mailbox")
|
|
|
|
err = th.service.SendInviteEmails(th.BasicTeam, "test-user", th.BasicUser.Id, []string{emailTo}, "http://testserver", nil, false, false, false)
|
|
require.NoError(t, err)
|
|
|
|
verifyMailbox(t)
|
|
})
|
|
|
|
t.Run("SendInviteEmails can return error when SMTP connection fails", func(t *testing.T) {
|
|
originalPort := *th.service.config().EmailSettings.SMTPPort
|
|
originalTimeout := *th.service.config().EmailSettings.SMTPServerTimeout
|
|
th.UpdateConfig(t, func(cfg *model.Config) {
|
|
*cfg.EmailSettings.SMTPPort = "5432"
|
|
*cfg.EmailSettings.SMTPServerTimeout = 4
|
|
})
|
|
defer th.UpdateConfig(t, func(cfg *model.Config) {
|
|
*cfg.EmailSettings.SMTPPort = originalPort
|
|
*cfg.EmailSettings.SMTPServerTimeout = originalTimeout
|
|
})
|
|
|
|
err := th.service.SendInviteEmails(th.BasicTeam, "test-user", th.BasicUser.Id, []string{emailTo}, "http://testserver", nil, true, false, false)
|
|
require.Error(t, err)
|
|
|
|
err = th.service.SendInviteEmails(th.BasicTeam, "test-user", th.BasicUser.Id, []string{emailTo}, "http://testserver", nil, false, false, false)
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
t.Run("SendGuestInviteEmails", func(t *testing.T) {
|
|
err := mail.DeleteMailBox(emailTo)
|
|
require.NoError(t, err, "Failed to delete mailbox")
|
|
|
|
err = th.service.SendGuestInviteEmails(
|
|
th.BasicTeam,
|
|
[]*model.Channel{th.BasicChannel},
|
|
"test-user",
|
|
th.BasicUser.Id,
|
|
nil,
|
|
[]string{emailTo},
|
|
"http://testserver",
|
|
"hello world",
|
|
false,
|
|
false,
|
|
false,
|
|
false,
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
verifyMailbox(t)
|
|
})
|
|
|
|
t.Run("SendGuestInviteEmail can return error when SMTP connection fails", func(t *testing.T) {
|
|
originalTimeout := *th.service.config().EmailSettings.SMTPServerTimeout
|
|
originalPort := *th.service.config().EmailSettings.SMTPPort
|
|
th.UpdateConfig(t, func(cfg *model.Config) {
|
|
*cfg.EmailSettings.SMTPPort = "5432"
|
|
*cfg.EmailSettings.SMTPServerTimeout = 4
|
|
})
|
|
defer th.UpdateConfig(t, func(cfg *model.Config) {
|
|
*cfg.EmailSettings.SMTPPort = originalPort
|
|
*cfg.EmailSettings.SMTPServerTimeout = originalTimeout
|
|
})
|
|
|
|
err := th.service.SendGuestInviteEmails(
|
|
th.BasicTeam,
|
|
[]*model.Channel{th.BasicChannel},
|
|
"test-user",
|
|
th.BasicUser.Id,
|
|
nil,
|
|
[]string{emailTo},
|
|
"http://testserver",
|
|
"hello world",
|
|
false,
|
|
false,
|
|
false,
|
|
false,
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
err = th.service.SendGuestInviteEmails(
|
|
th.BasicTeam,
|
|
[]*model.Channel{th.BasicChannel},
|
|
"test-user",
|
|
th.BasicUser.Id,
|
|
nil,
|
|
[]string{emailTo},
|
|
"http://testserver",
|
|
"hello world",
|
|
true,
|
|
false,
|
|
false,
|
|
false,
|
|
)
|
|
require.Error(t, err)
|
|
})
|
|
|
|
t.Run("SendGuestInviteEmails should sanitize HTML input", func(t *testing.T) {
|
|
err := mail.DeleteMailBox(emailTo)
|
|
require.NoError(t, err, "Failed to delete mailbox")
|
|
|
|
message := `<a href="http://testserver">sanitized message</a>`
|
|
err = th.service.SendGuestInviteEmails(
|
|
th.BasicTeam,
|
|
[]*model.Channel{th.BasicChannel},
|
|
"test-user",
|
|
th.BasicUser.Id,
|
|
nil,
|
|
[]string{emailTo},
|
|
"http://testserver",
|
|
message,
|
|
false,
|
|
false,
|
|
false,
|
|
false,
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
email := retrieveEmail(t)
|
|
require.NotContains(t, email.Body.HTML, message)
|
|
require.Contains(t, email.Body.HTML, "sanitized message")
|
|
require.Contains(t, email.Body.Text, "sanitized message")
|
|
})
|
|
|
|
t.Run("SendInviteEmails should contain button URL with 'started by role' param for system user", func(t *testing.T) {
|
|
err := mail.DeleteMailBox(emailTo)
|
|
require.NoError(t, err, "Failed to delete mailbox")
|
|
|
|
err = th.service.SendInviteEmails(
|
|
th.BasicTeam,
|
|
"test-user",
|
|
th.BasicUser.Id,
|
|
[]string{emailTo},
|
|
"http://testserver",
|
|
nil,
|
|
false,
|
|
false,
|
|
false,
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
email := retrieveEmail(t)
|
|
require.Contains(t, email.Body.HTML, "&sbr=su")
|
|
})
|
|
|
|
t.Run("SendInviteEmails should contain button URL with 'started by role' param for system admin", func(t *testing.T) {
|
|
err := mail.DeleteMailBox(emailTo)
|
|
require.NoError(t, err, "Failed to delete mailbox")
|
|
|
|
err = th.service.SendInviteEmails(
|
|
th.BasicTeam,
|
|
"test-user",
|
|
th.BasicUser.Id,
|
|
[]string{emailTo},
|
|
"http://testserver",
|
|
nil,
|
|
false,
|
|
true,
|
|
false,
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
email := retrieveEmail(t)
|
|
require.Contains(t, email.Body.HTML, "&sbr=sa")
|
|
})
|
|
|
|
t.Run("SendInviteEmails should contain button URL with 'started by role' param for first system admin", func(t *testing.T) {
|
|
err := mail.DeleteMailBox(emailTo)
|
|
require.NoError(t, err, "Failed to delete mailbox")
|
|
|
|
err = th.service.SendInviteEmails(
|
|
th.BasicTeam,
|
|
"test-user",
|
|
th.BasicUser.Id,
|
|
[]string{emailTo},
|
|
"http://testserver",
|
|
nil,
|
|
false,
|
|
true,
|
|
true,
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
email := retrieveEmail(t)
|
|
require.Contains(t, email.Body.HTML, "&sbr=fa")
|
|
})
|
|
}
|
|
|
|
func TestSendCloudWelcomeEmail(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t).InitBasic(t)
|
|
th.ConfigureInbucketMail(t)
|
|
|
|
emailTo := "[email protected]"
|
|
|
|
t.Run("TestSendCloudWelcomeEmail", func(t *testing.T) {
|
|
verifyMailbox := func(t *testing.T) {
|
|
t.Helper()
|
|
|
|
var resultsMailbox mail.JSONMessageHeaderInbucket
|
|
err2 := mail.RetryInbucket(5, func() error {
|
|
var err error
|
|
resultsMailbox, err = mail.GetMailBox(emailTo)
|
|
return err
|
|
})
|
|
if err2 != nil {
|
|
t.Skipf("No email was received, maybe due load on the server: %v", err2)
|
|
}
|
|
|
|
require.Len(t, resultsMailbox, 1)
|
|
require.Contains(t, resultsMailbox[0].To[0], emailTo, "Wrong To: recipient")
|
|
resultsEmail, err := mail.GetMessageFromMailbox(emailTo, resultsMailbox[0].ID)
|
|
require.NoError(t, err, "Could not get message from mailbox")
|
|
require.Contains(t, resultsEmail.Subject, "Congratulations!", "Wrong subject message %s", resultsEmail.Subject)
|
|
require.Contains(t, resultsEmail.Body.Text, "Your workspace is ready to go!", "Wrong body %s", resultsEmail.Body.Text)
|
|
}
|
|
|
|
err := mail.DeleteMailBox(emailTo)
|
|
require.NoError(t, err, "Failed to delete mailbox")
|
|
|
|
err = th.service.SendCloudWelcomeEmail(emailTo, th.BasicUser.Locale, "inviteID", "SomeName", "example.com", "https://example.com")
|
|
require.NoError(t, err)
|
|
|
|
verifyMailbox(t)
|
|
})
|
|
}
|
|
|
|
func TestMailServiceConfig(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
configuredReplyTo := "[email protected]"
|
|
customReplyTo := "[email protected]"
|
|
|
|
emailService := Service{
|
|
config: func() *model.Config {
|
|
return &model.Config{
|
|
ServiceSettings: model.ServiceSettings{
|
|
SiteURL: model.NewPointer(""),
|
|
},
|
|
EmailSettings: model.EmailSettings{
|
|
EnableSignUpWithEmail: new(bool),
|
|
EnableSignInWithEmail: new(bool),
|
|
EnableSignInWithUsername: new(bool),
|
|
SendEmailNotifications: new(bool),
|
|
UseChannelInEmailNotifications: new(bool),
|
|
RequireEmailVerification: new(bool),
|
|
FeedbackName: new(string),
|
|
FeedbackEmail: new(string),
|
|
ReplyToAddress: model.NewPointer(configuredReplyTo),
|
|
FeedbackOrganization: new(string),
|
|
EnableSMTPAuth: new(bool),
|
|
SMTPUsername: new(string),
|
|
SMTPPassword: new(string),
|
|
SMTPServer: new(string),
|
|
SMTPPort: new(string),
|
|
SMTPServerTimeout: new(int),
|
|
ConnectionSecurity: new(string),
|
|
SendPushNotifications: new(bool),
|
|
PushNotificationServer: new(string),
|
|
PushNotificationContents: new(string),
|
|
PushNotificationBuffer: new(int),
|
|
EnableEmailBatching: new(bool),
|
|
EmailBatchingBufferSize: new(int),
|
|
EmailBatchingInterval: new(int),
|
|
EnablePreviewModeBanner: new(bool),
|
|
SkipServerCertificateVerification: new(bool),
|
|
EmailNotificationContentsType: new(string),
|
|
LoginButtonColor: new(string),
|
|
LoginButtonBorderColor: new(string),
|
|
LoginButtonTextColor: new(string),
|
|
},
|
|
}
|
|
},
|
|
}
|
|
|
|
t.Run("use custom replyto instead of configured replyto", func(t *testing.T) {
|
|
mailConfig := emailService.mailServiceConfig(customReplyTo)
|
|
require.Equal(t, customReplyTo, mailConfig.ReplyToAddress)
|
|
})
|
|
|
|
t.Run("use configured replyto", func(t *testing.T) {
|
|
mailConfig := emailService.mailServiceConfig("")
|
|
require.Equal(t, configuredReplyTo, mailConfig.ReplyToAddress)
|
|
})
|
|
}
|