Coverage for cc_modules/tests/cc_session_tests.py: 25%

1"""

2 camcops_server/cc_modules/tests/cc_session_tests.py

3

4===============================================================================

5

6 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

8

9 This file is part of CamCOPS.

10

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

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

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

14 (at your option) any later version.

15

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

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

18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

19 GNU General Public License for more details.

20

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

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

23

24===============================================================================

25

26"""

27

28 from pendulum import DateTime as Pendulum

29

30 from camcops_server.cc_modules.cc_session import CamcopsSession, generate_token

31 from camcops_server.cc_modules.cc_taskfilter import TaskFilter

32 from camcops_server.cc_modules.cc_unittest import (

33 BasicDatabaseTestCase,

34 DemoDatabaseTestCase,

35)

36 from camcops_server.cc_modules.cc_user import User

37

38

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

40# Unit tests

41# =============================================================================

42

43

44 class SessionTests(DemoDatabaseTestCase):

45 """

46 Unit tests.

47 """

48

49 def test_sessions(self) -> None:

50 self.announce("test_sessions")

51 req = self.req

52 dbsession = self.dbsession

53

54 self.assertIsInstance(generate_token(), str)

55

56 CamcopsSession.delete_old_sessions(req)

57 self.assertIsInstance(

58 CamcopsSession.get_oldest_last_activity_allowed(req), Pendulum

59 )

60

61 s = req.camcops_session

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

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

64

65 self.assertIsInstance(s.last_activity_utc_iso, str)

66 self.assertIsInstanceOrNone(s.username, str)

67 s.logout()

68 s.login(u)

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

70

71 # Now test deletion cascade

72 dbsession.commit()

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

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

75

76 dbsession.delete(s)

77 dbsession.commit()

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

79 assert (

80 numfilters == 0

81 ), "TaskFilter count should be 0; cascade delete not working"

82

83

84 class GetSessionTests(BasicDatabaseTestCase):

85 old_ip_addr = "192.0.2.1"

86 new_ip_addr = "192.0.2.2"

87

88 def setUp(self) -> None:

89 super().setUp()

90 CamcopsSession.delete_old_sessions(self.req)

91

92 self.old_session = CamcopsSession(

93 ip_addr=self.old_ip_addr, last_activity_utc=self.req.now_utc

94 )

95 self.dbsession.add(self.old_session)

96 self.dbsession.flush()

97

98 def test_old_session_for_same_ip(self) -> None:

99 self.req.remote_addr = self.old_ip_addr

100 new_session = CamcopsSession.get_session(

101 self.req, str(self.old_session.id), self.old_session.token

102 )

103 self.dbsession.add(new_session)

104 self.dbsession.flush()

105 self.assertEqual(self.old_session.id, new_session.id)

106

107 def test_old_session_for_different_ip_when_ip_ignored(self) -> None:

108 self.req.config.session_check_user_ip = False

109 self.req.remote_addr = self.new_ip_addr

110 new_session = CamcopsSession.get_session(

111 self.req, str(self.old_session.id), self.old_session.token

112 )

113 self.dbsession.add(new_session)

114 self.dbsession.flush()

115 self.assertEqual(self.old_session.id, new_session.id)

116

117 def test_new_session_for_different_ip_when_ip_checked(self) -> None:

118 self.req.config.session_check_user_ip = True

119 self.req.remote_addr = self.new_ip_addr

120 new_session = CamcopsSession.get_session(

121 self.req, str(self.old_session.id), self.old_session.token

122 )

123 self.dbsession.add(new_session)

124 self.dbsession.flush()

125 self.assertNotEqual(self.old_session.id, new_session.id)