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_user_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_group import Group 

32from camcops_server.cc_modules.cc_unittest import DemoDatabaseTestCase 

33from camcops_server.cc_modules.cc_user import ( 

34 SecurityAccountLockout, 

35 SecurityLoginFailure, 

36 User, 

37) 

38 

39 

40# ============================================================================= 

41# Unit testing 

42# ============================================================================= 

43 

44class UserTests(DemoDatabaseTestCase): 

45 """ 

46 Unit tests. 

47 """ 

48 def test_user(self) -> None: 

49 self.announce("test_user") 

50 req = self.req 

51 

52 SecurityAccountLockout.delete_old_account_lockouts(req) 

53 self.assertIsInstance( 

54 SecurityAccountLockout.is_user_locked_out(req, "dummy_user"), 

55 bool 

56 ) 

57 self.assertIsInstanceOrNone( 

58 SecurityAccountLockout.user_locked_out_until(req, "dummy_user"), 

59 Pendulum 

60 ) 

61 

62 self.assertIsInstance( 

63 SecurityLoginFailure.how_many_login_failures(req, "dummy_user"), 

64 int 

65 ) 

66 SecurityLoginFailure.clear_login_failures_for_nonexistent_users(req) 

67 SecurityLoginFailure.clear_dummy_login_failures_if_necessary(req) 

68 SecurityLoginFailure.clear_dummy_login_failures_if_necessary(req) 

69 # ... do it twice (we had a bug relating to offset-aware vs 

70 # offset-naive date/time objects). 

71 

72 self.assertIsInstance(User.is_username_permissible("some_user"), bool) 

73 User.take_some_time_mimicking_password_encryption() 

74 

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

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

77 

78 g = self.dbsession.query(Group).first() # type: Group 

79 assert g, "Missing group in demo database!" 

80 

81 self.assertIsInstance(u.is_password_correct("dummy_password"), bool) 

82 self.assertIsInstance(u.must_agree_terms, bool) 

83 u.agree_terms(req) 

84 u.clear_login_failures(req) 

85 self.assertIsInstance(u.is_locked_out(req), bool) 

86 self.assertIsInstanceOrNone(u.locked_out_until(req), Pendulum) 

87 u.enable(req) 

88 self.assertIsInstance(u.may_login_as_tablet, bool) 

89 # TODO: cc_user.UserTests: could do more here 

90 self.assertIsInstance(u.authorized_as_groupadmin, bool) 

91 self.assertIsInstance(u.may_use_webviewer, bool) 

92 self.assertIsInstance(u.authorized_to_add_special_note(g.id), bool) 

93 self.assertIsInstance(u.authorized_to_erase_tasks(g.id), bool) 

94 self.assertIsInstance(u.authorized_to_dump, bool) 

95 self.assertIsInstance(u.authorized_for_reports, bool) 

96 self.assertIsInstance(u.may_view_all_patients_when_unfiltered, bool) 

97 self.assertIsInstance(u.may_view_no_patients_when_unfiltered, bool) 

98 self.assertIsInstance(u.may_upload_to_group(g.id), bool) 

99 self.assertIsInstance(u.may_upload, bool) 

100 self.assertIsInstance(u.may_register_devices, bool)