Skip to content

Commit

Permalink
allow sending queued messages to keep compatibility with previous yub…
Browse files Browse the repository at this point in the history
…in version
  • Loading branch information
sastred committed Dec 22, 2023
1 parent a661cea commit 1831c2d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
13 changes: 8 additions & 5 deletions django_yubin/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ def send_db_message(message_pk, log_message=None):
logger.exception(msg, extra={'message_pk': message_pk})
return False

if not message.can_be_enqueued():
msg = "Message can not be enqueued in it's current status."
logger.warning(msg)
message.add_log(msg)
return False
# Messages in STATUS_QUEUED can be sent to keep compatibility with previous yubin version.
# In future versions that condition can be removed and only check `can_be_enqueued()`.
if message.status != models.Message.STATUS_QUEUED:
if not message.can_be_enqueued():
msg = "Message can not be enqueued in it's current status."
logger.warning(msg)
message.add_log(msg)
return False

message.mark_as(models.Message.STATUS_QUEUED, log_message)
message.mark_as(models.Message.STATUS_IN_PROCESS, "Trying to send the message.")
Expand Down
12 changes: 11 additions & 1 deletion tests/tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,20 @@ def test_send_email_not_found(self):
self.assertFalse(send_db_message(-1))

def test_send_email_not_queueable(self):
self.message.status = Message.STATUS_SENT
self.message.status = Message.STATUS_IN_PROCESS
self.message.save()
self.assertFalse(send_db_message(self.message.pk))

last_log_action = self.message.log_set.first().action
self.assertEqual(last_log_action, Message.STATUS_IN_PROCESS)

def test_send_email_queued(self):
# Messages in STATUS_QUEUED can be sent to keep compatibility with previous yubin version.
# In future versions that condition can be removed with this test.
self.message.status = Message.STATUS_QUEUED
self.message.save()
self.assertTrue(send_db_message(self.message.pk))

last_log_action = self.message.log_set.first().action
self.assertEqual(last_log_action, Message.STATUS_SENT)

Expand Down
2 changes: 1 addition & 1 deletion tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_mark_as_queued(self):
self.assertEqual(self.message.enqueued_count, enqueued_count+1)

def test_enqueue_wrong_status(self):
self.message.mark_as(Message.STATUS_QUEUED)
self.message.mark_as(Message.STATUS_IN_PROCESS)
self.assertFalse(self.message.enqueue())

def test_enqueue_exception(self):
Expand Down

0 comments on commit 1831c2d

Please sign in to comment.