From 149ceadc4ed02b6ece1a64c13d43473a0d356242 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Wed, 9 Feb 2022 23:54:10 -0800 Subject: [PATCH] adding contact list test --- net/server/internal/api_contact.go | 5 -- net/server/internal/api_setCardProfile.go | 96 +++++++++++++++++++++++ net/server/internal/ucContactSync_test.go | 33 ++++++++ 3 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 net/server/internal/api_setCardProfile.go create mode 100644 net/server/internal/ucContactSync_test.go diff --git a/net/server/internal/api_contact.go b/net/server/internal/api_contact.go index 75422d53..270359e4 100644 --- a/net/server/internal/api_contact.go +++ b/net/server/internal/api_contact.go @@ -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) diff --git a/net/server/internal/api_setCardProfile.go b/net/server/internal/api_setCardProfile.go new file mode 100644 index 00000000..768c65d9 --- /dev/null +++ b/net/server/internal/api_setCardProfile.go @@ -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)) +} + + + + + + + + diff --git a/net/server/internal/ucContactSync_test.go b/net/server/internal/ucContactSync_test.go new file mode 100644 index 00000000..ddfba5c6 --- /dev/null +++ b/net/server/internal/ucContactSync_test.go @@ -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+JOiXP3bGOMfwLf7v3oAwAxiBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliBySIHZA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EDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBhWz2Az/Laj9UT4BIuOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgITbGGP1BoDLueyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7ICEnwAAAP//DQ4epwV6rzkAAAAASUVORK5CYII=" + assert.NoError(t, ApiTestMsg(SetProfileImage, "PUT", "/profile/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) +}