def test_queue_receiver(): receiver = server.QueueReceiver('run/queue') run_queue = queue.Queue('run/queue') run_queue.push(str(test_mail_response_plain_text())) assert run_queue.count() > 0 receiver.start(one_shot=True) assert_equal(run_queue.count(), 0) routing.Router.deliver.side_effect = raises_exception receiver.process_message(mail.MailRequest('localhost', 'test@localhost', 'test@localhost', 'Fake body.'))
def test_queue_receiver_pop_error(self, queue_mock, router_mock): def key_error(*args, **kwargs): queue_mock.return_value.count.return_value = 0 raise KeyError queue_mock.return_value.pop.side_effect = key_error receiver = server.QueueReceiver('run/queue') receiver.start(one_shot=True) self.assertEqual(queue_mock.return_value.pop.call_count, 1) self.assertEqual(router_mock.deliver.call_count, 0)
def relay(mail_fields, key, mail_request, final_rating): """If the relay is enabled, this function does the relaying. First, it destroys the attachment if it is enabled, then it sends the email on the MQTT topic, saves the email, and if relaying is enabled, it does the last check and the email is sent to the process_message method, which takes care of sending the email to the recipient. Args: mail_fields (dict): Email parsed into a dictionary. key (str): Name of the file picked up from queue/new. mail_request (MailRequest): Instance of the MailRequest class with eml data. final_rating (int): Final email rating. """ queuePath = utils.settings.data["directory"]["queuepath"] relay_enabled = utils.settings.data["relay"]["relay_enabled"] source = queuePath + "/new/" + key destination = utils.settings.data["directory"]["rawspampath"] + "/" + key if ( len(mail_fields["attachmentFile"]) > 0 and utils.settings.data["relay"]["save_attachment"] ): save_attachment(mail_fields) if ( utils.settings.data["relay"]["mqtt"] and mail_fields["text"] != "this is testing email from salmon" ): useMQTT(source, key) if ( utils.settings.data["relay"]["save_eml"] and mail_fields["text"] != "this is testing email from salmon" ): shutil.copy2(source, destination) if relay_enabled: relaycounter = utils.settings.data["relay"]["globalcounter"] # 12.check - number of relayed emails is limited by time if int(server.QueueReceiver.totalRelay) >= relaycounter: logging.info("[+] (salmonrelay.py) - Limit reached. No relay.") else: if final_rating >= 70: logging.info("[+] (salmonrelay.py) - Relaying!") mail_request = destroy_link(mail_fields, mail_request) mail_request = destroy_attachment(mail_fields, mail_request) mail_request = destroy_reply_to(mail_fields, mail_request) if utils.settings.data["relay"]["save_statistics"]: update_statistics(5) processMessage = server.QueueReceiver(queuePath) processMessage.process_message(mail_request) server.QueueReceiver.totalRelay += 1
def test_queue_receiver(self, router_mock): receiver = server.QueueReceiver('run/queue') run_queue = queue.Queue('run/queue') run_queue.push(str(generate_mail(factory=mail.MailResponse))) assert run_queue.count() > 0 receiver.start(one_shot=True) self.assertEqual(run_queue.count(), 0) self.assertEqual(run_queue.count(), 0) self.assertEqual(router_mock.deliver.call_count, 1) router_mock.deliver.side_effect = RuntimeError("Raised on purpose") receiver.process_message(mail.MailRequest('localhost', 'test@localhost', 'test@localhost', 'Fake body.'))
def test_queue_receiver_sleep(self, pool_mock, sleep_mock): class SleepCalled(Exception): pass def sleepy(*args, **kwargs): if sleep_mock.call_count > 1: raise SleepCalled() sleep_mock.side_effect = sleepy receiver = server.QueueReceiver('run/queue', sleep=10, workers=1) with self.assertRaises(SleepCalled): receiver.start() self.assertEqual(receiver.workers.apply_async.call_count, 0) self.assertEqual(sleep_mock.call_count, 2) self.assertEqual(sleep_mock.call_args_list, [call(receiver.sleep), call(receiver.sleep)])
def test_queue_receiver_pool(self, pool_mock): run_queue = queue.Queue('run/queue') msg = str(generate_mail(factory=mail.MailResponse)) run_queue.push(msg) receiver = server.QueueReceiver('run/queue', sleep=10, workers=1) receiver.start(one_shot=True) self.assertEqual(receiver.workers.apply_async.call_count, 1) self.assertEqual(receiver.workers.apply_async.call_args[0], (receiver.process_message,)) args = receiver.workers.apply_async.call_args[1]["args"] del receiver.workers.apply_async.call_args[1]["args"] # onlly the "args" kwarg should be present self.assertEqual(receiver.workers.apply_async.call_args[1], {}) # we can't compare two Mail* objects, so we'll just check the type self.assertEqual(len(args), 1) self.assertEqual(type(args[0]), mail.MailRequest)
def test_QueueReceiver_process_message(queue_mock): receiver = server.QueueReceiver("run/queue/thingy") msg = test_mail_request() with patch("salmon.server.routing.Router") as router_mock, \ patch("salmon.server.undeliverable_message"): response = receiver.process_message(msg) assert response is None, response with patch("salmon.server.routing.Router") as router_mock, \ patch("salmon.server.undeliverable_message"): router_mock.deliver.side_effect = Exception() response = receiver.process_message(msg) assert response is None, response with patch("salmon.server.routing.Router") as router_mock, \ patch("salmon.server.undeliverable_message"): router_mock.deliver.side_effect = server.SMTPError(450, "Not found") response = receiver.process_message(msg) # queue doesn't actually support SMTPErrors assert response is None, response