def setUp(self): self.source_contact = self.create_contact(data={"first_name": "John", "last_name": "Smith"}) self.dest_contact = self.create_contact(data={"first_name": "John", "last_name": "Smith"}) self.backend = self.create_backend(data={"name": "mockbackend"}) self.unreg_conn = self.create_connection({"backend": self.backend}) self.source_conn = self.create_connection( {"contact": self.source_contact, "backend": self.backend, "identity": "5678"} ) self.dest_conn = self.create_connection( {"contact": self.dest_contact, "backend": self.backend, "identity": "1234"} ) self.router = MockRouter() self.app = BroadcastApp(router=self.router) self.rule = self.create_forwarding_rule(data={"keyword": "abc"}) self.rule.source.contacts.add(self.source_contact)
def setUp(self): self.source_contact = self.create_contact(data={'first_name': 'John', 'last_name': 'Smith'}) self.dest_contact = self.create_contact(data={'first_name': 'John', 'last_name': 'Smith'}) self.backend = self.create_backend(data={'name': 'mockbackend'}) self.unreg_conn = self.create_connection({'backend': self.backend}) self.source_conn = self.create_connection({'contact': self.source_contact, 'backend': self.backend, 'identity': '5678'}) self.dest_conn = self.create_connection({'contact': self.dest_contact, 'backend': self.backend, 'identity': '1234'}) self.router = MockRouter() self.app = BroadcastApp(router=self.router) self.rule = self.create_forwarding_rule(data={'keyword': 'abc'}) self.rule.source.contacts.add(self.source_contact)
class BroadcastForwardingTest(BroadcastCreateDataTest, RemindersCreateDataTest): def setUp(self): self.source_contact = self.create_contact(data={'first_name': 'John', 'last_name': 'Smith'}) self.dest_contact = self.create_contact(data={'first_name': 'John', 'last_name': 'Smith'}) self.backend = self.create_backend(data={'name': 'mockbackend'}) self.unreg_conn = self.create_connection({'backend': self.backend}) self.source_conn = self.create_connection({'contact': self.source_contact, 'backend': self.backend, 'identity': '5678'}) self.dest_conn = self.create_connection({'contact': self.dest_contact, 'backend': self.backend, 'identity': '1234'}) self.router = MockRouter() self.app = BroadcastApp(router=self.router) self.rule = self.create_forwarding_rule(data={'keyword': 'abc'}) self.rule.source.contacts.add(self.source_contact) def _send(self, conn, text): msg = IncomingMessage(conn, text) self.app.handle(msg) return msg def test_non_matching_rule(self): """ tests that no response comes for non-matching keywords """ msg = self._send(self.source_conn, 'non-matching-keyword') self.assertEqual(len(msg.responses), 0) def test_unregistered(self): """ tests the response from an unregistered user """ msg = self._send(self.unreg_conn, 'abc') self.assertEqual(len(msg.responses), 1) self.assertEqual(msg.responses[0].text, self.app.not_registered) def test_rule_with_no_source(self): """ like test_unregistered, but a rule with no source sends everybody the unregistered response """ self.rule.source = None self.rule.save() msg = self._send(self.unreg_conn, 'abc') self.assertEqual(len(msg.responses), 1) self.assertEqual(msg.responses[0].text, self.app.not_registered) def test_wrong_group(self): """ tests the response from a user in non-source group """ msg = self._send(self.dest_conn, 'abc') self.assertEqual(len(msg.responses), 1) self.assertEqual(msg.responses[0].text, self.app.not_registered) def test_patient_identifier(self): """ Test that when we forward a message from a patient, we insert the patient's subject identifier""" patient = self.create_patient(data={'contact': self.source_contact}) msg = self._send(self.source_conn, 'abc my-message') self.assertEqual(Broadcast.objects.count(), 1) bc = Broadcast.objects.get() identifier = patient.subject_number expected_msg = 'From {name} ({number}): {msg} my-message'\ .format(name=identifier, number=self.source_conn.identity, msg=self.rule.message) self.assertEqual(bc.body, expected_msg) def test_creates_broadcast(self): """ tests the response from a user in non-source group """ msg = self._send(self.source_conn, 'abc my-message') now = datetime.datetime.now() self.assertEqual(len(msg.responses), 1) self.assertEqual(Broadcast.objects.count(), 1) bc = Broadcast.objects.get() self.assertDateEqual(bc.date_created, now) self.assertDateEqual(bc.date, now) self.assertEqual(bc.schedule_frequency, 'one-time') expected_msg = 'From {name} ({number}): {msg} my-message'\ .format(name=self.source_contact.name, number=self.source_conn.identity, msg=self.rule.message) self.assertEqual(bc.body, expected_msg) self.assertEqual(list(bc.groups.all()), [self.rule.dest]) self.assertEqual(msg.responses[0].text, self.app.thank_you) def test_creates_crufty_broadcast(self): """ tests the response from a user in non-source group """ msg = self._send(self.source_conn, 'abc: my-message') bc = Broadcast.objects.get() self.assertEqual(msg.responses[0].text, self.app.thank_you) def test_creates_spacy_broadcast(self): """ tests the response from a user in non-source group """ rule = self.create_forwarding_rule(data={'keyword': 'def '}) rule.source.contacts.add(self.source_contact) msg = self._send(self.source_conn, 'def: my-message') bc = Broadcast.objects.get() expected_msg = u'From {name} ({number}): {msg} my-message'\ .format(name=self.source_contact.name, number=self.source_conn.identity, msg=rule.message) self.assertEqual(bc.body, expected_msg) self.assertEqual(list(bc.groups.all()), [rule.dest]) self.assertEqual(msg.responses[0].text, self.app.thank_you) def test_unicode_broadcast_body(self): """ Make sure unicode strings can be broadcasted """ text = u'abc ' + self.random_unicode_string(2) msg = self._send(self.source_conn, text) self.assertEqual(len(msg.responses), 1) self.assertEqual(Broadcast.objects.count(), 1) def test_rule_tracking(self): """Test the broadcast is correctly associated with the rule via FK.""" msg = self._send(self.source_conn, 'abc my-message') self.assertEqual(Broadcast.objects.count(), 1) bc = Broadcast.objects.get() self.assertEqual(bc.forward, self.rule)