Always store primary email address into email_address table and also the state (#15956)
* Always store primary email address into email_address table and also the state * Add lower_email to not convert email to lower as what's added * Fix fixture * Fix tests * Use BeforeInsert to save lower email * Fix v180 migration * fix tests * Fix test * Remove wrong submited codes * Fix test * Fix test * Fix test * Add test for v181 migration * remove change user's email to lower * Revert change on user's email column * Fix lower email * Fix test * Fix test
This commit is contained in:
@ -36,7 +36,6 @@ func TestGPGKeys(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
|
||||
//Basic test on result code
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Run("ViewOwnGPGKeys", func(t *testing.T) {
|
||||
@ -87,9 +86,9 @@ func TestGPGKeys(t *testing.T) {
|
||||
assert.Empty(t, subKey.Emails)
|
||||
|
||||
primaryKey2 := keys[1] //Primary key 2
|
||||
assert.EqualValues(t, "FABF39739FE1E927", primaryKey2.KeyID)
|
||||
assert.EqualValues(t, "3CEF46EF40BEFC3E", primaryKey2.KeyID)
|
||||
assert.Len(t, primaryKey2.Emails, 1)
|
||||
assert.EqualValues(t, "user21@example.com", primaryKey2.Emails[0].Email)
|
||||
assert.EqualValues(t, "user2-2@example.com", primaryKey2.Emails[0].Email)
|
||||
assert.False(t, primaryKey2.Emails[0].Verified)
|
||||
|
||||
var key api.GPGKey
|
||||
@ -110,9 +109,9 @@ func TestGPGKeys(t *testing.T) {
|
||||
req = NewRequest(t, "GET", "/api/v1/user/gpg_keys/"+strconv.FormatInt(primaryKey2.ID, 10)+"?token="+token) //Primary key 2
|
||||
resp = session.MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, resp, &key)
|
||||
assert.EqualValues(t, "FABF39739FE1E927", key.KeyID)
|
||||
assert.EqualValues(t, "3CEF46EF40BEFC3E", key.KeyID)
|
||||
assert.Len(t, key.Emails, 1)
|
||||
assert.EqualValues(t, "user21@example.com", key.Emails[0].Email)
|
||||
assert.EqualValues(t, "user2-2@example.com", key.Emails[0].Email)
|
||||
assert.False(t, key.Emails[0].Verified)
|
||||
|
||||
})
|
||||
@ -231,35 +230,46 @@ uy6MA3VSB99SK9ducGmE1Jv8mcziREroz2TEGr0zPs6h
|
||||
}
|
||||
|
||||
func testCreateValidSecondaryEmailGPGKey(t *testing.T, makeRequest makeRequestFunc, token string, expected int) {
|
||||
//User2 <user21@example.com> //secondary and not activated
|
||||
//User2 <user2-2@example.com> //secondary and not activated
|
||||
testCreateGPGKey(t, makeRequest, token, expected, `-----BEGIN PGP PUBLIC KEY BLOCK-----
|
||||
|
||||
mQENBFmGWN4BCAC18V4tVGO65VLCV7p14FuXJlUtZ5CuYMvgEkcOqrvRaBSW9ao4
|
||||
PGESOhJpfWpnW3QgJniYndLzPpsmdHEclEER6aZjiNgReWPOjHD5tykWocZAJqXD
|
||||
eY1ym59gvVMLcfbV2yQsyR2hbJlc+dJsl16tigSEe3nwxZSw2IsW92pgEzT9JNUr
|
||||
Q+mC8dw4dqY0tYmFazYUGNxufUc/twgQT/Or1aNs0az5Q6Jft4rrTRsh/S7We0VB
|
||||
COKGkdcQyYgAls7HJBuPjQRi6DM9VhgBSHLAgSLyaUcZvhZBJr8Qe/q4PP3/kYDJ
|
||||
wm4RMnjOLz2pFZPgtRqgcAwpmFtLrACbEB3JABEBAAG0GlVzZXIyIDx1c2VyMjFA
|
||||
ZXhhbXBsZS5jb20+iQFUBBMBCAA+FiEEPOLHOjPSO42DWM57+r85c5/h6ScFAlmG
|
||||
WN4CGwMFCQPCZwAFCwkIBwIGFQgJCgsCBBYCAwECHgECF4AACgkQ+r85c5/h6Sfx
|
||||
Lgf/dq64NBV8+X9an3seaLxePRviva48e4K67/wV/JxtXNO5Z/DhMGz5kHXCsG9D
|
||||
CXuWYO8ehlTjEnMZ6qqdDnY+H6bQsb2OS5oPn4RwpPXslAjEKtojPAr0dDsMS2DB
|
||||
dUuIm1AoOnewOVO0OFRf1EqX1bivxnN0FVMcO0m8AczfnKDaGb0y/qg/Y9JAsKqp
|
||||
j5pZNMWUkntRtGySeJ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BHzzXSCALnVwhjbgkBEWPIg38APT3cr9KfnMM
|
||||
q8+tvsGLj4piAl3Lww7+GhSsDOUXH8btR41BSAQDrbO5q6oi/h4nuxoNmQIDW/Ug
|
||||
s+dd5hnY2FtHRjb4FCR9kAjdTE6stc8wzohWfbg1N+12TTA2ylByAumICVXixavH
|
||||
RJ7l0OiWJk388qw9mqh3k8HcBxL7OfDlFC9oPmCS0iYiIwW/Yc80kBhoxcvl/Xa7
|
||||
mIMMn8taHIaQO7v9ln2EVQYTzbNCmwTw9ovTM0j/Pbkg2EftfP1TCoxQHvBnsCED
|
||||
6qgtsUdi5eviONRkBgeZtN3oxA==
|
||||
=MgDv
|
||||
-----END PGP PUBLIC KEY BLOCK-----`)
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ func TestAPIListEmails(t *testing.T) {
|
||||
Primary: true,
|
||||
},
|
||||
{
|
||||
Email: "user21@example.com",
|
||||
Email: "user2-2@example.com",
|
||||
Verified: false,
|
||||
Primary: false,
|
||||
},
|
||||
@ -55,7 +55,7 @@ func TestAPIAddEmail(t *testing.T) {
|
||||
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
|
||||
|
||||
opts = api.CreateEmailOption{
|
||||
Emails: []string{"user22@example.com"},
|
||||
Emails: []string{"user2-3@example.com"},
|
||||
}
|
||||
req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails?token="+token, &opts)
|
||||
resp := session.MakeRequest(t, req, http.StatusCreated)
|
||||
@ -64,7 +64,7 @@ func TestAPIAddEmail(t *testing.T) {
|
||||
DecodeJSON(t, resp, &emails)
|
||||
assert.EqualValues(t, []*api.Email{
|
||||
{
|
||||
Email: "user22@example.com",
|
||||
Email: "user2-3@example.com",
|
||||
Verified: true,
|
||||
Primary: false,
|
||||
},
|
||||
@ -79,13 +79,13 @@ func TestAPIDeleteEmail(t *testing.T) {
|
||||
token := getTokenForLoggedInUser(t, session)
|
||||
|
||||
opts := api.DeleteEmailOption{
|
||||
Emails: []string{"user22@example.com"},
|
||||
Emails: []string{"user2-3@example.com"},
|
||||
}
|
||||
req := NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails?token="+token, &opts)
|
||||
session.MakeRequest(t, req, http.StatusNotFound)
|
||||
|
||||
opts = api.DeleteEmailOption{
|
||||
Emails: []string{"user21@example.com"},
|
||||
Emails: []string{"user2-2@example.com"},
|
||||
}
|
||||
req = NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails?token="+token, &opts)
|
||||
session.MakeRequest(t, req, http.StatusNoContent)
|
||||
|
@ -237,6 +237,21 @@ func (err ErrEmailAddressNotExist) Error() string {
|
||||
return fmt.Sprintf("Email address does not exist [email: %s]", err.Email)
|
||||
}
|
||||
|
||||
// ErrPrimaryEmailCannotDelete primary email address cannot be deleted
|
||||
type ErrPrimaryEmailCannotDelete struct {
|
||||
Email string
|
||||
}
|
||||
|
||||
// IsErrPrimaryEmailCannotDelete checks if an error is an ErrPrimaryEmailCannotDelete
|
||||
func IsErrPrimaryEmailCannotDelete(err error) bool {
|
||||
_, ok := err.(ErrPrimaryEmailCannotDelete)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrPrimaryEmailCannotDelete) Error() string {
|
||||
return fmt.Sprintf("Primary email address cannot be deleted [email: %s]", err.Email)
|
||||
}
|
||||
|
||||
// ErrOpenIDAlreadyUsed represents a "OpenIDAlreadyUsed" kind of error.
|
||||
type ErrOpenIDAlreadyUsed struct {
|
||||
OpenID string
|
||||
|
@ -1,35 +1,279 @@
|
||||
-
|
||||
id: 1
|
||||
uid: 1
|
||||
uid: 11
|
||||
email: user11@example.com
|
||||
lower_email: user11@example.com
|
||||
is_activated: false
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 2
|
||||
uid: 1
|
||||
uid: 12
|
||||
email: user12@example.com
|
||||
is_activated: false
|
||||
lower_email: user12@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 3
|
||||
uid: 2
|
||||
email: user2@example.com
|
||||
lower_email: user2@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 4
|
||||
uid: 2
|
||||
uid: 21
|
||||
email: user21@example.com
|
||||
is_activated: false
|
||||
lower_email: user21@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 5
|
||||
uid: 9999999
|
||||
email: user9999999@example.com
|
||||
lower_email: user9999999@example.com
|
||||
is_activated: true
|
||||
is_primary: false
|
||||
|
||||
-
|
||||
id: 6
|
||||
uid: 10
|
||||
email: user101@example.com
|
||||
email: user10@example.com
|
||||
lower_email: user10@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 7
|
||||
uid: 10
|
||||
email: user101@example.com
|
||||
lower_email: user101@example.com
|
||||
is_activated: true
|
||||
is_primary: false
|
||||
|
||||
-
|
||||
id: 8
|
||||
uid: 9
|
||||
email: user9@example.com
|
||||
lower_email: user9@example.com
|
||||
is_activated: false
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 9
|
||||
uid: 1
|
||||
email: user1@example.com
|
||||
lower_email: user1@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 10
|
||||
uid: 3
|
||||
email: user3@example.com
|
||||
lower_email: user3@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 11
|
||||
uid: 4
|
||||
email: user4@example.com
|
||||
lower_email: user4@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 12
|
||||
uid: 5
|
||||
email: user5@example.com
|
||||
lower_email: user5@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 13
|
||||
uid: 6
|
||||
email: user6@example.com
|
||||
lower_email: user6@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 14
|
||||
uid: 7
|
||||
email: user7@example.com
|
||||
lower_email: user7@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 15
|
||||
uid: 8
|
||||
email: user8@example.com
|
||||
lower_email: user8@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 16
|
||||
uid: 13
|
||||
email: user13@example.com
|
||||
lower_email: user13@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 17
|
||||
uid: 14
|
||||
email: user14@example.com
|
||||
lower_email: user14@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 18
|
||||
uid: 15
|
||||
email: user15@example.com
|
||||
lower_email: user15@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 19
|
||||
uid: 16
|
||||
email: user16@example.com
|
||||
lower_email: user16@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 20
|
||||
uid: 17
|
||||
email: user17@example.com
|
||||
lower_email: user17@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 21
|
||||
uid: 18
|
||||
email: user18@example.com
|
||||
lower_email: user18@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 22
|
||||
uid: 19
|
||||
email: user19@example.com
|
||||
lower_email: user19@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 23
|
||||
uid: 20
|
||||
email: user20@example.com
|
||||
lower_email: user20@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 24
|
||||
uid: 22
|
||||
email: limited_org@example.com
|
||||
lower_email: limited_org@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 25
|
||||
uid: 23
|
||||
email: privated_org@example.com
|
||||
lower_email: privated_org@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 26
|
||||
uid: 24
|
||||
email: user24@example.com
|
||||
lower_email: user24@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 27
|
||||
uid: 25
|
||||
email: org25@example.com
|
||||
lower_email: org25@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 28
|
||||
uid: 26
|
||||
email: org26@example.com
|
||||
lower_email: org26@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 29
|
||||
uid: 27
|
||||
email: user27@example.com
|
||||
lower_email: user27@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 30
|
||||
uid: 28
|
||||
email: user28@example.com
|
||||
lower_email: user28@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 31
|
||||
uid: 29
|
||||
email: user29@example.com
|
||||
lower_email: user29@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 32
|
||||
uid: 30
|
||||
email: user30@example.com
|
||||
lower_email: user30@example.com
|
||||
is_activated: true
|
||||
is_primary: true
|
||||
|
||||
-
|
||||
id: 33
|
||||
uid: 1
|
||||
email: user1-2@example.com
|
||||
lower_email: user1-2@example.com
|
||||
is_activated: true
|
||||
is_primary: false
|
||||
|
||||
-
|
||||
id: 34
|
||||
uid: 1
|
||||
email: user1-3@example.com
|
||||
lower_email: user1-3@example.com
|
||||
is_activated: true
|
||||
is_primary: false
|
||||
|
||||
-
|
||||
id: 35
|
||||
uid: 2
|
||||
email: user2-2@example.com
|
||||
lower_email: user2-2@example.com
|
||||
is_activated: false
|
||||
is_primary: false
|
@ -301,7 +301,7 @@ func parseGPGKey(ownerID int64, e *openpgp.Entity) (*GPGKey, error) {
|
||||
}
|
||||
email := strings.ToLower(strings.TrimSpace(ident.UserId.Email))
|
||||
for _, e := range userEmails {
|
||||
if e.Email == email {
|
||||
if e.LowerEmail == email {
|
||||
emails = append(emails, e)
|
||||
break
|
||||
}
|
||||
|
@ -311,6 +311,8 @@ var migrations = []Migration{
|
||||
NewMigration("Convert avatar url to text", convertAvatarURLToText),
|
||||
// v180 -> v181
|
||||
NewMigration("Delete credentials from past migrations", deleteMigrationCredentials),
|
||||
// v181 -> v182
|
||||
NewMigration("Always save primary email on email address table", addPrimaryEmail2EmailAddress),
|
||||
}
|
||||
|
||||
// GetCurrentDBVersion returns the current db version
|
||||
|
92
models/migrations/v181.go
Normal file
92
models/migrations/v181.go
Normal file
@ -0,0 +1,92 @@
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func addPrimaryEmail2EmailAddress(x *xorm.Engine) (err error) {
|
||||
type User struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
Email string `xorm:"NOT NULL"`
|
||||
IsActive bool `xorm:"INDEX"` // Activate primary email
|
||||
}
|
||||
|
||||
type EmailAddress1 struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
UID int64 `xorm:"INDEX NOT NULL"`
|
||||
Email string `xorm:"UNIQUE NOT NULL"`
|
||||
LowerEmail string
|
||||
IsActivated bool
|
||||
IsPrimary bool `xorm:"DEFAULT(false) NOT NULL"`
|
||||
}
|
||||
|
||||
// Add lower_email and is_primary columns
|
||||
if err = x.Table("email_address").Sync2(new(EmailAddress1)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if _, err = x.Exec("UPDATE email_address SET lower_email=LOWER(email), is_primary=?", false); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
type EmailAddress struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
UID int64 `xorm:"INDEX NOT NULL"`
|
||||
Email string `xorm:"UNIQUE NOT NULL"`
|
||||
LowerEmail string `xorm:"UNIQUE NOT NULL"`
|
||||
IsActivated bool
|
||||
IsPrimary bool `xorm:"DEFAULT(false) NOT NULL"`
|
||||
}
|
||||
|
||||
// change lower_email as unique
|
||||
if err = x.Sync2(new(EmailAddress)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
|
||||
const batchSize = 100
|
||||
|
||||
for start := 0; ; start += batchSize {
|
||||
users := make([]*User, 0, batchSize)
|
||||
if err = sess.Limit(batchSize, start).Find(&users); err != nil {
|
||||
return
|
||||
}
|
||||
if len(users) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
for _, user := range users {
|
||||
var exist bool
|
||||
exist, err = sess.Where("email=?", user.Email).Table("email_address").Exist()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !exist {
|
||||
if _, err = sess.Insert(&EmailAddress{
|
||||
UID: user.ID,
|
||||
Email: user.Email,
|
||||
LowerEmail: strings.ToLower(user.Email),
|
||||
IsActivated: user.IsActive,
|
||||
IsPrimary: true,
|
||||
}); err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if _, err = sess.Where("email=?", user.Email).Cols("is_primary").Update(&EmailAddress{
|
||||
IsPrimary: true,
|
||||
}); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
54
models/migrations/v181_test.go
Normal file
54
models/migrations/v181_test.go
Normal file
@ -0,0 +1,54 @@
|
||||
// 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 migrations
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_addPrimaryEmail2EmailAddress(t *testing.T) {
|
||||
type User struct {
|
||||
ID int64
|
||||
Email string
|
||||
IsActive bool
|
||||
}
|
||||
|
||||
// Prepare and load the testing database
|
||||
x, deferable := prepareTestEnv(t, 0, new(User))
|
||||
if x == nil || t.Failed() {
|
||||
defer deferable()
|
||||
return
|
||||
}
|
||||
defer deferable()
|
||||
|
||||
err := addPrimaryEmail2EmailAddress(x)
|
||||
assert.NoError(t, err)
|
||||
|
||||
type EmailAddress struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
UID int64 `xorm:"INDEX NOT NULL"`
|
||||
Email string `xorm:"UNIQUE NOT NULL"`
|
||||
LowerEmail string `xorm:"UNIQUE NOT NULL"`
|
||||
IsActivated bool
|
||||
IsPrimary bool `xorm:"DEFAULT(false) NOT NULL"`
|
||||
}
|
||||
|
||||
var users = make([]User, 0, 20)
|
||||
err = x.Find(&users)
|
||||
assert.NoError(t, err)
|
||||
|
||||
for _, user := range users {
|
||||
var emailAddress EmailAddress
|
||||
has, err := x.Where("lower_email=?", strings.ToLower(user.Email)).Get(&emailAddress)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, has)
|
||||
assert.True(t, emailAddress.IsPrimary)
|
||||
assert.EqualValues(t, user.IsActive, emailAddress.IsActivated)
|
||||
assert.EqualValues(t, user.ID, emailAddress.UID)
|
||||
}
|
||||
}
|
@ -74,9 +74,6 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrEmailNotExist e-mail does not exist error
|
||||
ErrEmailNotExist = errors.New("E-mail does not exist")
|
||||
|
||||
// ErrEmailNotActivated e-mail address has not been activated error
|
||||
ErrEmailNotActivated = errors.New("E-mail address has not been activated")
|
||||
|
||||
@ -876,15 +873,6 @@ func CreateUser(u *User) (err error) {
|
||||
}
|
||||
|
||||
u.Email = strings.ToLower(u.Email)
|
||||
isExist, err = sess.
|
||||
Where("email=?", u.Email).
|
||||
Get(new(User))
|
||||
if err != nil {
|
||||
return err
|
||||
} else if isExist {
|
||||
return ErrEmailAlreadyUsed{u.Email}
|
||||
}
|
||||
|
||||
if err = ValidateEmail(u.Email); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -915,6 +903,17 @@ func CreateUser(u *User) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
// insert email address
|
||||
if _, err := sess.Insert(&EmailAddress{
|
||||
UID: u.ID,
|
||||
Email: u.Email,
|
||||
LowerEmail: strings.ToLower(u.Email),
|
||||
IsActivated: u.IsActive,
|
||||
IsPrimary: true,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return sess.Commit()
|
||||
}
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -17,9 +17,9 @@ func TestGetEmailAddresses(t *testing.T) {
|
||||
|
||||
emails, _ := GetEmailAddresses(int64(1))
|
||||
if assert.Len(t, emails, 3) {
|
||||
assert.False(t, emails[0].IsPrimary)
|
||||
assert.True(t, emails[0].IsPrimary)
|
||||
assert.True(t, emails[2].IsActivated)
|
||||
assert.True(t, emails[2].IsPrimary)
|
||||
assert.False(t, emails[2].IsPrimary)
|
||||
}
|
||||
|
||||
emails, _ = GetEmailAddresses(int64(2))
|
||||
@ -45,13 +45,15 @@ func TestAddEmailAddress(t *testing.T) {
|
||||
|
||||
assert.NoError(t, AddEmailAddress(&EmailAddress{
|
||||
Email: "user1234567890@example.com",
|
||||
LowerEmail: "user1234567890@example.com",
|
||||
IsPrimary: true,
|
||||
IsActivated: true,
|
||||
}))
|
||||
|
||||
// ErrEmailAlreadyUsed
|
||||
err := AddEmailAddress(&EmailAddress{
|
||||
Email: "user1234567890@example.com",
|
||||
Email: "user1234567890@example.com",
|
||||
LowerEmail: "user1234567890@example.com",
|
||||
})
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrEmailAlreadyUsed(err))
|
||||
@ -64,10 +66,12 @@ func TestAddEmailAddresses(t *testing.T) {
|
||||
emails := make([]*EmailAddress, 2)
|
||||
emails[0] = &EmailAddress{
|
||||
Email: "user1234@example.com",
|
||||
LowerEmail: "user1234@example.com",
|
||||
IsActivated: true,
|
||||
}
|
||||
emails[1] = &EmailAddress{
|
||||
Email: "user5678@example.com",
|
||||
LowerEmail: "user5678@example.com",
|
||||
IsActivated: true,
|
||||
}
|
||||
assert.NoError(t, AddEmailAddresses(emails))
|
||||
@ -82,20 +86,23 @@ func TestDeleteEmailAddress(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
|
||||
assert.NoError(t, DeleteEmailAddress(&EmailAddress{
|
||||
UID: int64(1),
|
||||
ID: int64(1),
|
||||
Email: "user11@example.com",
|
||||
UID: int64(1),
|
||||
ID: int64(33),
|
||||
Email: "user1-2@example.com",
|
||||
LowerEmail: "user1-2@example.com",
|
||||
}))
|
||||
|
||||
assert.NoError(t, DeleteEmailAddress(&EmailAddress{
|
||||
UID: int64(1),
|
||||
Email: "user12@example.com",
|
||||
UID: int64(1),
|
||||
Email: "user1-3@example.com",
|
||||
LowerEmail: "user1-3@example.com",
|
||||
}))
|
||||
|
||||
// Email address does not exist
|
||||
err := DeleteEmailAddress(&EmailAddress{
|
||||
UID: int64(1),
|
||||
Email: "user1234567890@example.com",
|
||||
UID: int64(1),
|
||||
Email: "user1234567890@example.com",
|
||||
LowerEmail: "user1234567890@example.com",
|
||||
})
|
||||
assert.Error(t, err)
|
||||
}
|
||||
@ -106,13 +113,15 @@ func TestDeleteEmailAddresses(t *testing.T) {
|
||||
// delete multiple email address
|
||||
emails := make([]*EmailAddress, 2)
|
||||
emails[0] = &EmailAddress{
|
||||
UID: int64(2),
|
||||
ID: int64(3),
|
||||
Email: "user2@example.com",
|
||||
UID: int64(2),
|
||||
ID: int64(3),
|
||||
Email: "user2@example.com",
|
||||
LowerEmail: "user2@example.com",
|
||||
}
|
||||
emails[1] = &EmailAddress{
|
||||
UID: int64(2),
|
||||
Email: "user21@example.com",
|
||||
UID: int64(2),
|
||||
Email: "user2-2@example.com",
|
||||
LowerEmail: "user2-2@example.com",
|
||||
}
|
||||
assert.NoError(t, DeleteEmailAddresses(emails))
|
||||
|
||||
@ -129,7 +138,7 @@ func TestMakeEmailPrimary(t *testing.T) {
|
||||
}
|
||||
err := MakeEmailPrimary(email)
|
||||
assert.Error(t, err)
|
||||
assert.EqualError(t, err, ErrEmailNotExist.Error())
|
||||
assert.EqualError(t, err, ErrEmailAddressNotExist{email.Email}.Error())
|
||||
|
||||
email = &EmailAddress{
|
||||
Email: "user11@example.com",
|
||||
@ -168,15 +177,21 @@ func TestActivate(t *testing.T) {
|
||||
emails, _ := 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.True(t, emails[2].IsPrimary)
|
||||
assert.False(t, emails[2].IsPrimary)
|
||||
}
|
||||
|
||||
func TestListEmails(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
|
||||
// Must find all users and their emails
|
||||
opts := &SearchEmailOptions{}
|
||||
opts := &SearchEmailOptions{
|
||||
ListOptions: ListOptions{
|
||||
PageSize: 10000,
|
||||
},
|
||||
}
|
||||
emails, count, err := SearchEmails(opts)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEqual(t, int64(0), count)
|
||||
|
Reference in New Issue
Block a user