chore: add delete mail and deete user function
This commit is contained in:
parent
b0578f5eda
commit
dab6e121a2
4 changed files with 97 additions and 0 deletions
pkg/userstore
|
@ -34,3 +34,15 @@ func (s Store) MailCheckExists(email string) (bool, error) {
|
|||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (s Store) MailRemove(email string) error {
|
||||
query := "DELETE FROM mail WHERE mail = ?"
|
||||
log := s.log.With().Str("func", "MailRemove").Str("email", email).Str("query", query).Logger()
|
||||
|
||||
_, err := s.db.Exec(query, email)
|
||||
if err != nil {
|
||||
return utils.WrapError(ErrCantExecuteQuery, err, log)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -97,3 +97,28 @@ func TestAddMail(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteMail(t *testing.T) {
|
||||
store, err := NewDummyStore()
|
||||
assert.Nil(t, err, "[setup] should be abel to create dummystore")
|
||||
|
||||
_, err = store.UserWrite("kekskurse", "kekskurse")
|
||||
assert.Nil(t, err, "[setup] should be abel to store user")
|
||||
|
||||
err = store.MailAdd("mail1@example.com", 1, true)
|
||||
assert.Nil(t, err, "[setup] should be abel to store mail")
|
||||
err = store.MailAdd("mail2@example.com", 1, false)
|
||||
assert.Nil(t, err, "[setup] should be abel to store mail2")
|
||||
|
||||
var count int
|
||||
err = store.db.QueryRow("SELECT COUNT(*) FROM mail").Scan(&count)
|
||||
assert.Nil(t, err, "should execute count query for mail")
|
||||
assert.Equal(t, 2, count, "should found 2 mails in the database")
|
||||
|
||||
err = store.MailRemove("mail1@example.com")
|
||||
assert.Nil(t, err, "should be abel to remove mail wthout error")
|
||||
|
||||
err = store.db.QueryRow("SELECT COUNT(*) FROM mail").Scan(&count)
|
||||
assert.Nil(t, err, "should execute count query for mail")
|
||||
assert.Equal(t, 1, count, "should found 1 mails in the database")
|
||||
}
|
||||
|
|
|
@ -42,3 +42,15 @@ func (s Store) UserExists(username string) (bool, error) {
|
|||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s Store) UserDelete(username string) error {
|
||||
query := "DELETE FROM users WHERE username = ?"
|
||||
log := s.log.With().Str("func", "UserExists").Str("query", query).Str("username", username).Logger()
|
||||
|
||||
_, err := s.db.Exec(query, username)
|
||||
if err != nil {
|
||||
return utils.WrapError(ErrCantExecuteQuery, err, log)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -84,3 +84,51 @@ func TestUserExists(t *testing.T) {
|
|||
assert.Nil(t, err)
|
||||
assert.True(t, exists)
|
||||
}
|
||||
|
||||
func TestUserRemove(t *testing.T) {
|
||||
store, err := NewDummyStore()
|
||||
assert.Nil(t, err, "[setup] should get dummy store without error")
|
||||
|
||||
_, err = store.UserWrite("kekskurse", "kekskurse")
|
||||
assert.Nil(t, err, "[setup] should be abel to create user")
|
||||
|
||||
_, err = store.UserWrite("kekskurse2", "kekskurse2")
|
||||
assert.Nil(t, err, "[setup] should be abel to create user2")
|
||||
|
||||
var count int
|
||||
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, 2, count, "should found 2 users")
|
||||
|
||||
err = store.UserDelete("kekskurse2")
|
||||
assert.Nil(t, err, "should be abel to delete user without 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")
|
||||
}
|
||||
|
||||
func TestUserRemoveFaildMailExists(t *testing.T) {
|
||||
store, err := NewDummyStore()
|
||||
assert.Nil(t, err, "[setup] should get dummy store without error")
|
||||
|
||||
_, err = store.UserWrite("kekskurse", "kekskurse")
|
||||
assert.Nil(t, err, "[setup] should be abel to create user")
|
||||
|
||||
id, err := store.UserWrite("kekskurse2", "kekskurse2")
|
||||
assert.Nil(t, err, "[setup] should be abel to create user2")
|
||||
|
||||
err = store.MailAdd("foo@example.com", id, true)
|
||||
|
||||
var count int
|
||||
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, 2, count, "should found 2 users")
|
||||
|
||||
err = store.UserDelete("kekskurse2")
|
||||
assert.Nil(t, err, "should be abel to delete user without 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")
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue