Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Apr 27, 2023
1 parent 72e8df0 commit 4968ed2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 55 deletions.
24 changes: 0 additions & 24 deletions internal/request_test.go

This file was deleted.

10 changes: 10 additions & 0 deletions internal/v2_devices_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package internal

import "testing"

func TestIsNewDeviceID(t *testing.T) {
proxyID := ProxyDeviceID("@alice:test", "example")
if !IsNewDeviceID(proxyID) {
t.Fatalf("%s was not considered a new-style device ID", proxyID)
}
}
65 changes: 44 additions & 21 deletions sync2/storage_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sync2

import (
"github.com/matrix-org/sliding-sync/internal"
"os"
"sort"
"testing"
Expand All @@ -17,46 +18,46 @@ func TestMain(m *testing.M) {
}

func TestStorage(t *testing.T) {
deviceID := "ALICE"
accessToken := "my_access_token"
alice := "@alice:localhost"
aliceHSDeviceID := "ALICE"
aliceDeviceID := internal.ProxyDeviceID(alice, aliceHSDeviceID)
aliceAccessToken := "my_access_token"
store := NewStore(postgresConnectionString, "my_secret")
device, err := store.InsertDevice(deviceID, accessToken)
device, err := store.InsertDevice(alice, aliceDeviceID, aliceAccessToken)
if err != nil {
t.Fatalf("Failed to InsertDevice: %s", err)
}
assertEqual(t, device.DeviceID, deviceID, "Device.DeviceID mismatch")
assertEqual(t, device.AccessToken, accessToken, "Device.AccessToken mismatch")
if err = store.UpdateDeviceSince(deviceID, "s1"); err != nil {
assertEqual(t, device.DeviceID, aliceDeviceID, "Device.DeviceID mismatch")
assertEqual(t, device.AccessToken, aliceAccessToken, "Device.AccessToken mismatch")
if err = store.UpdateDeviceSince(aliceDeviceID, "s1"); err != nil {
t.Fatalf("UpdateDeviceSince returned error: %s", err)
}
if err = store.UpdateUserIDForDevice(deviceID, "@alice:localhost"); err != nil {
t.Fatalf("UpdateUserIDForDevice returned error: %s", err)
}

// now check that device retrieval has the latest values
device, err = store.Device(deviceID)
device, err = store.Device(aliceDeviceID)
if err != nil {
t.Fatalf("Device returned error: %s", err)
}
assertEqual(t, device.DeviceID, deviceID, "Device.DeviceID mismatch")
assertEqual(t, device.DeviceID, aliceDeviceID, "Device.DeviceID mismatch")
assertEqual(t, device.Since, "s1", "Device.Since mismatch")
assertEqual(t, device.UserID, "@alice:localhost", "Device.UserID mismatch")
assertEqual(t, device.AccessToken, accessToken, "Device.AccessToken mismatch")
assertEqual(t, device.UserID, alice, "Device.UserID mismatch")
assertEqual(t, device.AccessToken, aliceAccessToken, "Device.AccessToken mismatch")

// now check new devices remember the v2 since value and user ID
s2, err := store.InsertDevice(deviceID, accessToken)
// now check that we correctly upsert: we should remember the v2 since value and user ID
s2, err := store.InsertDevice(alice, aliceDeviceID, aliceAccessToken)
if err != nil {
t.Fatalf("InsertDevice returned error: %s", err)
}
assertEqual(t, s2.DeviceID, aliceDeviceID, "Device.DeviceID mismatch")
assertEqual(t, s2.Since, "s1", "Device.Since mismatch")
assertEqual(t, s2.UserID, "@alice:localhost", "Device.UserID mismatch")
assertEqual(t, s2.DeviceID, deviceID, "Device.DeviceID mismatch")
assertEqual(t, s2.AccessToken, accessToken, "Device.AccessToken mismatch")
assertEqual(t, s2.UserID, alice, "Device.UserID mismatch")
assertEqual(t, s2.AccessToken, aliceAccessToken, "Device.AccessToken mismatch")

// check all devices works
deviceID2 := "BOB"
accessToken2 := "BOB_ACCESS_TOKEN"
bobDevice, err := store.InsertDevice(deviceID2, accessToken2)
bob := "@bob:localhost"
bobDeviceID := "BOB"
bobAccessToken := "BOB_ACCESS_TOKEN"
bobDevice, err := store.InsertDevice(bob, bobDeviceID, bobAccessToken)
if err != nil {
t.Fatalf("InsertDevice returned error: %s", err)
}
Expand All @@ -79,6 +80,28 @@ func TestStorage(t *testing.T) {
assertEqual(t, devices[i].DeviceID, wantDevices[i].DeviceID, "Device.DeviceID mismatch")
assertEqual(t, devices[i].AccessToken, wantDevices[i].AccessToken, "Device.AccessToken mismatch")
}

// check that alice can update her device with a new access token
aliceAccessToken2 := "let me in please"
s3, err := store.InsertDevice(alice, aliceDeviceID, aliceAccessToken2)
if err != nil {
t.Fatalf("InsertDevice returned error: %s", err)
}
assertEqual(t, s3.DeviceID, aliceDeviceID, "Device.DeviceID mismatch")
assertEqual(t, s3.Since, "s1", "Device.Since mismatch")
assertEqual(t, s3.UserID, alice, "Device.UserID mismatch")
assertEqual(t, s3.AccessToken, aliceAccessToken2, "Device.AccessToken mismatch")

// check that fetching the device by ID afterwards returns the new access token
s4, err := store.Device(aliceDeviceID)
if err != nil {
t.Fatalf("Device returned error: %s", err)
}
assertEqual(t, s4.DeviceID, aliceDeviceID, "Device.DeviceID mismatch")
assertEqual(t, s4.Since, "s1", "Device.Since mismatch")
assertEqual(t, s4.UserID, alice, "Device.UserID mismatch")
assertEqual(t, s4.AccessToken, aliceAccessToken2, "Device.AccessToken mismatch")

}

func assertEqual(t *testing.T, got, want, msg string) {
Expand Down
9 changes: 1 addition & 8 deletions tests-integration/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,17 +639,10 @@ func TestExpiredAccessToken(t *testing.T) {
})
// now expire the token
v2.invalidateToken(aliceToken)
// now do another request, this should 400 as it expires the session
// now do another request, this should 401 as the auth token has expired
req := sync3.Request{}
req.SetTimeoutMSecs(1)
_, body, statusCode := v3.doV3Request(t, context.Background(), aliceToken, res.Pos, req)
if statusCode != 400 {
t.Fatalf("got %d want 400 : %v", statusCode, string(body))
}
// do a fresh request, this should 401
req = sync3.Request{}
req.SetTimeoutMSecs(1)
_, body, statusCode = v3.doV3Request(t, context.Background(), aliceToken, "", req)
if statusCode != 401 {
t.Fatalf("got %d want 401 : %v", statusCode, string(body))
}
Expand Down
4 changes: 2 additions & 2 deletions tests-integration/poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestSecondPollerFiltersToDevice(t *testing.T) {
defer v2.close()
defer v3.close()
deviceAToken := "DEVICE_A_TOKEN"
v2.addAccount(alice, deviceAToken)
v2.addAccountWithDeviceID(alice, "A", deviceAToken)
v2.queueResponse(deviceAToken, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
Expand All @@ -38,7 +38,7 @@ func TestSecondPollerFiltersToDevice(t *testing.T) {

// now sync with device B, and check we send the filter up
deviceBToken := "DEVICE_B_TOKEN"
v2.addAccount(alice, deviceBToken)
v2.addAccountWithDeviceID(alice, "B", deviceBToken)
seenInitialRequest := false
v2.CheckRequest = func(userID, token string, req *http.Request) {
if userID != alice || token != deviceBToken {
Expand Down
3 changes: 3 additions & 0 deletions tests-integration/v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ type testV2Server struct {
timeToWaitForV2Response time.Duration
}

// Most tests only use a single device per user. Give them this helper so they don't
// have to care about providing a device name.
func (s *testV2Server) addAccount(userID, token string) {
s.addAccountWithDeviceID(userID, userID+"_device", token)
}

// Tests that use multiple devices for the same user need to be more explicit.
func (s *testV2Server) addAccountWithDeviceID(userID, deviceID, token string) {
s.mu.Lock()
defer s.mu.Unlock()
Expand Down

0 comments on commit 4968ed2

Please sign in to comment.