Coverage for .tox/coverage/lib/python3.11/site-packages/wuttaweb/testing.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-06 17:06 -0600

1# -*- coding: utf-8; -*- 

2################################################################################ 

3# 

4# wuttaweb -- Web App for Wutta Framework 

5# Copyright © 2024 Lance Edgar 

6# 

7# This file is part of Wutta Framework. 

8# 

9# Wutta Framework is free software: you can redistribute it and/or modify it 

10# under the terms of the GNU General Public License as published by the Free 

11# Software Foundation, either version 3 of the License, or (at your option) any 

12# later version. 

13# 

14# Wutta Framework is distributed in the hope that it will be useful, but 

15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 

16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 

17# more details. 

18# 

19# You should have received a copy of the GNU General Public License along with 

20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>. 

21# 

22################################################################################ 

23""" 

24WuttaWeb - test utilities 

25""" 

26 

27from unittest.mock import MagicMock 

28 

29import fanstatic 

30from pyramid import testing 

31 

32from wuttjamaican.testing import DataTestCase 

33 

34from wuttaweb import subscribers 

35 

36 

37class WebTestCase(DataTestCase): 

38 """ 

39 Base class for test suites requiring a full (typical) web app. 

40 """ 

41 

42 def setUp(self): 

43 self.setup_web() 

44 

45 def setup_web(self): 

46 self.setup_db() 

47 self.request = self.make_request() 

48 self.pyramid_config = testing.setUp(request=self.request, settings={ 

49 'wutta_config': self.config, 

50 'mako.directories': ['wuttaweb:templates'], 

51 'pyramid_deform.template_search_path': 'wuttaweb:templates/deform', 

52 }) 

53 

54 # init web 

55 self.pyramid_config.include('pyramid_deform') 

56 self.pyramid_config.include('pyramid_mako') 

57 self.pyramid_config.add_directive('add_wutta_permission_group', 

58 'wuttaweb.auth.add_permission_group') 

59 self.pyramid_config.add_directive('add_wutta_permission', 

60 'wuttaweb.auth.add_permission') 

61 self.pyramid_config.add_subscriber('wuttaweb.subscribers.before_render', 

62 'pyramid.events.BeforeRender') 

63 self.pyramid_config.include('wuttaweb.static') 

64 

65 # nb. mock out fanstatic env..good enough for now to avoid errors.. 

66 needed = fanstatic.init_needed() 

67 self.request.environ[fanstatic.NEEDED] = needed 

68 

69 # setup new request w/ anonymous user 

70 event = MagicMock(request=self.request) 

71 subscribers.new_request(event) 

72 def user_getter(request, **kwargs): pass 

73 subscribers.new_request_set_user(event, db_session=self.session, 

74 user_getter=user_getter) 

75 

76 def tearDown(self): 

77 self.teardown_web() 

78 

79 def teardown_web(self): 

80 testing.tearDown() 

81 self.teardown_db() 

82 

83 def make_request(self): 

84 return testing.DummyRequest()