2021-11-11 02:03:30 -05:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-11-11 02:03:30 -05:00
|
|
|
|
2022-06-15 03:02:00 -04:00
|
|
|
package user_test
|
2021-11-11 02:03:30 -05:00
|
|
|
|
|
|
|
import (
|
2023-11-16 18:17:40 -05:00
|
|
|
"fmt"
|
2021-11-11 02:03:30 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2022-06-15 03:02:00 -04:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2024-02-29 13:52:49 -05:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2021-11-11 02:03:30 -05:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 15:41:10 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-11-11 02:03:30 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetEmailAddresses(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-11 02:03:30 -05:00
|
|
|
|
2023-09-14 13:09:32 -04:00
|
|
|
emails, _ := user_model.GetEmailAddresses(db.DefaultContext, int64(1))
|
2021-11-11 02:03:30 -05: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 13:09:32 -04:00
|
|
|
emails, _ = user_model.GetEmailAddresses(db.DefaultContext, int64(2))
|
2021-11-11 02:03:30 -05:00
|
|
|
if assert.Len(t, emails, 2) {
|
|
|
|
assert.True(t, emails[0].IsPrimary)
|
|
|
|
assert.True(t, emails[0].IsActivated)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsEmailUsed(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-11 02:03:30 -05:00
|
|
|
|
2022-06-15 03:02:00 -04:00
|
|
|
isExist, _ := user_model.IsEmailUsed(db.DefaultContext, "")
|
2021-11-11 02:03:30 -05:00
|
|
|
assert.True(t, isExist)
|
2022-06-15 03:02:00 -04:00
|
|
|
isExist, _ = user_model.IsEmailUsed(db.DefaultContext, "user11@example.com")
|
2021-11-11 02:03:30 -05:00
|
|
|
assert.True(t, isExist)
|
2022-06-15 03:02:00 -04:00
|
|
|
isExist, _ = user_model.IsEmailUsed(db.DefaultContext, "user1234567890@example.com")
|
2021-11-11 02:03:30 -05:00
|
|
|
assert.False(t, isExist)
|
|
|
|
}
|
|
|
|
|
2021-11-28 09:11:58 -05:00
|
|
|
func TestActivate(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-28 09:11:58 -05:00
|
|
|
|
2022-06-15 03:02:00 -04:00
|
|
|
email := &user_model.EmailAddress{
|
2021-11-28 09:11:58 -05:00
|
|
|
ID: int64(1),
|
|
|
|
UID: int64(1),
|
|
|
|
Email: "user11@example.com",
|
|
|
|
}
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, user_model.ActivateEmail(db.DefaultContext, email))
|
2021-11-28 09:11:58 -05:00
|
|
|
|
2023-09-14 13:09:32 -04:00
|
|
|
emails, _ := user_model.GetEmailAddresses(db.DefaultContext, int64(1))
|
2021-11-28 09:11:58 -05: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) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-28 09:11:58 -05:00
|
|
|
|
|
|
|
// Must find all users and their emails
|
2022-06-15 03:02:00 -04:00
|
|
|
opts := &user_model.SearchEmailOptions{
|
2021-11-28 09:11:58 -05:00
|
|
|
ListOptions: db.ListOptions{
|
|
|
|
PageSize: 10000,
|
|
|
|
},
|
|
|
|
}
|
2023-09-14 13:09:32 -04:00
|
|
|
emails, count, err := user_model.SearchEmails(db.DefaultContext, opts)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Greater(t, count, int64(5))
|
2021-11-28 09:11:58 -05:00
|
|
|
|
2022-06-15 03:02:00 -04:00
|
|
|
contains := func(match func(s *user_model.SearchEmailResult) bool) bool {
|
2021-11-28 09:11:58 -05:00
|
|
|
for _, v := range emails {
|
|
|
|
if match(v) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-06-15 03:02:00 -04:00
|
|
|
assert.True(t, contains(func(s *user_model.SearchEmailResult) bool { return s.UID == 18 }))
|
2023-09-13 22:59:53 -04:00
|
|
|
// 'org3' is an organization
|
2022-06-15 03:02:00 -04:00
|
|
|
assert.False(t, contains(func(s *user_model.SearchEmailResult) bool { return s.UID == 3 }))
|
2021-11-28 09:11:58 -05:00
|
|
|
|
|
|
|
// Must find no records
|
2022-06-15 03:02:00 -04:00
|
|
|
opts = &user_model.SearchEmailOptions{Keyword: "NOTFOUND"}
|
2023-09-14 13:09:32 -04:00
|
|
|
emails, count, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-11-28 09:11:58 -05:00
|
|
|
assert.Equal(t, int64(0), count)
|
|
|
|
|
|
|
|
// Must find users 'user2', 'user28', etc.
|
2022-06-15 03:02:00 -04:00
|
|
|
opts = &user_model.SearchEmailOptions{Keyword: "user2"}
|
2023-09-14 13:09:32 -04:00
|
|
|
emails, count, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-11-28 09:11:58 -05:00
|
|
|
assert.NotEqual(t, int64(0), count)
|
2022-06-15 03:02:00 -04: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 09:11:58 -05:00
|
|
|
|
|
|
|
// Must find only primary addresses (i.e. from the `user` table)
|
2024-02-29 13:52:49 -05:00
|
|
|
opts = &user_model.SearchEmailOptions{IsPrimary: optional.Some(true)}
|
2023-09-14 13:09:32 -04:00
|
|
|
emails, _, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-06-15 03:02:00 -04: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 09:11:58 -05:00
|
|
|
|
|
|
|
// Must find only inactive addresses (i.e. not validated)
|
2024-02-29 13:52:49 -05:00
|
|
|
opts = &user_model.SearchEmailOptions{IsActivated: optional.Some(false)}
|
2023-09-14 13:09:32 -04:00
|
|
|
emails, _, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-06-15 03:02:00 -04: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 09:11:58 -05:00
|
|
|
|
|
|
|
// Must find more than one page, but retrieve only one
|
2022-06-15 03:02:00 -04:00
|
|
|
opts = &user_model.SearchEmailOptions{
|
2021-11-28 09:11:58 -05:00
|
|
|
ListOptions: db.ListOptions{
|
|
|
|
PageSize: 5,
|
|
|
|
Page: 1,
|
|
|
|
},
|
|
|
|
}
|
2023-09-14 13:09:32 -04:00
|
|
|
emails, count, err = user_model.SearchEmails(db.DefaultContext, opts)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-11-28 09:11:58 -05:00
|
|
|
assert.Len(t, emails, 5)
|
|
|
|
assert.Greater(t, count, int64(len(emails)))
|
|
|
|
}
|
2022-03-14 13:39:54 -04:00
|
|
|
|
2023-11-16 18:17:40 -05:00
|
|
|
func TestGetActivatedEmailAddresses(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2023-11-16 18:17:40 -05:00
|
|
|
|
|
|
|
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)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-11-16 18:17:40 -05:00
|
|
|
assert.Equal(t, testCase.expected, emails)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-11-20 07:31:34 -05:00
|
|
|
|
|
|
|
func TestDeletePrimaryEmailAddressOfUser(t *testing.T) {
|
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
user, err := user_model.GetUserByName(db.DefaultContext, "org3")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "org3@example.com", user.Email)
|
|
|
|
|
|
|
|
require.NoError(t, user_model.DeletePrimaryEmailAddressOfUser(db.DefaultContext, user.ID))
|
|
|
|
|
|
|
|
user, err = user_model.GetUserByName(db.DefaultContext, "org3")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, user.Email)
|
|
|
|
|
|
|
|
email, err := user_model.GetPrimaryEmailAddressOfUser(db.DefaultContext, user.ID)
|
|
|
|
assert.True(t, user_model.IsErrEmailAddressNotExist(err))
|
|
|
|
assert.Nil(t, email)
|
|
|
|
}
|