signaller/internal/backends/memory/backend_test.go

180 lines
3.7 KiB
Go
Raw Normal View History

2019-07-22 17:18:58 +00:00
package memory
import (
"testing"
"github.com/stretchr/testify/assert"
2019-07-31 15:01:20 +00:00
"github.com/nxshock/signaller/internal/models/createroom"
2019-07-22 17:18:58 +00:00
)
func TestRegisterUser(t *testing.T) {
backend := NewBackend("localhost")
var (
username = "user1"
password = "password1"
device = "device1"
)
user, token, err := backend.Register(username, password, device)
assert.Nil(t, err)
assert.Equal(t, username, user.Name())
assert.Equal(t, password, user.Password())
assert.NotEmpty(t, token)
}
func TestRegisterUserWithAlreadyTakenName(t *testing.T) {
backend := NewBackend("localhost")
var (
userName = "user1"
)
_, _, err := backend.Register(userName, "", "")
assert.Nil(t, err)
_, _, err = backend.Register(userName, "", "")
assert.NotNil(t, err)
}
func TestLogin(t *testing.T) {
backend := NewBackend("localhost")
var (
userName = "user1"
password = "password1"
)
_, _, err := backend.Register(userName, password, "")
assert.Nil(t, err)
2019-07-31 15:53:36 +00:00
_, token, err := backend.Login(userName, password, "")
2019-07-22 17:18:58 +00:00
assert.Nil(t, err)
assert.NotZero(t, token)
}
func TestLoginWithWrongCredentials(t *testing.T) {
backend := NewBackend("localhost")
var (
userName = "user1"
password = "password1"
)
_, _, err := backend.Register(userName, password, "")
assert.Nil(t, err)
2019-07-31 15:53:36 +00:00
_, _, err = backend.Login(userName, "wrong password", "")
2019-07-22 17:18:58 +00:00
assert.NotNil(t, err)
2019-07-31 15:53:36 +00:00
_, _, err = backend.Login("wrong user name", password, "")
2019-07-22 17:18:58 +00:00
assert.NotNil(t, err)
}
func TestLogout(t *testing.T) {
backend := NewBackend("localhost")
var (
userName = "user1"
password = "password1"
)
2019-07-24 14:31:07 +00:00
user, _, err := backend.Register(userName, password, "")
2019-07-22 17:18:58 +00:00
assert.Nil(t, err)
2019-07-31 15:53:36 +00:00
_, token, err := backend.Login(userName, password, "")
2019-07-22 17:18:58 +00:00
assert.Nil(t, err)
assert.NotZero(t, token)
2019-07-24 14:31:07 +00:00
user.Logout(token)
2019-07-29 14:53:15 +00:00
assert.Nil(t, backend.GetUserByToken(token))
2019-07-22 17:18:58 +00:00
}
2019-07-31 15:01:20 +00:00
func TestGetRoomByID(t *testing.T) {
backend := NewBackend("localhost")
user, token, err := backend.Register("user", "", "")
assert.Nil(t, err)
assert.NotNil(t, user)
assert.NotEmpty(t, token)
request := createroom.Request{
RoomAliasName: "room1",
Name: "room1"}
room, err := user.CreateRoom(request)
assert.Nil(t, err)
assert.NotNil(t, room)
assert.Equal(t, room.ID(), backend.GetRoomByID(room.ID()).ID())
2019-07-31 16:42:43 +00:00
// Get room with wrong id
room = backend.GetRoomByID("worng id")
assert.Nil(t, room)
2019-07-31 15:01:20 +00:00
}
2019-08-02 13:17:18 +00:00
func TestGetUserByName(t *testing.T) {
backend := NewBackend("localhost")
var (
userName = "user"
)
user, token, err := backend.Register(userName, "", "")
assert.Nil(t, err)
assert.NotNil(t, user)
assert.NotEmpty(t, token)
t.Run("Test picking user with username", func(_ *testing.T) {
user2 := backend.GetUserByName(userName)
assert.Equal(t, user, user2)
})
t.Run("Test picking user with wrong username", func(_ *testing.T) {
user2 := backend.GetUserByName("wrong username")
assert.Nil(t, user2)
})
}
2019-08-02 18:11:32 +00:00
func TestPublicRooms(t *testing.T) {
backend := NewBackend("localhost")
user1, _, err := backend.Register("user1", "", "")
assert.Nil(t, err)
assert.NotNil(t, user1)
// Create first room
request := createroom.Request{
RoomAliasName: "room1",
Name: "room1",
Preset: createroom.PublicChat}
room1, err := user1.CreateRoom(request)
assert.Nil(t, err)
assert.NotNil(t, room1)
// Create second room
request = createroom.Request{
RoomAliasName: "room2",
Name: "room2",
Preset: createroom.PublicChat}
room2, err := user1.CreateRoom(request)
assert.Nil(t, err)
assert.NotNil(t, room2)
// Make room2 has 2 users
user2, _, err := backend.Register("user2", "", "")
assert.Nil(t, err)
assert.NotNil(t, user2)
err = user2.JoinRoom(room2)
assert.Nil(t, err)
rooms := backend.PublicRooms()
assert.Len(t, rooms, 2)
assert.Equal(t, rooms[0], room2)
assert.Equal(t, rooms[1], room1)
}