mirror of
https://github.com/dnote/dnote
synced 2026-03-15 15:05:51 +01:00
test
This commit is contained in:
parent
ff2ba3c4dc
commit
0d1cb684d5
3 changed files with 60 additions and 5 deletions
|
|
@ -27,9 +27,10 @@ import (
|
|||
"github.com/dnote/dnote/pkg/server/database"
|
||||
"github.com/dnote/dnote/pkg/server/testutils"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func TestCreateUser(t *testing.T) {
|
||||
func TestCreateUser_ProValue(t *testing.T) {
|
||||
testCases := []struct {
|
||||
onPremise bool
|
||||
expectedPro bool
|
||||
|
|
@ -68,3 +69,53 @@ func TestCreateUser(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateUser(t *testing.T) {
|
||||
t.Run("success", func(t *testing.T) {
|
||||
defer testutils.ClearData(testutils.DB)
|
||||
|
||||
c := config.Load()
|
||||
a := NewTest(&App{
|
||||
Config: c,
|
||||
})
|
||||
if _, err := a.CreateUser("alice@example.com", "pass1234"); err != nil {
|
||||
t.Fatal(errors.Wrap(err, "executing"))
|
||||
}
|
||||
|
||||
var userCount int
|
||||
testutils.MustExec(t, testutils.DB.Model(&database.User{}).Count(&userCount), "counting user")
|
||||
assert.Equal(t, userCount, 1, "book count mismatch")
|
||||
|
||||
var accountCount int
|
||||
var accountRecord database.Account
|
||||
testutils.MustExec(t, testutils.DB.Model(&database.Account{}).Count(&accountCount), "counting account")
|
||||
testutils.MustExec(t, testutils.DB.First(&accountRecord), "finding account")
|
||||
|
||||
assert.Equal(t, accountCount, 1, "account count mismatch")
|
||||
assert.Equal(t, accountRecord.Email.String, "alice@example.com", "account email mismatch")
|
||||
|
||||
passwordErr := bcrypt.CompareHashAndPassword([]byte(accountRecord.Password.String), []byte("pass1234"))
|
||||
assert.Equal(t, passwordErr, nil, "Password mismatch")
|
||||
})
|
||||
|
||||
t.Run("duplicate email", func(t *testing.T) {
|
||||
defer testutils.ClearData(testutils.DB)
|
||||
|
||||
aliceUser := database.User{}
|
||||
aliceAccount := database.Account{UserID: aliceUser.ID, Email: database.ToNullString("alice@example.com")}
|
||||
testutils.MustExec(t, testutils.DB.Save(&aliceUser), "preparing a user")
|
||||
testutils.MustExec(t, testutils.DB.Save(&aliceAccount), "preparing an account")
|
||||
|
||||
a := NewTest(nil)
|
||||
_, err := a.CreateUser("alice@example.com", "newpassword")
|
||||
|
||||
assert.Equal(t, err, ErrDuplicateEmail, "error mismatch")
|
||||
|
||||
var userCount, accountCount int
|
||||
testutils.MustExec(t, testutils.DB.Model(&database.User{}).Count(&userCount), "counting user")
|
||||
testutils.MustExec(t, testutils.DB.Model(&database.Account{}).Count(&accountCount), "counting account")
|
||||
|
||||
assert.Equal(t, userCount, 1, "user count mismatch")
|
||||
assert.Equal(t, accountCount, 1, "account count mismatch")
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env bash
|
||||
# shellcheck disable=SC1090
|
||||
# test-local.sh runs api tests using local setting
|
||||
set -eux
|
||||
set -ex
|
||||
|
||||
dir=$(dirname "${BASH_SOURCE[0]}")
|
||||
|
||||
|
|
@ -9,4 +9,4 @@ set -a
|
|||
source "$dir/../../pkg/server/.env.test"
|
||||
set +a
|
||||
|
||||
"$dir/test.sh"
|
||||
"$dir/test.sh" "$1"
|
||||
|
|
|
|||
|
|
@ -10,7 +10,11 @@ emailTemplateDir=$(realpath "$dir/../../pkg/server/mailer/templates/src")
|
|||
export DNOTE_TEST_EMAIL_TEMPLATE_DIR="$emailTemplateDir"
|
||||
|
||||
function run_test {
|
||||
go test ./... -cover -p 1
|
||||
if [ -z "$1" ]; then
|
||||
go test ./... -cover -p 1
|
||||
else
|
||||
go test "$1" -cover -p 1
|
||||
fi
|
||||
}
|
||||
|
||||
if [ "${WATCH-false}" == true ]; then
|
||||
|
|
@ -18,7 +22,7 @@ if [ "${WATCH-false}" == true ]; then
|
|||
while inotifywait --exclude .swp -e modify -r .; do run_test; done;
|
||||
set -e
|
||||
else
|
||||
run_test
|
||||
run_test "$1"
|
||||
fi
|
||||
|
||||
popd
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue