Coverage for cc_modules/tests/cc_sms_tests.py: 36%

1"""

2 camcops_server/cc_modules/tests/cc_sms_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 import logging

29 from typing import cast

30 from unittest import mock, TestCase

31

32 from camcops_server.cc_modules.cc_constants import SmsBackendNames

33 from camcops_server.cc_modules.cc_sms import (

34 get_sms_backend,

35 ConsoleSmsBackend,

36 KapowSmsBackend,

37 TwilioSmsBackend,

38)

39

40

41 TEST_MESSAGE = "Test Message"

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

43# 07700 900000 to 900999 reserved for TV and Radio drama purposes

44 TEST_RECIPIENT = "+447700900123"

45 TEST_SENDER = "+447700900456"

46

47

48 class KapowSmsBackendTests(TestCase):

49 @mock.patch("camcops_server.cc_modules.cc_sms.requests.post")

50 def test_sends_sms(self, mock_post: mock.Mock) -> None:

51

52 config = {

53 KapowSmsBackend.PARAM_USERNAME: "testuser",

54 KapowSmsBackend.PARAM_PASSWORD: "testpass",

55 }

56

57 backend = get_sms_backend(SmsBackendNames.KAPOW, config)

58 backend.send_sms(TEST_RECIPIENT, TEST_MESSAGE)

59

60 args, kwargs = mock_post.call_args

61

62 self.assertEqual(

63 args[0], "https://www.kapow.co.uk/scripts/sendsms.php"

64 )

65

66 data = kwargs["data"]

67 self.assertEqual(data["username"], "testuser")

68 self.assertEqual(data["password"], "testpass")

69 self.assertEqual(data["mobile"], TEST_RECIPIENT)

70 self.assertEqual(data["sms"], TEST_MESSAGE)

71

72

73 class TwilioSmsBackendTests(TestCase):

74 def test_backend_creates_client(self) -> None:

75 config = {

76 TwilioSmsBackend.PARAM_SID: "testsid",

77 TwilioSmsBackend.PARAM_TOKEN: "testtoken",

78 TwilioSmsBackend.PARAM_FROM_PHONE_NUMBER: TEST_SENDER,

79 }

80

81 backend = cast(

82 "TwilioSmsBackend", get_sms_backend(SmsBackendNames.TWILIO, config)

83 )

84

85 self.assertEqual(backend.client.username, "testsid")

86 self.assertEqual(backend.client.password, "testtoken")

87

88 def test_sends_sms(self) -> None:

89 config = {

90 TwilioSmsBackend.PARAM_SID: "testsid",

91 TwilioSmsBackend.PARAM_TOKEN: "testtoken",

92 TwilioSmsBackend.PARAM_FROM_PHONE_NUMBER: TEST_SENDER,

93 }

94

95 backend = cast(

96 "TwilioSmsBackend", get_sms_backend(SmsBackendNames.TWILIO, config)

97 )

98

99 with mock.patch.object(

100 backend.client.messages, "create"

101 ) as mock_create:

102 backend.send_sms(TEST_RECIPIENT, TEST_MESSAGE)

103

104 args, kwargs = mock_create.call_args

105

106 self.assertEqual(kwargs["to"], TEST_RECIPIENT)

107 self.assertEqual(kwargs["from_"], TEST_SENDER)

108 self.assertEqual(kwargs["body"], TEST_MESSAGE)

109

110

111 class ConsoleSmsBackendTests(TestCase):

112 def test_sends_sms(self) -> None:

113

114 config = {}

115

116 backend = get_sms_backend(SmsBackendNames.CONSOLE, config)

117

118 with self.assertLogs(level=logging.INFO) as logging_cm:

119 backend.send_sms(TEST_RECIPIENT, TEST_MESSAGE)

120

121 logger_name = "camcops_server.cc_modules.cc_sms"

122

123 self.assertIn(f"INFO:{logger_name}", logging_cm.output[0])

124

125 self.assertIn(

126 ConsoleSmsBackend.make_msg(TEST_RECIPIENT, TEST_MESSAGE),

127 logging_cm.output[0],

128 )