github.com/stampzilla/stampzilla-go@v2.0.0-rc9+incompatible/nodes/stampzilla-server/e2e/main_test.go (about) 1 package main 2 3 import ( 4 "io/ioutil" 5 "net/http" 6 "testing" 7 "time" 8 9 "github.com/gorilla/websocket" 10 "github.com/posener/wstest" 11 "github.com/stampzilla/stampzilla-go/nodes/stampzilla-server/models/devices" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestDownloadCA(t *testing.T) { 16 17 main, cleanup := setupServer(t) 18 defer cleanup() 19 w := makeRequest(t, main.HTTPServer, "GET", "http://localhost/ca.crt", nil) 20 21 resp := w.Result() 22 body, _ := ioutil.ReadAll(resp.Body) 23 24 assert.Equal(t, "application/x-x509-ca-cert", resp.Header.Get("Content-Type")) 25 assert.Equal(t, http.StatusOK, resp.StatusCode) 26 assert.Contains(t, string(body), "BEGIN CERTIFICATE") 27 } 28 29 func TestInsecureWebsocket(t *testing.T) { 30 31 main, cleanup := setupServer(t) 32 defer cleanup() 33 34 d := wstest.NewDialer(main.HTTPServer) 35 d.Subprotocols = []string{"node"} 36 c, _, err := d.Dial("ws://example.org/ws", nil) 37 if err != nil { 38 t.Fatal(err) 39 } 40 41 msgType, msgByte, err := c.ReadMessage() 42 if err != nil { 43 t.Fatal(err) 44 } 45 msg := string(msgByte) 46 47 assert.Equal(t, websocket.TextMessage, msgType) 48 assert.Contains(t, msg, `"type":"server-info"`) 49 assert.Contains(t, msg, `"uuid":"123"`) 50 assert.Contains(t, msg, `"name":"testserver"`) 51 52 // Verify store has saved the connection 53 assert.Len(t, main.Store.GetConnections(), 1) 54 for _, v := range main.Store.GetConnections() { 55 assert.Equal(t, "node", v.Attributes["protocol"]) 56 } 57 c.Close() 58 59 waitFor(t, 1*time.Second, "connections should be zero after connection close", func() bool { 60 return len(main.Store.GetConnections()) == 0 61 }) 62 } 63 64 // TestInsecureWebsocketRequestCertificate is a full end to end test between a node and the server going through a node initial connection process etc 65 func TestInsecureWebsocketRequestCertificate(t *testing.T) { 66 main, node, cleanup := setupWebsocketTest(t) 67 defer cleanup() 68 69 acceptCertificateRequest(t, main) 70 71 err := node.Connect() 72 assert.NoError(t, err) 73 74 waitFor(t, 1*time.Second, "nodes should be 1", func() bool { 75 return len(main.Store.GetNodes()) == 1 76 }) 77 78 assert.Contains(t, main.Store.GetNodes(), node.UUID) 79 assert.Len(t, main.Store.GetConnections(), 1) 80 assert.Equal(t, true, main.Store.GetNode(node.UUID).Connected()) 81 82 go func() { 83 <-time.After(50 * time.Millisecond) 84 node.Stop() 85 }() 86 node.Wait() 87 88 waitFor(t, 1*time.Second, "connections should be 0", func() bool { 89 return len(main.Store.GetConnections()) == 0 90 }) 91 assert.Len(t, main.Store.GetConnections(), 0) 92 assert.Equal(t, false, main.Store.GetNode(node.UUID).Connected()) 93 } 94 95 func TestNodeToServerDevices(t *testing.T) { 96 main, node, cleanup := setupWebsocketTest(t) 97 defer cleanup() 98 99 acceptCertificateRequest(t, main) 100 101 err := node.Connect() 102 assert.NoError(t, err) 103 104 dev1 := &devices.Device{ 105 Name: "Device1", 106 ID: devices.ID{ 107 ID: "1", 108 }, 109 Online: true, 110 Traits: []string{"OnOff"}, 111 State: devices.State{ 112 "on": false, 113 }, 114 } 115 node.AddOrUpdate(dev1) 116 waitFor(t, 1*time.Second, "should have some devices", func() bool { 117 return len(main.Store.Devices.All()) != 0 118 }) 119 120 //log.Println("devs", main.Store.Devices.All()) 121 122 //Make sure node and server has the correct device key which is unique with nodeuuid + device id 123 assert.Contains(t, main.Store.Devices.All(), devices.ID{Node: node.UUID, ID: "1"}) 124 assert.Contains(t, node.Devices.All(), devices.ID{Node: node.UUID, ID: "1"}) 125 126 }