diff options
Diffstat (limited to 'models/user/email_address_test.go')
-rw-r--r-- | models/user/email_address_test.go | 165 |
1 files changed, 83 insertions, 82 deletions
diff --git a/models/user/email_address_test.go b/models/user/email_address_test.go index 79de4c0b48..471598c897 100644 --- a/models/user/email_address_test.go +++ b/models/user/email_address_test.go @@ -2,13 +2,14 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package user +package user_test 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" @@ -17,14 +18,14 @@ import ( func TestGetEmailAddresses(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - emails, _ := GetEmailAddresses(int64(1)) + emails, _ := user_model.GetEmailAddresses(int64(1)) if assert.Len(t, emails, 3) { assert.True(t, emails[0].IsPrimary) assert.True(t, emails[2].IsActivated) assert.False(t, emails[2].IsPrimary) } - emails, _ = GetEmailAddresses(int64(2)) + emails, _ = user_model.GetEmailAddresses(int64(2)) if assert.Len(t, emails, 2) { assert.True(t, emails[0].IsPrimary) assert.True(t, emails[0].IsActivated) @@ -34,18 +35,18 @@ func TestGetEmailAddresses(t *testing.T) { func TestIsEmailUsed(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - isExist, _ := IsEmailUsed(db.DefaultContext, "") + isExist, _ := user_model.IsEmailUsed(db.DefaultContext, "") assert.True(t, isExist) - isExist, _ = IsEmailUsed(db.DefaultContext, "user11@example.com") + isExist, _ = user_model.IsEmailUsed(db.DefaultContext, "user11@example.com") assert.True(t, isExist) - isExist, _ = IsEmailUsed(db.DefaultContext, "user1234567890@example.com") + isExist, _ = user_model.IsEmailUsed(db.DefaultContext, "user1234567890@example.com") assert.False(t, isExist) } func TestAddEmailAddress(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - assert.NoError(t, AddEmailAddress(db.DefaultContext, &EmailAddress{ + assert.NoError(t, user_model.AddEmailAddress(db.DefaultContext, &user_model.EmailAddress{ Email: "user1234567890@example.com", LowerEmail: "user1234567890@example.com", IsPrimary: true, @@ -53,55 +54,55 @@ func TestAddEmailAddress(t *testing.T) { })) // ErrEmailAlreadyUsed - err := AddEmailAddress(db.DefaultContext, &EmailAddress{ + err := user_model.AddEmailAddress(db.DefaultContext, &user_model.EmailAddress{ Email: "user1234567890@example.com", LowerEmail: "user1234567890@example.com", }) assert.Error(t, err) - assert.True(t, IsErrEmailAlreadyUsed(err)) + assert.True(t, user_model.IsErrEmailAlreadyUsed(err)) } func TestAddEmailAddresses(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // insert multiple email address - emails := make([]*EmailAddress, 2) - emails[0] = &EmailAddress{ + emails := make([]*user_model.EmailAddress, 2) + emails[0] = &user_model.EmailAddress{ Email: "user1234@example.com", LowerEmail: "user1234@example.com", IsActivated: true, } - emails[1] = &EmailAddress{ + emails[1] = &user_model.EmailAddress{ Email: "user5678@example.com", LowerEmail: "user5678@example.com", IsActivated: true, } - assert.NoError(t, AddEmailAddresses(emails)) + assert.NoError(t, user_model.AddEmailAddresses(emails)) // ErrEmailAlreadyUsed - err := AddEmailAddresses(emails) + err := user_model.AddEmailAddresses(emails) assert.Error(t, err) - assert.True(t, IsErrEmailAlreadyUsed(err)) + assert.True(t, user_model.IsErrEmailAlreadyUsed(err)) } func TestDeleteEmailAddress(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - assert.NoError(t, DeleteEmailAddress(&EmailAddress{ + assert.NoError(t, user_model.DeleteEmailAddress(&user_model.EmailAddress{ UID: int64(1), ID: int64(33), Email: "user1-2@example.com", LowerEmail: "user1-2@example.com", })) - assert.NoError(t, DeleteEmailAddress(&EmailAddress{ + assert.NoError(t, user_model.DeleteEmailAddress(&user_model.EmailAddress{ UID: int64(1), Email: "user1-3@example.com", LowerEmail: "user1-3@example.com", })) // Email address does not exist - err := DeleteEmailAddress(&EmailAddress{ + err := user_model.DeleteEmailAddress(&user_model.EmailAddress{ UID: int64(1), Email: "user1234567890@example.com", LowerEmail: "user1234567890@example.com", @@ -113,70 +114,70 @@ func TestDeleteEmailAddresses(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // delete multiple email address - emails := make([]*EmailAddress, 2) - emails[0] = &EmailAddress{ + emails := make([]*user_model.EmailAddress, 2) + emails[0] = &user_model.EmailAddress{ UID: int64(2), ID: int64(3), Email: "user2@example.com", LowerEmail: "user2@example.com", } - emails[1] = &EmailAddress{ + emails[1] = &user_model.EmailAddress{ UID: int64(2), Email: "user2-2@example.com", LowerEmail: "user2-2@example.com", } - assert.NoError(t, DeleteEmailAddresses(emails)) + assert.NoError(t, user_model.DeleteEmailAddresses(emails)) // ErrEmailAlreadyUsed - err := DeleteEmailAddresses(emails) + err := user_model.DeleteEmailAddresses(emails) assert.Error(t, err) } func TestMakeEmailPrimary(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - email := &EmailAddress{ + email := &user_model.EmailAddress{ Email: "user567890@example.com", } - err := MakeEmailPrimary(email) + err := user_model.MakeEmailPrimary(email) assert.Error(t, err) - assert.EqualError(t, err, ErrEmailAddressNotExist{Email: email.Email}.Error()) + assert.EqualError(t, err, user_model.ErrEmailAddressNotExist{Email: email.Email}.Error()) - email = &EmailAddress{ + email = &user_model.EmailAddress{ Email: "user11@example.com", } - err = MakeEmailPrimary(email) + err = user_model.MakeEmailPrimary(email) assert.Error(t, err) - assert.EqualError(t, err, ErrEmailNotActivated.Error()) + assert.EqualError(t, err, user_model.ErrEmailNotActivated.Error()) - email = &EmailAddress{ + email = &user_model.EmailAddress{ Email: "user9999999@example.com", } - err = MakeEmailPrimary(email) + err = user_model.MakeEmailPrimary(email) assert.Error(t, err) - assert.True(t, IsErrUserNotExist(err)) + assert.True(t, user_model.IsErrUserNotExist(err)) - email = &EmailAddress{ + email = &user_model.EmailAddress{ Email: "user101@example.com", } - err = MakeEmailPrimary(email) + err = user_model.MakeEmailPrimary(email) assert.NoError(t, err) - user, _ := GetUserByID(int64(10)) + 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 := &EmailAddress{ + email := &user_model.EmailAddress{ ID: int64(1), UID: int64(1), Email: "user11@example.com", } - assert.NoError(t, ActivateEmail(email)) + assert.NoError(t, user_model.ActivateEmail(email)) - emails, _ := GetEmailAddresses(int64(1)) + emails, _ := user_model.GetEmailAddresses(int64(1)) assert.Len(t, emails, 3) assert.True(t, emails[0].IsActivated) assert.True(t, emails[0].IsPrimary) @@ -189,17 +190,17 @@ func TestListEmails(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // Must find all users and their emails - opts := &SearchEmailOptions{ + opts := &user_model.SearchEmailOptions{ ListOptions: db.ListOptions{ PageSize: 10000, }, } - emails, count, err := SearchEmails(opts) + emails, count, err := user_model.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 { + contains := func(match func(s *user_model.SearchEmailResult) bool) bool { for _, v := range emails { if match(v) { return true @@ -208,46 +209,46 @@ func TestListEmails(t *testing.T) { return false } - assert.True(t, contains(func(s *SearchEmailResult) bool { return s.UID == 18 })) + assert.True(t, contains(func(s *user_model.SearchEmailResult) bool { return s.UID == 18 })) // 'user3' is an organization - assert.False(t, contains(func(s *SearchEmailResult) bool { return s.UID == 3 })) + assert.False(t, contains(func(s *user_model.SearchEmailResult) bool { return s.UID == 3 })) // Must find no records - opts = &SearchEmailOptions{Keyword: "NOTFOUND"} - emails, count, err = SearchEmails(opts) + opts = &user_model.SearchEmailOptions{Keyword: "NOTFOUND"} + emails, count, err = user_model.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) + opts = &user_model.SearchEmailOptions{Keyword: "user2"} + emails, count, err = user_model.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 })) + 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 })) // Must find only primary addresses (i.e. from the `user` table) - opts = &SearchEmailOptions{IsPrimary: util.OptionalBoolTrue} - emails, _, err = SearchEmails(opts) + opts = &user_model.SearchEmailOptions{IsPrimary: util.OptionalBoolTrue} + emails, _, err = user_model.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 })) + 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 })) // Must find only inactive addresses (i.e. not validated) - opts = &SearchEmailOptions{IsActivated: util.OptionalBoolFalse} - emails, _, err = SearchEmails(opts) + opts = &user_model.SearchEmailOptions{IsActivated: util.OptionalBoolFalse} + emails, _, err = user_model.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 })) + 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 })) // Must find more than one page, but retrieve only one - opts = &SearchEmailOptions{ + opts = &user_model.SearchEmailOptions{ ListOptions: db.ListOptions{ PageSize: 5, Page: 1, }, } - emails, count, err = SearchEmails(opts) + emails, count, err = user_model.SearchEmails(opts) assert.NoError(t, err) assert.Len(t, emails, 5) assert.Greater(t, count, int64(len(emails))) @@ -278,32 +279,32 @@ func TestEmailAddressValidate(t *testing.T) { `first|last@iana.org`: nil, `first}last@iana.org`: nil, `first~last@iana.org`: nil, - `first;last@iana.org`: ErrEmailCharIsNotSupported{`first;last@iana.org`}, - ".233@qq.com": ErrEmailInvalid{".233@qq.com"}, - "!233@qq.com": ErrEmailInvalid{"!233@qq.com"}, - "#233@qq.com": ErrEmailInvalid{"#233@qq.com"}, - "$233@qq.com": ErrEmailInvalid{"$233@qq.com"}, - "%233@qq.com": ErrEmailInvalid{"%233@qq.com"}, - "&233@qq.com": ErrEmailInvalid{"&233@qq.com"}, - "'233@qq.com": ErrEmailInvalid{"'233@qq.com"}, - "*233@qq.com": ErrEmailInvalid{"*233@qq.com"}, - "+233@qq.com": ErrEmailInvalid{"+233@qq.com"}, - "/233@qq.com": ErrEmailInvalid{"/233@qq.com"}, - "=233@qq.com": ErrEmailInvalid{"=233@qq.com"}, - "?233@qq.com": ErrEmailInvalid{"?233@qq.com"}, - "^233@qq.com": ErrEmailInvalid{"^233@qq.com"}, - "`233@qq.com": ErrEmailInvalid{"`233@qq.com"}, - "{233@qq.com": ErrEmailInvalid{"{233@qq.com"}, - "|233@qq.com": ErrEmailInvalid{"|233@qq.com"}, - "}233@qq.com": ErrEmailInvalid{"}233@qq.com"}, - "~233@qq.com": ErrEmailInvalid{"~233@qq.com"}, - ";233@qq.com": ErrEmailCharIsNotSupported{";233@qq.com"}, - "Foo <foo@bar.com>": ErrEmailCharIsNotSupported{"Foo <foo@bar.com>"}, - string([]byte{0xE2, 0x84, 0xAA}): ErrEmailCharIsNotSupported{string([]byte{0xE2, 0x84, 0xAA})}, + `first;last@iana.org`: user_model.ErrEmailCharIsNotSupported{`first;last@iana.org`}, + ".233@qq.com": user_model.ErrEmailInvalid{".233@qq.com"}, + "!233@qq.com": user_model.ErrEmailInvalid{"!233@qq.com"}, + "#233@qq.com": user_model.ErrEmailInvalid{"#233@qq.com"}, + "$233@qq.com": user_model.ErrEmailInvalid{"$233@qq.com"}, + "%233@qq.com": user_model.ErrEmailInvalid{"%233@qq.com"}, + "&233@qq.com": user_model.ErrEmailInvalid{"&233@qq.com"}, + "'233@qq.com": user_model.ErrEmailInvalid{"'233@qq.com"}, + "*233@qq.com": user_model.ErrEmailInvalid{"*233@qq.com"}, + "+233@qq.com": user_model.ErrEmailInvalid{"+233@qq.com"}, + "/233@qq.com": user_model.ErrEmailInvalid{"/233@qq.com"}, + "=233@qq.com": user_model.ErrEmailInvalid{"=233@qq.com"}, + "?233@qq.com": user_model.ErrEmailInvalid{"?233@qq.com"}, + "^233@qq.com": user_model.ErrEmailInvalid{"^233@qq.com"}, + "`233@qq.com": user_model.ErrEmailInvalid{"`233@qq.com"}, + "{233@qq.com": user_model.ErrEmailInvalid{"{233@qq.com"}, + "|233@qq.com": user_model.ErrEmailInvalid{"|233@qq.com"}, + "}233@qq.com": user_model.ErrEmailInvalid{"}233@qq.com"}, + "~233@qq.com": user_model.ErrEmailInvalid{"~233@qq.com"}, + ";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})}, } for kase, err := range kases { t.Run(kase, func(t *testing.T) { - assert.EqualValues(t, err, ValidateEmail(kase)) + assert.EqualValues(t, err, user_model.ValidateEmail(kase)) }) } } |