mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 12:39:17 +00:00
more test cleanup
This commit is contained in:
parent
ff5c34e0df
commit
6ee801747c
@ -55,6 +55,10 @@ func TestMain(m *testing.M) {
|
||||
panic("failed to set account storage");
|
||||
}
|
||||
|
||||
go SendNotifications()
|
||||
|
||||
m.Run()
|
||||
|
||||
ExitNotifications()
|
||||
}
|
||||
|
||||
|
@ -1,26 +0,0 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type StatusHandler struct {}
|
||||
|
||||
func (h *StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
Status(w, r)
|
||||
}
|
||||
|
||||
func getTestWebsocket() *websocket.Conn {
|
||||
h := StatusHandler{}
|
||||
s := httptest.NewServer(&h)
|
||||
wsUrl, _ := url.Parse(s.URL)
|
||||
wsUrl.Scheme = "ws"
|
||||
ws, _, err := websocket.DefaultDialer.Dial(wsUrl.String(), nil)
|
||||
if err != nil {
|
||||
PrintMsg(err.Error());
|
||||
}
|
||||
return ws
|
||||
}
|
@ -3,18 +3,16 @@ package databag
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
"net/url"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type TestAccount struct {
|
||||
AppToken string
|
||||
ContactGuid string
|
||||
ContactToken string
|
||||
ContactCardId string
|
||||
}
|
||||
const TEST_READDEADLINE = 2
|
||||
|
||||
type TestCard struct {
|
||||
Guid string
|
||||
@ -39,14 +37,21 @@ type TestGroup struct {
|
||||
D TestContact
|
||||
}
|
||||
|
||||
// A-B
|
||||
// |x|
|
||||
// C-D
|
||||
//
|
||||
// A: B[connected,group] : C[requested,group] : D[requested,nogroup]
|
||||
// B: A[connected,group] : C[confirmed,group] : D[,]
|
||||
// C: A[confirmed,group] : B[confirmed,nogroup] : D[connected,group]
|
||||
// D: A[pending,nogroup] : B[,] : C[connected,group]
|
||||
// A --- connected,group connected,group --- B
|
||||
// | \ /|
|
||||
// | requested,nogroup confirmed,group |
|
||||
// | |
|
||||
// requested,group ,
|
||||
// |
|
||||
// --x--
|
||||
// |
|
||||
// confirmed,group ,
|
||||
// | |
|
||||
// | confirmed,nogroup pending,nogroup |
|
||||
// |/ \|
|
||||
// C --- connected,group connected,group --- D
|
||||
//
|
||||
func AddTestGroup(prefix string) (*TestGroup, error) {
|
||||
var err error
|
||||
|
||||
@ -374,3 +379,62 @@ func NewRequest(rest string, path string, obj interface{}) (*http.Request, *http
|
||||
return httptest.NewRequest(rest, path, nil), w, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Websocket test support
|
||||
type statusHandler struct {}
|
||||
func (h *statusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
Status(w, r)
|
||||
}
|
||||
func StatusConnection(token string, rev *Revision) (ws *websocket.Conn, err error) {
|
||||
var data []byte
|
||||
var dataType int
|
||||
|
||||
// connect to websocket
|
||||
s := httptest.NewServer(&statusHandler{})
|
||||
wsUrl, _ := url.Parse(s.URL)
|
||||
wsUrl.Scheme = "ws"
|
||||
if ws, _, err = websocket.DefaultDialer.Dial(wsUrl.String(), nil); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// send authentication
|
||||
announce := Announce{ AppToken: token }
|
||||
if data, err = json.Marshal(&announce); err != nil {
|
||||
return
|
||||
}
|
||||
ws.WriteMessage(websocket.TextMessage, data)
|
||||
|
||||
// read revision response
|
||||
ws.SetReadDeadline(time.Now().Add(TEST_READDEADLINE * time.Second))
|
||||
if dataType, data, err = ws.ReadMessage(); err != nil {
|
||||
return
|
||||
}
|
||||
if dataType != websocket.TextMessage {
|
||||
err = errors.New("invalid status data type")
|
||||
return
|
||||
}
|
||||
if err = json.Unmarshal(data, rev); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
func StatusRevision(ws *websocket.Conn, rev *Revision) (err error) {
|
||||
var data []byte
|
||||
var dataType int
|
||||
|
||||
// read revision update
|
||||
ws.SetReadDeadline(time.Now().Add(TEST_READDEADLINE * time.Second))
|
||||
if dataType, data, err = ws.ReadMessage(); err != nil {
|
||||
return
|
||||
}
|
||||
if dataType != websocket.TextMessage {
|
||||
err = errors.New("invalid status data type")
|
||||
return
|
||||
}
|
||||
if err = json.Unmarshal(data, rev); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -2,13 +2,15 @@ package databag
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"encoding/json"
|
||||
"time"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAttachAccount(t *testing.T) {
|
||||
var err error
|
||||
var ws *websocket.Conn
|
||||
var revision Revision
|
||||
|
||||
// get account token
|
||||
r, w, _ := NewRequest("POST", "/admin/accounts", nil)
|
||||
@ -64,14 +66,8 @@ func TestAttachAccount(t *testing.T) {
|
||||
assert.Equal(t, msgType, APP_MSGAUTHENTICATE)
|
||||
|
||||
// app connects websocket
|
||||
ws := getTestWebsocket()
|
||||
announce := Announce{ AppToken: profile.Guid + "." + access }
|
||||
msg, _ := json.Marshal(&announce)
|
||||
ws.WriteMessage(websocket.TextMessage, msg)
|
||||
ws.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, msg, _ = ws.ReadMessage()
|
||||
var revision Revision
|
||||
assert.NoError(t, json.Unmarshal(msg, &revision))
|
||||
ws, err = StatusConnection(profile.Guid + "." + access, &revision);
|
||||
assert.NoError(t, err)
|
||||
profileRevision := revision.Profile
|
||||
|
||||
// set profile
|
||||
@ -95,9 +91,8 @@ func TestAttachAccount(t *testing.T) {
|
||||
assert.Equal(t, "Namer", profile.Name)
|
||||
|
||||
// profile revision incremented
|
||||
ws.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, msg, _ = ws.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(msg, &revision))
|
||||
err = StatusRevision(ws, &revision)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEqual(t, profileRevision, revision.Profile)
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,7 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"time"
|
||||
"testing"
|
||||
"encoding/json"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -16,6 +14,9 @@ func TestConnectContact(t *testing.T) {
|
||||
var vars map[string]string
|
||||
var cardRevision int64
|
||||
var contactStatus ContactStatus
|
||||
var ws *websocket.Conn
|
||||
var err error
|
||||
|
||||
|
||||
// create some contacts for this test
|
||||
_, a, _ := AddTestAccount("connect0")
|
||||
@ -29,14 +30,8 @@ func TestConnectContact(t *testing.T) {
|
||||
assert.NoError(t, ReadResponse(w, &msg))
|
||||
|
||||
// app connects websocket
|
||||
ws := getTestWebsocket()
|
||||
announce := Announce{ AppToken: b }
|
||||
data, _ := json.Marshal(&announce)
|
||||
ws.WriteMessage(websocket.TextMessage, data)
|
||||
ws.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, data, _ = ws.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(data, &revision))
|
||||
//cardRevision = revision.Card
|
||||
ws, err = StatusConnection(b, &revision);
|
||||
assert.NoError(t, err)
|
||||
|
||||
// add A card in B
|
||||
r, w, _ = NewRequest("POST", "/contact/cards", &msg)
|
||||
@ -45,10 +40,8 @@ func TestConnectContact(t *testing.T) {
|
||||
assert.NoError(t, ReadResponse(w, &card))
|
||||
|
||||
// profile revision incremented
|
||||
ws.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, data, _ = ws.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(data, &revision))
|
||||
assert.NotEqual(t, cardRevision, revision.Card)
|
||||
err = StatusRevision(ws, &revision)
|
||||
assert.NoError(t, err)
|
||||
cardRevision = revision.Card
|
||||
|
||||
// update A status to connecting
|
||||
@ -60,9 +53,8 @@ func TestConnectContact(t *testing.T) {
|
||||
assert.NoError(t, ReadResponse(w, &card))
|
||||
|
||||
// card revision incremented
|
||||
ws.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, data, _ = ws.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(data, &revision))
|
||||
err = StatusRevision(ws, &revision)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEqual(t, cardRevision, revision.Card)
|
||||
cardRevision = revision.Card
|
||||
|
||||
@ -118,9 +110,8 @@ func TestConnectContact(t *testing.T) {
|
||||
assert.Equal(t, APP_CARDCONNECTED, contactStatus.Status)
|
||||
|
||||
// card revision incremented
|
||||
ws.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, data, _ = ws.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(data, &revision))
|
||||
err = StatusRevision(ws, &revision)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEqual(t, cardRevision, revision.Card)
|
||||
cardRevision = revision.Card
|
||||
|
||||
|
@ -1,9 +1,7 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"time"
|
||||
"testing"
|
||||
"encoding/json"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -21,9 +19,9 @@ func TestGroupContact(t *testing.T) {
|
||||
var contactRevision int64
|
||||
var card Card
|
||||
var contactCardRevision int64
|
||||
|
||||
// start notification thread
|
||||
go SendNotifications()
|
||||
var wsA *websocket.Conn
|
||||
var wsB *websocket.Conn
|
||||
var err error
|
||||
|
||||
// connect contacts
|
||||
_, a, _ := AddTestAccount("groupcontact0")
|
||||
@ -34,23 +32,11 @@ func TestGroupContact(t *testing.T) {
|
||||
OpenTestCard(b, bCard)
|
||||
|
||||
// app connects websocket
|
||||
wsA := getTestWebsocket()
|
||||
announce := Announce{ AppToken: a }
|
||||
data, _ := json.Marshal(&announce)
|
||||
wsA.WriteMessage(websocket.TextMessage, data)
|
||||
wsB := getTestWebsocket()
|
||||
announce = Announce{ AppToken: b }
|
||||
data, _ = json.Marshal(&announce)
|
||||
wsB.WriteMessage(websocket.TextMessage, data)
|
||||
|
||||
// receive revision
|
||||
wsA.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, data, _ = wsA.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(data, &revision))
|
||||
wsA, err = StatusConnection(a, &revision);
|
||||
assert.NoError(t, err)
|
||||
groupRevision = revision.Group
|
||||
wsB.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, data, _ = wsB.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(data, &revision))
|
||||
wsB, err = StatusConnection(b, &revision);
|
||||
assert.NoError(t, err)
|
||||
contactRevision = revision.Card
|
||||
|
||||
// add group to conatact 0
|
||||
@ -64,9 +50,8 @@ func TestGroupContact(t *testing.T) {
|
||||
assert.NoError(t, ReadResponse(w, &group))
|
||||
|
||||
// receive revision
|
||||
wsA.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, data, _ = wsA.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(data, &revision))
|
||||
err = StatusRevision(wsA, &revision)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEqual(t, groupRevision, revision.Group)
|
||||
cardRevision = revision.Card
|
||||
|
||||
@ -101,14 +86,12 @@ func TestGroupContact(t *testing.T) {
|
||||
assert.Equal(t, len(card.CardData.Groups), 1)
|
||||
|
||||
// receive revision
|
||||
wsA.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, data, _ = wsA.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(data, &revision))
|
||||
err = StatusRevision(wsA, &revision)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEqual(t, cardRevision, revision.Card)
|
||||
groupRevision = revision.Group
|
||||
wsB.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, data, _ = wsB.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(data, &revision))
|
||||
err = StatusRevision(wsB, &revision)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEqual(t, contactRevision, revision.Card)
|
||||
contactRevision = revision.Card
|
||||
|
||||
@ -144,9 +127,8 @@ func TestGroupContact(t *testing.T) {
|
||||
assert.Equal(t, group.DataType, "imagroupEDIT")
|
||||
|
||||
// receive revision
|
||||
wsA.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, data, _ = wsA.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(data, &revision))
|
||||
err = StatusRevision(wsA, &revision)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEqual(t, groupRevision, revision.Group)
|
||||
groupRevision = revision.Group
|
||||
|
||||
@ -170,13 +152,11 @@ func TestGroupContact(t *testing.T) {
|
||||
assert.Equal(t, len(card.CardData.Groups), 0)
|
||||
|
||||
// receive revision
|
||||
wsA.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, data, _ = wsA.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(data, &revision))
|
||||
err = StatusRevision(wsA, &revision)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEqual(t, groupRevision, revision.Group)
|
||||
wsB.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, data, _ = wsB.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(data, &revision))
|
||||
err = StatusRevision(wsB, &revision)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEqual(t, contactRevision, revision.Card)
|
||||
|
||||
// get contact revision
|
||||
@ -194,7 +174,4 @@ func TestGroupContact(t *testing.T) {
|
||||
GetGroups(w, r)
|
||||
assert.NoError(t, ReadResponse(w, &groups))
|
||||
assert.Equal(t, 0, len(groups))
|
||||
|
||||
// stop notification thread
|
||||
ExitNotifications()
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"time"
|
||||
"testing"
|
||||
"encoding/json"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@ -11,10 +9,8 @@ import (
|
||||
func TestProfileNotification(t *testing.T) {
|
||||
var views []CardView
|
||||
var revision Revision
|
||||
var data []byte
|
||||
|
||||
// start notifcation thread
|
||||
go SendNotifications()
|
||||
var ws *websocket.Conn
|
||||
var err error
|
||||
|
||||
// connect contacts
|
||||
_, a, _ := AddTestAccount("profilenotification0")
|
||||
@ -33,15 +29,8 @@ func TestProfileNotification(t *testing.T) {
|
||||
profileRevision := views[0].RemoteProfile
|
||||
|
||||
// app connects websocket
|
||||
ws := getTestWebsocket()
|
||||
announce := Announce{ AppToken: a }
|
||||
data, _ = json.Marshal(&announce)
|
||||
ws.WriteMessage(websocket.TextMessage, data)
|
||||
|
||||
// receive revision
|
||||
ws.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, data, _ = ws.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(data, &revision))
|
||||
ws, err = StatusConnection(a, &revision);
|
||||
assert.NoError(t, err)
|
||||
cardRevision := revision.Card
|
||||
|
||||
// update B profile
|
||||
@ -56,9 +45,8 @@ func TestProfileNotification(t *testing.T) {
|
||||
assert.NoError(t, ReadResponse(w, nil))
|
||||
|
||||
// receive revision
|
||||
ws.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
_, data, _ = ws.ReadMessage()
|
||||
assert.NoError(t, json.Unmarshal(data, &revision))
|
||||
err = StatusRevision(ws, &revision)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEqual(t, cardRevision, revision.Card)
|
||||
|
||||
// get views list of cards
|
||||
@ -68,7 +56,4 @@ func TestProfileNotification(t *testing.T) {
|
||||
assert.NoError(t, ReadResponse(w, &views))
|
||||
assert.Equal(t, len(views), 1)
|
||||
assert.NotEqual(t, profileRevision, views[0].RemoteProfile)
|
||||
|
||||
// stop notification thread
|
||||
ExitNotifications()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user