2021-11-11 07:03:30 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-11-11 07:03:30 +00:00
|
|
|
|
2022-06-15 07:02:00 +00:00
|
|
|
package user_test
|
2021-11-11 07:03:30 +00:00
|
|
|
|
|
|
|
import (
|
2023-11-16 23:17:40 +00:00
|
|
|
"fmt"
|
2021-11-11 07:03:30 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 14:36:47 +00:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2022-06-15 07:02:00 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-28 14:11:58 +00:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-11-11 07:03:30 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetEmailAddresses(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-11 07:03:30 +00:00
|
|
|
|
2023-09-14 17:09:32 +00:00
|
|
|
emails, _ := user_model.GetEmailAddresses(db.DefaultContext, int64(1))
|
2021-11-11 07:03:30 +00:00
|
|
|
if assert.Len(t, emails, 3) {
|
|
|
|
assert.True(t, emails[0].IsPrimary)
|
|
|
|
assert.True(t, emails[2].IsActivated)
|
|
|
|
assert.False(t, emails[2].IsPrimary)
|
|
|
|
}
|
|
|
|
|
2023-09-14 17:09:32 +00:00
|
|
|
emails, _ = user_model.GetEmailAddresses(db.DefaultContext, int64(2))
|
2021-11-11 07:03:30 +00:00
|
|
|
if assert.Len(t, emails, 2) {
|
|
|
|
assert.True(t, emails[0].IsPrimary)
|
|
|
|
assert.True(t, emails[0].IsActivated)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsEmailUsed(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-11 07:03:30 +00:00
|
|
|
|
2022-06-15 07:02:00 +00:00
|
|
|
isExist, _ := user_model.IsEmailUsed(db.DefaultContext, "")
|
2021-11-11 07:03:30 +00:00
|
|
|
assert.True(t, isExist)
|
2022-06-15 07:02:00 +00:00
|
|
|
isExist, _ = user_model.IsEmailUsed(db.DefaultContext, "user11@example.com")
|
2021-11-11 07:03:30 +00:00
|
|
|
assert.True(t, isExist)
|
2022-06-15 07:02:00 +00:00
|
|
|
isExist, _ = user_model.IsEmailUsed(db.DefaultContext, "user1234567890@example.com")
|
2021-11-11 07:03:30 +00:00
|
|
|
assert.False(t, isExist)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddEmailAddress(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-11 07:03:30 +00:00
|
|
|
|
2022-06-15 07:02:00 +00:00
|
|
|
assert.NoError(t, user_model.AddEmailAddress(db.DefaultContext, &user_model.EmailAddress{
|
2021-11-11 07:03:30 +00:00
|
|
|
Email: "user1234567890@example.com",
|
|
|
|
LowerEmail: "user1234567890@example.com",
|
|
|
|
IsPrimary: true,
|
|
|
|
IsActivated: true,
|
|
|
|
}))
|
|
|
|
|
|
|
|
// ErrEmailAlreadyUsed
|
2022-06-15 07:02:00 +00:00
|
|
|
err := user_model.AddEmailAddress(db.DefaultContext, &user_model.EmailAddress{
|
2021-11-11 07:03:30 +00:00
|
|
|
Email: "user1234567890@example.com",
|
|
|
|
LowerEmail: "user1234567890@example.com",
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2022-06-15 07:02:00 +00:00
|
|
|
assert.True(t, user_model.IsErrEmailAlreadyUsed(err))
|
2021-11-11 07:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddEmailAddresses(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-11 07:03:30 +00:00
|
|
|
|
|
|
|
// insert multiple email address
|
2022-06-15 07:02:00 +00:00
|
|
|
emails := make([]*user_model.EmailAddress, 2)
|
|
|
|
emails[0] = &user_model.EmailAddress{
|
2021-11-11 07:03:30 +00:00
|
|
|
Email: "user1234@example.com",
|
|
|
|
LowerEmail: "user1234@example.com",
|
|
|
|
IsActivated: true,
|
|
|
|
}
|
2022-06-15 07:02:00 +00:00
|
|
|
emails[1] = &user_model.EmailAddress{
|
2021-11-11 07:03:30 +00:00
|
|
|
Email: "user5678@example.com",
|
|
|
|
LowerEmail: "user5678@example.com",
|
|
|
|
IsActivated: true,
|
|
|
|
}
|
2023-09-14 17:09:32 +00:00
|
|
|
assert.NoError(t, user_model.AddEmailAddresses(db.DefaultContext, emails))
|
2021-11-11 07:03:30 +00:00
|
|
|
|
|
|
|
// ErrEmailAlreadyUsed
|
2023-09-14 17:09:32 +00:00
|
|
|
err := user_model.AddEmailAddresses(db.DefaultContext, emails)
|
2021-11-11 07:03:30 +00:00
|
|
|
assert.Error(t, err)
|
2022-06-15 07:02:00 +00:00
|
|
|
assert.True(t, user_model.IsErrEmailAlreadyUsed(err))
|
2021-11-11 07:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteEmailAddress(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-11 07:03:30 +00:00
|
|
|
|
2023-09-14 17:09:32 +00:00
|
|
|
assert.NoError(t, user_model.DeleteEmailAddress(db.DefaultContext, &user_model.EmailAddress{
|
2021-11-11 07:03:30 +00:00
|
|
|
UID: int64(1),
|
|
|
|
ID: int64(33),
|
|
|
|
Email: "user1-2@example.com",
|
|
|
|
LowerEmail: "user1-2@example.com",
|
|
|
|
}))
|
|
|
|
|
2023-09-14 17:09:32 +00:00
|
|
|
assert.NoError(t, user_model.DeleteEmailAddress(db.DefaultContext, &user_model.EmailAddress{
|
2021-11-11 07:03:30 +00:00
|
|
|
UID: int64(1),
|
|
|
|
Email: "user1-3@example.com",
|
|
|
|
LowerEmail: "user1-3@example.com",
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Email address does not exist
|
2023-09-14 17:09:32 +00:00
|
|
|
err := user_model.DeleteEmailAddress(db.DefaultContext, &user_model.EmailAddress{
|
2021-11-11 07:03:30 +00:00
|
|
|
UID: int64(1),
|
|
|
|
Email: "user1234567890@example.com",
|
|
|
|
LowerEmail: "user1234567890@example.com",
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteEmailAddresses(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-11 07:03:30 +00:00
|
|
|
|
|
|
|
// delete multiple email address
|
2022-06-15 07:02:00 +00:00
|
|
|
emails := make([]*user_model.EmailAddress, 2)
|
|
|
|
emails[0] = &user_model.EmailAddress{
|
2021-11-11 07:03:30 +00:00
|
|
|
UID: int64(2),
|
|
|
|
ID: int64(3),
|
|
|
|
Email: "user2@example.com",
|
|
|
|
LowerEmail: "user2@example.com",
|
|
|
|
}
|
2022-06-15 07:02:00 +00:00
|
|
|
emails[1] = &user_model.EmailAddress{
|
2021-11-11 07:03:30 +00:00
|
|
|
UID: int64(2),
|
|
|
|
Email: "user2-2@example.com",
|
|
|
|
LowerEmail: "user2-2@example.com",
|
|
|
|
}
|
2023-09-14 17:09:32 +00:00
|
|
|
assert.NoError(t, user_model.DeleteEmailAddresses(db.DefaultContext, emails))
|
2021-11-11 07:03:30 +00:00
|
|
|
|
|
|
|
// ErrEmailAlreadyUsed
|
2023-09-14 17:09:32 +00:00
|
|
|
err := user_model.DeleteEmailAddresses(db.DefaultContext, emails)
|
2021-11-11 07:03:30 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2021-11-28 14:11:58 +00:00
|
|
|
|
|
|
|
func TestMakeEmailPrimary(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-06-15 07:02:00 +00:00
|
|
|
email := &user_model.EmailAddress{
|
2021-11-28 14:11:58 +00:00
|
|
|
Email: "user567890@example.com",
|
|
|
|
}
|
2023-09-14 17:09:32 +00:00
|
|
|
err := user_model.MakeEmailPrimary(db.DefaultContext, email)
|
2021-11-28 14:11:58 +00:00
|
|
|
assert.Error(t, err)
|
2022-06-15 07:02:00 +00:00
|
|
|
assert.EqualError(t, err, user_model.ErrEmailAddressNotExist{Email: email.Email}.Error())
|
2021-11-28 14:11:58 +00:00
|
|
|
|
2022-06-15 07:02:00 +00:00
|
|
|
email = &user_model.EmailAddress{
|
2021-11-28 14:11:58 +00:00
|
|
|
Email: "user11@example.com",
|
|
|
|
}
|
2023-09-14 17:09:32 +00:00
|
|
|
err = user_model.MakeEmailPrimary(db.DefaultContext, email)
|
2021-11-28 14:11:58 +00:00
|
|
|
assert.Error(t, err)
|
2022-06-15 07:02:00 +00:00
|
|
|
assert.EqualError(t, err, user_model.ErrEmailNotActivated.Error())
|
2021-11-28 14:11:58 +00:00
|
|
|
|
2022-06-15 07:02:00 +00:00
|
|
|
email = &user_model.EmailAddress{
|
2021-11-28 14:11:58 +00:00
|
|
|
Email: "user9999999@example.com",
|
|
|
|
}
|
2023-09-14 17:09:32 +00:00
|
|
|
err = user_model.MakeEmailPrimary(db.DefaultContext, email)
|
2021-11-28 14:11:58 +00:00
|
|
|
assert.Error(t, err)
|
2022-06-15 07:02:00 +00:00
|
|
|
assert.True(t, user_model.IsErrUserNotExist(err))
|
2021-11-28 14:11:58 +00:00
|
|
|
|
2022-06-15 07:02:00 +00:00
|
|
|
email = &user_model.EmailAddress{
|
2021-11-28 14:11:58 +00:00
|
|
|
Email: "user101@example.com",
|
|
|
|
}
|
2023-09-14 17:09:32 +00:00
|
|
|
err = user_model.MakeEmailPrimary(db.DefaultContext, email)
|
2021-11-28 14:11:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-12-03 02:48:26 +00:00
|
|
|
user, _ := user_model.GetUserByID(db.DefaultContext, int64(10))
|
2021-11-28 14:11:58 +00:00
|
|
|
assert.Equal(t, "user101@example.com", user.Email)
|
|
|
|
}
|
|
|
|
|
[GITEA] Allow changing the email address before activation
During registration, one may be required to give their email address, to
be verified and activated later. However, if one makes a mistake, a
typo, they may end up with an account that cannot be activated due to
having a wrong email address.
They can still log in, but not change the email address, thus, no way to
activate it without help from an administrator.
To remedy this issue, lets allow changing the email address for logged
in, but not activated users.
This fixes gitea#17785.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit aaaece28e4c6a8980cef932e224e84933d7c9262)
(cherry picked from commit 639dafabec0a5c1f943b44ca02f72c5ba2fc5e10)
(cherry picked from commit d699c12cebea7dbbda950ae257a46d53c39f22ea)
[GITEA] Allow changing the email address before activation (squash) cache is always active
This needs to be revisited because the MailResendLimit is not enforced
and turns out to not be tested.
See e7cb8da2a8 * Always enable caches (#28527)
(cherry picked from commit 43ded8ee30ab5c7a40a456600cdaa8a0fbdccec2)
Rate limit pre-activation email change separately
Changing the email address before any email address is activated should
be subject to a different rate limit than the normal activation email
resending. If there's only one rate limit for both, then if a newly
signed up quickly discovers they gave a wrong email address, they'd have
to wait three minutes to change it.
With the two separate limits, they don't - but they'll have to wait
three minutes before they can change the email address again.
The downside of this setup is that a malicious actor can alternate
between resending and changing the email address (to something like
`user+$idx@domain`, delivered to the same inbox) to effectively halving
the rate limit. I do not think there's a better solution, and this feels
like such a small attack surface that I'd deem it acceptable.
The way the code works after this change is that `ActivatePost` will now
check the `MailChangeLimit_user` key rather than `MailResendLimit_user`,
and if we're within the limit, it will set `MailChangedJustNow_user`. The
`Activate` method - which sends the activation email, whether it is a
normal resend, or one following an email change - will check
`MailChangedJustNow_user`, and if it is set, it will check the rate
limit against `MailChangedLimit_user`, otherwise against
`MailResendLimit_user`, and then will delete the
`MailChangedJustNow_user` key from the cache.
Fixes #2040.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e35d2af2e56f4ecb3a4f6d1109d02c8aa1a6d182)
(cherry picked from commit 03989418a70d3445e0edada7fbe5a4151d7836b1)
(cherry picked from commit f50e0dfe5e90d6a31c5b59e687580e8b2725c22b)
2023-12-07 11:57:16 +00:00
|
|
|
func TestReplaceInactivePrimaryEmail(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
email := &user_model.EmailAddress{
|
|
|
|
Email: "user9999999@example.com",
|
|
|
|
UID: 9999999,
|
|
|
|
}
|
|
|
|
err := user_model.ReplaceInactivePrimaryEmail(db.DefaultContext, "user10@example.com", email)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, user_model.IsErrUserNotExist(err))
|
|
|
|
|
|
|
|
email = &user_model.EmailAddress{
|
|
|
|
Email: "user201@example.com",
|
|
|
|
UID: 10,
|
|
|
|
}
|
|
|
|
err = user_model.ReplaceInactivePrimaryEmail(db.DefaultContext, "user10@example.com", email)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 10})
|
|
|
|
assert.Equal(t, "user201@example.com", user.Email)
|
|
|
|
}
|
|
|
|
|
2021-11-28 14:11:58 +00:00
|
|
|
func TestActivate(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-06-15 07:02:00 +00:00
|
|
|
email := &user_model.EmailAddress{
|
2021-11-28 14:11:58 +00:00
|
|
|
ID: int64(1),
|
|
|
|
UID: int64(1),
|
|
|
|
Email: "user11@example.com",
|
|
|
|
}
|
2023-09-14 17:09:32 +00:00
|
|
|
assert.NoError(t, user_model.ActivateEmail(db.DefaultContext, email))
|
2021-11-28 14:11:58 +00:00
|
|
|
|
2023-09-14 17:09:32 +00:00
|
|
|
emails, _ := user_model.GetEmailAddresses(db.DefaultContext, int64(1))
|
2021-11-28 14:11:58 +00:00
|
|
|
assert.Len(t, emails, 3)
|
|
|
|
assert.True(t, emails[0].IsActivated)
|
|
|
|
assert.True(t, emails[0].IsPrimary)
|
|
|
|
assert.False(t, emails[1].IsPrimary)
|
|
|
|
assert.True(t, emails[2].IsActivated)
|
|
|
|
assert.False(t, emails[2].IsPrimary)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListEmails(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
// Must find all users and their emails
|
2022-06-15 07:02:00 +00:00
|
|
|
opts := &user_model.SearchEmailOptions{
|
2021-11-28 14:11:58 +00:00
|
|
|
ListOptions: db.ListOptions{
|
|
|
|
PageSize: 10000,
|
|
|
|
},
|
|
|
|
}
|
2023-09-14 17:09:32 +00:00
|
|
|
emails, count, err := user_model.SearchEmails(db.DefaultContext, opts)
|
2021-11-28 14:11:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, int64(0), count)
|
|
|
|
assert.True(t, count > 5)
|
|
|
|
|
2022-06-15 07:02:00 +00:00
|
|
|
contains := func(match func(s *user_model.SearchEmailResult) bool) bool {
|
2021-11-28 14:11:58 +00:00
|
|
|
for _, v := range emails {
|
|
|
|
if match(v) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-06-15 07:02:00 +00:00
|
|
|
assert.True(t, contains(func(s *user_model.SearchEmailResult) bool { return s.UID == 18 }))
|
2023-09-14 02:59:53 +00:00
|
|
|
// 'org3' is an organization
|
2022-06-15 07:02:00 +00:00
|
|
|
assert.False(t, contains(func(s *user_model.SearchEmailResult) bool { return s.UID == 3 }))
|
2021-11-28 14:11:58 +00:00
|
|
|
|
|
|
|
// Must find no records
|
2022-06-15 07:02:00 +00:00
|
|
|
opts = &user_model.SearchEmailOptions{Keyword: "NOTFOUND"}
|
2023-09-14 17:09:32 +00:00
|
|
|
emails, count, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2021-11-28 14:11:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(0), count)
|
|
|
|
|
|
|
|
// Must find users 'user2', 'user28', etc.
|
2022-06-15 07:02:00 +00:00
|
|
|
opts = &user_model.SearchEmailOptions{Keyword: "user2"}
|
2023-09-14 17:09:32 +00:00
|
|
|
emails, count, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2021-11-28 14:11:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, int64(0), count)
|
2022-06-15 07:02:00 +00:00
|
|
|
assert.True(t, contains(func(s *user_model.SearchEmailResult) bool { return s.UID == 2 }))
|
|
|
|
assert.True(t, contains(func(s *user_model.SearchEmailResult) bool { return s.UID == 27 }))
|
2021-11-28 14:11:58 +00:00
|
|
|
|
|
|
|
// Must find only primary addresses (i.e. from the `user` table)
|
2022-06-15 07:02:00 +00:00
|
|
|
opts = &user_model.SearchEmailOptions{IsPrimary: util.OptionalBoolTrue}
|
2023-09-14 17:09:32 +00:00
|
|
|
emails, _, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2021-11-28 14:11:58 +00:00
|
|
|
assert.NoError(t, err)
|
2022-06-15 07:02:00 +00:00
|
|
|
assert.True(t, contains(func(s *user_model.SearchEmailResult) bool { return s.IsPrimary }))
|
|
|
|
assert.False(t, contains(func(s *user_model.SearchEmailResult) bool { return !s.IsPrimary }))
|
2021-11-28 14:11:58 +00:00
|
|
|
|
|
|
|
// Must find only inactive addresses (i.e. not validated)
|
2022-06-15 07:02:00 +00:00
|
|
|
opts = &user_model.SearchEmailOptions{IsActivated: util.OptionalBoolFalse}
|
2023-09-14 17:09:32 +00:00
|
|
|
emails, _, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2021-11-28 14:11:58 +00:00
|
|
|
assert.NoError(t, err)
|
2022-06-15 07:02:00 +00:00
|
|
|
assert.True(t, contains(func(s *user_model.SearchEmailResult) bool { return !s.IsActivated }))
|
|
|
|
assert.False(t, contains(func(s *user_model.SearchEmailResult) bool { return s.IsActivated }))
|
2021-11-28 14:11:58 +00:00
|
|
|
|
|
|
|
// Must find more than one page, but retrieve only one
|
2022-06-15 07:02:00 +00:00
|
|
|
opts = &user_model.SearchEmailOptions{
|
2021-11-28 14:11:58 +00:00
|
|
|
ListOptions: db.ListOptions{
|
|
|
|
PageSize: 5,
|
|
|
|
Page: 1,
|
|
|
|
},
|
|
|
|
}
|
2023-09-14 17:09:32 +00:00
|
|
|
emails, count, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2021-11-28 14:11:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, emails, 5)
|
|
|
|
assert.Greater(t, count, int64(len(emails)))
|
|
|
|
}
|
2022-03-14 17:39:54 +00:00
|
|
|
|
|
|
|
func TestEmailAddressValidate(t *testing.T) {
|
|
|
|
kases := map[string]error{
|
|
|
|
"abc@gmail.com": nil,
|
|
|
|
"132@hotmail.com": nil,
|
|
|
|
"1-3-2@test.org": nil,
|
|
|
|
"1.3.2@test.org": nil,
|
|
|
|
"a_123@test.org.cn": nil,
|
|
|
|
`first.last@iana.org`: nil,
|
|
|
|
`first!last@iana.org`: nil,
|
|
|
|
`first#last@iana.org`: nil,
|
|
|
|
`first$last@iana.org`: nil,
|
|
|
|
`first%last@iana.org`: nil,
|
|
|
|
`first&last@iana.org`: nil,
|
|
|
|
`first'last@iana.org`: nil,
|
|
|
|
`first*last@iana.org`: nil,
|
|
|
|
`first+last@iana.org`: nil,
|
|
|
|
`first/last@iana.org`: nil,
|
|
|
|
`first=last@iana.org`: nil,
|
|
|
|
`first?last@iana.org`: nil,
|
|
|
|
`first^last@iana.org`: nil,
|
|
|
|
"first`last@iana.org": nil,
|
|
|
|
`first{last@iana.org`: nil,
|
|
|
|
`first|last@iana.org`: nil,
|
|
|
|
`first}last@iana.org`: nil,
|
|
|
|
`first~last@iana.org`: nil,
|
2022-06-15 07:02:00 +00:00
|
|
|
`first;last@iana.org`: user_model.ErrEmailCharIsNotSupported{`first;last@iana.org`},
|
|
|
|
".233@qq.com": user_model.ErrEmailInvalid{".233@qq.com"},
|
2022-10-12 02:44:09 +00:00
|
|
|
"!233@qq.com": nil,
|
|
|
|
"#233@qq.com": nil,
|
|
|
|
"$233@qq.com": nil,
|
|
|
|
"%233@qq.com": nil,
|
|
|
|
"&233@qq.com": nil,
|
|
|
|
"'233@qq.com": nil,
|
|
|
|
"*233@qq.com": nil,
|
|
|
|
"+233@qq.com": nil,
|
|
|
|
"-233@qq.com": user_model.ErrEmailInvalid{"-233@qq.com"},
|
|
|
|
"/233@qq.com": nil,
|
|
|
|
"=233@qq.com": nil,
|
|
|
|
"?233@qq.com": nil,
|
|
|
|
"^233@qq.com": nil,
|
|
|
|
"_233@qq.com": nil,
|
|
|
|
"`233@qq.com": nil,
|
|
|
|
"{233@qq.com": nil,
|
|
|
|
"|233@qq.com": nil,
|
|
|
|
"}233@qq.com": nil,
|
|
|
|
"~233@qq.com": nil,
|
2022-06-15 07:02:00 +00:00
|
|
|
";233@qq.com": user_model.ErrEmailCharIsNotSupported{";233@qq.com"},
|
|
|
|
"Foo <foo@bar.com>": user_model.ErrEmailCharIsNotSupported{"Foo <foo@bar.com>"},
|
|
|
|
string([]byte{0xE2, 0x84, 0xAA}): user_model.ErrEmailCharIsNotSupported{string([]byte{0xE2, 0x84, 0xAA})},
|
2022-03-14 17:39:54 +00:00
|
|
|
}
|
|
|
|
for kase, err := range kases {
|
|
|
|
t.Run(kase, func(t *testing.T) {
|
2022-06-15 07:02:00 +00:00
|
|
|
assert.EqualValues(t, err, user_model.ValidateEmail(kase))
|
2022-03-14 17:39:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-11-16 23:17:40 +00:00
|
|
|
|
|
|
|
func TestGetActivatedEmailAddresses(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
UID int64
|
|
|
|
expected []*user_model.ActivatedEmailAddress
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
UID: 1,
|
|
|
|
expected: []*user_model.ActivatedEmailAddress{{ID: 9, Email: "user1@example.com"}, {ID: 33, Email: "user1-2@example.com"}, {ID: 34, Email: "user1-3@example.com"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UID: 2,
|
|
|
|
expected: []*user_model.ActivatedEmailAddress{{ID: 3, Email: "user2@example.com"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UID: 4,
|
|
|
|
expected: []*user_model.ActivatedEmailAddress{{ID: 11, Email: "user4@example.com"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UID: 11,
|
|
|
|
expected: []*user_model.ActivatedEmailAddress{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(fmt.Sprintf("User %d", testCase.UID), func(t *testing.T) {
|
|
|
|
emails, err := user_model.GetActivatedEmailAddresses(db.DefaultContext, testCase.UID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testCase.expected, emails)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|