mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 20:49:16 +00:00
connecting websocket to test
This commit is contained in:
parent
6a377a7fc0
commit
7930a10c42
@ -2,6 +2,7 @@ package databag
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"gorm.io/gorm"
|
||||||
"databag/internal/store"
|
"databag/internal/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,11 +31,24 @@ func SetProfile(w http.ResponseWriter, r *http.Request) {
|
|||||||
detail.Name = profileData.Name
|
detail.Name = profileData.Name
|
||||||
detail.Location = profileData.Location
|
detail.Location = profileData.Location
|
||||||
detail.Description = profileData.Description
|
detail.Description = profileData.Description
|
||||||
if store.DB.Save(&detail).Error != nil {
|
|
||||||
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if res := store.DB.Save(&detail).Error; res != nil {
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
if res := store.DB.Model(&account).Update("profile_revision", account.ProfileRevision + 1).Error; res != nil {
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
PrintMsg(err)
|
||||||
|
LogMsg("failed to store profile")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SetStatus(account)
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
@ -35,20 +35,27 @@ func Status(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
log.Println("CONNECTED")
|
// receive announce
|
||||||
// receive announce message
|
t, m, err := conn.ReadMessage()
|
||||||
var act uint = 0
|
if t != websocket.TextMessage || err != nil {
|
||||||
|
LogMsg("failed to receive announce")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var announce Announce
|
||||||
|
if json.Unmarshal(m, &announce) != nil {
|
||||||
|
LogMsg("invalid announce")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// get revisions for the account
|
// retrieve reference account
|
||||||
var account accountRevision
|
var app store.App
|
||||||
err = store.DB.Model(&store.Account{}).Where("ID = ?", act).First(&account).Error
|
if store.DB.Preload("Account").Where("token = ?", announce.AppToken).First(&app).Error != nil {
|
||||||
if err != nil {
|
LogMsg("invalid app token")
|
||||||
log.Println("Status - failed to get account revision")
|
return
|
||||||
// return
|
|
||||||
}
|
}
|
||||||
rev := getRevision(account)
|
|
||||||
|
|
||||||
// send current version
|
// send current version
|
||||||
|
rev := getRevision(app.Account)
|
||||||
var msg []byte
|
var msg []byte
|
||||||
msg, err = json.Marshal(rev)
|
msg, err = json.Marshal(rev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -66,8 +73,8 @@ func Status(w http.ResponseWriter, r *http.Request) {
|
|||||||
defer close(c)
|
defer close(c)
|
||||||
|
|
||||||
// register channel for revisions
|
// register channel for revisions
|
||||||
AddStatusListener(0, c)
|
AddStatusListener(app.Account.ID, c)
|
||||||
defer RemoveStatusListener(act, c)
|
defer RemoveStatusListener(app.Account.ID, c)
|
||||||
|
|
||||||
// send revision until channel is closed
|
// send revision until channel is closed
|
||||||
for {
|
for {
|
||||||
@ -86,15 +93,15 @@ func Status(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRevision(rev accountRevision) Revision {
|
func getRevision(account store.Account) Revision {
|
||||||
var r Revision
|
var r Revision
|
||||||
r.Profile = rev.ProfileRevision
|
r.Profile = account.ProfileRevision
|
||||||
r.Content = rev.ContentRevision
|
r.Content = account.ContentRevision
|
||||||
r.Label = rev.LabelRevision
|
r.Label = account.LabelRevision
|
||||||
r.Group = rev.GroupRevision
|
r.Group = account.GroupRevision
|
||||||
r.Card = rev.CardRevision
|
r.Card = account.CardRevision
|
||||||
r.Dialogue = rev.DialogueRevision
|
r.Dialogue = account.DialogueRevision
|
||||||
r.Insight = rev.InsightRevision
|
r.Insight = account.InsightRevision
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,18 +109,11 @@ func ExitStatus() {
|
|||||||
wsExit <- true
|
wsExit <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetStatus(act uint) {
|
func SetStatus(account store.Account) {
|
||||||
|
|
||||||
// get revisions for the account
|
// get revisions for the account
|
||||||
var ar accountRevision
|
rev := getRevision(account);
|
||||||
err := store.DB.Model(&Revision{}).Where("ID = ?", act).First(&ar).Error
|
msg, err := json.Marshal(rev)
|
||||||
if err != nil {
|
|
||||||
log.Println("SetStatus - failed to retrieve account revisions")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
rev := getRevision(ar)
|
|
||||||
var msg []byte
|
|
||||||
msg, err = json.Marshal(rev)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("SetStatus - failed to marshal revision")
|
log.Println("SetStatus - failed to marshal revision")
|
||||||
return
|
return
|
||||||
@ -124,7 +124,7 @@ func SetStatus(act uint) {
|
|||||||
defer wsSync.Unlock()
|
defer wsSync.Unlock()
|
||||||
|
|
||||||
// notify all listeners
|
// notify all listeners
|
||||||
chs, ok := statusListener[act]
|
chs, ok := statusListener[account.ID]
|
||||||
if ok {
|
if ok {
|
||||||
for _, ch := range chs {
|
for _, ch := range chs {
|
||||||
ch <- msg
|
ch <- msg
|
||||||
|
@ -53,24 +53,6 @@ func TestMain(m *testing.M) {
|
|||||||
panic("failed to set account storage");
|
panic("failed to set account storage");
|
||||||
}
|
}
|
||||||
|
|
||||||
// get account token
|
|
||||||
r, w, _ = NewRequest("POST", "/admin/accounts", nil)
|
|
||||||
SetBasicAuth(r, "admin:pass")
|
|
||||||
AddNodeAccount(w, r)
|
|
||||||
var token string
|
|
||||||
if ReadResponse(w, &token) != nil {
|
|
||||||
panic("failed to create token")
|
|
||||||
}
|
|
||||||
|
|
||||||
// set account profile
|
|
||||||
r, w, _ = NewRequest("GET", "/account/profile", nil)
|
|
||||||
SetBearerAuth(r, token);
|
|
||||||
SetCredentials(r, "test:pass")
|
|
||||||
AddAccount(w, r)
|
|
||||||
if ReadResponse(w, nil) != nil {
|
|
||||||
panic("failed to create account")
|
|
||||||
}
|
|
||||||
|
|
||||||
m.Run()
|
m.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ type Account struct {
|
|||||||
LabelRevision int64 `gorm:"not null;default:1"`
|
LabelRevision int64 `gorm:"not null;default:1"`
|
||||||
CardRevision int64 `gorm:"not null;default:1"`
|
CardRevision int64 `gorm:"not null;default:1"`
|
||||||
DialogueRevision int64 `gorm:"not null;default:1"`
|
DialogueRevision int64 `gorm:"not null;default:1"`
|
||||||
InsightRevision uint64 `gorm:"not null;default:1"`
|
InsightRevision int64 `gorm:"not null;default:1"`
|
||||||
Created int64 `gorm:"autoCreateTime"`
|
Created int64 `gorm:"autoCreateTime"`
|
||||||
Disabled bool `gorm:"not null;default:false"`
|
Disabled bool `gorm:"not null;default:false"`
|
||||||
AccountDetail AccountDetail
|
AccountDetail AccountDetail
|
||||||
|
@ -22,7 +22,7 @@ func TestAddAccount(t *testing.T) {
|
|||||||
assert.NoError(t, ReadResponse(w, &tokenType))
|
assert.NoError(t, ReadResponse(w, &tokenType))
|
||||||
|
|
||||||
// check if username is available
|
// check if username is available
|
||||||
r, w, _ = NewRequest("GET", "/account/claimable?username=user", nil)
|
r, w, _ = NewRequest("GET", "/account/claimable?username=addaccount", nil)
|
||||||
SetBearerAuth(r, token)
|
SetBearerAuth(r, token)
|
||||||
GetAccountUsername(w, r)
|
GetAccountUsername(w, r)
|
||||||
var available bool
|
var available bool
|
||||||
@ -31,7 +31,7 @@ func TestAddAccount(t *testing.T) {
|
|||||||
|
|
||||||
// create account
|
// create account
|
||||||
r, w, _ = NewRequest("GET", "/account/profile", nil)
|
r, w, _ = NewRequest("GET", "/account/profile", nil)
|
||||||
SetCredentials(r, "user:pass")
|
SetCredentials(r, "addaccount:pass")
|
||||||
SetBearerAuth(r, token)
|
SetBearerAuth(r, token)
|
||||||
AddAccount(w, r)
|
AddAccount(w, r)
|
||||||
var profile Profile
|
var profile Profile
|
||||||
@ -44,7 +44,7 @@ func TestAddAccount(t *testing.T) {
|
|||||||
assert.NoError(t, ReadResponse(w, &token))
|
assert.NoError(t, ReadResponse(w, &token))
|
||||||
|
|
||||||
// check if dup is available
|
// check if dup is available
|
||||||
r, w, _ = NewRequest("GET", "/account/claimable?username=user", nil)
|
r, w, _ = NewRequest("GET", "/account/claimable?username=addaccount", nil)
|
||||||
SetBearerAuth(r, token)
|
SetBearerAuth(r, token)
|
||||||
GetAccountUsername(w, r)
|
GetAccountUsername(w, r)
|
||||||
assert.NoError(t, ReadResponse(w, &available))
|
assert.NoError(t, ReadResponse(w, &available))
|
||||||
@ -52,7 +52,7 @@ func TestAddAccount(t *testing.T) {
|
|||||||
|
|
||||||
// create dup account
|
// create dup account
|
||||||
r, w, _ = NewRequest("GET", "/account/profile", nil)
|
r, w, _ = NewRequest("GET", "/account/profile", nil)
|
||||||
SetCredentials(r, "user:pass")
|
SetCredentials(r, "addaccount:pass")
|
||||||
SetBearerAuth(r, token);
|
SetBearerAuth(r, token);
|
||||||
AddAccount(w, r)
|
AddAccount(w, r)
|
||||||
assert.Error(t, ReadResponse(w, &profile))
|
assert.Error(t, ReadResponse(w, &profile))
|
||||||
|
@ -9,14 +9,48 @@ import (
|
|||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto"
|
"crypto"
|
||||||
"time"
|
"time"
|
||||||
|
"net/url"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type StatusHandler struct {}
|
||||||
|
|
||||||
|
func (h *StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
Status(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
func TestAttachAccount(t *testing.T) {
|
func TestAttachAccount(t *testing.T) {
|
||||||
|
|
||||||
|
// setup websocket server
|
||||||
|
h := StatusHandler{}
|
||||||
|
s := httptest.NewServer(&h)
|
||||||
|
wsUrl, _ := url.Parse(s.URL)
|
||||||
|
wsUrl.Scheme = "ws"
|
||||||
|
|
||||||
|
// get account token
|
||||||
|
r, w, _ := NewRequest("POST", "/admin/accounts", nil)
|
||||||
|
SetBasicAuth(r, "admin:pass")
|
||||||
|
AddNodeAccount(w, r)
|
||||||
|
var account string
|
||||||
|
if ReadResponse(w, &account) != nil {
|
||||||
|
panic("failed to create token")
|
||||||
|
}
|
||||||
|
|
||||||
|
// set account profile
|
||||||
|
r, w, _ = NewRequest("GET", "/account/profile", nil)
|
||||||
|
SetBearerAuth(r, account);
|
||||||
|
SetCredentials(r, "attachapp:pass")
|
||||||
|
AddAccount(w, r)
|
||||||
|
if ReadResponse(w, nil) != nil {
|
||||||
|
panic("failed to create account")
|
||||||
|
}
|
||||||
|
|
||||||
// acquire new token for attaching app
|
// acquire new token for attaching app
|
||||||
r, w, _ := NewRequest("POST", "/account/apps", nil)
|
r, w, _ = NewRequest("POST", "/account/apps", nil)
|
||||||
SetBasicAuth(r, "user:pass");
|
SetBasicAuth(r, "attachapp:pass");
|
||||||
AddAccountApp(w, r);
|
AddAccountApp(w, r);
|
||||||
var token string
|
var token string
|
||||||
assert.NoError(t, ReadResponse(w, &token))
|
assert.NoError(t, ReadResponse(w, &token))
|
||||||
@ -35,7 +69,7 @@ func TestAttachAccount(t *testing.T) {
|
|||||||
|
|
||||||
// autorize app
|
// autorize app
|
||||||
r, w, _ = NewRequest("PUT", "/authorize", "aabbccdd")
|
r, w, _ = NewRequest("PUT", "/authorize", "aabbccdd")
|
||||||
SetBearerAuth(r, access);
|
SetBearerAuth(r, access)
|
||||||
Authorize(w, r);
|
Authorize(w, r);
|
||||||
var message DataMessage
|
var message DataMessage
|
||||||
assert.NoError(t, ReadResponse(w, &message))
|
assert.NoError(t, ReadResponse(w, &message))
|
||||||
@ -61,6 +95,16 @@ func TestAttachAccount(t *testing.T) {
|
|||||||
assert.GreaterOrEqual(t, cur, auth.Timestamp)
|
assert.GreaterOrEqual(t, cur, auth.Timestamp)
|
||||||
assert.Less(t, cur - 60, auth.Timestamp)
|
assert.Less(t, cur - 60, auth.Timestamp)
|
||||||
|
|
||||||
|
// app connects websocket
|
||||||
|
ws, _, _ := websocket.DefaultDialer.Dial(wsUrl.String(), nil)
|
||||||
|
announce := Announce{ AppToken: access }
|
||||||
|
msg, _ := json.Marshal(&announce)
|
||||||
|
ws.WriteMessage(websocket.TextMessage, msg)
|
||||||
|
_, msg, _ = ws.ReadMessage()
|
||||||
|
var revision Revision
|
||||||
|
assert.NoError(t, json.Unmarshal(msg, &revision))
|
||||||
|
profileRevision := revision.Profile
|
||||||
|
|
||||||
// set profile
|
// set profile
|
||||||
profileData := ProfileData{
|
profileData := ProfileData{
|
||||||
Name: "Namer",
|
Name: "Namer",
|
||||||
@ -79,7 +123,12 @@ func TestAttachAccount(t *testing.T) {
|
|||||||
var profile Profile
|
var profile Profile
|
||||||
assert.NoError(t, ReadResponse(w, &profile))
|
assert.NoError(t, ReadResponse(w, &profile))
|
||||||
assert.Equal(t, guid, profile.Guid)
|
assert.Equal(t, guid, profile.Guid)
|
||||||
assert.Equal(t, "user", profile.Handle)
|
assert.Equal(t, "attachapp", profile.Handle)
|
||||||
assert.Equal(t, "Namer", profile.Name)
|
assert.Equal(t, "Namer", profile.Name)
|
||||||
|
|
||||||
|
// profile revision incremented
|
||||||
|
_, msg, _ = ws.ReadMessage()
|
||||||
|
assert.NoError(t, json.Unmarshal(msg, &revision))
|
||||||
|
assert.NotEqual(t, profileRevision, revision.Profile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user