mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-25 03:42:20 +00:00
106 lines
2.4 KiB
Go
106 lines
2.4 KiB
Go
package memory
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/nxshock/signaller/internal/models/createroom"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCreateRoom(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)
|
|
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())
|
|
assert.Equal(t, 1, len(backend.rooms))
|
|
}
|
|
|
|
func TestCreateAlreadyExistingRoom(t *testing.T) {
|
|
backend := NewBackend("localhost")
|
|
|
|
user, _, _ := backend.Register("user1", "", "")
|
|
|
|
request := createroom.Request{
|
|
RoomAliasName: "room1",
|
|
Name: "room1",
|
|
Topic: "topic"}
|
|
|
|
_, err := user.CreateRoom(request)
|
|
assert.Nil(t, err)
|
|
|
|
_, err = user.CreateRoom(request)
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
func TestSetRoomTopic(t *testing.T) {
|
|
backend := NewBackend("localhost")
|
|
|
|
user, _, _ := backend.Register("user1", "", "")
|
|
|
|
request := createroom.Request{
|
|
RoomAliasName: "room1",
|
|
Name: "room1",
|
|
Topic: "topic"}
|
|
|
|
room, _ := user.CreateRoom(request)
|
|
|
|
var newTopic = "new topic"
|
|
err := user.SetTopic(room, newTopic)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, newTopic, room.Topic())
|
|
assert.Equal(t, 5, len(room.Events())) // TODO: check start event count
|
|
}
|
|
|
|
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"}
|
|
|
|
room, _ := creator.CreateRoom(request)
|
|
|
|
var newTopic = "new topic"
|
|
err := user2.SetTopic(room, newTopic)
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
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)
|
|
}
|