@@ -15,15 +15,15 @@ limitations under the License.
15
15
*/
16
16
17
17
import { mocked , Mocked } from "jest-mock" ;
18
- import { CryptoApi , MatrixClient , Device } from "matrix-js-sdk/src/matrix" ;
18
+ import { CryptoApi , MatrixClient , Device , Preset } from "matrix-js-sdk/src/matrix" ;
19
19
import { RoomType } from "matrix-js-sdk/src/@types/event" ;
20
20
21
- import { stubClient , setupAsyncStoreWithClient , mockPlatformPeg } from "./test-utils" ;
21
+ import { stubClient , setupAsyncStoreWithClient , mockPlatformPeg , getMockClientWithEventEmitter } from "./test-utils" ;
22
22
import { MatrixClientPeg } from "../src/MatrixClientPeg" ;
23
23
import WidgetStore from "../src/stores/WidgetStore" ;
24
24
import WidgetUtils from "../src/utils/WidgetUtils" ;
25
25
import { JitsiCall , ElementCall } from "../src/models/Call" ;
26
- import createRoom , { canEncryptToAllUsers } from "../src/createRoom" ;
26
+ import createRoom , { checkUserIsAllowedToChangeEncryption , canEncryptToAllUsers } from "../src/createRoom" ;
27
27
import SettingsStore from "../src/settings/SettingsStore" ;
28
28
29
29
describe ( "createRoom" , ( ) => {
@@ -207,3 +207,55 @@ describe("canEncryptToAllUsers", () => {
207
207
expect ( result ) . toBe ( true ) ;
208
208
} ) ;
209
209
} ) ;
210
+
211
+ describe ( "checkUserIsAllowedToChangeEncryption()" , ( ) => {
212
+ const mockClient = getMockClientWithEventEmitter ( {
213
+ doesServerForceEncryptionForPreset : jest . fn ( ) ,
214
+ getClientWellKnown : jest . fn ( ) . mockReturnValue ( { } ) ,
215
+ } ) ;
216
+ beforeEach ( ( ) => {
217
+ mockClient . doesServerForceEncryptionForPreset . mockClear ( ) . mockResolvedValue ( false ) ;
218
+ mockClient . getClientWellKnown . mockClear ( ) . mockReturnValue ( { } ) ;
219
+ } ) ;
220
+
221
+ it ( "should allow changing when neither server nor well known force encryption" , async ( ) => {
222
+ expect ( await checkUserIsAllowedToChangeEncryption ( mockClient , Preset . PrivateChat ) ) . toEqual ( {
223
+ allowChange : true ,
224
+ } ) ;
225
+
226
+ expect ( mockClient . doesServerForceEncryptionForPreset ) . toHaveBeenCalledWith ( Preset . PrivateChat ) ;
227
+ } ) ;
228
+
229
+ it ( "should not allow changing when server forces encryption" , async ( ) => {
230
+ mockClient . doesServerForceEncryptionForPreset . mockResolvedValue ( true ) ;
231
+ expect ( await checkUserIsAllowedToChangeEncryption ( mockClient , Preset . PrivateChat ) ) . toEqual ( {
232
+ allowChange : false ,
233
+ forcedValue : true ,
234
+ } ) ;
235
+ } ) ;
236
+
237
+ it ( "should not allow changing when well-known force_disable is true" , async ( ) => {
238
+ mockClient . getClientWellKnown . mockReturnValue ( {
239
+ "io.element.e2ee" : {
240
+ force_disable : true ,
241
+ } ,
242
+ } ) ;
243
+ expect ( await checkUserIsAllowedToChangeEncryption ( mockClient , Preset . PrivateChat ) ) . toEqual ( {
244
+ allowChange : false ,
245
+ forcedValue : false ,
246
+ } ) ;
247
+ } ) ;
248
+
249
+ it ( "should not allow changing when server forces enabled and wk forces disabled encryption" , async ( ) => {
250
+ mockClient . getClientWellKnown . mockReturnValue ( {
251
+ "io.element.e2ee" : {
252
+ force_disable : true ,
253
+ } ,
254
+ } ) ;
255
+ mockClient . doesServerForceEncryptionForPreset . mockResolvedValue ( true ) ;
256
+ expect ( await checkUserIsAllowedToChangeEncryption ( mockClient , Preset . PrivateChat ) ) . toEqual (
257
+ // server's forced enable takes precedence
258
+ { allowChange : false , forcedValue : true } ,
259
+ ) ;
260
+ } ) ;
261
+ } ) ;
0 commit comments