diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-28 22:11:58 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-28 22:11:58 +0800 |
commit | 9defddb2862aa0a54da21328ca091284291363f7 (patch) | |
tree | 1126670579198f00d89c8a4f0240aa7d99995eb8 /models/user_email_test.go | |
parent | b1df89095163d8ce3eecfeb855e7f03f17b473c4 (diff) | |
download | gitea-9defddb2862aa0a54da21328ca091284291363f7.tar.gz gitea-9defddb2862aa0a54da21328ca091284291363f7.zip |
Move more model into models/user (#17826)
* Move more model into models/user
* Remove unnecessary comment
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'models/user_email_test.go')
-rw-r--r-- | models/user_email_test.go | 137 |
1 files changed, 0 insertions, 137 deletions
diff --git a/models/user_email_test.go b/models/user_email_test.go deleted file mode 100644 index a91e24d363..0000000000 --- a/models/user_email_test.go +++ /dev/null @@ -1,137 +0,0 @@ -// Copyright 2021 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package models - -import ( - "testing" - - "code.gitea.io/gitea/models/db" - "code.gitea.io/gitea/models/unittest" - user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/util" - - "github.com/stretchr/testify/assert" -) - -func TestMakeEmailPrimary(t *testing.T) { - assert.NoError(t, unittest.PrepareTestDatabase()) - - email := &user_model.EmailAddress{ - Email: "user567890@example.com", - } - err := MakeEmailPrimary(email) - assert.Error(t, err) - assert.EqualError(t, err, user_model.ErrEmailAddressNotExist{Email: email.Email}.Error()) - - email = &user_model.EmailAddress{ - Email: "user11@example.com", - } - err = MakeEmailPrimary(email) - assert.Error(t, err) - assert.EqualError(t, err, user_model.ErrEmailNotActivated.Error()) - - email = &user_model.EmailAddress{ - Email: "user9999999@example.com", - } - err = MakeEmailPrimary(email) - assert.Error(t, err) - assert.True(t, user_model.IsErrUserNotExist(err)) - - email = &user_model.EmailAddress{ - Email: "user101@example.com", - } - err = MakeEmailPrimary(email) - assert.NoError(t, err) - - user, _ := user_model.GetUserByID(int64(10)) - assert.Equal(t, "user101@example.com", user.Email) -} - -func TestActivate(t *testing.T) { - assert.NoError(t, unittest.PrepareTestDatabase()) - - email := &user_model.EmailAddress{ - ID: int64(1), - UID: int64(1), - Email: "user11@example.com", - } - assert.NoError(t, ActivateEmail(email)) - - emails, _ := user_model.GetEmailAddresses(int64(1)) - 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 - opts := &SearchEmailOptions{ - ListOptions: db.ListOptions{ - PageSize: 10000, - }, - } - 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} - emails, _, err = SearchEmails(opts) - 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} - emails, _, err = SearchEmails(opts) - 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{ - ListOptions: db.ListOptions{ - PageSize: 5, - Page: 1, - }, - } - emails, count, err = SearchEmails(opts) - assert.NoError(t, err) - assert.Len(t, emails, 5) - assert.Greater(t, count, int64(len(emails))) -} |