signaller/internal/backends/memory/user_test.go

121 lines
2.5 KiB
Go
Raw Normal View History

2019-07-22 14:31:33 +00:00
package memory
import (
"testing"
2019-07-24 15:04:45 +00:00
"github.com/nxshock/signaller/internal"
2019-07-22 14:31:33 +00:00
"github.com/stretchr/testify/assert"
"github.com/nxshock/signaller/internal/models/createroom"
2019-07-22 14:31:33 +00:00
)
func TestUserID(t *testing.T) {
var (
userName = "user1"
hostName = "localhost"
expectedUserID = "@user1:localhost"
)
backend := NewBackend(hostName)
user, _, err := backend.Register(userName, "", "")
assert.Nil(t, err)
assert.Equal(t, expectedUserID, user.ID())
}
func TestUserMessage(t *testing.T) {
backend := NewBackend("localhost")
user, _, err := backend.Register("user1", "", "")
assert.Nil(t, err)
request := createroom.Request{
RoomAliasName: "room1",
Name: "room1"}
room, err := user.CreateRoom(request)
assert.Nil(t, err)
err = user.SendMessage(room, "hello")
assert.Nil(t, err)
}
func TestUserMessageInWrongRoom(t *testing.T) {
backend := NewBackend("localhost")
user1, _, err := backend.Register("user1", "", "")
assert.Nil(t, err)
request := createroom.Request{
RoomAliasName: "room1",
Name: "room1"}
room, err := user1.CreateRoom(request)
assert.Nil(t, err)
user2, _, err := backend.Register("user2", "", "")
assert.Nil(t, err)
err = user2.SendMessage(room, "hello")
assert.NotNil(t, err)
}
2019-07-23 15:46:12 +00:00
func TestGetUserByToken(t *testing.T) {
backend := NewBackend("localhost")
user, token, err := backend.Register("user1", "", "")
assert.Nil(t, err)
assert.NotEmpty(t, token)
gotUser := backend.GetUserByToken(token)
assert.Equal(t, user, gotUser)
}
func TestGetUserByWrongToken(t *testing.T) {
backend := NewBackend("localhost")
_, token, err := backend.Register("user1", "", "")
assert.Nil(t, err)
assert.NotEmpty(t, token)
gotUser := backend.GetUserByToken("wrong token")
assert.Nil(t, gotUser)
}
2019-07-24 14:36:47 +00:00
func TestLogoutWithWrongToken(t *testing.T) {
backend := NewBackend("localhost")
var (
userName = "user1"
password = "password1"
)
user, _, err := backend.Register(userName, password, "")
assert.Nil(t, err)
token, err := backend.Login(userName, password, "")
assert.Nil(t, err)
assert.NotZero(t, token)
user.Logout("worng token")
}
2019-07-24 15:04:45 +00:00
func TestJoinedRooms(t *testing.T) {
backend := NewBackend("localhost")
user, _, err := backend.Register("user1", "", "")
assert.Nil(t, err)
request := createroom.Request{
RoomAliasName: "room1",
Name: "room1",
Topic: "topic"}
room, err := user.CreateRoom(request)
assert.Nil(t, err)
rooms := user.JoinedRooms()
assert.Equal(t, []internal.Room{room}, rooms)
}