def test_save(self):
        """ Test saving the form.

        Saving the form should create a new thread from the title field
        in the form, and a new message on that thread containing the
        body field from the form.
        """
        topic = create_topic()
        data = {
            'topic': '%d' % topic.pk,
            'title': 'Test thread title',
            'body': 'Test message body',
        }
        form = forms.ThreadCreationForm(data)
        user = get_user_model().objects.create_user(
            username='******',
            password='******')

        form.save(user)

        self.assertEqual(1, models.Thread.objects.count())
        self.assertEqual(1, models.Message.objects.count())

        thread = models.Thread.objects.get()
        message = models.Message.objects.get()

        self.assertEqual(data['title'], thread.title)
        self.assertEqual(user, message.user)
        self.assertEqual(thread, message.thread)
        self.assertEqual(data['body'], message.body)
    def test_slug_generation_for_long_title(self):
        """ Test generating a slug when the title is really long.

        If the title is longer than 50 characters, the slug should be
        truncated to 50 chars.
        """
        topic = create_topic(title='a' * 51)

        self.assertEqual('a' * 50, topic.slug)
    def test_slug_generation(self):
        """ Test the automatic generation of a url slug.

        When creating a topic instance, the instance should generate a
        url slug based on its title.
        """
        topic = create_topic(title='test title')

        self.assertEqual('test-title', topic.slug)
    def test_get_absolute_url(self):
        """Test getting a Topic instance's absolute url.

        It should return the url of the thread list view for the topic.
        """
        topic = create_topic()

        expected = reverse('simple-forums:thread-list', kwargs={
            'topic_pk': topic.pk,
            'topic_slug': topic.slug,
        })

        self.assertEqual(expected, topic.get_absolute_url())
    def test_create_with_all_fields(self):
        """ Test creation of a thread with all its fields.

        A thread instance should be able to be created with title text.
        """
        topic = create_topic()
        time = timezone.now() - timedelta(days=1)

        thread = models.Thread.objects.create(
            topic=topic,
            title='test',
            time_created=time)

        self.assertEqual(topic, thread.topic)
        self.assertEqual('test', thread.title)
        self.assertEqual(time, thread.time_created)
    def test_topic(self):
        """ Test the view when there is a topic.

        If there is a topic, it should be listed with its title and
        description.
        """
        topic = create_topic()

        response = self.client.get(self.URL)

        topic_url = thread_list_url(topic=topic)
        link_href = 'href="%s"' % topic_url

        self.assertEqual(200, response.status_code)
        self.assertQuerysetEqual(
            response.context['topic_list'],
            ['<Topic: %s>' % topic])
        self.assertContains(response, topic.title)
        self.assertContains(response, topic.description)
        self.assertContains(response, link_href)
    def test_valid_form(self):
        """ Test authenticated user submitting a valid form.

        If an authenticated user submits a valid form, then a new
        thread should be created.
        """
        self.login()

        topic = create_topic()
        data = {
            'topic': '%d' % topic.pk,
            'title': 'Test Thread Title',
            'body': 'Test thread body',
        }

        response = self.client.post(self.URL, data)

        thread = models.Thread.objects.get()
        message = thread.message_set.get()

        self.assertRedirects(response, thread_detail_url(thread=thread))
        self.assertEqual(data['title'], thread.title)
        self.assertEqual(data['body'], message.body)
 def setUp(self):
     """ Create topic for testing """
     self.topic = create_topic()