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")
Example #2
0
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")