2019-07-22 17:18:58 +00:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
2019-08-18 14:56:16 +00:00
|
|
|
"reflect"
|
2019-07-22 17:18:58 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-08-18 14:56:16 +00:00
|
|
|
"github.com/signaller-matrix/signaller/internal"
|
2019-08-03 14:19:18 +00:00
|
|
|
"github.com/signaller-matrix/signaller/internal/models/createroom"
|
2019-08-18 14:56:16 +00:00
|
|
|
"github.com/signaller-matrix/signaller/internal/models/events"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-07-22 17:18:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRegisterUser(t *testing.T) {
|
|
|
|
backend := NewBackend("localhost")
|
|
|
|
|
|
|
|
var (
|
2019-08-03 13:54:58 +00:00
|
|
|
username = "username1"
|
2019-07-22 17:18:58 +00:00
|
|
|
password = "password1"
|
|
|
|
device = "device1"
|
|
|
|
)
|
|
|
|
|
|
|
|
user, token, err := backend.Register(username, password, device)
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-22 17:18:58 +00:00
|
|
|
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 (
|
2019-08-03 13:54:58 +00:00
|
|
|
userName = "username1"
|
2019-07-22 17:18:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
_, _, err := backend.Register(userName, "", "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-22 17:18:58 +00:00
|
|
|
|
|
|
|
_, _, err = backend.Register(userName, "", "")
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogin(t *testing.T) {
|
|
|
|
backend := NewBackend("localhost")
|
|
|
|
|
|
|
|
var (
|
2019-08-03 13:54:58 +00:00
|
|
|
userName = "username1"
|
2019-07-22 17:18:58 +00:00
|
|
|
password = "password1"
|
|
|
|
)
|
|
|
|
|
|
|
|
_, _, err := backend.Register(userName, password, "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-22 17:18:58 +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-22 17:18:58 +00:00
|
|
|
assert.NotZero(t, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoginWithWrongCredentials(t *testing.T) {
|
|
|
|
backend := NewBackend("localhost")
|
|
|
|
|
|
|
|
var (
|
2019-08-03 13:54:58 +00:00
|
|
|
userName = "username1"
|
2019-07-22 17:18:58 +00:00
|
|
|
password = "password1"
|
|
|
|
)
|
|
|
|
|
|
|
|
_, _, err := backend.Register(userName, password, "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-22 17:18:58 +00:00
|
|
|
|
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 (
|
2019-08-03 13:54:58 +00:00
|
|
|
userName = "username1"
|
2019-07-22 17:18:58 +00:00
|
|
|
password = "password1"
|
|
|
|
)
|
|
|
|
|
2019-07-24 14:31:07 +00:00
|
|
|
user, _, err := backend.Register(userName, password, "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-22 17:18:58 +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-22 17:18:58 +00:00
|
|
|
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")
|
|
|
|
|
2019-08-03 13:54:58 +00:00
|
|
|
user, token, err := backend.Register("username", "", "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-31 15:01:20 +00:00
|
|
|
assert.NotNil(t, user)
|
|
|
|
assert.NotEmpty(t, token)
|
|
|
|
|
|
|
|
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-31 15:01:20 +00:00
|
|
|
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 (
|
2019-08-03 13:54:58 +00:00
|
|
|
userName = "username"
|
2019-08-02 13:17:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
user, token, err := backend.Register(userName, "", "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-02 13:17:18 +00:00
|
|
|
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")
|
|
|
|
|
2019-08-03 13:54:58 +00:00
|
|
|
username1, _, err := backend.Register("username1", "", "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-03 13:54:58 +00:00
|
|
|
assert.NotNil(t, username1)
|
2019-08-02 18:11:32 +00:00
|
|
|
|
|
|
|
// Create first room
|
|
|
|
request := createroom.Request{
|
|
|
|
RoomAliasName: "room1",
|
|
|
|
Name: "room1",
|
|
|
|
Preset: createroom.PublicChat}
|
|
|
|
|
2019-08-03 13:54:58 +00:00
|
|
|
room1, err := username1.CreateRoom(request)
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-02 18:11:32 +00:00
|
|
|
assert.NotNil(t, room1)
|
|
|
|
|
|
|
|
// Create second room
|
|
|
|
request = createroom.Request{
|
|
|
|
RoomAliasName: "room2",
|
|
|
|
Name: "room2",
|
|
|
|
Preset: createroom.PublicChat}
|
|
|
|
|
2019-08-03 13:54:58 +00:00
|
|
|
room2, err := username1.CreateRoom(request)
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-02 18:11:32 +00:00
|
|
|
assert.NotNil(t, room2)
|
|
|
|
|
|
|
|
// Make room2 has 2 users
|
|
|
|
user2, _, err := backend.Register("user2", "", "")
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-02 18:11:32 +00:00
|
|
|
assert.NotNil(t, user2)
|
|
|
|
|
|
|
|
err = user2.JoinRoom(room2)
|
2019-08-03 06:46:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-02 18:11:32 +00:00
|
|
|
|
2019-08-03 17:39:35 +00:00
|
|
|
rooms := backend.PublicRooms("")
|
2019-08-02 18:11:32 +00:00
|
|
|
assert.Len(t, rooms, 2)
|
|
|
|
assert.Equal(t, rooms[0], room2)
|
|
|
|
assert.Equal(t, rooms[1], room1)
|
|
|
|
}
|
2019-08-06 13:59:28 +00:00
|
|
|
|
|
|
|
func TestNewUserNameValidate(t *testing.T) {
|
|
|
|
backend := NewBackend("localhost")
|
|
|
|
|
|
|
|
var shortName = "u1"
|
|
|
|
|
|
|
|
user, token, err := backend.Register(shortName, "", "")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, user)
|
|
|
|
assert.Empty(t, token)
|
|
|
|
}
|
2019-08-18 14:56:16 +00:00
|
|
|
|
|
|
|
func TestGetEventsSince(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
user internal.User
|
|
|
|
sinceToken string
|
|
|
|
limit int
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []events.Event
|
|
|
|
}{
|
|
|
|
// TODO: Add test cases.
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
backend := NewBackend("localhost")
|
|
|
|
if got := backend.GetEventsSince(tt.args.user, tt.args.sinceToken, tt.args.limit); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("Backend.GetEventsSince() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|