2021-06-07 23:52:51 -04:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-07 23:52:51 -04:00
|
|
|
|
2022-11-02 04:54:36 -04:00
|
|
|
package v1_15 //nolint
|
2021-06-07 23:52:51 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2024-07-14 09:34:42 -04:00
|
|
|
migration_tests "code.gitea.io/gitea/models/migrations/test"
|
2022-11-02 04:54:36 -04:00
|
|
|
|
2021-06-07 23:52:51 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-11-02 04:54:36 -04:00
|
|
|
func Test_AddPrimaryEmail2EmailAddress(t *testing.T) {
|
2021-06-07 23:52:51 -04:00
|
|
|
type User struct {
|
|
|
|
ID int64
|
|
|
|
Email string
|
|
|
|
IsActive bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare and load the testing database
|
2024-07-14 09:34:42 -04:00
|
|
|
x, deferable := migration_tests.PrepareTestEnv(t, 0, new(User))
|
2021-06-07 23:52:51 -04:00
|
|
|
if x == nil || t.Failed() {
|
|
|
|
defer deferable()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer deferable()
|
|
|
|
|
2022-11-02 04:54:36 -04:00
|
|
|
err := AddPrimaryEmail2EmailAddress(x)
|
2021-06-07 23:52:51 -04:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
users := make([]User, 0, 20)
|
2021-06-07 23:52:51 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|