2021-11-11 08:03:30 +01:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-11-11 08:03:30 +01:00
|
|
|
|
2022-06-15 09:02:00 +02:00
|
|
|
package user_test
|
2021-11-11 08:03:30 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 15:36:47 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2022-06-15 09:02:00 +02:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2024-02-29 19:52:49 +01:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2021-11-11 08:03:30 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetEmailAddresses(t *testing.T) {
|
2021-11-12 15:36:47 +01:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-11 08:03:30 +01:00
|
|
|
|
2023-09-14 19:09:32 +02:00
|
|
|
emails, _ := user_model.GetEmailAddresses(db.DefaultContext, int64(1))
|
2021-11-11 08:03:30 +01: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 19:09:32 +02:00
|
|
|
emails, _ = user_model.GetEmailAddresses(db.DefaultContext, int64(2))
|
2021-11-11 08:03:30 +01: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 15:36:47 +01:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-11 08:03:30 +01:00
|
|
|
|
2022-06-15 09:02:00 +02:00
|
|
|
isExist, _ := user_model.IsEmailUsed(db.DefaultContext, "")
|
2021-11-11 08:03:30 +01:00
|
|
|
assert.True(t, isExist)
|
2022-06-15 09:02:00 +02:00
|
|
|
isExist, _ = user_model.IsEmailUsed(db.DefaultContext, "user11@example.com")
|
2021-11-11 08:03:30 +01:00
|
|
|
assert.True(t, isExist)
|
2022-06-15 09:02:00 +02:00
|
|
|
isExist, _ = user_model.IsEmailUsed(db.DefaultContext, "user1234567890@example.com")
|
2021-11-11 08:03:30 +01:00
|
|
|
assert.False(t, isExist)
|
|
|
|
}
|
|
|
|
|
2021-11-28 15:11:58 +01:00
|
|
|
func TestMakeEmailPrimary(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2024-02-27 11:55:13 +01:00
|
|
|
err := user_model.MakeActiveEmailPrimary(db.DefaultContext, 9999999)
|
2021-11-28 15:11:58 +01:00
|
|
|
assert.Error(t, err)
|
2024-02-27 11:55:13 +01:00
|
|
|
assert.ErrorIs(t, err, user_model.ErrEmailAddressNotExist{})
|
2021-11-28 15:11:58 +01:00
|
|
|
|
2024-02-27 11:55:13 +01:00
|
|
|
email := unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{Email: "user11@example.com"})
|
|
|
|
err = user_model.MakeActiveEmailPrimary(db.DefaultContext, email.ID)
|
2021-11-28 15:11:58 +01:00
|
|
|
assert.Error(t, err)
|
2024-02-27 11:55:13 +01:00
|
|
|
assert.ErrorIs(t, err, user_model.ErrEmailAddressNotExist{}) // inactive email is considered as not exist for "MakeActiveEmailPrimary"
|
2021-11-28 15:11:58 +01:00
|
|
|
|
2024-02-27 11:55:13 +01:00
|
|
|
email = unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{Email: "user9999999@example.com"})
|
|
|
|
err = user_model.MakeActiveEmailPrimary(db.DefaultContext, email.ID)
|
2021-11-28 15:11:58 +01:00
|
|
|
assert.Error(t, err)
|
2022-06-15 09:02:00 +02:00
|
|
|
assert.True(t, user_model.IsErrUserNotExist(err))
|
2021-11-28 15:11:58 +01:00
|
|
|
|
2024-02-27 11:55:13 +01:00
|
|
|
email = unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{Email: "user101@example.com"})
|
|
|
|
err = user_model.MakeActiveEmailPrimary(db.DefaultContext, email.ID)
|
2021-11-28 15:11:58 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-12-03 03:48:26 +01:00
|
|
|
user, _ := user_model.GetUserByID(db.DefaultContext, int64(10))
|
2021-11-28 15:11:58 +01:00
|
|
|
assert.Equal(t, "user101@example.com", user.Email)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActivate(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-06-15 09:02:00 +02:00
|
|
|
email := &user_model.EmailAddress{
|
2021-11-28 15:11:58 +01:00
|
|
|
ID: int64(1),
|
|
|
|
UID: int64(1),
|
|
|
|
Email: "user11@example.com",
|
|
|
|
}
|
2023-09-14 19:09:32 +02:00
|
|
|
assert.NoError(t, user_model.ActivateEmail(db.DefaultContext, email))
|
2021-11-28 15:11:58 +01:00
|
|
|
|
2023-09-14 19:09:32 +02:00
|
|
|
emails, _ := user_model.GetEmailAddresses(db.DefaultContext, int64(1))
|
2021-11-28 15:11:58 +01: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 09:02:00 +02:00
|
|
|
opts := &user_model.SearchEmailOptions{
|
2021-11-28 15:11:58 +01:00
|
|
|
ListOptions: db.ListOptions{
|
|
|
|
PageSize: 10000,
|
|
|
|
},
|
|
|
|
}
|
2023-09-14 19:09:32 +02:00
|
|
|
emails, count, err := user_model.SearchEmails(db.DefaultContext, opts)
|
2021-11-28 15:11:58 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, int64(0), count)
|
|
|
|
assert.True(t, count > 5)
|
|
|
|
|
2022-06-15 09:02:00 +02:00
|
|
|
contains := func(match func(s *user_model.SearchEmailResult) bool) bool {
|
2021-11-28 15:11:58 +01:00
|
|
|
for _, v := range emails {
|
|
|
|
if match(v) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-06-15 09:02:00 +02:00
|
|
|
assert.True(t, contains(func(s *user_model.SearchEmailResult) bool { return s.UID == 18 }))
|
2023-09-14 04:59:53 +02:00
|
|
|
// 'org3' is an organization
|
2022-06-15 09:02:00 +02:00
|
|
|
assert.False(t, contains(func(s *user_model.SearchEmailResult) bool { return s.UID == 3 }))
|
2021-11-28 15:11:58 +01:00
|
|
|
|
|
|
|
// Must find no records
|
2022-06-15 09:02:00 +02:00
|
|
|
opts = &user_model.SearchEmailOptions{Keyword: "NOTFOUND"}
|
2023-09-14 19:09:32 +02:00
|
|
|
emails, count, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2021-11-28 15:11:58 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(0), count)
|
|
|
|
|
|
|
|
// Must find users 'user2', 'user28', etc.
|
2022-06-15 09:02:00 +02:00
|
|
|
opts = &user_model.SearchEmailOptions{Keyword: "user2"}
|
2023-09-14 19:09:32 +02:00
|
|
|
emails, count, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2021-11-28 15:11:58 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, int64(0), count)
|
2022-06-15 09:02:00 +02: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 15:11:58 +01:00
|
|
|
|
|
|
|
// Must find only primary addresses (i.e. from the `user` table)
|
2024-02-29 19:52:49 +01:00
|
|
|
opts = &user_model.SearchEmailOptions{IsPrimary: optional.Some(true)}
|
2023-09-14 19:09:32 +02:00
|
|
|
emails, _, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2021-11-28 15:11:58 +01:00
|
|
|
assert.NoError(t, err)
|
2022-06-15 09:02:00 +02: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 15:11:58 +01:00
|
|
|
|
|
|
|
// Must find only inactive addresses (i.e. not validated)
|
2024-02-29 19:52:49 +01:00
|
|
|
opts = &user_model.SearchEmailOptions{IsActivated: optional.Some(false)}
|
2023-09-14 19:09:32 +02:00
|
|
|
emails, _, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2021-11-28 15:11:58 +01:00
|
|
|
assert.NoError(t, err)
|
2022-06-15 09:02:00 +02: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 15:11:58 +01:00
|
|
|
|
|
|
|
// Must find more than one page, but retrieve only one
|
2022-06-15 09:02:00 +02:00
|
|
|
opts = &user_model.SearchEmailOptions{
|
2021-11-28 15:11:58 +01:00
|
|
|
ListOptions: db.ListOptions{
|
|
|
|
PageSize: 5,
|
|
|
|
Page: 1,
|
|
|
|
},
|
|
|
|
}
|
2023-09-14 19:09:32 +02:00
|
|
|
emails, count, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2021-11-28 15:11:58 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, emails, 5)
|
|
|
|
assert.Greater(t, count, int64(len(emails)))
|
|
|
|
}
|
2022-03-14 18:39:54 +01: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 09:02:00 +02:00
|
|
|
`first;last@iana.org`: user_model.ErrEmailCharIsNotSupported{`first;last@iana.org`},
|
|
|
|
".233@qq.com": user_model.ErrEmailInvalid{".233@qq.com"},
|
2022-10-12 04:44:09 +02: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 09:02:00 +02: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 18:39:54 +01:00
|
|
|
}
|
|
|
|
for kase, err := range kases {
|
|
|
|
t.Run(kase, func(t *testing.T) {
|
2022-06-15 09:02:00 +02:00
|
|
|
assert.EqualValues(t, err, user_model.ValidateEmail(kase))
|
2022-03-14 18:39:54 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|