Coverage for cc_modules/tests/cc_config_tests.py: 18%

1"""

2 camcops_server/cc_modules/tests/cc_config_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 import configparser

28 from unittest import TestCase

29

30 from camcops_server.cc_modules.cc_config import CamcopsConfig, get_demo_config

31

32

33# =============================================================================

34# Unit tests

35# =============================================================================

36

37

38 class EmailConfigTests(TestCase):

39 def setUp(self):

40 super().setUp()

41

42 from io import StringIO

43

44 # Start with a working config and just set the things we want to test

45 config_text = get_demo_config()

46 self.parser = configparser.ConfigParser()

47 self.parser.read_string(config_text)

48

49 self.parser.set("export", "RECIPIENTS", "recipient_A")

50 self.parser.set(

51 "recipient:recipient_A", "TRANSMISSION_METHOD", "email"

52 )

53

54 self.parser.set("site", "EMAIL_HOST", "smtp.example.com")

55 self.parser.set("site", "EMAIL_PORT", "587")

56 self.parser.set("site", "EMAIL_USE_TLS", "true")

57 self.parser.set("site", "EMAIL_HOST_USERNAME", "username")

58 self.parser.set("site", "EMAIL_HOST_PASSWORD", "mypassword")

59 self.parser.set(

60 "site", "EMAIL_FROM", "CamCOPS computer <from@example.com>"

61 )

62 self.parser.set(

63 "site", "EMAIL_SENDER", "CamCOPS computer <sender@example.com>"

64 )

65 self.parser.set(

66 "site",

67 "EMAIL_REPLY_TO",

68 "CamCOPS clinical administrator <admin@example.com>",

69 )

70

71 with StringIO() as buffer:

72 self.parser.write(buffer)

73 self.config = CamcopsConfig(

74 config_filename="", config_text=buffer.getvalue()

75 )

76

77 def test_export_recipients_use_site_email_config(self) -> None:

78 recipient = self.config._export_recipients[0]

79 self.assertEqual(recipient.recipient_name, "recipient_A")

80

81 self.assertEqual(recipient.email_host, "smtp.example.com")

82 self.assertEqual(recipient.email_port, 587)

83 self.assertTrue(recipient.email_use_tls)

84 self.assertEqual(recipient.email_host_username, "username")

85 self.assertEqual(recipient.email_host_password, "mypassword")

86 self.assertEqual(

87 recipient.email_from, "CamCOPS computer <from@example.com>"

88 )

89 self.assertEqual(

90 recipient.email_sender, "CamCOPS computer <sender@example.com>"

91 )

92 self.assertEqual(

93 recipient.email_reply_to,

94 "CamCOPS clinical administrator <admin@example.com>",

95 )