@@ -95,6 +95,54 @@ async def test_download_auth_media(self) -> None:
9595 self .assertTrue (args [0 ].startswith ("http://my-site.com/" ))
9696 self .assertIn ("/_matrix/client/v1/media/download/" + MEDIA_PATH , args [0 ])
9797
98+ async def test_download_auth_media_invalid_token (self ) -> None :
99+ """Tests that downloading an authenticated media file with an invalid access
100+ token returns the correct error code.
101+ """
102+ self .media_status = 401
103+ self .media_body = (
104+ b'{"errcode":"M_UNKNOWN_TOKEN","error":"Invalid access token"}'
105+ )
106+ self ._set_headers ({"content-type" : ["application/json" ]})
107+
108+ # Check that we fail at downloading the file.
109+ with self .assertRaises (ContentScannerRestError ) as cm :
110+ await self .downloader .download_file (
111+ MEDIA_PATH , auth_header = "Bearer access_token"
112+ )
113+
114+ self .assertEqual (cm .exception .http_status , 401 )
115+ self .assertEqual (cm .exception .reason , "M_UNKNOWN_TOKEN" )
116+
117+ # Check that we tried downloading from the set base URL.
118+ args = self .get_mock .call_args .args
119+ self .assertTrue (args [0 ].startswith ("http://my-site.com/" ))
120+ self .assertIn ("/_matrix/client/v1/media/download/" + MEDIA_PATH , args [0 ])
121+
122+ async def test_download_auth_media_missing_token (self ) -> None :
123+ """Tests that downloading an authenticated media file with a missing access
124+ token returns the correct error code.
125+ """
126+ self .media_status = 401
127+ self .media_body = (
128+ b'{"errcode":"M_MISSING_TOKEN","error":"Missing access token"}'
129+ )
130+ self ._set_headers ({"content-type" : ["application/json" ]})
131+
132+ # Check that we fail at downloading the file.
133+ with self .assertRaises (ContentScannerRestError ) as cm :
134+ await self .downloader .download_file (
135+ MEDIA_PATH , auth_header = "Bearer access_token"
136+ )
137+
138+ self .assertEqual (cm .exception .http_status , 401 )
139+ self .assertEqual (cm .exception .reason , "M_MISSING_TOKEN" )
140+
141+ # Check that we tried downloading from the set base URL.
142+ args = self .get_mock .call_args .args
143+ self .assertTrue (args [0 ].startswith ("http://my-site.com/" ))
144+ self .assertIn ("/_matrix/client/v1/media/download/" + MEDIA_PATH , args [0 ])
145+
98146 async def test_no_base_url (self ) -> None :
99147 """Tests that configuring a base homeserver URL means files are downloaded from
100148 that homeserver (rather than the one the files were uploaded to) and .well-known
0 commit comments