1+ /*
2+ Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ */
16+
17+ import {
18+ MatrixClient ,
19+ Room ,
20+ } from 'matrix-js-sdk/src/matrix' ;
21+
122import { shieldStatusForRoom } from '../../src/utils/ShieldUtils' ;
223import DMRoomMap from '../../src/utils/DMRoomMap' ;
324
4- function mkClient ( selfTrust ) {
25+ function mkClient ( selfTrust = false ) {
526 return {
627 getUserId : ( ) => "@self:localhost" ,
728 checkUserTrust : ( userId ) => ( {
@@ -12,7 +33,7 @@ function mkClient(selfTrust) {
1233 isVerified : ( ) => userId === "@self:localhost" ? selfTrust : userId [ 2 ] == "T" ,
1334 } ) ,
1435 getStoredDevicesForUser : ( userId ) => [ "DEVICE" ] ,
15- } ;
36+ } as unknown as MatrixClient ;
1637}
1738
1839describe ( "mkClient self-test" , function ( ) {
@@ -42,9 +63,14 @@ describe("mkClient self-test", function() {
4263
4364describe ( "shieldStatusForMembership self-trust behaviour" , function ( ) {
4465 beforeAll ( ( ) => {
45- DMRoomMap . sharedInstance = {
66+ const mockInstance = {
4667 getUserIdForRoomId : ( roomId ) => roomId === "DM" ? "@any:h" : null ,
47- } ;
68+ } as unknown as DMRoomMap ;
69+ jest . spyOn ( DMRoomMap , 'shared' ) . mockReturnValue ( mockInstance ) ;
70+ } ) ;
71+
72+ afterAll ( ( ) => {
73+ jest . spyOn ( DMRoomMap , 'shared' ) . mockRestore ( ) ;
4874 } ) ;
4975
5076 it . each (
@@ -55,7 +81,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
5581 const room = {
5682 roomId : dm ? "DM" : "other" ,
5783 getEncryptionTargetMembers : ( ) => [ "@self:localhost" , "@FF1:h" , "@FF2:h" ] . map ( ( userId ) => ( { userId } ) ) ,
58- } ;
84+ } as unknown as Room ;
5985 const status = await shieldStatusForRoom ( client , room ) ;
6086 expect ( status ) . toEqual ( "normal" ) ;
6187 } ) ;
@@ -68,7 +94,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
6894 const room = {
6995 roomId : dm ? "DM" : "other" ,
7096 getEncryptionTargetMembers : ( ) => [ "@self:localhost" , "@TT1:h" , "@TT2:h" ] . map ( ( userId ) => ( { userId } ) ) ,
71- } ;
97+ } as unknown as Room ;
7298 const status = await shieldStatusForRoom ( client , room ) ;
7399 expect ( status ) . toEqual ( result ) ;
74100 } ) ;
@@ -81,7 +107,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
81107 const room = {
82108 roomId : dm ? "DM" : "other" ,
83109 getEncryptionTargetMembers : ( ) => [ "@self:localhost" , "@TT1:h" , "@FF2:h" ] . map ( ( userId ) => ( { userId } ) ) ,
84- } ;
110+ } as unknown as Room ;
85111 const status = await shieldStatusForRoom ( client , room ) ;
86112 expect ( status ) . toEqual ( result ) ;
87113 } ) ;
@@ -94,7 +120,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
94120 const room = {
95121 roomId : dm ? "DM" : "other" ,
96122 getEncryptionTargetMembers : ( ) => [ "@self:localhost" ] . map ( ( userId ) => ( { userId } ) ) ,
97- } ;
123+ } as unknown as Room ;
98124 const status = await shieldStatusForRoom ( client , room ) ;
99125 expect ( status ) . toEqual ( result ) ;
100126 } ) ;
@@ -107,7 +133,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
107133 const room = {
108134 roomId : dm ? "DM" : "other" ,
109135 getEncryptionTargetMembers : ( ) => [ "@self:localhost" , "@TT:h" ] . map ( ( userId ) => ( { userId } ) ) ,
110- } ;
136+ } as unknown as Room ;
111137 const status = await shieldStatusForRoom ( client , room ) ;
112138 expect ( status ) . toEqual ( result ) ;
113139 } ) ;
@@ -120,17 +146,18 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
120146 const room = {
121147 roomId : dm ? "DM" : "other" ,
122148 getEncryptionTargetMembers : ( ) => [ "@self:localhost" , "@FF:h" ] . map ( ( userId ) => ( { userId } ) ) ,
123- } ;
149+ } as unknown as Room ;
124150 const status = await shieldStatusForRoom ( client , room ) ;
125151 expect ( status ) . toEqual ( result ) ;
126152 } ) ;
127153} ) ;
128154
129155describe ( "shieldStatusForMembership other-trust behaviour" , function ( ) {
130156 beforeAll ( ( ) => {
131- DMRoomMap . sharedInstance = {
157+ const mockInstance = {
132158 getUserIdForRoomId : ( roomId ) => roomId === "DM" ? "@any:h" : null ,
133- } ;
159+ } as unknown as DMRoomMap ;
160+ jest . spyOn ( DMRoomMap , 'shared' ) . mockReturnValue ( mockInstance ) ;
134161 } ) ;
135162
136163 it . each (
@@ -140,7 +167,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
140167 const room = {
141168 roomId : dm ? "DM" : "other" ,
142169 getEncryptionTargetMembers : ( ) => [ "@self:localhost" , "@TF:h" ] . map ( ( userId ) => ( { userId } ) ) ,
143- } ;
170+ } as unknown as Room ;
144171 const status = await shieldStatusForRoom ( client , room ) ;
145172 expect ( status ) . toEqual ( result ) ;
146173 } ) ;
@@ -152,7 +179,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
152179 const room = {
153180 roomId : dm ? "DM" : "other" ,
154181 getEncryptionTargetMembers : ( ) => [ "@self:localhost" , "@TF:h" , "@TT:h" ] . map ( ( userId ) => ( { userId } ) ) ,
155- } ;
182+ } as unknown as Room ;
156183 const status = await shieldStatusForRoom ( client , room ) ;
157184 expect ( status ) . toEqual ( result ) ;
158185 } ) ;
@@ -164,7 +191,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
164191 const room = {
165192 roomId : dm ? "DM" : "other" ,
166193 getEncryptionTargetMembers : ( ) => [ "@self:localhost" , "@FF:h" , "@FT:h" ] . map ( ( userId ) => ( { userId } ) ) ,
167- } ;
194+ } as unknown as Room ;
168195 const status = await shieldStatusForRoom ( client , room ) ;
169196 expect ( status ) . toEqual ( result ) ;
170197 } ) ;
@@ -176,7 +203,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
176203 const room = {
177204 roomId : dm ? "DM" : "other" ,
178205 getEncryptionTargetMembers : ( ) => [ "@self:localhost" , "@WF:h" , "@FT:h" ] . map ( ( userId ) => ( { userId } ) ) ,
179- } ;
206+ } as unknown as Room ;
180207 const status = await shieldStatusForRoom ( client , room ) ;
181208 expect ( status ) . toEqual ( result ) ;
182209 } ) ;
0 commit comments