2019-07-22 14:31:33 +00:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-07-23 14:24:18 +00:00
|
|
|
|
2019-08-03 14:19:18 +00:00
|
|
|
"github.com/signaller-matrix/signaller/internal"
|
|
|
|
"github.com/signaller-matrix/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, "", "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-22 14:31:33 +00:00
|
|
|
|
|
|
|
assert.Equal(t, expectedUserID, user.ID())
|
|
|
|
}
|
2019-07-23 14:24:18 +00:00
|
|
|
|
|
|
|
func TestUserMessage(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-23 14:24:18 +00:00
|
|
|
|
|
|
|
request := createroom.Request{
|
|
|
|
RoomAliasName: "room1",
|
|
|
|
Name: "room1"}
|
|
|
|
|
|
|
|
room, err := user.CreateRoom(request)
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-23 14:24:18 +00:00
|
|
|
|
|
|
|
err = user.SendMessage(room, "hello")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-23 14:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserMessageInWrongRoom(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-23 14:24:18 +00:00
|
|
|
|
|
|
|
request := createroom.Request{
|
|
|
|
RoomAliasName: "room1",
|
|
|
|
Name: "room1"}
|
|
|
|
|
|
|
|
room, err := user1.CreateRoom(request)
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-23 14:24:18 +00:00
|
|
|
|
|
|
|
user2, _, err := backend.Register("user2", "", "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-23 14:24:18 +00:00
|
|
|
|
|
|
|
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", "", "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-23 15:46:12 +00:00
|
|
|
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", "", "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-23 15:46:12 +00:00
|
|
|
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, "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-24 14:36:47 +00:00
|
|
|
|
2019-07-31 15:53:36 +00:00
|
|
|
_, token, err := backend.Login(userName, password, "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-24 14:36:47 +00:00
|
|
|
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", "", "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-24 15:04:45 +00:00
|
|
|
|
|
|
|
request := createroom.Request{
|
|
|
|
RoomAliasName: "room1",
|
|
|
|
Name: "room1",
|
|
|
|
Topic: "topic"}
|
|
|
|
|
|
|
|
room, err := user.CreateRoom(request)
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-24 15:04:45 +00:00
|
|
|
|
|
|
|
rooms := user.JoinedRooms()
|
|
|
|
assert.Equal(t, []internal.Room{room}, rooms)
|
|
|
|
}
|
2019-07-25 16:56:31 +00:00
|
|
|
|
|
|
|
func TestNewPassword(t *testing.T) {
|
|
|
|
backend := NewBackend("localhost")
|
|
|
|
|
|
|
|
var newPassword = "new password"
|
|
|
|
|
|
|
|
user, _, err := backend.Register("user1", "old password", "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-25 16:56:31 +00:00
|
|
|
|
|
|
|
user.ChangePassword(newPassword)
|
|
|
|
assert.Equal(t, newPassword, user.Password())
|
|
|
|
}
|
2019-07-29 14:32:38 +00:00
|
|
|
|
|
|
|
func TestDevices(t *testing.T) {
|
|
|
|
backend := NewBackend("localhost")
|
|
|
|
|
|
|
|
var expectedDeviceID = "my device"
|
|
|
|
|
|
|
|
user, _, err := backend.Register("user1", "", expectedDeviceID)
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-29 14:32:38 +00:00
|
|
|
|
|
|
|
devices := user.Devices()
|
|
|
|
assert.Len(t, devices, 1)
|
|
|
|
assert.Equal(t, expectedDeviceID, devices[0].DeviceID)
|
|
|
|
}
|
2019-07-31 15:01:20 +00:00
|
|
|
|
|
|
|
func TestSetRoomVisibility(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-31 15:01:20 +00:00
|
|
|
|
|
|
|
request := createroom.Request{
|
|
|
|
RoomAliasName: "room1",
|
|
|
|
Name: "room1",
|
|
|
|
Visibility: createroom.VisibilityTypePrivate}
|
|
|
|
|
|
|
|
room, err := user.CreateRoom(request)
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-31 15:01:20 +00:00
|
|
|
assert.NotNil(t, room)
|
|
|
|
assert.Equal(t, createroom.VisibilityTypePrivate, room.Visibility())
|
|
|
|
|
|
|
|
err = user.SetRoomVisibility(room, createroom.VisibilityTypePublic)
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-31 15:01:20 +00:00
|
|
|
assert.Equal(t, createroom.VisibilityTypePublic, room.Visibility())
|
2019-07-31 15:10:16 +00:00
|
|
|
|
|
|
|
// TODO: Only owner can change room visibility
|
|
|
|
notOwnerUser, _, err := backend.Register("user2", "", "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-31 15:10:16 +00:00
|
|
|
|
|
|
|
err = notOwnerUser.SetRoomVisibility(room, createroom.VisibilityTypePrivate)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.NotEqual(t, createroom.VisibilityTypePrivate, room.Visibility())
|
2019-07-31 15:01:20 +00:00
|
|
|
}
|
2019-07-31 15:13:52 +00:00
|
|
|
|
|
|
|
func TestLogoutAll(t *testing.T) {
|
|
|
|
backend := NewBackend("localhost")
|
|
|
|
|
|
|
|
var (
|
|
|
|
userName = "user1"
|
|
|
|
password = "password1"
|
|
|
|
)
|
|
|
|
|
|
|
|
user, _, err := backend.Register(userName, password, "dev1")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-31 15:13:52 +00:00
|
|
|
assert.Len(t, user.Devices(), 1)
|
|
|
|
|
2019-07-31 15:53:36 +00:00
|
|
|
_, _, err = backend.Login(userName, password, "dev2")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-31 15:13:52 +00:00
|
|
|
assert.Len(t, user.Devices(), 2)
|
|
|
|
|
|
|
|
user.LogoutAll()
|
|
|
|
|
|
|
|
assert.Len(t, user.Devices(), 0)
|
|
|
|
}
|