testing article delete

This commit is contained in:
Roland Osborne 2022-02-13 22:47:18 -08:00
parent 579052d7bc
commit d26522fc86
5 changed files with 280 additions and 15 deletions

View File

@ -13,24 +13,10 @@ import (
"net/http"
)
func ClearArticleGroup(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func GetArticleSubjectField(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func RemoveArticle(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func SetArticleGroup(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}

View File

@ -0,0 +1,80 @@
package databag
import (
"errors"
"net/http"
"gorm.io/gorm"
"github.com/gorilla/mux"
"databag/internal/store"
)
func ClearArticleGroup(w http.ResponseWriter, r *http.Request) {
account, code, err := BearerAppToken(r, false);
if err != nil {
ErrResponse(w, code, err)
return
}
// scan parameters
params := mux.Vars(r)
articleId := params["articleId"]
groupId := params["groupId"]
// load referenced article
var articleSlot store.ArticleSlot
if err := store.DB.Preload("Article").Where("account_id = ? AND article_slot_id = ?", account.ID, articleId).First(&articleSlot).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
ErrResponse(w, http.StatusNotFound, err)
}
return
}
if articleSlot.Article == nil {
ErrResponse(w, http.StatusNotFound, errors.New("article has been deleted"))
return
}
// load referenced group
var groupSlot store.GroupSlot
if err := store.DB.Preload("Group.Cards").Where("account_id = ? AND group_slot_id = ?", account.ID, groupId).First(&groupSlot).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
ErrResponse(w, http.StatusNotFound, err)
}
return
}
if groupSlot.Group == nil {
ErrResponse(w, http.StatusNotFound, errors.New("group has been deleted"))
return
}
// save and update contact revision
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Model(&articleSlot.Article).Association("Groups").Delete(groupSlot.Group); res != nil {
return res
}
if res := tx.Model(&articleSlot).Update("revision", account.ArticleRevision + 1).Error; res != nil {
return res
}
if res := tx.Model(&account).Update("article_revision", account.ArticleRevision + 1).Error; res != nil {
return res
}
return nil
})
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
// notify contacts of content change
SetStatus(account)
for _, card := range groupSlot.Group.Cards {
SetContactArticleNotification(account, &card)
}
WriteResponse(w, getArticleModel(&articleSlot, true));
}

View File

@ -0,0 +1,79 @@
package databag
import (
"errors"
"net/http"
"gorm.io/gorm"
"github.com/gorilla/mux"
"databag/internal/store"
)
func RemoveArticle(w http.ResponseWriter, r *http.Request) {
account, code, err := BearerAppToken(r, false);
if err != nil {
ErrResponse(w, code, err)
return
}
// scan parameters
params := mux.Vars(r)
articleId := params["articleId"]
// load referenced article
var slot store.ArticleSlot
if err := store.DB.Preload("Article.Groups.Cards").Where("account_id = ? AND article_slot_id = ?", account.ID, articleId).First(&slot).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
ErrResponse(w, http.StatusNotFound, err)
}
return
}
if slot.Article == nil {
ErrResponse(w, http.StatusNotFound, errors.New("article has been deleted"))
return
}
// determine affected contact list
cards := make(map[string]*store.Card)
for _, group := range slot.Article.Groups {
for _, card := range group.Cards {
cards[card.Guid] = &card
}
}
// save and update contact revision
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Model(&slot.Article).Association("Groups").Clear(); res != nil {
return res
}
if res := tx.Delete(&slot.Article).Error; res != nil {
return res
}
slot.Article = nil
if res := tx.Model(&slot).Update("article_id", 0).Error; res != nil {
return res
}
if res := tx.Model(&slot).Update("revision", account.ArticleRevision + 1).Error; res != nil {
return res
}
if res := tx.Model(&account).Update("article_revision", account.ArticleRevision + 1).Error; res != nil {
return res
}
return nil
})
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
// notify contacts of content change
SetStatus(account)
for _, card := range cards {
SetContactArticleNotification(account, card)
}
WriteResponse(w, getArticleModel(&slot, true));
}

View File

