diff --git a/pkg/miniauth/miniauth.go b/pkg/miniauth/miniauth.go index f961d07..54b26ea 100644 --- a/pkg/miniauth/miniauth.go +++ b/pkg/miniauth/miniauth.go @@ -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"}) if err != nil { + m.store.MailRemove(email) + m.store.UserDelete(username) return utils.WrapError(ErrCantSendMail, err, log) } diff --git a/pkg/miniauth/miniauth_test.go b/pkg/miniauth/miniauth_test.go index 3c79660..024ae29 100644 --- a/pkg/miniauth/miniauth_test.go +++ b/pkg/miniauth/miniauth_test.go @@ -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 { us, err := userstore.NewDummyStore() assert.Nil(t, err, "[setup] should be abel to creat dummy store") diff --git a/pkg/userstore/migrations/1741770848_init.up.sql b/pkg/userstore/migrations/1741770848_init.up.sql index 98f773c..24fd078 100644 --- a/pkg/userstore/migrations/1741770848_init.up.sql +++ b/pkg/userstore/migrations/1741770848_init.up.sql @@ -20,3 +20,4 @@ CREATE TABLE mail ( FOREIGN KEY(user_id) REFERENCES users(id) ); +PRAGMA foreign_keys = ON; diff --git a/pkg/userstore/store.go b/pkg/userstore/store.go index fbfc2a4..e13d4f2 100644 --- a/pkg/userstore/store.go +++ b/pkg/userstore/store.go @@ -48,6 +48,11 @@ func NewStore(c Config) (Store, error) { log: log, } + _, err = s.db.Exec("PRAGMA foreign_keys = ON;") + if err != nil { + return Store{}, err + } + return s, nil } @@ -59,6 +64,7 @@ func NewDummyStore() (Store, error) { c := Config{} c.SQLite.Path = fmt.Sprintf("file://%v/%v.sqlite", name, uuid.NewString()) + fmt.Println(c.SQLite.Path) store, err := NewStore(c) if err != nil { @@ -71,5 +77,10 @@ func NewDummyStore() (Store, error) { return Store{}, err } + _, err = store.db.Exec("PRAGMA foreign_keys = ON;") + if err != nil { + return Store{}, err + } + return store, nil } diff --git a/pkg/userstore/users_test.go b/pkg/userstore/users_test.go index 8e4c15f..b445c63 100644 --- a/pkg/userstore/users_test.go +++ b/pkg/userstore/users_test.go @@ -119,6 +119,7 @@ func TestUserRemoveFaildMailExists(t *testing.T) { assert.Nil(t, err, "[setup] should be abel to create user2") err = store.MailAdd("foo@example.com", id, true) + assert.Nil(t, err, "[setup] should be abel to create mail for user2") var count int 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") 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) 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") }