|
| 1 | +package csapi_tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/matrix-org/complement/internal/b" |
| 7 | + "github.com/matrix-org/complement/internal/client" |
| 8 | + "github.com/matrix-org/complement/runtime" |
| 9 | + "github.com/tidwall/gjson" |
| 10 | +) |
| 11 | + |
| 12 | +func mustHaveTransactionID(t *testing.T, roomID, eventID string) client.SyncCheckOpt { |
| 13 | + return client.SyncTimelineHas(roomID, func(r gjson.Result) bool { |
| 14 | + if r.Get("event_id").Str == eventID { |
| 15 | + if !r.Get("unsigned.transaction_id").Exists() { |
| 16 | + t.Fatalf("Event %s in room %s should have a 'transaction_id', but it did not", eventID, roomID) |
| 17 | + } |
| 18 | + |
| 19 | + return true |
| 20 | + } |
| 21 | + |
| 22 | + return false |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +func mustNotHaveTransactionID(t *testing.T, roomID, eventID string) client.SyncCheckOpt { |
| 27 | + return client.SyncTimelineHas(roomID, func(r gjson.Result) bool { |
| 28 | + if r.Get("event_id").Str == eventID { |
| 29 | + res := r.Get("unsigned.transaction_id") |
| 30 | + if res.Exists() { |
| 31 | + t.Fatalf("Event %s in room %s should NOT have a 'transaction_id', but it did (%s)", eventID, roomID, res.Str) |
| 32 | + } |
| 33 | + |
| 34 | + return true |
| 35 | + } |
| 36 | + |
| 37 | + return false |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +// TestTxnAfterRefresh tests that when a client refreshes its access token, |
| 42 | +// it still gets back a transaction ID in the sync response. |
| 43 | +func TestTxnAfterRefresh(t *testing.T) { |
| 44 | + // Dendrite doesn't support refresh tokens yet. |
| 45 | + runtime.SkipIf(t, runtime.Dendrite) |
| 46 | + |
| 47 | + deployment := Deploy(t, b.BlueprintCleanHS) |
| 48 | + defer deployment.Destroy(t) |
| 49 | + |
| 50 | + deployment.RegisterUser(t, "hs1", "alice", "password", false) |
| 51 | + |
| 52 | + c := deployment.Client(t, "hs1", "") |
| 53 | + |
| 54 | + var refreshToken string |
| 55 | + c.UserID, c.AccessToken, refreshToken, c.DeviceID, _ = c.LoginUserWithRefreshToken(t, "alice", "password") |
| 56 | + |
| 57 | + // Create a room where we can send events. |
| 58 | + roomID := c.CreateRoom(t, map[string]interface{}{}) |
| 59 | + |
| 60 | + // Let's send an event, and wait for it to appear in the sync. |
| 61 | + eventID := c.SendEventUnsynced(t, roomID, b.Event{ |
| 62 | + Type: "m.room.message", |
| 63 | + Content: map[string]interface{}{ |
| 64 | + "msgtype": "m.text", |
| 65 | + "body": "first", |
| 66 | + }, |
| 67 | + }) |
| 68 | + |
| 69 | + // When syncing, we should find the event and it should have a transaction ID. |
| 70 | + token := c.MustSyncUntil(t, client.SyncReq{}, mustHaveTransactionID(t, roomID, eventID)) |
| 71 | + |
| 72 | + // Now do the same, but refresh the token before syncing. |
| 73 | + eventID = c.SendEventUnsynced(t, roomID, b.Event{ |
| 74 | + Type: "m.room.message", |
| 75 | + Content: map[string]interface{}{ |
| 76 | + "msgtype": "m.text", |
| 77 | + "body": "second", |
| 78 | + }, |
| 79 | + }) |
| 80 | + |
| 81 | + // Use the refresh token to get a new access token. |
| 82 | + c.AccessToken, refreshToken, _ = c.ConsumeRefreshToken(t, refreshToken) |
| 83 | + |
| 84 | + // When syncing, we should find the event and it should also have a transaction ID. |
| 85 | + c.MustSyncUntil(t, client.SyncReq{Since: token}, mustHaveTransactionID(t, roomID, eventID)) |
| 86 | +} |
| 87 | + |
| 88 | +// TestTxnScope tests that transaction IDs are scoped to the access token, not the device |
| 89 | +func TestTxnScope(t *testing.T) { |
| 90 | + deployment := Deploy(t, b.BlueprintCleanHS) |
| 91 | + defer deployment.Destroy(t) |
| 92 | + |
| 93 | + deployment.RegisterUser(t, "hs1", "alice", "password", false) |
| 94 | + |
| 95 | + // Create a first client, which allocates a device ID. |
| 96 | + c1 := deployment.Client(t, "hs1", "") |
| 97 | + c1.UserID, c1.AccessToken, c1.DeviceID = c1.LoginUser(t, "alice", "password") |
| 98 | + |
| 99 | + // Create a room where we can send events. |
| 100 | + roomID := c1.CreateRoom(t, map[string]interface{}{}) |
| 101 | + |
| 102 | + // Let's send an event, and wait for it to appear in the timeline. |
| 103 | + eventID := c1.SendEventUnsynced(t, roomID, b.Event{ |
| 104 | + Type: "m.room.message", |
| 105 | + Content: map[string]interface{}{ |
| 106 | + "msgtype": "m.text", |
| 107 | + "body": "first", |
| 108 | + }, |
| 109 | + }) |
| 110 | + |
| 111 | + // When syncing, we should find the event and it should have a transaction ID on the first client. |
| 112 | + c1.MustSyncUntil(t, client.SyncReq{}, mustHaveTransactionID(t, roomID, eventID)) |
| 113 | + |
| 114 | + // Create a second client, inheriting the first device ID. |
| 115 | + c2 := deployment.Client(t, "hs1", "") |
| 116 | + c2.UserID, c2.AccessToken = c2.LoginUserWithDeviceID(t, "alice", "password", c1.DeviceID) |
| 117 | + c2.DeviceID = c1.DeviceID |
| 118 | + |
| 119 | + // When syncing, we should find the event and it should *not* have a transaction ID on the second client. |
| 120 | + c2.MustSyncUntil(t, client.SyncReq{}, mustNotHaveTransactionID(t, roomID, eventID)) |
| 121 | +} |
0 commit comments