Skip to content

Commit

Permalink
Tests client writing on non-permitted channel
Browse files Browse the repository at this point in the history
  • Loading branch information
pote committed Jun 30, 2016
1 parent 253f94c commit 9c209ec
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions socket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,66 @@ func TestChannelIsolation (t *testing.T) {
// NoOp, all is well.
}
}

func TestWritePermissionsApplied (t *testing.T) {
done := make(chan bool)
go RunServer(done, os.Getenv("PORT"))
defer func() { done <- true }()

ak1 := &AccessKey{
Read: []string{},
Write: []string{},
Token: "blah",
}

ak1.Save()

ak2 := &AccessKey{
Read: []string{"test-channel"},
Write: []string{},
Token: "bleh",
}

ak2.Save()

url1 := fmt.Sprintf("ws://localhost:%v/%v", os.Getenv("PORT"), ak1.Token)
url2 := fmt.Sprintf("ws://localhost:%v/%v", os.Getenv("PORT"), ak2.Token)

ws1, err := websocket.Dial(url1, "", "http://localhost"); if err != nil {
t.Fatal(err)
}

ws2, err := websocket.Dial(url2, "", "http://localhost"); if err != nil {
t.Fatal(err)
}

channel := make(chan Message, 1)

go func() {
receivedMsg := Message{}
err := websocket.JSON.Receive(ws2, &receivedMsg); if err != nil {
t.Fatal(err)
}

channel <- receivedMsg
}()


originalMessage := &Message{
Channel: "test-channel",
Data: "hey there!",
Event: "message",
}

go func() {
time.Sleep(time.Second * 1)
websocket.JSON.Send(ws1, &originalMessage)
}()

select {
case <- channel:
t.Error("This message should not have been received")
case <- time.After(time.Second * 3):
// NoOp, all is good.
}
}

0 comments on commit 9c209ec

Please sign in to comment.