@ -0,0 +1,80 @@
package databag
import (
"errors"
"net/http"
"gorm.io/gorm"
"github.com/gorilla/mux"
"databag/internal/store"
)
func SetArticleGroup(w http.ResponseWriter, r *http.Request) {
account, code, err := BearerAppToken(r, false);
if err != nil {
ErrResponse(w, code, err)
return
}
// scan parameters
params := mux.Vars(r)
articleId := params["articleId"]
groupId := params["groupId"]
// load referenced article
var articleSlot store.ArticleSlot
if err := store.DB.Preload("Article").Where("account_id = ? AND article_slot_id = ?", account.ID, articleId).First(&articleSlot).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
ErrResponse(w, http.StatusNotFound, err)
}
return
}
if articleSlot.Article == nil {
ErrResponse(w, http.StatusNotFound, errors.New("article has been deleted"))
return
}
// load referenced group
var groupSlot store.GroupSlot
if err := store.DB.Preload("Group.Cards").Preload("Group.GroupSlot").Where("account_id = ? AND group_slot_id = ?", account.ID, groupId).First(&groupSlot).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
ErrResponse(w, http.StatusNotFound, err)
}
return
}
if groupSlot.Group == nil {
ErrResponse(w, http.StatusNotFound, errors.New("group has been deleted"))
return
}
// save and update contact revision
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Model(&articleSlot.Article).Association("Groups").Append(groupSlot.Group); res != nil {
return res
}
if res := tx.Model(&articleSlot).Update("revision", account.ArticleRevision + 1).Error; res != nil {
return res
}
if res := tx.Model(&account).Update("article_revision", account.ArticleRevision + 1).Error; res != nil {
return res
}
return nil
})
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
// notify contacts of content change
SetStatus(account)
for _, card := range groupSlot.Group.Cards {
SetContactArticleNotification(account, &card)
}
WriteResponse(w, getArticleModel(&articleSlot, true));
}

View File

@ -31,6 +31,15 @@ func TestShareAttribute(t *testing.T) {
nil, nil, APP_TOKENAPP, set.A.Token, articles, nil))
assert.Equal(t, 1, len(*articles))
// share article with B
param["articleId"] = article.Id
param["groupId"] = set.A.B.GroupId
article = &Article{}
assert.NoError(t, ApiTestMsg(SetArticleGroup, "PUT", "/attribute/articles/{articleId}/groups/{groupId}",
&param, nil, APP_TOKENAPP, set.A.Token, article, nil))
// TODO validate B & C view
// update attribute
data := "{ \"nested\" : { \"image\" : \"iVBORw0KGgoAAAANSUhEUgAAAaQAAAGkCAIAAADxLsZiAAAFzElEQVR4nOzWUY3jMBhG0e0qSEqoaIqiaEIoGAxh3gZAldid3nMI+JOiXP3bGOMfwLf7v3oAwAxiBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJGzTXnrtx7S3pnk+7qsnnMk3+ny+0dtcdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQnbtJeej/u0t+Bb+Y/e5rIDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEs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QeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgITbGGP1BoDLueyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7ICEnwAAAP//DQ4epwV6rzkAAAAASUVORK5CYII=\" } }"
subject = &Subject{ Data: data, DataType: "nestedimage" }
@ -39,6 +48,37 @@ func TestShareAttribute(t *testing.T) {
assert.NoError(t, ApiTestMsg(SetArticleSubject, "PUT", "/attribute/articles/{articleId}/subject",
&param, subject, APP_TOKENAPP, set.A.Token, article, nil))
PrintMsg(article)
// TODO validate B & C view
// share article with C
param["articleId"] = article.Id
param["groupId"] = set.A.C.GroupId
article = &Article{}
assert.NoError(t, ApiTestMsg(SetArticleGroup, "PUT", "/attribute/articles/{articleId}/groups/{groupId}",
&param, nil, APP_TOKENAPP, set.A.Token, article, nil))
// TODO C retrieve image
// TODO validate B & C view
// unshare article with B
param["articleId"] = article.Id
param["groupId"] = set.A.B.GroupId
article = &Article{}
assert.NoError(t, ApiTestMsg(ClearArticleGroup, "DELETE", "/attribute/articles/{articleId}/groups/{groupId}",
&param, nil, APP_TOKENAPP, set.A.Token, article, nil))
// TODO validate B & C view
// delete article
param["articleId"] = article.Id
assert.NoError(t, ApiTestMsg(RemoveArticle, "DELETE", "/attribute/articles/{articleId}",
&param, nil, APP_TOKENAPP, set.A.Token, nil, nil))
articles = &[]Article{}
assert.NoError(t, ApiTestMsg(GetArticles, "GET", "/attribute/articles",
nil, nil, APP_TOKENAPP, set.A.Token, articles, nil))
assert.Equal(t, 0, len(*articles))
// TODO validate B & C view
}