tulip/database/tx_test.go

53 lines
1.7 KiB
Go
Raw Permalink Normal View History

2023-09-06 22:20:09 +01:00
package database
import (
2024-01-29 10:44:45 +00:00
"github.com/1f349/tulip/utils"
2023-09-06 22:20:09 +01:00
"github.com/MrMelon54/pronouns"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"golang.org/x/text/language"
"testing"
"time"
)
func TestTx_ChangeUserPassword(t *testing.T) {
u := uuid.New()
2024-01-29 10:44:45 +00:00
pw, err := utils.HashPassword("test")
2023-09-06 22:20:09 +01:00
assert.NoError(t, err)
d, err := Open("file::memory:")
assert.NoError(t, err)
_, err = d.db.Exec(`INSERT INTO users (subject, name, username, password, email, updated_at) VALUES (?, ?, ?, ?, ?, ?)`, u.String(), "Test", "test", pw, "test@localhost", updatedAt())
2023-09-06 22:20:09 +01:00
assert.NoError(t, err)
tx, err := d.Begin()
assert.NoError(t, err)
err = tx.ChangeUserPassword(u, "test", "new")
assert.NoError(t, err)
assert.NoError(t, tx.Commit())
query, err := d.db.Query(`SELECT password FROM users WHERE subject = ? AND username = ?`, u.String(), "test")
assert.NoError(t, err)
assert.True(t, query.Next())
2024-01-29 10:44:45 +00:00
var oldPw utils.HashString
2023-09-06 22:20:09 +01:00
assert.NoError(t, query.Scan(&oldPw))
2024-01-29 10:44:45 +00:00
assert.NoError(t, utils.CheckPasswordHash(oldPw, "new"))
2023-09-06 22:20:09 +01:00
assert.NoError(t, query.Err())
assert.NoError(t, query.Close())
}
func TestTx_ModifyUser(t *testing.T) {
u := uuid.New()
2024-01-29 10:44:45 +00:00
pw, err := utils.HashPassword("test")
2023-09-06 22:20:09 +01:00
assert.NoError(t, err)
d, err := Open("file::memory:")
assert.NoError(t, err)
_, err = d.db.Exec(`INSERT INTO users (subject, name, username, password, email, updated_at) VALUES (?, ?, ?, ?, ?, ?)`, u.String(), "Test", "test", pw, "test@localhost", updatedAt())
2023-09-06 22:20:09 +01:00
assert.NoError(t, err)
tx, err := d.Begin()
assert.NoError(t, err)
assert.NoError(t, tx.ModifyUser(u, &UserPatch{
Name: "example",
Pronouns: pronouns.TheyThem,
ZoneInfo: time.UTC,
Locale: language.AmericanEnglish,
2023-09-06 22:20:09 +01:00
}))
}