chore: dont store user if cant send mail to
Some checks failed
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/playwright Pipeline failed
ci/woodpecker/push/deplyoment unknown status

This commit is contained in:
kekskurse 2025-03-16 14:29:21 +01:00
parent dab6e121a2
commit 42358e03a4
5 changed files with 32 additions and 2 deletions

View file

@ -65,6 +65,8 @@ func (m Miniauth) RegisterUser(username string, email string, password string) e
err = m.smtp.SendMail("register", email, map[string]any{"hash": "abc"}) err = m.smtp.SendMail("register", email, map[string]any{"hash": "abc"})
if err != nil { if err != nil {
m.store.MailRemove(email)
m.store.UserDelete(username)
return utils.WrapError(ErrCantSendMail, err, log) return utils.WrapError(ErrCantSendMail, err, log)
} }

View file

@ -169,6 +169,20 @@ func TestInvalideUsernames(t *testing.T) {
} }
} }
func TestUserAndMailIsDeletedIfMailCantBeSend(t *testing.T) {
ma := getMiniAuth(t)
err := ma.RegisterUser("kekskurse", "error@error.error", "GM18slErbHpuGl7M$!!DD9ZayQzzMWwxqEuTfzJ%XEMjf")
assert.ErrorIs(t, err, ErrCantSendMail, "should not be abeld to create user")
res, err := ma.store.MailCheckExists("error@error.error")
assert.Nil(t, err)
assert.False(t, res, "should not found mail")
res, err = ma.store.UserExists("kekskurse")
assert.Nil(t, err)
assert.False(t, res, "should not found user")
}
func getMiniAuth(t *testing.T) Miniauth { func getMiniAuth(t *testing.T) Miniauth {
us, err := userstore.NewDummyStore() us, err := userstore.NewDummyStore()
assert.Nil(t, err, "[setup] should be abel to creat dummy store") assert.Nil(t, err, "[setup] should be abel to creat dummy store")

View file

@ -20,3 +20,4 @@ CREATE TABLE mail (
FOREIGN KEY(user_id) REFERENCES users(id) FOREIGN KEY(user_id) REFERENCES users(id)
); );
PRAGMA foreign_keys = ON;

View file

@ -48,6 +48,11 @@ func NewStore(c Config) (Store, error) {
log: log, log: log,
} }
_, err = s.db.Exec("PRAGMA foreign_keys = ON;")
if err != nil {
return Store{}, err
}
return s, nil return s, nil
} }
@ -59,6 +64,7 @@ func NewDummyStore() (Store, error) {
c := Config{} c := Config{}
c.SQLite.Path = fmt.Sprintf("file://%v/%v.sqlite", name, uuid.NewString()) c.SQLite.Path = fmt.Sprintf("file://%v/%v.sqlite", name, uuid.NewString())
fmt.Println(c.SQLite.Path)
store, err := NewStore(c) store, err := NewStore(c)
if err != nil { if err != nil {
@ -71,5 +77,10 @@ func NewDummyStore() (Store, error) {
return Store{}, err return Store{}, err
} }
_, err = store.db.Exec("PRAGMA foreign_keys = ON;")
if err != nil {
return Store{}, err
}
return store, nil return store, nil
} }

View file

@ -119,6 +119,7 @@ func TestUserRemoveFaildMailExists(t *testing.T) {
assert.Nil(t, err, "[setup] should be abel to create user2") assert.Nil(t, err, "[setup] should be abel to create user2")
err = store.MailAdd("foo@example.com", id, true) err = store.MailAdd("foo@example.com", id, true)
assert.Nil(t, err, "[setup] should be abel to create mail for user2")
var count int var count int
err = store.db.QueryRow("SELECT COUNT(*) FROM users").Scan(&count) err = store.db.QueryRow("SELECT COUNT(*) FROM users").Scan(&count)
@ -126,9 +127,10 @@ func TestUserRemoveFaildMailExists(t *testing.T) {
assert.Equal(t, 2, count, "should found 2 users") assert.Equal(t, 2, count, "should found 2 users")
err = store.UserDelete("kekskurse2") err = store.UserDelete("kekskurse2")
assert.Nil(t, err, "should be abel to delete user without error") assert.NotNil(t, err)
assert.Equal(t, "cant execute query: constraint failed: FOREIGN KEY constraint failed (787)", err.Error())
err = store.db.QueryRow("SELECT COUNT(*) FROM users").Scan(&count) err = store.db.QueryRow("SELECT COUNT(*) FROM users").Scan(&count)
assert.Nil(t, err, "should be abel to get user count without error") assert.Nil(t, err, "should be abel to get user count without error")
assert.Equal(t, 1, count, "should found 2 users") assert.Equal(t, 2, count, "should found 2 users")
} }