@@ -1010,7 +1010,7 @@ mod test {
10101010 use serde_json:: json;
10111011 use wiremock:: {
10121012 matchers:: { header, method, path} ,
1013- Mock , MockServer , ResponseTemplate ,
1013+ Mock , MockGuard , MockServer , ResponseTemplate ,
10141014 } ;
10151015
10161016 use super :: * ;
@@ -1124,22 +1124,7 @@ mod test {
11241124 let client = logged_in_client ( Some ( server. uri ( ) ) ) . await ;
11251125
11261126 {
1127- let _scope = Mock :: given ( method ( "GET" ) )
1128- . and ( path ( "_matrix/client/r0/room_keys/version" ) )
1129- . and ( header ( "authorization" , "Bearer 1234" ) )
1130- . respond_with ( ResponseTemplate :: new ( 200 ) . set_body_json ( json ! ( {
1131- "algorithm" : "m.megolm_backup.v1.curve25519-aes-sha2" ,
1132- "auth_data" : {
1133- "public_key" : "abcdefg" ,
1134- "signatures" : { } ,
1135- } ,
1136- "count" : 42 ,
1137- "etag" : "anopaquestring" ,
1138- "version" : "1" ,
1139- } ) ) )
1140- . expect ( 1 )
1141- . mount_as_scoped ( & server)
1142- . await ;
1127+ let _scope = mock_backup_exists ( & server) . await ;
11431128
11441129 let exists = client
11451130 . encryption ( )
@@ -1148,20 +1133,11 @@ mod test {
11481133 . await
11491134 . expect ( "We should be able to check if backups exist on the server" ) ;
11501135
1151- assert ! ( exists, "We should deduce that a backup exist on the server" ) ;
1136+ assert ! ( exists, "We should deduce that a backup exists on the server" ) ;
11521137 }
11531138
11541139 {
1155- let _scope = Mock :: given ( method ( "GET" ) )
1156- . and ( path ( "_matrix/client/r0/room_keys/version" ) )
1157- . and ( header ( "authorization" , "Bearer 1234" ) )
1158- . respond_with ( ResponseTemplate :: new ( 404 ) . set_body_json ( json ! ( {
1159- "errcode" : "M_NOT_FOUND" ,
1160- "error" : "No current backup version"
1161- } ) ) )
1162- . expect ( 1 )
1163- . mount_as_scoped ( & server)
1164- . await ;
1140+ let _scope = mock_backup_none ( & server) . await ;
11651141
11661142 let exists = client
11671143 . encryption ( )
@@ -1170,35 +1146,19 @@ mod test {
11701146 . await
11711147 . expect ( "We should be able to check if backups exist on the server" ) ;
11721148
1173- assert ! ( !exists, "We should deduce that no backup exist on the server" ) ;
1149+ assert ! ( !exists, "We should deduce that no backup exists on the server" ) ;
11741150 }
11751151
11761152 {
1177- let _scope = Mock :: given ( method ( "GET" ) )
1178- . and ( path ( "_matrix/client/r0/room_keys/version" ) )
1179- . and ( header ( "authorization" , "Bearer 1234" ) )
1180- . respond_with ( ResponseTemplate :: new ( 429 ) . set_body_json ( json ! ( {
1181- "errcode" : "M_LIMIT_EXCEEDED" ,
1182- "error" : "Too many requests" ,
1183- "retry_after_ms" : 2000
1184- } ) ) )
1185- . expect ( 1 )
1186- . mount_as_scoped ( & server)
1187- . await ;
1153+ let _scope = mock_backup_too_many_requests ( & server) . await ;
11881154
11891155 client. encryption ( ) . backups ( ) . exists_on_server ( ) . await . expect_err (
11901156 "If the /version endpoint returns a non 404 error we should throw an error" ,
11911157 ) ;
11921158 }
11931159
11941160 {
1195- let _scope = Mock :: given ( method ( "GET" ) )
1196- . and ( path ( "_matrix/client/r0/room_keys/version" ) )
1197- . and ( header ( "authorization" , "Bearer 1234" ) )
1198- . respond_with ( ResponseTemplate :: new ( 404 ) )
1199- . expect ( 1 )
1200- . mount_as_scoped ( & server)
1201- . await ;
1161+ let _scope = mock_backup_404 ( & server) ;
12021162
12031163 client. encryption ( ) . backups ( ) . exists_on_server ( ) . await . expect_err (
12041164 "If the /version endpoint returns a non-Matrix 404 error we should throw an error" ,
@@ -1282,4 +1242,60 @@ mod test {
12821242
12831243 server. verify ( ) . await ;
12841244 }
1245+
1246+ async fn mock_backup_exists ( server : & MockServer ) -> MockGuard {
1247+ Mock :: given ( method ( "GET" ) )
1248+ . and ( path ( "_matrix/client/r0/room_keys/version" ) )
1249+ . and ( header ( "authorization" , "Bearer 1234" ) )
1250+ . respond_with ( ResponseTemplate :: new ( 200 ) . set_body_json ( json ! ( {
1251+ "algorithm" : "m.megolm_backup.v1.curve25519-aes-sha2" ,
1252+ "auth_data" : {
1253+ "public_key" : "abcdefg" ,
1254+ "signatures" : { } ,
1255+ } ,
1256+ "count" : 42 ,
1257+ "etag" : "anopaquestring" ,
1258+ "version" : "1" ,
1259+ } ) ) )
1260+ . expect ( 1 )
1261+ . mount_as_scoped ( server)
1262+ . await
1263+ }
1264+
1265+ async fn mock_backup_none ( server : & MockServer ) -> MockGuard {
1266+ Mock :: given ( method ( "GET" ) )
1267+ . and ( path ( "_matrix/client/r0/room_keys/version" ) )
1268+ . and ( header ( "authorization" , "Bearer 1234" ) )
1269+ . respond_with ( ResponseTemplate :: new ( 404 ) . set_body_json ( json ! ( {
1270+ "errcode" : "M_NOT_FOUND" ,
1271+ "error" : "No current backup version"
1272+ } ) ) )
1273+ . expect ( 1 )
1274+ . mount_as_scoped ( server)
1275+ . await
1276+ }
1277+
1278+ async fn mock_backup_too_many_requests ( server : & MockServer ) -> MockGuard {
1279+ Mock :: given ( method ( "GET" ) )
1280+ . and ( path ( "_matrix/client/r0/room_keys/version" ) )
1281+ . and ( header ( "authorization" , "Bearer 1234" ) )
1282+ . respond_with ( ResponseTemplate :: new ( 429 ) . set_body_json ( json ! ( {
1283+ "errcode" : "M_LIMIT_EXCEEDED" ,
1284+ "error" : "Too many requests" ,
1285+ "retry_after_ms" : 2000
1286+ } ) ) )
1287+ . expect ( 1 )
1288+ . mount_as_scoped ( server)
1289+ . await
1290+ }
1291+
1292+ async fn mock_backup_404 ( server : & MockServer ) -> MockGuard {
1293+ Mock :: given ( method ( "GET" ) )
1294+ . and ( path ( "_matrix/client/r0/room_keys/version" ) )
1295+ . and ( header ( "authorization" , "Bearer 1234" ) )
1296+ . respond_with ( ResponseTemplate :: new ( 404 ) )
1297+ . expect ( 1 )
1298+ . mount_as_scoped ( server)
1299+ . await
1300+ }
12851301}
0 commit comments