mirror of
https://github.com/balzack/databag.git
synced 2025-03-13 00:50:03 +00:00
disconnection test
This commit is contained in:
parent
ee2e9edd68
commit
71dc276c93
@ -13,18 +13,8 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetCloseMessage(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func RemoveCard(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)
|
||||
}
|
||||
|
||||
|
54
net/server/internal/api_getCloseMessage.go
Normal file
54
net/server/internal/api_getCloseMessage.go
Normal file
@ -0,0 +1,54 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"gorm.io/gorm"
|
||||
"github.com/gorilla/mux"
|
||||
"databag/internal/store"
|
||||
)
|
||||
|
||||
func GetCloseMessage(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
account, code, res := BearerAppToken(r, true);
|
||||
if res != nil {
|
||||
ErrResponse(w, code, res)
|
||||
return
|
||||
}
|
||||
detail := account.AccountDetail
|
||||
cardId := mux.Vars(r)["cardId"]
|
||||
|
||||
var slot store.CardSlot
|
||||
if err := store.DB.Preload("Card").Where("account_id = ? AND card_slot_id = ?", account.ID, cardId).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.Card == nil {
|
||||
ErrResponse(w, http.StatusNotFound, errors.New("card has been deleted"))
|
||||
return
|
||||
}
|
||||
|
||||
if slot.Card.Status == APP_CARDCONNECTING || slot.Card.Status == APP_CARDCONNECTED {
|
||||
ErrResponse(w, http.StatusMethodNotAllowed, errors.New("invalid card state"))
|
||||
return
|
||||
}
|
||||
|
||||
disconnect := &Disconnect{
|
||||
Contact: slot.Card.Guid,
|
||||
}
|
||||
|
||||
msg, err := WriteDataMessage(detail.PrivateKey, detail.PublicKey, detail.KeyType,
|
||||
APP_SIGNPKCS1V15, account.Guid, APP_MSGDISCONNECT, &disconnect)
|
||||
if err != nil {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
WriteResponse(w, msg)
|
||||
}
|
||||
|
||||
|
82
net/server/internal/api_setCloseMessage.go
Normal file
82
net/server/internal/api_setCloseMessage.go
Normal file
@ -0,0 +1,82 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"time"
|
||||
"errors"
|
||||
"net/http"
|
||||
"gorm.io/gorm"
|
||||
"databag/internal/store"
|
||||
)
|
||||
|
||||
func SetCloseMessage(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var message DataMessage
|
||||
if err := ParseRequest(r, w, &message); err != nil {
|
||||
ErrResponse(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
var disconnect Disconnect
|
||||
guid, messageType, ts, err := ReadDataMessage(&message, &disconnect)
|
||||
if messageType != APP_MSGDISCONNECT || err != nil {
|
||||
ErrResponse(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
if ts + APP_CONNECTEXPIRE < time.Now().Unix() {
|
||||
ErrResponse(w, http.StatusBadRequest, errors.New("message has expired"))
|
||||
return
|
||||
}
|
||||
|
||||
// load referenced account
|
||||
var account store.Account
|
||||
if err := store.DB.Where("guid = ?", disconnect.Contact).First(&account).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
ErrResponse(w, http.StatusNotFound, err)
|
||||
} else {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// see if card exists
|
||||
var card store.Card
|
||||
if err := store.DB.Preload("CardSlot").Where("account_id = ? AND guid = ?", account.Guid, guid).First(&card).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
} else {
|
||||
WriteResponse(w, nil)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
slot := card.CardSlot
|
||||
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if card.Status == APP_CARDPENDING {
|
||||
if res := tx.Model(&slot).Update("card_id", 0).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
if res := tx.Delete(&card).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
} else {
|
||||
if res := tx.Model(&card).Update("status", APP_CARDCONFIRMED).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
}
|
||||
if res := tx.Model(&slot).Update("revision", account.CardRevision + 1).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, nil)
|
||||
}
|
||||
|
@ -109,8 +109,10 @@ func ApiTestMsg(
|
||||
if params != nil {
|
||||
r = mux.SetURLVars(r, *params)
|
||||
}
|
||||
if token != "" {
|
||||
if tokenType != "" {
|
||||
r.Header.Add("TokenType", tokenType)
|
||||
}
|
||||
if token != "" {
|
||||
SetBearerAuth(r, token)
|
||||
}
|
||||
endpoint(w, r)
|
||||
@ -239,6 +241,9 @@ func AddTestGroup(prefix string) (*TestGroup, error) {
|
||||
if g.D.C.GroupId, err = GroupTestCard(g.D.Token, g.D.C.CardId); err != nil {
|
||||
return g, err
|
||||
}
|
||||
if g.D.A.CardId, err = GetCardId(g.D.Token, g.A.Guid); err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// get contact tokens
|
||||
if g.A.B.Token, err = GetCardToken(g.A.Token, g.A.B.CardId); err != nil {
|
||||
@ -354,6 +359,30 @@ func GetCardToken(account string, cardId string) (token string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func GetCardId(account string, guid string) (cardId string, err error) {
|
||||
var r *http.Request
|
||||
var w *httptest.ResponseRecorder
|
||||
var cards []Card
|
||||
|
||||
if r, w, err = NewRequest("GET", "/contact/cards", nil); err != nil {
|
||||
return
|
||||
}
|
||||
SetBearerAuth(r, account)
|
||||
GetCards(w, r)
|
||||
if err = ReadResponse(w, &cards); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, card := range cards {
|
||||
if card.Data.CardProfile.Guid == guid {
|
||||
cardId = card.Id
|
||||
return
|
||||
}
|
||||
}
|
||||
err = errors.New("card not found")
|
||||
return
|
||||
}
|
||||
|
||||
func GroupTestCard(account string, cardId string) (groupId string, err error) {
|
||||
var r *http.Request
|
||||
var w *httptest.ResponseRecorder
|
||||
|
@ -87,8 +87,8 @@ func TestContactSync(t *testing.T) {
|
||||
param["cardId"] = set.B.A.CardId
|
||||
assert.NoError(t, ApiTestMsg(GetCard, "GET", "/contact/cards/{cardId}",
|
||||
¶m, nil, APP_TOKENAPP, set.B.Token, card, nil))
|
||||
assert.Equal(t, "connected", card.Data.CardDetail.Status)
|
||||
viewRevision = card.Data.NotifiedView
|
||||
|
||||
card = &Card{}
|
||||
param["cardId"] = set.A.B.CardId
|
||||
param["groupId"] = set.A.B.GroupId
|
||||
@ -96,12 +96,43 @@ func TestContactSync(t *testing.T) {
|
||||
¶m, nil, APP_TOKENAPP, set.A.Token, card, nil))
|
||||
assert.Equal(t, 0, len(card.Data.CardDetail.Groups))
|
||||
assert.NotEqual(t, rev.Card, GetTestRevision(set.B.Revisions).Card)
|
||||
|
||||
card = &Card{}
|
||||
param["cardId"] = set.B.A.CardId
|
||||
assert.NoError(t, ApiTestMsg(GetCard, "GET", "/contact/cards/{cardId}",
|
||||
¶m, nil, APP_TOKENAPP, set.B.Token, card, nil))
|
||||
assert.NotEqual(t, viewRevision, card.Data.NotifiedView)
|
||||
PrintMsg(card)
|
||||
|
||||
// disconnect card
|
||||
card = &Card{}
|
||||
param["cardId"] = set.A.B.CardId
|
||||
assert.NoError(t, ApiTestMsg(SetCardStatus, "PUT", "/contact/cards/{cardId}/status",
|
||||
¶m, APP_CARDCONFIRMED, APP_TOKENAPP, set.A.Token, card, nil))
|
||||
assert.NoError(t, ApiTestMsg(GetCloseMessage, "GET", "/contact/cards/{cardId}/closeMessage",
|
||||
¶m, nil, APP_TOKENAPP, set.A.Token, &msg, nil))
|
||||
assert.NoError(t, ApiTestMsg(SetCloseMessage, "GET", "/contact/closeMessage",
|
||||
nil, &msg, "", "", nil, nil))
|
||||
assert.NotNil(t, GetTestRevision(set.B.Revisions))
|
||||
card = &Card{}
|
||||
param["cardId"] = set.B.A.CardId
|
||||
assert.NoError(t, ApiTestMsg(GetCard, "GET", "/contact/cards/{cardId}",
|
||||
¶m, nil, APP_TOKENAPP, set.B.Token, card, nil))
|
||||
assert.Equal(t, "confirmed", card.Data.CardDetail.Status)
|
||||
|
||||
// cancel request
|
||||
card = &Card{}
|
||||
param["cardId"] = set.D.A.CardId
|
||||
assert.NoError(t, ApiTestMsg(GetCard, "GET", "/contact/cards/{cardId}",
|
||||
¶m, nil, APP_TOKENAPP, set.D.Token, card, nil))
|
||||
param["cardId"] = set.A.D.CardId
|
||||
assert.NoError(t, ApiTestMsg(SetCardStatus, "PUT", "/contact/cards/{cardId}/status",
|
||||
¶m, APP_CARDCONFIRMED, APP_TOKENAPP, set.A.Token, card, nil))
|
||||
assert.NoError(t, ApiTestMsg(GetCloseMessage, "GET", "/contact/cards/{cardId}/closeMessage",
|
||||
¶m, nil, APP_TOKENAPP, set.A.Token, &msg, nil))
|
||||
assert.NoError(t, ApiTestMsg(SetCloseMessage, "GET", "/contact/closeMessage",
|
||||
nil, &msg, "", "", nil, nil))
|
||||
param["cardId"] = set.D.A.CardId
|
||||
assert.NoError(t, ApiTestMsg(GetCard, "GET", "/contact/cards/{cardId}",
|
||||
¶m, nil, APP_TOKENAPP, set.D.Token, card, nil))
|
||||
assert.Nil(t, card.Data)
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user