2021-11-11 12:33:30 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2017-02-04 06:50:56 +05:30
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 20:06:47 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-11 12:33:30 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-03-02 23:55:36 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
|
2017-02-04 06:50:56 +05:30
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMakeEmailPrimary(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-04 06:50:56 +05:30
|
|
|
|
2021-11-11 12:33:30 +05:30
|
|
|
email := &user_model.EmailAddress{
|
2017-02-04 06:50:56 +05:30
|
|
|
Email: "user567890@example.com",
|
|
|
|
}
|
|
|
|
err := MakeEmailPrimary(email)
|
|
|
|
assert.Error(t, err)
|
2021-11-11 12:33:30 +05:30
|
|
|
assert.EqualError(t, err, user_model.ErrEmailAddressNotExist{Email: email.Email}.Error())
|
2017-02-04 06:50:56 +05:30
|
|
|
|
2021-11-11 12:33:30 +05:30
|
|
|
email = &user_model.EmailAddress{
|
2017-02-04 06:50:56 +05:30
|
|
|
Email: "user11@example.com",
|
|
|
|
}
|
|
|
|
err = MakeEmailPrimary(email)
|
|
|
|
assert.Error(t, err)
|
2021-11-11 12:33:30 +05:30
|
|
|
assert.EqualError(t, err, user_model.ErrEmailNotActivated.Error())
|
2017-02-04 06:50:56 +05:30
|
|
|
|
2021-11-11 12:33:30 +05:30
|
|
|
email = &user_model.EmailAddress{
|
2017-02-04 06:50:56 +05:30
|
|
|
Email: "user9999999@example.com",
|
|
|
|
}
|
|
|
|
err = MakeEmailPrimary(email)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrUserNotExist(err))
|
|
|
|
|
2021-11-11 12:33:30 +05:30
|
|
|
email = &user_model.EmailAddress{
|
2017-02-04 06:50:56 +05:30
|
|
|
Email: "user101@example.com",
|
|
|
|
}
|
|
|
|
err = MakeEmailPrimary(email)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
user, _ := GetUserByID(int64(10))
|
|
|
|
assert.Equal(t, "user101@example.com", user.Email)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActivate(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-04 06:50:56 +05:30
|
|
|
|
2021-11-11 12:33:30 +05:30
|
|
|
email := &user_model.EmailAddress{
|
2017-02-04 06:50:56 +05:30
|
|
|
ID: int64(1),
|
|
|
|
UID: int64(1),
|
|
|
|
Email: "user11@example.com",
|
|
|
|
}
|
2021-11-11 12:33:30 +05:30
|
|
|
assert.NoError(t, ActivateEmail(email))
|
2017-02-04 06:50:56 +05:30
|
|
|
|
2021-11-11 12:33:30 +05:30
|
|
|
emails, _ := user_model.GetEmailAddresses(int64(1))
|
2017-02-04 06:50:56 +05:30
|
|
|
assert.Len(t, emails, 3)
|
|
|
|
assert.True(t, emails[0].IsActivated)
|
2021-06-08 09:22:51 +05:30
|
|
|
assert.True(t, emails[0].IsPrimary)
|
|
|
|
assert.False(t, emails[1].IsPrimary)
|
2017-02-04 06:50:56 +05:30
|
|
|
assert.True(t, emails[2].IsActivated)
|
2021-06-08 09:22:51 +05:30
|
|
|
assert.False(t, emails[2].IsPrimary)
|
2017-02-04 06:50:56 +05:30
|
|
|
}
|
2020-03-02 23:55:36 +05:30
|
|
|
|
|
|
|
func TestListEmails(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-03-02 23:55:36 +05:30
|
|
|
|
|
|
|
// Must find all users and their emails
|
2021-06-08 09:22:51 +05:30
|
|
|
opts := &SearchEmailOptions{
|
2021-09-24 17:02:56 +05:30
|
|
|
ListOptions: db.ListOptions{
|
2021-06-08 09:22:51 +05:30
|
|
|
PageSize: 10000,
|
|
|
|
},
|
|
|
|
}
|
2020-03-02 23:55:36 +05:30
|
|
|
emails, count, err := SearchEmails(opts)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, int64(0), count)
|
|
|
|
assert.True(t, count > 5)
|
|
|
|
|
|
|
|
contains := func(match func(s *SearchEmailResult) bool) bool {
|
|
|
|
for _, v := range emails {
|
|
|
|
if match(v) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, contains(func(s *SearchEmailResult) bool { return s.UID == 18 }))
|
|
|
|
// 'user3' is an organization
|
|
|
|
assert.False(t, contains(func(s *SearchEmailResult) bool { return s.UID == 3 }))
|
|
|
|
|
|
|
|
// Must find no records
|
|
|
|
opts = &SearchEmailOptions{Keyword: "NOTFOUND"}
|
|
|
|
emails, count, err = SearchEmails(opts)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(0), count)
|
|
|
|
|
|
|
|
// Must find users 'user2', 'user28', etc.
|
|
|
|
opts = &SearchEmailOptions{Keyword: "user2"}
|
|
|
|
emails, count, err = SearchEmails(opts)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, int64(0), count)
|
|
|
|
assert.True(t, contains(func(s *SearchEmailResult) bool { return s.UID == 2 }))
|
|
|
|
assert.True(t, contains(func(s *SearchEmailResult) bool { return s.UID == 27 }))
|
|
|
|
|
|
|
|
// Must find only primary addresses (i.e. from the `user` table)
|
|
|
|
opts = &SearchEmailOptions{IsPrimary: util.OptionalBoolTrue}
|
2021-11-18 07:03:06 +05:30
|
|
|
emails, _, err = SearchEmails(opts)
|
2020-03-02 23:55:36 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, contains(func(s *SearchEmailResult) bool { return s.IsPrimary }))
|
|
|
|
assert.False(t, contains(func(s *SearchEmailResult) bool { return !s.IsPrimary }))
|
|
|
|
|
|
|
|
// Must find only inactive addresses (i.e. not validated)
|
|
|
|
opts = &SearchEmailOptions{IsActivated: util.OptionalBoolFalse}
|
2021-11-18 07:03:06 +05:30
|
|
|
emails, _, err = SearchEmails(opts)
|
2020-03-02 23:55:36 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, contains(func(s *SearchEmailResult) bool { return !s.IsActivated }))
|
|
|
|
assert.False(t, contains(func(s *SearchEmailResult) bool { return s.IsActivated }))
|
|
|
|
|
|
|
|
// Must find more than one page, but retrieve only one
|
|
|
|
opts = &SearchEmailOptions{
|
2021-09-24 17:02:56 +05:30
|
|
|
ListOptions: db.ListOptions{
|
2020-03-02 23:55:36 +05:30
|
|
|
PageSize: 5,
|
|
|
|
Page: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
emails, count, err = SearchEmails(opts)
|
|
|
|
assert.NoError(t, err)
|
2021-06-07 10:57:09 +05:30
|
|
|
assert.Len(t, emails, 5)
|
|
|
|
assert.Greater(t, count, int64(len(emails)))
|
2020-03-02 23:55:36 +05:30
|
|
|
}
|