package memory import ( "reflect" "testing" "github.com/signaller-matrix/signaller/internal" "github.com/signaller-matrix/signaller/internal/models/createroom" "github.com/signaller-matrix/signaller/internal/models/events" "github.com/stretchr/testify/assert" ) func TestRegisterUser(t *testing.T) { backend := NewBackend("localhost") var ( username = "username1" password = "password1" device = "device1" ) user, token, err := backend.Register(username, password, device) assert.NoError(t, err) 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" ) _, _, err := backend.Register(userName, "", "") assert.NoError(t, err) _, _, err = backend.Register(userName, "", "") assert.NotNil(t, err) } func TestLogin(t *testing.T) { backend := NewBackend("localhost") var ( userName = "username1" password = "password1" ) _, _, err := backend.Register(userName, password, "") assert.NoError(t, err) _, token, err := backend.Login(userName, password, "") assert.NoError(t, err) assert.NotZero(t, token) } func TestLoginWithWrongCredentials(t *testing.T) { backend := NewBackend("localhost") var ( userName = "username1" password = "password1" ) _, _, err := backend.Register(userName, password, "") assert.NoError(t, err) _, _, err = backend.Login(userName, "wrong password", "") assert.NotNil(t, err) _, _, err = backend.Login("wrong user name", password, "") assert.NotNil(t, err) } func TestLogout(t *testing.T) { backend := NewBackend("localhost") var ( userName = "username1" password = "password1" ) user, _, err := backend.Register(userName, password, "") assert.NoError(t, err) _, token, err := backend.Login(userName, password, "") assert.NoError(t, err) assert.NotZero(t, token) user.Logout(token) assert.Nil(t, backend.GetUserByToken(token)) } func TestGetRoomByID(t *testing.T) { backend := NewBackend("localhost") user, token, err := backend.Register("username", "", "") assert.NoError(t, err) assert.NotNil(t, user) assert.NotEmpty(t, token) request := createroom.Request{ RoomAliasName: "room1", Name: "room1"} room, err := user.CreateRoom(request) assert.NoError(t, err) assert.NotNil(t, room) assert.Equal(t, room.ID(), backend.GetRoomByID(room.ID()).ID()) // Get room with wrong id room = backend.GetRoomByID("worng id") assert.Nil(t, room) } func TestGetUserByName(t *testing.T) { backend := NewBackend("localhost") var ( userName = "username" ) user, token, err := backend.Register(userName, "", "") assert.NoError(t, err) 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) }) } func TestPublicRooms(t *testing.T) { backend := NewBackend("localhost") username1, _, err := backend.Register("username1", "", "") assert.NoError(t, err) assert.NotNil(t, username1) // Create first room request := createroom.Request{ RoomAliasName: "room1", Name: "room1", Preset: createroom.PublicChat} room1, err := username1.CreateRoom(request) assert.NoError(t, err) 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) assert.NotNil(t, room2) // Make room2 has 2 users user2, _, err := backend.Register("user2", "", "") assert.NoError(t, err) assert.NotNil(t, user2) err = user2.JoinRoom(room2) assert.NoError(t, err) rooms := backend.PublicRooms("") assert.Len(t, rooms, 2) assert.Equal(t, rooms[0], room2) assert.Equal(t, rooms[1], room1) } 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) } }) } }