signaller/internal/backends/memory/backend_test.go

216 lines
4.7 KiB
Go
Raw Normal View History

2019-07-22 17:18:58 +00:00
package memory
import (
"reflect"
2019-07-22 17:18:58 +00:00
"testing"
"github.com/signaller-matrix/signaller/internal"
2019-08-03 14:19:18 +00:00
"github.com/signaller-matrix/signaller/internal/models/createroom"
"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 (
username = "username1"
2019-07-22 17:18:58 +00:00
password = "password1"
device = "device1"
)
user, token, err := backend.Register(username, password, device)
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 (
userName = "username1"
2019-07-22 17:18:58 +00:00
)
_, _, err := backend.Register(userName, "", "")
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 (
userName = "username1"
2019-07-22 17:18:58 +00:00
password = "password1"
)
_, _, err := backend.Register(userName, password, "")
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, "")
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 (
userName = "username1"
2019-07-22 17:18:58 +00:00
password = "password1"
)
_, _, err := backend.Register(userName, password, "")
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 (
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, "")
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, "")
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")
user, token, err := backend.Register("username", "", "")
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)
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 (
userName = "username"
2019-08-02 13:17:18 +00:00
)
user, token, err := backend.Register(userName, "", "")
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")
username1, _, err := backend.Register("username1", "", "")
assert.NoError(t, err)
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}
room1, err := username1.CreateRoom(request)
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}
room2, err := username1.CreateRoom(request)
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", "", "")
assert.NoError(t, err)
2019-08-02 18:11:32 +00:00
assert.NotNil(t, user2)
err = user2.JoinRoom(room2)
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)
}
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)
}
})
}
}