Coverage for an_website/redirect/redirect.py: 90.000%

20 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-16 19:56 +0000

1# This program is free software: you can redistribute it and/or modify 

2# it under the terms of the GNU Affero General Public License as 

3# published by the Free Software Foundation, either version 3 of the 

4# License, or (at your option) any later version. 

5# 

6# This program is distributed in the hope that it will be useful, 

7# but WITHOUT ANY WARRANTY; without even the implied warranty of 

8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

9# GNU Affero General Public License for more details. 

10# 

11# You should have received a copy of the GNU Affero General Public License 

12# along with this program. If not, see <https://www.gnu.org/licenses/>. 

13 

14""" 

15The redirect page of the website. 

16 

17This page is used to redirect users to third party websites. 

18This page will ask users if they want to leave this website. 

19""" 

20 

21from __future__ import annotations 

22 

23from urllib.parse import urlsplit 

24 

25from tornado.web import Application 

26 

27from .. import pytest_is_running 

28from ..utils.request_handler import HTMLRequestHandler 

29from ..utils.utils import ModuleInfo 

30 

31 

32def get_module_info() -> ModuleInfo: 

33 """Create and return the ModuleInfo for this module.""" 

34 return ModuleInfo( 

35 handlers=((r"/redirect", RedirectPage),), 

36 name="Weiterleitungsseite", 

37 description=( 

38 "Seite, die User davon abhält versehentlich eine fremde " 

39 "Webseite zu besuchen" 

40 ), 

41 path="/redirect", 

42 short_name="Weiterleitung", 

43 hidden=True, 

44 required_background_tasks=(_confirm_loading,), 

45 ) 

46 

47 

48async def _confirm_loading(app: Application, worker: int | None) -> None: 

49 """Save in settings that the redirect module has been loaded.""" 

50 app.settings["REDIRECT_MODULE_LOADED"] = worker or True # noqa: SIM222 

51 

52 

53class RedirectPage(HTMLRequestHandler): 

54 """The redirect page that redirects you to another page.""" 

55 

56 async def get(self, *, head: bool = False) -> None: 

57 """Handle GET requests to the redirect page.""" 

58 # pylint: disable=unused-argument 

59 

60 redirect_url = self.get_argument("to", None) or "/" 

61 referrer = self.request.headers.get_list("Referer") 

62 from_url = referrer[0] if referrer else None 

63 

64 if redirect_url.startswith("/"): 

65 # it is a local URL, so just redirect 

66 # use fix_url to maybe add no_3rd_party 

67 return self.redirect(self.fix_url(redirect_url)) 

68 

69 if ( 

70 (domain := urlsplit(redirect_url).hostname) 

71 and domain.endswith(".asozial.org") 

72 and not pytest_is_running() 

73 ): 

74 return self.redirect(redirect_url) 

75 

76 await self.render( 

77 "pages/redirect.html", 

78 redirect_url=redirect_url, 

79 send_referrer=False, 

80 from_url=from_url, 

81 discord=False, 

82 )