testing account deletion

This commit is contained in:
Roland Osborne 2022-03-10 10:07:06 -08:00
parent 5609704760
commit 3642d0f232
4 changed files with 112 additions and 23 deletions

View File

@ -23,11 +23,6 @@ func GetAccountAsset(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
func RemoveAccount(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func RemoveAccountApp(w http.ResponseWriter, r *http.Request) { func RemoveAccountApp(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)

View File

@ -0,0 +1,88 @@
package databag
import (
"os"
"gorm.io/gorm"
"net/http"
"databag/internal/store"
)
func RemoveAccount(w http.ResponseWriter, r *http.Request) {
account, err := AccountLogin(r)
if err != nil {
ErrResponse(w, http.StatusUnauthorized, err)
return
}
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Where("account_id = ?", account.ID).Delete(&store.Tag{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.TagSlot{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.Asset{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.Topic{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.TopicSlot{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.ChannelSlot{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.Channel{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.Article{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.ArticleSlot{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.CardSlot{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.Card{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.Group{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.GroupData{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.Group{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.App{}).Error; res != nil {
return res
}
if res := tx.Where("account_id = ?", account.ID).Delete(&store.AccountToken{}).Error; res != nil {
return res
}
if res := tx.Delete(&store.AccountDetail{}, account.AccountDetailID).Error; res != nil {
return res
}
if res := tx.Delete(account).Error; res != nil {
return res
}
return nil
})
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
// delete asset files
path := getStrConfigValue(CONFIG_ASSETPATH, ".") + "/" + account.Guid
if err = os.RemoveAll(path); err != nil {
ErrMsg(err)
}
WriteResponse(w, nil)
}

View File

@ -36,58 +36,58 @@ func RemoveNodeAccount(w http.ResponseWriter, r *http.Request) {
} }
err := store.DB.Transaction(func(tx *gorm.DB) error { err := store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Where("account_id = ?", accountId).Delete(&store.Tag{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.Tag{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.TagSlot{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.TagSlot{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.Asset{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.Asset{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.Topic{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.Topic{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.TopicSlot{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.TopicSlot{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.ChannelSlot{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.ChannelSlot{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.Channel{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.Channel{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.Article{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.Article{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.ArticleSlot{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.ArticleSlot{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.CardSlot{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.CardSlot{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.Card{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.Card{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.Group{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.Group{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.GroupData{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.GroupData{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.Group{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.Group{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.App{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.App{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.AccountDetail{}).Error; res != nil { if res := tx.Where("account_id = ?", account.ID).Delete(&store.AccountToken{}).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.Account{}).Error; res != nil { if res := tx.Delete(&store.AccountDetail{}, account.AccountDetailID).Error; res != nil {
return res return res
} }
if res := tx.Where("account_id = ?", accountId).Delete(&store.AccountToken{}).Error; res != nil { if res := tx.Delete(account).Error; res != nil {
return res return res
} }
return nil return nil

View File

@ -119,4 +119,10 @@ func TestAccountConfig(t *testing.T) {
assert.Equal(t, 1, len(profiles)) assert.Equal(t, 1, len(profiles))
assert.Equal(t, set.A.Guid, profiles[0].Guid); assert.Equal(t, set.A.Guid, profiles[0].Guid);
PrintMsg(set.A.Guid)
// delete account
params = &TestApiParams{ query: "/account/profile", authorization: "newguy:ssap" }
assert.NoError(t, TestApiRequest(RemoveAccount, params, nil))
} }