mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
adding contact list test
This commit is contained in:
parent
daf0f44170
commit
149ceadc4e
@ -48,11 +48,6 @@ func SetCardNotes(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func SetCardProfile(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func SetCloseMessage(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
96
net/server/internal/api_setCardProfile.go
Normal file
96
net/server/internal/api_setCardProfile.go
Normal file
@ -0,0 +1,96 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"gorm.io/gorm"
|
||||
"github.com/gorilla/mux"
|
||||
"databag/internal/store"
|
||||
)
|
||||
|
||||
func SetCardProfile(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)
|
||||
cardId := params["cardId"]
|
||||
|
||||
var message DataMessage
|
||||
if err := ParseRequest(r, w, &message); err != nil {
|
||||
ErrResponse(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
var identity Identity
|
||||
guid, messageType, _, err := ReadDataMessage(&message, &identity)
|
||||
if messageType != APP_MSGIDENTITY || err != nil {
|
||||
ErrResponse(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
PrintMsg(cardId)
|
||||
slot := store.CardSlot{}
|
||||
if err := store.DB.Preload("Card.Groups").Where("account_id = ? AND card_slot_id = ?", account.ID, cardId).First(&slot).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
ErrResponse(w, http.StatusNotFound, err)
|
||||
} else {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
card := slot.Card
|
||||
if card == nil {
|
||||
ErrResponse(w, http.StatusNotFound, errors.New("referenced empty card"))
|
||||
return
|
||||
}
|
||||
if card.Guid != guid {
|
||||
ErrResponse(w, http.StatusBadRequest, errors.New("invalid profile"))
|
||||
return
|
||||
}
|
||||
|
||||
if identity.Revision > card.ProfileRevision {
|
||||
card.Username = identity.Handle
|
||||
card.Name = identity.Name
|
||||
card.Description = identity.Description
|
||||
card.Location = identity.Location
|
||||
card.Image = identity.Image
|
||||
card.Version = identity.Version
|
||||
card.Node = identity.Node
|
||||
card.ProfileRevision = identity.Revision
|
||||
}
|
||||
|
||||
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
||||
|
||||
if res := tx.Save(card).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
slot.Revision = account.CardRevision + 1
|
||||
if res := tx.Save(&slot).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
if res := tx.Model(&account).Update("card_revision", account.CardRevision + 1).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
SetStatus(account)
|
||||
WriteResponse(w, getCardModel(&slot))
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
33
net/server/internal/ucContactSync_test.go
Normal file
33
net/server/internal/ucContactSync_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestContactSync(t *testing.T) {
|
||||
var profile Profile
|
||||
var msg DataMessage
|
||||
var card Card
|
||||
param := map[string]string{}
|
||||
|
||||
// setup testing group
|
||||
set, err := AddTestGroup("contactsync")
|
||||
assert.NoError(t, err)
|
||||
|
||||
// set profile image
|
||||
image := "iVBORw0KGgoAAAANSUhEUgAAAaQAAAGkCAIAAADxLsZiAAAFzElEQVR4nOzWUY3jMBhG0e0qSEqoaIqiaEIoGAxh3gZAldid3nMI+JOiXP3bGOMfwLf7v3oAwAxiBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJGzTXnrtx7S3pnk+7qsnnMk3+ny+0dtcdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQnbtJeej/u0t+Bb+Y/e5rIDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSbmOM1RsALueyAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsg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image",
|
||||
nil, image,
|
||||
APP_TOKENAPP, set.A.Token, &profile, nil))
|
||||
|
||||
// sync profile
|
||||
assert.NoError(t, ApiTestMsg(GetProfileMessage, "GET", "/profile/message",
|
||||
nil, nil,
|
||||
APP_TOKENCONTACT, set.B.A.Token, &msg, nil))
|
||||
param["cardId"] = set.B.A.CardId
|
||||
assert.NoError(t, ApiTestMsg(SetCardProfile, "PUT", "/contact/cards/{cardId}/profile",
|
||||
¶m, &msg,
|
||||
APP_TOKENAPP, set.B.Token, &card, nil))
|
||||
assert.True(t, card.Data.CardProfile.ImageSet)
|
||||
}
|
Loading…
Reference in New Issue
Block a user