2019-07-21 09:43:14 +00:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-08-03 14:19:18 +00:00
|
|
|
"github.com/signaller-matrix/signaller/internal/models/createroom"
|
2019-07-21 09:43:14 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateRoom(t *testing.T) {
|
|
|
|
backend := NewBackend("localhost")
|
|
|
|
|
|
|
|
user, _, err := backend.Register("user1", "", "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-21 09:43:14 +00:00
|
|
|
|
|
|
|
request := createroom.Request{
|
|
|
|
RoomAliasName: "room1",
|
|
|
|
Name: "room1",
|
2019-08-03 06:59:52 +00:00
|
|
|
Topic: "topic",
|
|
|
|
Preset: createroom.PublicChat}
|
2019-07-21 09:43:14 +00:00
|
|
|
|
2019-07-21 11:52:32 +00:00
|
|
|
room, err := user.CreateRoom(request)
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-21 09:43:14 +00:00
|
|
|
assert.Equal(t, request.RoomAliasName, room.AliasName())
|
|
|
|
assert.Equal(t, request.Name, room.Name())
|
|
|
|
assert.Equal(t, request.Topic, room.Topic())
|
|
|
|
assert.Equal(t, user.ID(), room.Creator().ID())
|
2019-08-03 06:59:52 +00:00
|
|
|
assert.Equal(t, request.Preset, room.State())
|
2019-07-21 09:43:14 +00:00
|
|
|
assert.Equal(t, 1, len(backend.rooms))
|
2019-07-24 15:02:04 +00:00
|
|
|
assert.Equal(t, "!"+room.(*Room).id+":"+backend.hostname, room.ID())
|
2019-07-21 09:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateAlreadyExistingRoom(t *testing.T) {
|
|
|
|
backend := NewBackend("localhost")
|
|
|
|
|
|
|
|
user, _, _ := backend.Register("user1", "", "")
|
|
|
|
|
|
|
|
request := createroom.Request{
|
|
|
|
RoomAliasName: "room1",
|
|
|
|
Name: "room1",
|
|
|
|
Topic: "topic"}
|
|
|
|
|
2019-07-21 11:52:32 +00:00
|
|
|
_, err := user.CreateRoom(request)
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-21 09:43:14 +00:00
|
|
|
|
2019-07-21 11:52:32 +00:00
|
|
|
_, err = user.CreateRoom(request)
|
2019-07-21 09:43:14 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
2019-07-21 11:04:24 +00:00
|
|
|
|
|
|
|
func TestSetRoomTopic(t *testing.T) {
|
|
|
|
backend := NewBackend("localhost")
|
|
|
|
|
|
|
|
user, _, _ := backend.Register("user1", "", "")
|
|
|
|
|
|
|
|
request := createroom.Request{
|
|
|
|
RoomAliasName: "room1",
|
|
|
|
Name: "room1",
|
|
|
|
Topic: "topic"}
|
|
|
|
|
2019-07-21 11:52:32 +00:00
|
|
|
room, _ := user.CreateRoom(request)
|
2019-07-21 11:04:24 +00:00
|
|
|
|
|
|
|
var newTopic = "new topic"
|
2019-07-22 13:38:02 +00:00
|
|
|
err := user.SetTopic(room, newTopic)
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-21 11:04:24 +00:00
|
|
|
assert.Equal(t, newTopic, room.Topic())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetRoomTopicWithnprivelegedUser(t *testing.T) {
|
|
|
|
backend := NewBackend("localhost")
|
|
|
|
|
|
|
|
creator, _, _ := backend.Register("user1", "", "")
|
|
|
|
user2, _, _ := backend.Register("user2", "", "")
|
|
|
|
|
|
|
|
request := createroom.Request{
|
|
|
|
RoomAliasName: "room1",
|
|
|
|
Name: "room1",
|
|
|
|
Topic: "topic"}
|
|
|
|
|
2019-07-21 11:52:32 +00:00
|
|
|
room, _ := creator.CreateRoom(request)
|
2019-07-21 11:04:24 +00:00
|
|
|
|
|
|
|
var newTopic = "new topic"
|
2019-07-22 13:38:02 +00:00
|
|
|
err := user2.SetTopic(room, newTopic)
|
2019-07-21 11:04:24 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
2019-07-22 13:58:18 +00:00
|
|
|
|
|
|
|
func TestLeaveRoom(t *testing.T) {
|
|
|
|
backend := NewBackend("localhost")
|
|
|
|
|
|
|
|
user, _, _ := backend.Register("user1", "", "")
|
|
|
|
|
|
|
|
request := createroom.Request{
|
|
|
|
RoomAliasName: "room1",
|
|
|
|
Name: "room1",
|
|
|
|
Topic: "topic"}
|
|
|
|
|
|
|
|
room, _ := user.CreateRoom(request)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, len(room.(*Room).joined))
|
|
|
|
|
|
|
|
err := user.LeaveRoom(room)
|
|
|
|
assert.Equal(t, 0, len(room.(*Room).joined))
|
|
|
|
|
|
|
|
// Try to leave room again must throw error
|
|
|
|
err = user.LeaveRoom(room)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
2019-07-22 17:29:18 +00:00
|
|
|
|
|
|
|
func TestRoomUserCount(t *testing.T) {
|
|
|
|
backend := NewBackend("localhost")
|
|
|
|
|
|
|
|
user1, _, err := backend.Register("user1", "", "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-22 17:29:18 +00:00
|
|
|
|
|
|
|
request := createroom.Request{
|
|
|
|
RoomAliasName: "room1",
|
|
|
|
Name: "room1",
|
|
|
|
Topic: "topic"}
|
|
|
|
|
|
|
|
room, err := user1.CreateRoom(request)
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-22 17:29:18 +00:00
|
|
|
assert.Len(t, room.Users(), 1)
|
|
|
|
|
|
|
|
// TODO: add join another user test
|
|
|
|
}
|