Coverage for cc_modules/tests/cc_user_tests.py: 20%

1"""

2 camcops_server/cc_modules/tests/cc_user_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 import phonenumbers

30

31 from camcops_server.cc_modules.cc_constants import (

32 OBSCURE_EMAIL_ASTERISKS,

33 OBSCURE_PHONE_ASTERISKS,

34)

35 from camcops_server.cc_modules.cc_group import Group

36 from camcops_server.cc_modules.cc_testfactories import (

37 GroupFactory,

38 UserFactory,

39 UserGroupMembershipFactory,

40)

41 from camcops_server.cc_modules.cc_unittest import (

42 DemoRequestTestCase,

43)

44 from camcops_server.cc_modules.cc_user import (

45 SecurityAccountLockout,

46 SecurityLoginFailure,

47 User,

48)

49

50

51# =============================================================================

52# Unit testing

53# =============================================================================

54

55

56 class UserTests(DemoRequestTestCase):

57 """

58 Unit tests.

59 """

60

61 def test_user(self) -> None:

62 UserFactory()

63 GroupFactory()

64

65 req = self.req

66

67 SecurityAccountLockout.delete_old_account_lockouts(req)

68 self.assertIsInstance(

69 SecurityAccountLockout.is_user_locked_out(req, "dummy_user"), bool

70 )

71 self.assertIsInstanceOrNone(

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

73 Pendulum,

74 )

75

76 self.assertIsInstance(

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

78 int,

79 )

80 SecurityLoginFailure.clear_login_failures_for_nonexistent_users(req)

81 SecurityLoginFailure.clear_dummy_login_failures_if_necessary(req)

82 SecurityLoginFailure.clear_dummy_login_failures_if_necessary(req)

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

84 # offset-naive date/time objects).

85

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

87 User.take_some_time_mimicking_password_encryption()

88

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

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

91

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

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

94

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

96 self.assertIsInstance(u.must_agree_terms, bool)

97 u.agree_terms(req)

98 u.clear_login_failures(req)

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

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

101 u.enable(req)

102 self.assertIsInstance(u.may_login_as_tablet, bool)

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

104 self.assertIsInstance(u.authorized_as_groupadmin, bool)

105 self.assertIsInstance(u.may_use_webviewer, bool)

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

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

108 self.assertIsInstance(u.authorized_to_dump, bool)

109 self.assertIsInstance(u.authorized_for_reports, bool)

110 self.assertIsInstance(u.may_view_all_patients_when_unfiltered, bool)

111 self.assertIsInstance(u.may_view_no_patients_when_unfiltered, bool)

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

113 self.assertIsInstance(u.may_upload, bool)

114 self.assertIsInstance(u.may_register_devices, bool)

115

116 def test_partial_email(self) -> None:

117 # https://en.wikipedia.org/wiki/Email_address

118 a = OBSCURE_EMAIL_ASTERISKS

119 tests = (

120 ("simple@example.com", f"s{a}e@example.com"),

121 ("very.common@example.com", f"v{a}n@example.com"),

122 (

123 "disposable.style.email.with+symbol@example.com",

124 f"d{a}l@example.com",

125 ),

126 ("other.email-with-hyphen@example.com", f"o{a}n@example.com"),

127 ("x@example.com", f"x{a}x@example.com"),

128 (

129 "example-indeed@strange-example.com",

130 f"e{a}d@strange-example.com",

131 ),

132 ("test/test@test.com", f"t{a}t@test.com"),

133 ("admin@mailserver1", f"a{a}n@mailserver1"),

134 ("example@s.example", f"e{a}e@s.example"),

135 ('" "@example.org', f'"{a}"@example.org'),

136 ('"john..doe"@example.org', f'"{a}"@example.org'),

137 ("mailhost!username@example.org", f"m{a}e@example.org"),

138 ("user%example.com@example.org", f"u{a}m@example.org"),

139 ("user-@example.org", f"u{a}-@example.org"),

140 ("very.unusual.”@”.unusual.com@example.com", f"v{a}m@example.com"),

141 )

142

143 user = UserFactory()

144

145 for email, expected_partial in tests:

146 user.email = email

147 self.assertEqual(

148 user.partial_email, expected_partial, msg=f"Failed for {email}"

149 )

150

151 def test_partial_phone_number(self) -> None:

152 # https://www.ofcom.org.uk/phones-telecoms-and-internet/information-for-industry/numbering/numbers-for-drama # noqa: E501

153 user = UserFactory(phone_number=phonenumbers.parse("+447700900123"))

154

155 a = OBSCURE_PHONE_ASTERISKS

156 self.assertEqual(user.partial_phone_number, f"{a}23")

157

158

159 class UserPermissionTests(DemoRequestTestCase):

160 def setUp(self) -> None:

161 super().setUp()

162 # Deliberately not in alphabetical order to test sorting

163 self.group_c = GroupFactory(name="groupc")

164 self.group_b = GroupFactory(name="groupb")

165 self.group_a = GroupFactory(name="groupa")

166 self.group_d = GroupFactory(name="groupd")

167

168 def test_groups_user_may_manage_patients_in(self) -> None:

169 user = UserFactory()

170

171 UserGroupMembershipFactory(

172 user_id=user.id, group_id=self.group_d.id, may_manage_patients=True

173 )

174 UserGroupMembershipFactory(

175 user_id=user.id, group_id=self.group_c.id, may_manage_patients=True

176 )

177 UserGroupMembershipFactory(

178 user_id=user.id,

179 group_id=self.group_a.id,

180 may_manage_patients=False,

181 )

182

183 self.assertEqual(

184 [self.group_c, self.group_d],

185 user.groups_user_may_manage_patients_in,

186 )

187

188 def test_groups_user_may_email_patients_in(self) -> None:

189 user = UserFactory()

190

191 UserGroupMembershipFactory(

192 user_id=user.id, group_id=self.group_d.id, may_email_patients=True

193 )

194 UserGroupMembershipFactory(

195 user_id=user.id, group_id=self.group_c.id, may_email_patients=True

196 )

197 UserGroupMembershipFactory(

198 user_id=user.id, group_id=self.group_a.id, may_email_patients=False

199 )

200

201 self.assertEqual(

202 [self.group_c, self.group_d],

203 user.groups_user_may_email_patients_in,

204 )

205

206 def test_ids_of_groups_user_may_report_on(self) -> None:

207 user = UserFactory()

208

209 UserGroupMembershipFactory(

210 user_id=user.id, group_id=self.group_a.id, may_run_reports=False

211 )

212 UserGroupMembershipFactory(

213 user_id=user.id, group_id=self.group_c.id, may_run_reports=True

214 )

215 UserGroupMembershipFactory(

216 user_id=user.id, group_id=self.group_d.id, may_run_reports=True

217 )

218

219 ids = user.ids_of_groups_user_may_report_on

220

221 self.assertIn(self.group_c.id, ids)

222 self.assertIn(self.group_d.id, ids)

223 self.assertNotIn(self.group_a.id, ids)

224 self.assertNotIn(self.group_b.id, ids)

225

226 def test_ids_of_groups_superuser_may_report_on(self) -> None:

227 user = UserFactory(superuser=True)

228

229 ids = user.ids_of_groups_user_may_report_on

230

231 self.assertIn(self.group_a.id, ids)

232 self.assertIn(self.group_b.id, ids)

233 self.assertIn(self.group_c.id, ids)

234 self.assertIn(self.group_d.id, ids)

235

236 def test_ids_of_groups_user_is_admin_for(self) -> None:

237 user = UserFactory()

238

239 UserGroupMembershipFactory(

240 user_id=user.id, group_id=self.group_a.id, groupadmin=False

241 )

242 UserGroupMembershipFactory(

243 user_id=user.id, group_id=self.group_c.id, groupadmin=True

244 )

245 UserGroupMembershipFactory(

246 user_id=user.id, group_id=self.group_d.id, groupadmin=True

247 )

248

249 ids = user.ids_of_groups_user_is_admin_for

250

251 self.assertIn(self.group_c.id, ids)

252 self.assertIn(self.group_d.id, ids)

253 self.assertNotIn(self.group_a.id, ids)

254 self.assertNotIn(self.group_b.id, ids)

255

256 def test_ids_of_groups_superuser_is_admin_for(self) -> None:

257 user = UserFactory(superuser=True)

258

259 ids = user.ids_of_groups_user_is_admin_for

260

261 self.assertIn(self.group_a.id, ids)

262 self.assertIn(self.group_b.id, ids)

263 self.assertIn(self.group_c.id, ids)

264 self.assertIn(self.group_d.id, ids)

265

266 def test_names_of_groups_user_is_admin_for(self) -> None:

267 user = UserFactory()

268

269 UserGroupMembershipFactory(

270 user_id=user.id, group_id=self.group_a.id, groupadmin=False

271 )

272 UserGroupMembershipFactory(

273 user_id=user.id, group_id=self.group_c.id, groupadmin=True

274 )

275 UserGroupMembershipFactory(

276 user_id=user.id, group_id=self.group_d.id, groupadmin=True

277 )

278

279 names = user.names_of_groups_user_is_admin_for

280

281 self.assertIn(self.group_c.name, names)

282 self.assertIn(self.group_d.name, names)

283 self.assertNotIn(self.group_a.name, names)

284 self.assertNotIn(self.group_b.name, names)

285

286 def test_names_of_groups_superuser_is_admin_for(self) -> None:

287 user = UserFactory(superuser=True)

288

289 names = user.names_of_groups_user_is_admin_for

290

291 self.assertIn(self.group_a.name, names)

292 self.assertIn(self.group_b.name, names)

293 self.assertIn(self.group_c.name, names)

294 self.assertIn(self.group_d.name, names)

295

296 def test_groups_user_is_admin_for(self) -> None:

297 user = UserFactory()

298

299 UserGroupMembershipFactory(

300 user_id=user.id, group_id=self.group_a.id, groupadmin=False

301 )

302 UserGroupMembershipFactory(

303 user_id=user.id, group_id=self.group_c.id, groupadmin=True

304 )

305 UserGroupMembershipFactory(

306 user_id=user.id, group_id=self.group_d.id, groupadmin=True

307 )

308

309 self.assertEqual(

310 [self.group_c, self.group_d], user.groups_user_is_admin_for

311 )

312

313 def test_user_may_administer_group(self) -> None:

314 user = UserFactory()

315

316 UserGroupMembershipFactory(

317 user_id=user.id, group_id=self.group_a.id, groupadmin=False

318 )

319 UserGroupMembershipFactory(

320 user_id=user.id, group_id=self.group_c.id, groupadmin=True

321 )

322 UserGroupMembershipFactory(

323 user_id=user.id, group_id=self.group_d.id, groupadmin=True

324 )

325

326 self.assertFalse(user.may_administer_group(self.group_a.id))

327 self.assertTrue(user.may_administer_group(self.group_c.id))

328 self.assertTrue(user.may_administer_group(self.group_d.id))

329

330 def test_superuser_may_administer_group(self) -> None:

331 user = UserFactory(superuser=True)

332

333 self.assertTrue(user.may_administer_group(self.group_a.id))

334 self.assertTrue(user.may_administer_group(self.group_b.id))

335 self.assertTrue(user.may_administer_group(self.group_c.id))

336 self.assertTrue(user.may_administer_group(self.group_d.id))

337

338 def test_groups_user_may_dump(self) -> None:

339 user = UserFactory()

340

341 UserGroupMembershipFactory(

342 user_id=user.id, group_id=self.group_d.id, may_dump_data=True

343 )

344 UserGroupMembershipFactory(

345 user_id=user.id, group_id=self.group_c.id, may_dump_data=True

346 )

347 UserGroupMembershipFactory(

348 user_id=user.id, group_id=self.group_a.id, may_dump_data=False

349 )

350

351 self.assertEqual(

352 [self.group_c, self.group_d], user.groups_user_may_dump

353 )

354

355 def test_groups_user_may_report_on(self) -> None:

356 user = UserFactory()

357

358 UserGroupMembershipFactory(

359 user_id=user.id, group_id=self.group_d.id, may_run_reports=True

360 )

361 UserGroupMembershipFactory(

362 user_id=user.id, group_id=self.group_c.id, may_run_reports=True

363 )

364 UserGroupMembershipFactory(

365 user_id=user.id, group_id=self.group_a.id, may_run_reports=False

366 )

367

368 self.assertEqual(

369 [self.group_c, self.group_d], user.groups_user_may_report_on

370 )

371

372 def test_groups_user_may_upload_into(self) -> None:

373 user = UserFactory()

374

375 UserGroupMembershipFactory(

376 user_id=user.id, group_id=self.group_d.id, may_upload=True

377 )

378 UserGroupMembershipFactory(

379 user_id=user.id, group_id=self.group_c.id, may_upload=True

380 )

381 UserGroupMembershipFactory(

382 user_id=user.id, group_id=self.group_a.id, may_upload=False

383 )

384

385 self.assertEqual(

386 [self.group_c, self.group_d], user.groups_user_may_upload_into

387 )

388

389 def test_groups_user_may_add_special_notes(self) -> None:

390 user = UserFactory()

391

392 UserGroupMembershipFactory(

393 user_id=user.id, group_id=self.group_d.id, may_add_notes=True

394 )

395 UserGroupMembershipFactory(

396 user_id=user.id, group_id=self.group_c.id, may_add_notes=True

397 )

398 UserGroupMembershipFactory(

399 user_id=user.id, group_id=self.group_a.id, may_add_notes=False

400 )

401

402 self.assertEqual(

403 [self.group_c, self.group_d],

404 user.groups_user_may_add_special_notes,

405 )

406

407 def test_groups_user_may_see_all_pts_when_unfiltered(self) -> None:

408 user = UserFactory()

409

410 UserGroupMembershipFactory(

411 user_id=user.id,

412 group_id=self.group_d.id,

413 view_all_patients_when_unfiltered=True,

414 )

415 UserGroupMembershipFactory(

416 user_id=user.id,

417 group_id=self.group_c.id,

418 view_all_patients_when_unfiltered=True,

419 )

420 UserGroupMembershipFactory(

421 user_id=user.id,

422 group_id=self.group_a.id,

423 view_all_patients_when_unfiltered=False,

424 )

425

426 self.assertEqual(

427 [self.group_c, self.group_d],

428 user.groups_user_may_see_all_pts_when_unfiltered,

429 )

430

431 def test_is_a_group_admin(self) -> None:

432 user = UserFactory()

433

434 UserGroupMembershipFactory(

435 user_id=user.id, group_id=self.group_d.id, groupadmin=True

436 )

437

438 self.assertTrue(user.is_a_groupadmin)

439

440 def test_is_not_a_group_admin(self) -> None:

441 user = UserFactory()

442

443 UserGroupMembershipFactory(

444 user_id=user.id, group_id=self.group_d.id, groupadmin=False

445 )

446

447 self.assertFalse(user.is_a_groupadmin)

448

449 def test_authorized_as_groupadmin(self) -> None:

450 user = UserFactory()

451

452 UserGroupMembershipFactory(

453 user_id=user.id, group_id=self.group_d.id, groupadmin=True

454 )

455

456 self.assertTrue(user.authorized_as_groupadmin)

457

458 def test_not_authorized_as_groupadmin(self) -> None:

459 user = UserFactory()

460

461 UserGroupMembershipFactory(

462 user_id=user.id, group_id=self.group_d.id, groupadmin=False

463 )

464

465 self.assertFalse(user.authorized_as_groupadmin)

466

467 def test_superuser_authorized_as_groupadmin(self) -> None:

468 user = UserFactory(superuser=True)

469

470 self.assertTrue(user.authorized_as_groupadmin)

471

472 def test_membership_for_group_id(self) -> None:

473 user = UserFactory()

474

475 ugm = UserGroupMembershipFactory(

476 user_id=user.id, group_id=self.group_a.id

477 )

478

479 self.assertEqual(user.membership_for_group_id(self.group_a.id), ugm)

480

481 def test_no_membership_for_group_id(self) -> None:

482 user = UserFactory()

483

484 self.assertIsNone(user.membership_for_group_id(self.group_a.id))

485

486 def test_may_use_webviewer(self) -> None:

487 user = UserFactory()

488

489 UserGroupMembershipFactory(

490 user_id=user.id, group_id=self.group_a.id, may_use_webviewer=False

491 )

492 UserGroupMembershipFactory(

493 user_id=user.id, group_id=self.group_c.id, may_use_webviewer=True

494 )

495

496 self.assertTrue(user.may_use_webviewer)

497

498 def test_may_not_use_webviewer(self) -> None:

499 user = UserFactory()

500

501 self.assertFalse(user.may_use_webviewer)

502

503 def test_superuser_may_use_webviewer(self) -> None:

504 user = UserFactory(superuser=True)

505

506 self.assertTrue(user.may_use_webviewer)

507

508 def test_authorized_to_add_special_note(self) -> None:

509 user = UserFactory()

510

511 UserGroupMembershipFactory(

512 user_id=user.id, group_id=self.group_c.id, may_add_notes=True

513 )

514

515 self.assertTrue(user.authorized_to_add_special_note(self.group_c.id))

516

517 def test_not_authorized_to_add_special_note(self) -> None:

518 user = UserFactory()

519

520 UserGroupMembershipFactory(

521 user_id=user.id, group_id=self.group_c.id, may_add_notes=False

522 )

523

524 self.assertFalse(user.authorized_to_add_special_note(self.group_c.id))

525

526 def test_superuser_authorized_to_add_special_note(self) -> None:

527 user = UserFactory(superuser=True)

528

529 self.assertTrue(user.authorized_to_add_special_note(self.group_c.id))

530

531 def test_groupadmin_authorized_to_erase_tasks(self) -> None:

532 user = UserFactory()

533

534 UserGroupMembershipFactory(

535 user_id=user.id, group_id=self.group_c.id, groupadmin=True

536 )

537

538 self.assertTrue(user.authorized_to_erase_tasks(self.group_c.id))

539

540 def test_non_member_not_authorized_to_erase_tasks(self) -> None:

541 user = UserFactory()

542

543 UserGroupMembershipFactory(

544 user_id=user.id, group_id=self.group_a.id, groupadmin=True

545 )

546

547 self.assertFalse(user.authorized_to_erase_tasks(self.group_c.id))

548

549 def test_non_admin_not_authorized_to_erase_tasks(self) -> None:

550 user = UserFactory()

551

552 UserGroupMembershipFactory(user_id=user.id, group_id=self.group_c.id)

553

554 self.assertFalse(user.authorized_to_erase_tasks(self.group_c.id))

555

556 def test_superuser_authorized_to_erase_tasks(self) -> None:

557 user = UserFactory(superuser=True)

558

559 self.assertTrue(user.authorized_to_erase_tasks(self.group_c.id))

560

561 def test_authorized_to_dump(self) -> None:

562 user = UserFactory()

563

564 UserGroupMembershipFactory(

565 user_id=user.id, group_id=self.group_a.id, may_dump_data=False

566 )

567 UserGroupMembershipFactory(

568 user_id=user.id, group_id=self.group_c.id, may_dump_data=True

569 )

570

571 self.assertTrue(user.authorized_to_dump)

572

573 def test_not_authorized_to_dump(self) -> None:

574 user = UserFactory()

575

576 self.assertFalse(user.authorized_to_dump)

577

578 def test_superuser_authorized_to_dump(self) -> None:

579 user = UserFactory(superuser=True)

580

581 self.assertTrue(user.authorized_to_dump)

582

583 def test_authorized_for_reports(self) -> None:

584 user = UserFactory()

585

586 UserGroupMembershipFactory(

587 user_id=user.id, group_id=self.group_a.id, may_run_reports=False

588 )

589 UserGroupMembershipFactory(

590 user_id=user.id, group_id=self.group_c.id, may_run_reports=True

591 )

592

593 self.assertTrue(user.authorized_for_reports)

594

595 def test_not_authorized_for_reports(self) -> None:

596 user = UserFactory()

597

598 self.assertFalse(user.authorized_for_reports)

599

600 def test_superuser_authorized_for_reports(self) -> None:

601 user = UserFactory(superuser=True)

602

603 self.assertTrue(user.authorized_for_reports)

604

605 def test_may_view_all_patients_when_unfiltered(self) -> None:

606 user = UserFactory()

607

608 UserGroupMembershipFactory(

609 user_id=user.id,

610 group_id=self.group_a.id,

611 view_all_patients_when_unfiltered=True,

612 )

613 UserGroupMembershipFactory(

614 user_id=user.id,

615 group_id=self.group_c.id,

616 view_all_patients_when_unfiltered=True,

617 )

618

619 self.assertTrue(user.may_view_all_patients_when_unfiltered)

620

621 def test_may_not_view_all_patients_when_unfiltered(self) -> None:

622 user = UserFactory()

623

624 UserGroupMembershipFactory(

625 user_id=user.id,

626 group_id=self.group_a.id,

627 view_all_patients_when_unfiltered=True,

628 )

629 UserGroupMembershipFactory(

630 user_id=user.id,

631 group_id=self.group_c.id,

632 view_all_patients_when_unfiltered=False,

633 )

634

635 self.assertFalse(user.may_view_all_patients_when_unfiltered)

636

637 def test_superuser_may_view_all_patients_when_unfiltered(self) -> None:

638 user = UserFactory(superuser=True)

639

640 self.assertTrue(user.may_view_all_patients_when_unfiltered)

641

642 def test_may_view_no_patients_when_unfiltered(self) -> None:

643 user = UserFactory()

644

645 self.assertTrue(user.may_view_no_patients_when_unfiltered)

646

647 def test_may_not_view_no_patients_when_unfiltered(self) -> None:

648 user = UserFactory()

649

650 UserGroupMembershipFactory(

651 user_id=user.id,

652 group_id=self.group_a.id,

653 view_all_patients_when_unfiltered=True,

654 )

655 UserGroupMembershipFactory(

656 user_id=user.id,

657 group_id=self.group_c.id,

658 view_all_patients_when_unfiltered=True,

659 )

660

661 self.assertFalse(user.may_view_no_patients_when_unfiltered)

662

663 def test_superuser_may_not_view_no_patients_when_unfiltered(self) -> None:

664 user = UserFactory(superuser=True)

665

666 self.assertFalse(user.may_view_no_patients_when_unfiltered)

667

668 def test_group_ids_that_nonsuperuser_may_see_when_unfiltered(self) -> None:

669 user = UserFactory()

670

671 UserGroupMembershipFactory(

672 user_id=user.id,

673 group_id=self.group_a.id,

674 view_all_patients_when_unfiltered=False,

675 )

676 UserGroupMembershipFactory(

677 user_id=user.id,

678 group_id=self.group_c.id,

679 view_all_patients_when_unfiltered=True,

680 )

681 UserGroupMembershipFactory(

682 user_id=user.id,

683 group_id=self.group_d.id,

684 view_all_patients_when_unfiltered=True,

685 )

686

687 ids = user.group_ids_nonsuperuser_may_see_when_unfiltered()

688

689 self.assertIn(self.group_c.id, ids)

690 self.assertIn(self.group_d.id, ids)

691 self.assertNotIn(self.group_a.id, ids)

692 self.assertNotIn(self.group_b.id, ids)

693

694 def test_may_upload_to_group(self) -> None:

695 user = UserFactory()

696

697 UserGroupMembershipFactory(

698 user_id=user.id, group_id=self.group_a.id, may_upload=True

699 )

700

701 self.assertTrue(user.may_upload_to_group(self.group_a.id))

702

703 def test_may_not_upload_to_group(self) -> None:

704 user = UserFactory()

705

706 UserGroupMembershipFactory(

707 user_id=user.id, group_id=self.group_a.id, may_upload=False

708 )

709

710 self.assertFalse(user.may_upload_to_group(self.group_a.id))

711

712 def test_superuser_may_upload_to_group(self) -> None:

713 user = UserFactory(superuser=True)

714

715 self.assertTrue(user.may_upload_to_group(self.group_a.id))

716

717 def test_may_upload_to_upload_group(self) -> None:

718 user = UserFactory(upload_group_id=self.group_a.id)

719

720 UserGroupMembershipFactory(

721 user_id=user.id, group_id=self.group_a.id, may_upload=True

722 )

723

724 self.assertTrue(user.may_upload)

725

726 def test_may_not_upload_with_no_upload_group(self) -> None:

727 user = UserFactory()

728

729 UserGroupMembershipFactory(

730 user_id=user.id, group_id=self.group_a.id, may_upload=True

731 )

732

733 self.assertFalse(user.may_upload)

734

735 def test_may_not_upload_with_upload_group_but_no_permission(self) -> None:

736 user = UserFactory(upload_group_id=self.group_a.id)

737

738 UserGroupMembershipFactory(

739 user_id=user.id, group_id=self.group_a.id, may_upload=False

740 )

741

742 self.assertFalse(user.may_upload)

743

744 def test_may_register_devices_with_upload_group(self) -> None:

745 user = UserFactory(upload_group_id=self.group_a.id)

746

747 UserGroupMembershipFactory(

748 user_id=user.id,

749 group_id=self.group_a.id,

750 may_register_devices=True,

751 )

752

753 self.assertTrue(user.may_register_devices)

754

755 def test_may_not_register_devices_with_no_upload_group(self) -> None:

756 user = UserFactory()

757

758 self.assertFalse(user.may_register_devices)

759

760 def test_may_not_register_devices_with_upload_group_but_no_permission(

761 self,

762 ) -> None:

763 user = UserFactory(upload_group_id=self.group_a.id)

764

765 UserGroupMembershipFactory(

766 user_id=user.id,

767 group_id=self.group_a.id,

768 may_register_devices=False,

769 )

770

771 self.assertFalse(user.may_register_devices)

772

773 def test_superuser_may_register_devices_with_upload_group(self) -> None:

774 user = UserFactory(upload_group_id=self.group_a.id, superuser=True)

775

776 self.assertTrue(user.may_register_devices)

777

778 def test_superuser_may_not_register_devices_with_no_upload_group(

779 self,

780 ) -> None:

781 user = UserFactory(superuser=True)

782

783 self.assertFalse(user.may_register_devices)

784

785 def test_authorized_to_manage_patients(self) -> None:

786 user = UserFactory()

787

788 UserGroupMembershipFactory(

789 user_id=user.id,

790 group_id=self.group_a.id,

791 may_manage_patients=False,

792 )

793 UserGroupMembershipFactory(

794 user_id=user.id, group_id=self.group_c.id, may_manage_patients=True

795 )

796

797 self.assertTrue(user.authorized_to_manage_patients)

798

799 def test_not_authorized_to_manage_patients(self) -> None:

800 user = UserFactory()

801

802 self.assertFalse(user.authorized_to_manage_patients)

803

804 def test_groupadmin_authorized_to_manage_patients(self) -> None:

805 user = UserFactory()

806

807 UserGroupMembershipFactory(

808 user_id=user.id, group_id=self.group_a.id, groupadmin=True

809 )

810

811 self.assertTrue(user.authorized_to_manage_patients)

812

813 def test_superuser_authorized_to_manage_patients(self) -> None:

814 user = UserFactory(superuser=True)

815

816 self.assertTrue(user.authorized_to_manage_patients)

817

818 def test_user_may_manage_patients_in_group(self) -> None:

819 user = UserFactory()

820

821 UserGroupMembershipFactory(

822 user_id=user.id,

823 group_id=self.group_a.id,

824 may_manage_patients=False,

825 )

826 UserGroupMembershipFactory(

827 user_id=user.id, group_id=self.group_c.id, may_manage_patients=True

828 )

829 UserGroupMembershipFactory(

830 user_id=user.id, group_id=self.group_d.id, may_manage_patients=True

831 )

832

833 self.assertFalse(user.may_manage_patients_in_group(self.group_a.id))

834 self.assertTrue(user.may_manage_patients_in_group(self.group_c.id))

835 self.assertTrue(user.may_manage_patients_in_group(self.group_d.id))

836

837 def test_groupadmin_may_manage_patients_in_group(self) -> None:

838 user = UserFactory()

839

840 UserGroupMembershipFactory(

841 user_id=user.id, group_id=self.group_a.id, groupadmin=False

842 )

843 UserGroupMembershipFactory(

844 user_id=user.id, group_id=self.group_c.id, groupadmin=True

845 )

846 UserGroupMembershipFactory(

847 user_id=user.id, group_id=self.group_d.id, groupadmin=True

848 )

849

850 self.assertFalse(user.may_manage_patients_in_group(self.group_a.id))

851 self.assertTrue(user.may_manage_patients_in_group(self.group_c.id))

852 self.assertTrue(user.may_manage_patients_in_group(self.group_d.id))

853

854 def test_superuser_may_manage_patients_in_group(self) -> None:

855 user = UserFactory(superuser=True)

856

857 self.assertTrue(user.may_manage_patients_in_group(self.group_a.id))

858 self.assertTrue(user.may_manage_patients_in_group(self.group_b.id))

859 self.assertTrue(user.may_manage_patients_in_group(self.group_c.id))

860 self.assertTrue(user.may_manage_patients_in_group(self.group_d.id))

861

862 def test_authorized_to_email_patients(self) -> None:

863 user = UserFactory()

864

865 UserGroupMembershipFactory(

866 user_id=user.id, group_id=self.group_a.id, may_email_patients=False

867 )

868 UserGroupMembershipFactory(

869 user_id=user.id, group_id=self.group_c.id, may_email_patients=True

870 )

871

872 self.assertTrue(user.authorized_to_email_patients)

873

874 def test_not_authorized_to_email_patients(self) -> None:

875 user = UserFactory()

876

877 self.assertFalse(user.authorized_to_email_patients)

878

879 def test_groupadmin_authorized_to_email_patients(self) -> None:

880 user = UserFactory()

881

882 UserGroupMembershipFactory(

883 user_id=user.id, group_id=self.group_a.id, groupadmin=True

884 )

885

886 self.assertTrue(user.authorized_to_email_patients)

887

888 def test_superuser_authorized_to_email_patients(self) -> None:

889 user = UserFactory(superuser=True)

890

891 self.assertTrue(user.authorized_to_email_patients)

892

893 def test_user_may_email_patients_in_group(self) -> None:

894 user = UserFactory()

895

896 UserGroupMembershipFactory(

897 user_id=user.id, group_id=self.group_a.id, may_email_patients=False

898 )

899 UserGroupMembershipFactory(

900 user_id=user.id, group_id=self.group_c.id, may_email_patients=True

901 )

902 UserGroupMembershipFactory(

903 user_id=user.id, group_id=self.group_d.id, may_email_patients=True

904 )

905

906 self.assertFalse(user.may_email_patients_in_group(self.group_a.id))

907 self.assertTrue(user.may_email_patients_in_group(self.group_c.id))

908 self.assertTrue(user.may_email_patients_in_group(self.group_d.id))

909

910 def test_groupadmin_may_email_patients_in_group(self) -> None:

911 user = UserFactory()

912

913 UserGroupMembershipFactory(

914 user_id=user.id, group_id=self.group_a.id, groupadmin=False

915 )

916 UserGroupMembershipFactory(

917 user_id=user.id, group_id=self.group_c.id, groupadmin=True

918 )

919 UserGroupMembershipFactory(

920 user_id=user.id, group_id=self.group_d.id, groupadmin=True

921 )

922

923 self.assertFalse(user.may_email_patients_in_group(self.group_a.id))

924 self.assertTrue(user.may_email_patients_in_group(self.group_c.id))

925 self.assertTrue(user.may_email_patients_in_group(self.group_d.id))

926

927 def test_superuser_may_email_patients_in_group(self) -> None:

928 user = UserFactory(superuser=True)

929

930 self.assertTrue(user.may_email_patients_in_group(self.group_a.id))

931 self.assertTrue(user.may_email_patients_in_group(self.group_b.id))

932 self.assertTrue(user.may_email_patients_in_group(self.group_c.id))

933 self.assertTrue(user.may_email_patients_in_group(self.group_d.id))

934

935

936 class SetGroupIdsTests(DemoRequestTestCase):

937 def test_old_group_ids_removed(self) -> None:

938 group_a = GroupFactory()

939 group_b = GroupFactory()

940

941 user = UserFactory()

942

943 UserGroupMembershipFactory(user_id=user.id, group_id=group_a.id)

944 UserGroupMembershipFactory(user_id=user.id, group_id=group_b.id)

945 self.assertEqual(len(user.user_group_memberships), 2)

946

947 user.set_group_ids([])

948 self.dbsession.refresh(user)

949

950 self.assertEqual(len(user.user_group_memberships), 0)

951

952 def test_new_group_ids_added(self) -> None:

953 group_a = GroupFactory()

954 group_b = GroupFactory()

955

956 user = UserFactory()

957

958 self.assertEqual(len(user.user_group_memberships), 0)

959

960 user.set_group_ids([group_a.id, group_b.id])

961 self.dbsession.refresh(user)

962

963 self.assertEqual(len(user.user_group_memberships), 2)