def setUp(self): super(EmailSendingErrorHandling, self).setUp() self.writeitinstance1 = WriteItInstance.objects.get(id=1) self.Marcel = Person.objects.get(id=2) felipe = Person.objects.get(id=3) self.channel = MentalMessage() self.user = User.objects.get(id=1) self.mental_contact1 = Contact.objects.create( person=felipe, contact_type=self.channel.get_contact_type(), writeitinstance=self.writeitinstance1) self.message = Message.objects.create( content='hello there', author_name='Felipe', author_email="*****@*****.**", subject='Wow!', writeitinstance=self.writeitinstance1, persons=[felipe], ) self.message2 = Message.objects.create( content='hello there', author_name='Marcel', author_email="*****@*****.**", subject='Wow!', writeitinstance=self.writeitinstance1, persons=[self.Marcel], )
def setUp(self): super(OutboundMessageRecordTestCase,self).setUp() self.channel = MentalMessage() self.person1 = Person.objects.all()[0] self.user = User.objects.all()[0] self.mental_contact1 = Contact.objects.create( person=self.person1, contact_type=self.channel.get_contact_type(), owner=self.user) self.writeitinstance1 = WriteItInstance.objects.all()[0] self.fatal_error_message = Message.objects.create(content = 'Content 1', subject='RaiseFatalErrorPlz', writeitinstance= self.writeitinstance1, persons = [self.person1]) self.fatal_error_outboundmessage = OutboundMessage.objects.get(message=self.fatal_error_message, contact=self.mental_contact1) self.try_again_error_message = Message.objects.create(content = 'Content 1', subject='RaiseTryAgainErrorPlz', writeitinstance= self.writeitinstance1, persons = [self.person1]) self.try_again_outbound_message = OutboundMessage.objects.get(message=self.try_again_error_message, contact=self.mental_contact1) self.successful_message = Message.objects.create(content = 'Content 1', subject='Come on! send me!', writeitinstance= self.writeitinstance1, persons = [self.person1]) self.successful_outbound_message = OutboundMessage.objects.get(message=self.successful_message, contact=self.mental_contact1) self.channel = MentalMessage() self.plugin_model = self.channel.get_model()
def test_plugin_gets_contact_type(self): """From a plugin I can get its contact type""" the_mental_channel = MentalMessage() contact_type = the_mental_channel.get_contact_type() self.assertEquals(contact_type.label_name, "The Mind") self.assertEquals(contact_type.name, "mind")
def setUp(self): super(PluginMentalMessageTestCase, self).setUp() self.outbound_message = OutboundMessage.objects.get(id=1) self.message = Message.objects.get(id=1) self.message_type = ContentType.objects.get(id=1) self.writeitinstance1 = WriteItInstance.objects.get(id=1) self.person1 = Person.objects.get(id=1) self.channel = MentalMessage() self.user = User.objects.get(id=1) self.mental_contact1 = Contact.objects.create( person=self.person1, contact_type=self.channel.get_contact_type(), writeitinstance=self.writeitinstance1)
class EmailSendingErrorHandling(TestCase): def setUp(self): super(EmailSendingErrorHandling,self).setUp() self.writeitinstance1 = WriteItInstance.objects.all()[0] self.Marcel = Person.objects.all()[1] felipe = Person.objects.all()[2] self.channel = MentalMessage() self.user = User.objects.all()[0] self.mental_contact1 = Contact.objects.create(person=felipe, contact_type=self.channel.get_contact_type(), owner=self.user) self.message = Message.objects.create(content = 'hello there', author_name='Felipe', author_email="*****@*****.**", subject='Wow!', writeitinstance= self.writeitinstance1, persons = [felipe]) self.message2 = Message.objects.create(content = 'hello there', author_name='Marcel', author_email="*****@*****.**", subject='Wow!', writeitinstance= self.writeitinstance1, persons = [self.Marcel]) def test_confirmation_sending_error_destroys_message(self): with patch("django.core.mail.EmailMultiAlternatives.send") as send: send.side_effect = Exception("The message was not sent") confirmation = Confirmation.objects.create(message=self.message2) self.assertEquals(len(mail.outbox), 0) #ok I'm taking the desition that a message with a confirmation error #will be deleted and also the confirmation #this is obviously subjected to change #but I see no point on keeping the message or the confirmation messages = Message.objects.filter(id=self.message2.id) confirmations = Confirmation.objects.filter(id=confirmation.id) #I'm Changing this cause it makes no sense right now self.assertEquals(messages.count(), 1) self.assertEquals(confirmations.count(), 1)
def setUp(self): super(EmailSendingErrorHandling, self).setUp() self.writeitinstance1 = WriteItInstance.objects.get(id=1) self.Marcel = Person.objects.get(id=2) felipe = Person.objects.get(id=3) self.channel = MentalMessage() self.user = User.objects.get(id=1) self.mental_contact1 = Contact.objects.create(person=felipe, contact_type=self.channel.get_contact_type(), writeitinstance=self.writeitinstance1) self.message = Message.objects.create( content='hello there', author_name='Felipe', author_email="*****@*****.**", subject='Wow!', writeitinstance=self.writeitinstance1, persons=[felipe], ) self.message2 = Message.objects.create( content='hello there', author_name='Marcel', author_email="*****@*****.**", subject='Wow!', writeitinstance=self.writeitinstance1, persons=[self.Marcel], )
def setUp(self): super(OutboundMessageRecordTestCase, self).setUp() self.channel = MentalMessage() self.person1 = Person.objects.get(id=1) self.user = User.objects.get(id=1) self.writeitinstance1 = WriteItInstance.objects.get(id=1) self.mental_contact1 = Contact.objects.create( person=self.person1, contact_type=self.channel.get_contact_type(), writeitinstance=self.writeitinstance1 ) self.fatal_error_message = Message.objects.create( content='Content 1', subject='RaiseFatalErrorPlz', writeitinstance=self.writeitinstance1, persons=[self.person1], ) self.fatal_error_outboundmessage = OutboundMessage.objects.get(message=self.fatal_error_message, contact=self.mental_contact1) self.try_again_error_message = Message.objects.create( content='Content 1', subject='RaiseTryAgainErrorPlz', writeitinstance=self.writeitinstance1, persons=[self.person1], ) self.try_again_outbound_message = OutboundMessage.objects.get(message=self.try_again_error_message, contact=self.mental_contact1) self.successful_message = Message.objects.create( content='Content 1', subject='Come on! send me!', writeitinstance=self.writeitinstance1, persons=[self.person1], ) self.successful_outbound_message = OutboundMessage.objects.get(message=self.successful_message, contact=self.mental_contact1) self.channel = MentalMessage() self.plugin_model = self.channel.get_model()
def setUp(self): super(PluginMentalMessageTestCase,self).setUp() self.outbound_message = OutboundMessage.objects.all()[0] self.message = Message.objects.all()[0] self.message_type = ContentType.objects.all()[0] self.writeitinstance1 = WriteItInstance.objects.all()[0] self.person1 = Person.objects.all()[0] self.channel = MentalMessage() self.user = User.objects.all()[0] self.mental_contact1 = Contact.objects.create( person=self.person1, contact_type=self.channel.get_contact_type(), owner= self.user)
def setUp(self): super(ConfirmationTestCase,self).setUp() self.writeitinstance1 = WriteItInstance.objects.all()[0] self.Marcel = Person.objects.all()[1] felipe = Person.objects.all()[2] self.channel = MentalMessage() self.user = User.objects.all()[0] self.mental_contact1 = Contact.objects.create(person=felipe, contact_type=self.channel.get_contact_type(), owner=self.user) self.message = Message.objects.create(content = 'hello there', author_name='Felipe', author_email="*****@*****.**", subject='Wow!', writeitinstance= self.writeitinstance1, persons = [felipe]) self.message2 = Message.objects.create(content = 'hello there', author_name='Felipe', author_email="*****@*****.**", subject='Wow!', writeitinstance= self.writeitinstance1, persons = [self.Marcel])
class EmailSendingErrorHandling(TestCase): def setUp(self): super(EmailSendingErrorHandling, self).setUp() self.writeitinstance1 = WriteItInstance.objects.get(id=1) self.Marcel = Person.objects.get(id=2) felipe = Person.objects.get(id=3) self.channel = MentalMessage() self.user = User.objects.get(id=1) self.mental_contact1 = Contact.objects.create( person=felipe, contact_type=self.channel.get_contact_type(), writeitinstance=self.writeitinstance1) self.message = Message.objects.create( content='hello there', author_name='Felipe', author_email="*****@*****.**", subject='Wow!', writeitinstance=self.writeitinstance1, persons=[felipe], ) self.message2 = Message.objects.create( content='hello there', author_name='Marcel', author_email="*****@*****.**", subject='Wow!', writeitinstance=self.writeitinstance1, persons=[self.Marcel], ) def test_confirmation_sending_error_destroys_message(self): with patch("django.core.mail.EmailMultiAlternatives.send") as send: send.side_effect = Exception("The message was not sent") confirmation = Confirmation.objects.create(message=self.message2) self.assertEquals(len(mail.outbox), 0) # ok I'm taking the desition that a message with a confirmation error # will be deleted and also the confirmation # this is obviously subjected to change # but I see no point on keeping the message or the confirmation messages = Message.objects.filter(id=self.message2.id) confirmations = Confirmation.objects.filter(id=confirmation.id) # I'm Changing this cause it makes no sense right now self.assertEquals(messages.count(), 1) self.assertEquals(confirmations.count(), 1)
class EmailSendingErrorHandling(TestCase): def setUp(self): super(EmailSendingErrorHandling, self).setUp() self.writeitinstance1 = WriteItInstance.objects.get(id=1) self.Marcel = Person.objects.get(id=2) felipe = Person.objects.get(id=3) self.channel = MentalMessage() self.user = User.objects.get(id=1) self.mental_contact1 = Contact.objects.create(person=felipe, contact_type=self.channel.get_contact_type(), writeitinstance=self.writeitinstance1) self.message = Message.objects.create( content='hello there', author_name='Felipe', author_email="*****@*****.**", subject='Wow!', writeitinstance=self.writeitinstance1, persons=[felipe], ) self.message2 = Message.objects.create( content='hello there', author_name='Marcel', author_email="*****@*****.**", subject='Wow!', writeitinstance=self.writeitinstance1, persons=[self.Marcel], ) def test_confirmation_sending_error_does_not_destroy_message(self): with patch("django.core.mail.EmailMultiAlternatives.send") as send: send.side_effect = Exception("The message was not sent") with self.assertRaisesRegexp(Exception, r'The message was not sent'): Confirmation.objects.create(message=self.message2) self.assertEquals(len(mail.outbox), 0) messages = Message.objects.filter(id=self.message2.id) self.assertEquals(messages.count(), 1) self.assertTrue(messages[0].confirmation)
class PluginMentalMessageTestCase(TestCase): ''' This testcase is going to be used as an example for the creation of new plugins MentalMessage is the plugin for sending messages in a telepathic way ''' def setUp(self): super(PluginMentalMessageTestCase, self).setUp() self.outbound_message = OutboundMessage.objects.get(id=1) self.message = Message.objects.get(id=1) self.message_type = ContentType.objects.get(id=1) self.writeitinstance1 = WriteItInstance.objects.get(id=1) self.person1 = Person.objects.get(id=1) self.channel = MentalMessage() self.user = User.objects.get(id=1) self.mental_contact1 = Contact.objects.create( person=self.person1, contact_type=self.channel.get_contact_type(), writeitinstance=self.writeitinstance1) def test_it_only_sends_messages_to_contacts_of_the_same_channel(self): otubound_message = OutboundMessage.objects.create( contact=self.mental_contact1, message=self.message, site=Site.objects.get_current()) otubound_message.send() record = OutboundMessagePluginRecord.objects.get( outbound_message=otubound_message) self.assertEquals(record.plugin, self.channel.get_model()) def test_it_has_a_send_method_and_does_whatever(self): # it sends the message self.channel.send(self.outbound_message) # And I'm gonna prove it by testing that a new record was created the_records = MessageRecord.objects.filter( object_id=self.outbound_message.id, status="sent using mental messages") self.assertEquals(the_records.count(), 1) # It should send using all the channels available def test_it_should_create_a_new_kind_of_outbox_message(self): otubound_message = OutboundMessage.objects.create( contact=self.mental_contact1, message=self.message, site=Site.objects.get_current()) otubound_message.send() the_records = MessageRecord.objects.filter( object_id=otubound_message.id, status="sent using mental messages", ) self.assertEquals(the_records.count(), 1) def test_fatal_exception_when_sending_a_mental_message(self): ''' This type of error is when there is not much to do, like an inexisting email address and in Mental message it raises a fatal error when you send the message RaiseFatalErrorPlz ''' with self.assertRaises(FatalException): self.channel.send_mental_message("RaiseFatalErrorPlz") def test_non_fatal_exception(self): with self.assertRaises(TryAgainException): self.channel.send_mental_message("RaiseTryAgainErrorPlz") def test_it_raises_an_error_when_sending_error_in_the_subject(self): # this is a test for when you send a message with RaiseFatalErrorPlz in subject then is going # to raise an exception error_message = Message.objects.create( content='Content 1', subject='RaiseFatalErrorPlz', writeitinstance=self.writeitinstance1, persons=[self.person1], ) outbound_message = OutboundMessage.objects.filter( message=error_message)[0] result = self.channel.send(outbound_message) successfully_sent = result[0] fatal_error = result[1] self.assertFalse(successfully_sent) self.assertTrue(fatal_error) def test_non_fatal_error_keeps_outbound_message_status_as_ready(self): ''' This type of error is a soft error, like someone having full inbox and we should retry sending the message ''' error_message = Message.objects.create( content='Content 1', subject='RaiseTryAgainErrorPlz', writeitinstance=self.writeitinstance1, persons=[self.person1], ) outbound_message = OutboundMessage.objects.filter( message=error_message)[0] result = self.channel.send(outbound_message) successfully_sent = result[0] fatal_error = result[1] self.assertFalse(successfully_sent) self.assertFalse(fatal_error) def test_success_sending_a_message(self): error_message = Message.objects.create( content='Content 1', subject='Come on! send me!', writeitinstance=self.writeitinstance1, persons=[self.person1], ) outbound_message = OutboundMessage.objects.filter( message=error_message)[0] result = self.channel.send(outbound_message) successfully_sent = result[0] fatal_error = result[1] self.assertTrue(successfully_sent) self.assertTrue(fatal_error is None) def test_plugin_gets_contact_type(self): """From a plugin I can get its contact type""" the_mental_channel = MentalMessage() contact_type = the_mental_channel.get_contact_type() self.assertEquals(contact_type.label_name, "The Mind") self.assertEquals(contact_type.name, "mind")
class ConfirmationTestCase(TestCase): def setUp(self): super(ConfirmationTestCase, self).setUp() self.writeitinstance1 = WriteItInstance.objects.get(id=1) self.Marcel = Person.objects.get(id=2) felipe = Person.objects.get(id=3) self.channel = MentalMessage() self.user = User.objects.get(id=1) self.mental_contact1 = Contact.objects.create( person=felipe, contact_type=self.channel.get_contact_type(), writeitinstance=self.writeitinstance1) self.message = Message.objects.create( content='hello there', author_name='Felipe', author_email="*****@*****.**", subject='Wow!', writeitinstance=self.writeitinstance1, persons=[felipe], ) self.message2 = Message.objects.create( content='hello there', author_name='Felipe', author_email="*****@*****.**", subject='Wow!', writeitinstance=self.writeitinstance1, persons=[self.Marcel], ) def test_instanciate(self): confirmation = Confirmation(message=self.message) self.assertTrue(confirmation) self.assertEquals(len(confirmation.key.strip()), 0) def test_creation_and_save(self): confirmation = Confirmation.objects.create(message=self.message) self.assertTrue(confirmation.id) self.assertEquals(confirmation.message, self.message) self.assertEquals(len(confirmation.key.strip()), 32) self.assertTrue(isinstance(confirmation.created, datetime)) self.assertTrue(confirmation.confirmated_at is None) def test_confirmation_has_a_key_generator(self): key1 = Confirmation.key_generator() key2 = Confirmation.key_generator() self.assertNotEquals(key1, key2) def test_duplication(self): # Serioulsly I'm getting to many times Duplicate entry for key 'key' confirmation1 = Confirmation.objects.create(message=self.message) confirmation2 = Confirmation.objects.create(message=self.message2) self.assertNotEquals(confirmation1.key, confirmation2.key) def test_it_sends_an_email_to_the_author_asking_for_confirmation(self): confirmation = Confirmation.objects.create(message=self.message) url = reverse( 'confirm', subdomain=self.message.writeitinstance.slug, kwargs={'slug': confirmation.key}, ) confirmation_full_url = url self.assertEquals(len(mail.outbox), 1) # it is sent to one person pointed in the contact self.assertEquals(mail.outbox[0].subject, u'Please confirm your message to Felipe') self.assertTrue(self.message.author_name in mail.outbox[0].body) self.assertIn(confirmation_full_url, mail.outbox[0].body) self.assertTrue(url in mail.outbox[0].body) self.assertEquals(len(mail.outbox[0].to), 1) self.assertTrue(self.message.author_email in mail.outbox[0].to) expected_from_email = self.message.writeitinstance.slug + "@" + settings.DEFAULT_FROM_DOMAIN self.assertEquals(mail.outbox[0].from_email, expected_from_email) def test_sends_confirmation_from_a_custom_domain_if_specified(self): '''Sending confirmation from a custom domain if specified''' config = self.message.writeitinstance.config config.custom_from_domain = "custom.domain.cl" config.email_host = 'cuttlefish.au.org' config.email_host_password = '******' config.email_host_user = '******' config.email_port = 25 config.email_use_tls = True config.save() Confirmation.objects.create(message=self.message) self.assertEquals(len(mail.outbox), 1) expected_from_email = self.message.writeitinstance.slug + "@" + config.custom_from_domain confirmation_mail = mail.outbox[0] self.assertEquals(confirmation_mail.from_email, expected_from_email) connection = confirmation_mail.connection self.assertEquals(connection.host, config.email_host) self.assertEquals(connection.password, config.email_host_password) self.assertEquals(connection.username, config.email_host_user) self.assertEquals(connection.port, config.email_port) self.assertEquals(connection.use_tls, config.email_use_tls) ''' I'm moving all the site to use cuttlefish but in the meantime in order to test I'm using this specific config per instance EMAIL_HOST = 'cuttlefish.oaf.org.au' EMAIL_PORT = 2525 EMAIL_HOST_USER = '******' EMAIL_HOST_PASSWORD = '******' EMAIL_USE_TLS = True ''' @override_settings(SEND_ALL_EMAILS_FROM_DEFAULT_FROM_EMAIL=True) def test_send_confirmation_from_a_single_email_address(self): ''' In some cases it is needed that the email is sent from a single email, that email should be the default_from_email ''' Confirmation.objects.create(message=self.message) expected_from_email = settings.DEFAULT_FROM_EMAIL self.assertEquals(mail.outbox[0].from_email, expected_from_email) def test_confirmation_get_absolute_url(self): confirmation = Confirmation.objects.create(message=self.message) expected_url = reverse( 'confirm', subdomain=self.message.writeitinstance.slug, kwargs={'slug': confirmation.key}, ) self.assertEquals(expected_url, confirmation.get_absolute_url()) def test_access_the_confirmation_url(self): confirmation = Confirmation.objects.create(message=self.message) url = reverse( 'confirm', subdomain=self.message.writeitinstance.slug, kwargs={'slug': confirmation.key}, ) message_url = reverse('thread_read', subdomain=self.message.writeitinstance.slug, kwargs={'slug': self.message.slug}) response = self.client.get(url) self.assertRedirects(response, message_url) confirmation = Confirmation.objects.get(id=confirmation.id) self.assertTrue(confirmation.confirmated_at is not None) outbound_messages = OutboundMessage.objects.filter( message=confirmation.message) self.assertEquals(outbound_messages[0].status, "ready") def test_confirmed_property(self): confirmation = Confirmation.objects.create(message=self.message) self.assertFalse(confirmation.is_confirmed) confirmation.confirmated_at = datetime.now() confirmation.save() self.assertTrue(confirmation.is_confirmed) def test_it_does_not_confirm_twice(self): confirmation = Confirmation.objects.create(message=self.message) url = reverse( 'confirm', subdomain=self.message.writeitinstance.slug, kwargs={'slug': confirmation.key}, ) response1 = self.client.get(url) message_thread = reverse('thread_read', subdomain=self.message.writeitinstance.slug, kwargs={'slug': self.message.slug}) response2 = self.client.get(url) self.assertEquals(response1.status_code, 302) self.assertRedirects(response2, message_thread) def test_i_cannot_access_a_non_confirmed_message(self): Confirmation.objects.create(message=self.message) url = reverse('thread_read', subdomain=self.message.writeitinstance.slug, kwargs={'slug': self.message.slug}) response = self.client.get(url) self.assertEquals(response.status_code, 404)
class PluginMentalMessageTestCase(TestCase): ''' This testcase is going to be used as an example for the creation of new plugins MentalMessage is the plugin for sending messages in a telepathic way ''' def setUp(self): super(PluginMentalMessageTestCase,self).setUp() self.outbound_message = OutboundMessage.objects.all()[0] self.message = Message.objects.all()[0] self.message_type = ContentType.objects.all()[0] self.writeitinstance1 = WriteItInstance.objects.all()[0] self.person1 = Person.objects.all()[0] self.channel = MentalMessage() self.user = User.objects.all()[0] self.mental_contact1 = Contact.objects.create( person=self.person1, contact_type=self.channel.get_contact_type(), owner= self.user) def test_it_only_sends_messages_to_contacts_of_the_same_channel(self): otubound_message = OutboundMessage.objects.create(contact=self.mental_contact1, message=self.message) otubound_message.send() record = OutboundMessagePluginRecord.objects.get(outbound_message=otubound_message) self.assertEquals(record.plugin, self.channel.get_model()) def test_it_has_a_send_method_and_does_whatever(self): #it sends the message self.channel.send(self.outbound_message) #And I'm gonna prove it by testing that a new record was created the_records = MessageRecord.objects.filter(object_id=self.outbound_message.id, status="sent using mental messages") self.assertEquals(the_records.count(),1) #It should send using all the channels available def test_it_should_create_a_new_kind_of_outbox_message(self): otubound_message = OutboundMessage.objects.create(contact=self.mental_contact1, message=self.message) otubound_message.send() the_records = MessageRecord.objects.filter(object_id=otubound_message.id , status="sent using mental messages") self.assertEquals(the_records.count(),1) def test_fatal_exception_when_sending_a_mental_message(self): ''' This type of error is when there is not much to do, like an inexisting email address and in Mental message it raises a fatal error when you send the message RaiseFatalErrorPlz ''' with self.assertRaises(FatalException) as cm: self.channel.send_mental_message("RaiseFatalErrorPlz") def test_non_fatal_exception(self): with self.assertRaises(TryAgainException) as cm: self.channel.send_mental_message("RaiseTryAgainErrorPlz") def test_it_raises_an_error_when_sending_error_in_the_subject(self): #this is a test for when you send a message with RaiseFatalErrorPlz in subject then is going #to raise an exception error_message = Message.objects.create(content = 'Content 1', subject='RaiseFatalErrorPlz', writeitinstance= self.writeitinstance1, persons = [self.person1]) outbound_message = OutboundMessage.objects.filter(message=error_message)[0] result = self.channel.send(outbound_message) successfully_sent = result[0] fatal_error = result[1] self.assertFalse(successfully_sent) self.assertTrue(fatal_error) def test_non_fatal_error_keeps_outbound_message_status_as_ready(self): ''' This type of error is a soft error, like someone having full inbox and we should retry sending the message ''' error_message = Message.objects.create(content = 'Content 1', subject='RaiseTryAgainErrorPlz', writeitinstance= self.writeitinstance1, persons = [self.person1]) outbound_message = OutboundMessage.objects.filter(message=error_message)[0] result = self.channel.send(outbound_message) successfully_sent = result[0] fatal_error = result[1] self.assertFalse(successfully_sent) self.assertFalse(fatal_error) def test_success_sending_a_message(self): ''' ''' error_message = Message.objects.create(content = 'Content 1', subject='Come on! send me!', writeitinstance= self.writeitinstance1, persons = [self.person1]) outbound_message = OutboundMessage.objects.filter(message=error_message)[0] result = self.channel.send(outbound_message) successfully_sent = result[0] fatal_error = result[1] self.assertTrue(successfully_sent) self.assertTrue(fatal_error is None) def test_plugin_gets_contact_type(self): """From a plugin I can get its contact type""" the_mental_channel = MentalMessage() contact_type = the_mental_channel.get_contact_type() self.assertEquals(contact_type.label_name, "The Mind") self.assertEquals(contact_type.name, "mind")
class ConfirmationTestCase(TestCase): def setUp(self): super(ConfirmationTestCase, self).setUp() self.writeitinstance1 = WriteItInstance.objects.get(id=1) self.Marcel = Person.objects.get(id=2) felipe = Person.objects.get(id=3) self.channel = MentalMessage() self.user = User.objects.get(id=1) self.mental_contact1 = Contact.objects.create( person=felipe, contact_type=self.channel.get_contact_type(), writeitinstance=self.writeitinstance1) self.message = Message.objects.create( content='hello there', author_name='Felipe', author_email="*****@*****.**", subject='Wow!', writeitinstance=self.writeitinstance1, persons=[felipe], ) self.message2 = Message.objects.create( content='hello there', author_name='Felipe', author_email="*****@*****.**", subject='Wow!', writeitinstance=self.writeitinstance1, persons=[self.Marcel], ) def test_instanciate(self): confirmation = Confirmation(message=self.message) self.assertTrue(confirmation) self.assertEquals(len(confirmation.key.strip()), 0) def test_creation_and_save(self): confirmation = Confirmation.objects.create(message=self.message) self.assertTrue(confirmation.id) self.assertEquals(confirmation.message, self.message) self.assertEquals(len(confirmation.key.strip()), 32) self.assertTrue(isinstance(confirmation.created, datetime)) self.assertTrue(confirmation.confirmated_at is None) def test_confirmation_has_a_key_generator(self): key1 = Confirmation.key_generator() key2 = Confirmation.key_generator() self.assertNotEquals(key1, key2) def test_duplication(self): # Serioulsly I'm getting to many times Duplicate entry for key 'key' confirmation1 = Confirmation.objects.create(message=self.message) confirmation2 = Confirmation.objects.create(message=self.message2) self.assertNotEquals(confirmation1.key, confirmation2.key) def test_it_sends_an_email_to_the_author_asking_for_confirmation(self): confirmation = Confirmation.objects.create(message=self.message) url = reverse( 'confirm', subdomain=self.message.writeitinstance.slug, kwargs={'slug': confirmation.key}, ) confirmation_full_url = url self.assertEquals(len(mail.outbox), 1) # it is sent to one person pointed in the contact self.assertEquals(mail.outbox[0].subject, u'Please confirm your message to Felipe') self.assertTrue(self.message.author_name in mail.outbox[0].body) self.assertIn(confirmation_full_url, mail.outbox[0].body) self.assertTrue(url in mail.outbox[0].body) self.assertEquals(len(mail.outbox[0].to), 1) self.assertTrue(self.message.author_email in mail.outbox[0].to) expected_from_email = self.message.writeitinstance.slug + "@" + settings.DEFAULT_FROM_DOMAIN self.assertEquals(mail.outbox[0].from_email, expected_from_email) def test_it_sends_an_email_to_the_author_asking_for_confirmation_with_real_name(self): config = self.message.writeitinstance.config config.real_name_for_site_emails = u'☃ The Snowman ☃' config.save() Confirmation.objects.create(message=self.message) self.assertEquals(len(mail.outbox), 1) # it is sent to one person pointed in the contact email_to_check = mail.outbox[0] expected_email_address = self.message.writeitinstance.slug + "@" + settings.DEFAULT_FROM_DOMAIN expected_real_name = u'☃ The Snowman ☃' expected_from = u'{real_name} <{email}>'.format( real_name=expected_real_name, email=expected_email_address) self.assertEquals(email_to_check.from_email, expected_from) self.assertIn( 'From: =?utf-8?b?4piDIFRoZSBTbm93bWFuIOKYgw==?=', str(email_to_check.message())) def test_sends_confirmation_from_a_custom_domain_if_specified(self): '''Sending confirmation from a custom domain if specified''' config = self.message.writeitinstance.config config.custom_from_domain = "custom.domain.cl" config.email_host = 'cuttlefish.au.org' config.email_host_password = '******' config.email_host_user = '******' config.email_port = 25 config.email_use_tls = True config.save() Confirmation.objects.create(message=self.message) self.assertEquals(len(mail.outbox), 1) expected_from_email = self.message.writeitinstance.slug + "@" + config.custom_from_domain confirmation_mail = mail.outbox[0] self.assertEquals(confirmation_mail.from_email, expected_from_email) connection = confirmation_mail.connection self.assertEquals(connection.host, config.email_host) self.assertEquals(connection.password, config.email_host_password) self.assertEquals(connection.username, config.email_host_user) self.assertEquals(connection.port, config.email_port) self.assertEquals(connection.use_tls, config.email_use_tls) ''' I'm moving all the site to use cuttlefish but in the meantime in order to test I'm using this specific config per instance EMAIL_HOST = 'cuttlefish.oaf.org.au' EMAIL_PORT = 2525 EMAIL_HOST_USER = '******' EMAIL_HOST_PASSWORD = '******' EMAIL_USE_TLS = True ''' @override_settings(SEND_ALL_EMAILS_FROM_DEFAULT_FROM_EMAIL=True) def test_send_confirmation_from_a_single_email_address(self): ''' In some cases it is needed that the email is sent from a single email, that email should be the default_from_email ''' Confirmation.objects.create(message=self.message) expected_from_email = settings.DEFAULT_FROM_EMAIL self.assertEquals(mail.outbox[0].from_email, expected_from_email) def test_confirmation_get_absolute_url(self): confirmation = Confirmation.objects.create(message=self.message) expected_url = reverse( 'confirm', subdomain=self.message.writeitinstance.slug, kwargs={'slug': confirmation.key}, ) self.assertEquals(expected_url, confirmation.get_absolute_url()) def test_access_the_confirmation_url(self): confirmation = Confirmation.objects.create(message=self.message) url = reverse( 'confirm', subdomain=self.message.writeitinstance.slug, kwargs={'slug': confirmation.key}, ) message_url = reverse('thread_read', subdomain=self.message.writeitinstance.slug, kwargs={'slug': self.message.slug} ) response = self.client.get(url) self.assertRedirects(response, message_url) confirmation = Confirmation.objects.get(id=confirmation.id) self.assertTrue(confirmation.confirmated_at is not None) outbound_messages = OutboundMessage.objects.filter(message=confirmation.message) self.assertEquals(outbound_messages[0].status, "ready") def test_confirmed_property(self): confirmation = Confirmation.objects.create(message=self.message) self.assertFalse(confirmation.is_confirmed) confirmation.confirmated_at = datetime.now() confirmation.save() self.assertTrue(confirmation.is_confirmed) def test_it_does_not_confirm_twice(self): confirmation = Confirmation.objects.create(message=self.message) url = reverse( 'confirm', subdomain=self.message.writeitinstance.slug, kwargs={'slug': confirmation.key}, ) response1 = self.client.get(url) message_thread = reverse( 'thread_read', subdomain=self.message.writeitinstance.slug, kwargs={ 'slug': self.message.slug }) response2 = self.client.get(url) self.assertEquals(response1.status_code, 302) self.assertRedirects(response2, message_thread) def test_i_cannot_access_a_non_confirmed_message(self): Confirmation.objects.create(message=self.message) url = reverse('thread_read', subdomain=self.message.writeitinstance.slug, kwargs={'slug': self.message.slug}) response = self.client.get(url) self.assertEquals(response.status_code, 404)
class OutboundMessageRecordTestCase(TestCase): def setUp(self): super(OutboundMessageRecordTestCase, self).setUp() self.channel = MentalMessage() self.person1 = Person.objects.get(id=1) self.user = User.objects.get(id=1) self.writeitinstance1 = WriteItInstance.objects.get(id=1) self.mental_contact1 = Contact.objects.create( person=self.person1, contact_type=self.channel.get_contact_type(), writeitinstance=self.writeitinstance1 ) self.fatal_error_message = Message.objects.create( content='Content 1', subject='RaiseFatalErrorPlz', writeitinstance=self.writeitinstance1, persons=[self.person1], ) self.fatal_error_outboundmessage = OutboundMessage.objects.get(message=self.fatal_error_message, contact=self.mental_contact1) self.try_again_error_message = Message.objects.create( content='Content 1', subject='RaiseTryAgainErrorPlz', writeitinstance=self.writeitinstance1, persons=[self.person1], ) self.try_again_outbound_message = OutboundMessage.objects.get(message=self.try_again_error_message, contact=self.mental_contact1) self.successful_message = Message.objects.create( content='Content 1', subject='Come on! send me!', writeitinstance=self.writeitinstance1, persons=[self.person1], ) self.successful_outbound_message = OutboundMessage.objects.get(message=self.successful_message, contact=self.mental_contact1) self.channel = MentalMessage() self.plugin_model = self.channel.get_model() def test_create_new(self): record = OutboundMessagePluginRecord.objects.create( outbound_message=self.successful_outbound_message, plugin=self.plugin_model, sent=True) self.assertTrue(record) self.assertEquals(record.outbound_message, self.successful_outbound_message) self.assertEquals(record.plugin, self.plugin_model) self.assertTrue(record.sent) self.assertEquals(record.number_of_attempts, 0) self.assertTrue(record.try_again) # I know this is not a scenario or something like that # but this is the first name that I came up with for this test def test_successfully_sending_an_outbound_message(self): self.successful_outbound_message.send() record = OutboundMessagePluginRecord.objects.get( outbound_message=self.successful_outbound_message, plugin=self.plugin_model) self.assertTrue(record.sent) self.assertEquals(record.number_of_attempts, 1) self.assertFalse(record.try_again) def test_fatal_error_in_sending(self): self.fatal_error_outboundmessage.send() record = OutboundMessagePluginRecord.objects.get( outbound_message=self.fatal_error_outboundmessage, plugin=self.plugin_model) self.assertFalse(record.sent) self.assertEquals(record.number_of_attempts, 1) self.assertFalse(record.try_again) def test_non_fatal_error_in_sending(self): self.try_again_outbound_message.send() record = OutboundMessagePluginRecord.objects.get(outbound_message=self.try_again_outbound_message, plugin=self.plugin_model) self.assertFalse(record.sent) self.assertEquals(record.number_of_attempts, 1) self.assertTrue(record.try_again) def test_it_does_not_create_a_new_record_when_sending_again(self): self.try_again_outbound_message.send() record = OutboundMessagePluginRecord.objects.get(outbound_message=self.try_again_outbound_message, plugin=self.plugin_model) self.try_again_outbound_message.send() record = OutboundMessagePluginRecord.objects.get(outbound_message=self.try_again_outbound_message, plugin=self.plugin_model) self.assertFalse(record.sent) self.assertEquals(record.number_of_attempts, 2) self.assertTrue(record.try_again) def test_it_should_not_send_again_when_it_says_do_not_send_me(self): self.fatal_error_outboundmessage.send() self.fatal_error_outboundmessage.send() record = OutboundMessagePluginRecord.objects.get(outbound_message=self.fatal_error_outboundmessage, plugin=self.plugin_model) self.assertEquals(record.number_of_attempts, 1)
class ConfirmationTestCase(TestCase): def setUp(self): super(ConfirmationTestCase,self).setUp() self.writeitinstance1 = WriteItInstance.objects.all()[0] self.Marcel = Person.objects.all()[1] felipe = Person.objects.all()[2] self.channel = MentalMessage() self.user = User.objects.all()[0] self.mental_contact1 = Contact.objects.create(person=felipe, contact_type=self.channel.get_contact_type(), owner=self.user) self.message = Message.objects.create(content = 'hello there', author_name='Felipe', author_email="*****@*****.**", subject='Wow!', writeitinstance= self.writeitinstance1, persons = [felipe]) self.message2 = Message.objects.create(content = 'hello there', author_name='Felipe', author_email="*****@*****.**", subject='Wow!', writeitinstance= self.writeitinstance1, persons = [self.Marcel]) def test_instanciate(self): confirmation = Confirmation(message=self.message) self.assertTrue(confirmation) self.assertEquals(len(confirmation.key.strip()),0) def test_creation_and_save(self): confirmation = Confirmation.objects.create(message=self.message) self.assertTrue(confirmation.id) self.assertEquals(confirmation.message, self.message) self.assertEquals(len(confirmation.key.strip()),32) self.assertTrue(isinstance(confirmation.created,datetime)) self.assertTrue(confirmation.confirmated_at is None) def test_confirmation_has_a_key_generator(self): key1 = Confirmation.key_generator() key2 = Confirmation.key_generator() self.assertNotEquals(key1, key2) def test_duplication(self): #Serioulsly I'm getting to many times Duplicate entry for key 'key' confirmation1 = Confirmation.objects.create(message=self.message) confirmation2 = Confirmation.objects.create(message=self.message2) self.assertNotEquals(confirmation1.key, confirmation2.key) def test_it_sends_an_email_to_the_author_asking_for_confirmation(self): confirmation = Confirmation.objects.create(message=self.message) url = reverse('confirm', kwargs={ 'slug':confirmation.key }) current_site = Site.objects.get_current() confirmation_full_url = "http://"+current_site.domain+url message_full_url = 'http://'+current_site.domain+self.message.get_absolute_url() self.assertEquals(len(mail.outbox), 1) #it is sent to one person pointed in the contact self.assertEquals(mail.outbox[0].subject, 'Confirmation email for a message in WriteIt') self.assertTrue(self.message.author_name in mail.outbox[0].body) self.assertFalse(confirmation_full_url in mail.outbox[0].body) self.assertTrue(url in mail.outbox[0].body) self.assertTrue(self.message.get_absolute_url() in mail.outbox[0].body) self.assertFalse(message_full_url in mail.outbox[0].body) self.assertEquals(len(mail.outbox[0].to), 1) self.assertTrue(self.message.author_email in mail.outbox[0].to) expected_from_email = self.message.writeitinstance.slug+"@"+settings.DEFAULT_FROM_DOMAIN self.assertEquals(mail.outbox[0].from_email, expected_from_email) @override_settings(SEND_ALL_EMAILS_FROM_DEFAULT_FROM_EMAIL=True) def test_send_confirmation_from_a_single_email_address(self): ''' In some cases it is needed that the email is sent from a single email, that email should be the default_from_email ''' confirmation = Confirmation.objects.create(message=self.message) expected_from_email = settings.DEFAULT_FROM_EMAIL self.assertEquals(mail.outbox[0].from_email, expected_from_email) def test_confirmation_get_absolute_url(self): confirmation = Confirmation.objects.create(message=self.message) expected_url = reverse('confirm', kwargs={ 'slug':confirmation.key }) self.assertEquals(expected_url, confirmation.get_absolute_url()) def test_private_messages_do_not_have_its_absolute_url(self): self.message.public = False self.message.save() confirmation = Confirmation.objects.create(message=self.message) self.assertFalse(self.message.get_absolute_url() in mail.outbox[0].body) def test_access_the_confirmation_url(self): confirmation = Confirmation.objects.create(message=self.message) url = reverse('confirm', kwargs={ 'slug':confirmation.key }) response = self.client.get(url) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'nuntium/confirm.html') confirmation = Confirmation.objects.get(id=confirmation.id) self.assertTrue(confirmation.confirmated_at is not None) outbound_messages = OutboundMessage.objects.filter(message=confirmation.message) self.assertEquals(outbound_messages[0].status, "ready") def test_confirmed_property(self): confirmation = Confirmation.objects.create(message=self.message) self.assertFalse(confirmation.is_confirmed) confirmation.confirmated_at = datetime.now() confirmation.save() self.assertTrue(confirmation.is_confirmed) def test_it_does_not_confirm_twice(self): confirmation = Confirmation.objects.create(message=self.message) url = reverse('confirm', kwargs={ 'slug':confirmation.key }) response1 = self.client.get(url) response2 = self.client.get(url) self.assertEquals(response1.status_code, 200) self.assertEquals(response2.status_code, 404) def test_i_cannot_access_a_non_confirmed_message(self): confirmation = Confirmation.objects.create(message=self.message) url = reverse('message_detail', kwargs={'slug':self.message.slug, 'instance_slug':self.message.writeitinstance.slug}) response = self.client.get(url) self.assertEquals(response.status_code, 404)