The purpose of this test is to make sure that the storage layer is generating sequential stream IDs per user, and that they are returned correctly when querying for device keys.
(t *testing.T)
| 110 | // The purpose of this test is to make sure that the storage layer is generating sequential stream IDs per user, |
| 111 | // and that they are returned correctly when querying for device keys. |
| 112 | func TestDeviceKeysStreamIDGeneration(t *testing.T) { |
| 113 | var err error |
| 114 | test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) { |
| 115 | db, clean := MustCreateDatabase(t, dbType) |
| 116 | defer clean() |
| 117 | alice := "@alice:TestDeviceKeysStreamIDGeneration" |
| 118 | bob := "@bob:TestDeviceKeysStreamIDGeneration" |
| 119 | msgs := []api.DeviceMessage{ |
| 120 | { |
| 121 | Type: api.TypeDeviceKeyUpdate, |
| 122 | DeviceKeys: &api.DeviceKeys{ |
| 123 | DeviceID: "AAA", |
| 124 | UserID: alice, |
| 125 | KeyJSON: []byte(`{"key":"v1"}`), |
| 126 | }, |
| 127 | // StreamID: 1 |
| 128 | }, |
| 129 | { |
| 130 | Type: api.TypeDeviceKeyUpdate, |
| 131 | DeviceKeys: &api.DeviceKeys{ |
| 132 | DeviceID: "AAA", |
| 133 | UserID: bob, |
| 134 | KeyJSON: []byte(`{"key":"v1"}`), |
| 135 | }, |
| 136 | // StreamID: 1 as this is a different user |
| 137 | }, |
| 138 | { |
| 139 | Type: api.TypeDeviceKeyUpdate, |
| 140 | DeviceKeys: &api.DeviceKeys{ |
| 141 | DeviceID: "another_device", |
| 142 | UserID: alice, |
| 143 | KeyJSON: []byte(`{"key":"v1"}`), |
| 144 | }, |
| 145 | // StreamID: 2 as this is a 2nd device key |
| 146 | }, |
| 147 | } |
| 148 | MustNotError(t, db.StoreLocalDeviceKeys(ctx, msgs)) |
| 149 | if msgs[0].StreamID != 1 { |
| 150 | t.Fatalf("Expected StoreLocalDeviceKeys to set StreamID=1 but got %d", msgs[0].StreamID) |
| 151 | } |
| 152 | if msgs[1].StreamID != 1 { |
| 153 | t.Fatalf("Expected StoreLocalDeviceKeys to set StreamID=1 (different user) but got %d", msgs[1].StreamID) |
| 154 | } |
| 155 | if msgs[2].StreamID != 2 { |
| 156 | t.Fatalf("Expected StoreLocalDeviceKeys to set StreamID=2 (another device) but got %d", msgs[2].StreamID) |
| 157 | } |
| 158 | |
| 159 | // updating a device sets the next stream ID for that user |
| 160 | msgs = []api.DeviceMessage{ |
| 161 | { |
| 162 | Type: api.TypeDeviceKeyUpdate, |
| 163 | DeviceKeys: &api.DeviceKeys{ |
| 164 | DeviceID: "AAA", |
| 165 | UserID: alice, |
| 166 | KeyJSON: []byte(`{"key":"v2"}`), |
| 167 | }, |
| 168 | // StreamID: 3 |
| 169 | }, |
nothing calls this directly
no test coverage detected