Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/env python 

2 

3""" 

4camcops_server/cc_modules/tests/cc_session_tests.py 

5 

6=============================================================================== 

7 

8 Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com). 

9 

10 This file is part of CamCOPS. 

11 

12 CamCOPS is free software: you can redistribute it and/or modify 

13 it under the terms of the GNU General Public License as published by 

14 the Free Software Foundation, either version 3 of the License, or 

15 (at your option) any later version. 

16 

17 CamCOPS is distributed in the hope that it will be useful, 

18 but WITHOUT ANY WARRANTY; without even the implied warranty of 

19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

20 GNU General Public License for more details. 

21 

22 You should have received a copy of the GNU General Public License 

23 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>. 

24 

25=============================================================================== 

26 

27""" 

28 

29from pendulum import DateTime as Pendulum 

30 

31from camcops_server.cc_modules.cc_session import CamcopsSession, generate_token 

32from camcops_server.cc_modules.cc_taskfilter import TaskFilter 

33from camcops_server.cc_modules.cc_unittest import DemoDatabaseTestCase 

34from camcops_server.cc_modules.cc_user import User 

35 

36 

37# ============================================================================= 

38# Unit tests 

39# ============================================================================= 

40 

41class SessionTests(DemoDatabaseTestCase): 

42 """ 

43 Unit tests. 

44 """ 

45 def test_sessions(self) -> None: 

46 self.announce("test_sessions") 

47 req = self.req 

48 dbsession = self.dbsession 

49 

50 self.assertIsInstance(generate_token(), str) 

51 

52 CamcopsSession.delete_old_sessions(req) 

53 self.assertIsInstance( 

54 CamcopsSession.get_oldest_last_activity_allowed(req), Pendulum) 

55 

56 s = req.camcops_session 

57 u = self.dbsession.query(User).first() # type: User 

58 assert u, "Missing user in demo database!" 

59 

60 self.assertIsInstance(s.last_activity_utc_iso, str) 

61 self.assertIsInstanceOrNone(s.username, str) 

62 s.logout() 

63 s.login(u) 

64 self.assertIsInstance(s.get_task_filter(), TaskFilter) 

65 

66 # Now test deletion cascade 

67 dbsession.commit() 

68 numfilters = dbsession.query(TaskFilter).count() 

69 assert numfilters == 1, "TaskFilter count should be 1" 

70 

71 dbsession.delete(s) 

72 dbsession.commit() 

73 numfilters = dbsession.query(TaskFilter).count() 

74 assert numfilters == 0, ( 

75 "TaskFilter count should be 0; cascade delete not working" 

76 )