summaryrefslogtreecommitdiffstats
path: root/news/tests.py
blob: b7cdd2dd06a38c9be75987364f6ae3e81aa00fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from django.core import mail
from django.test import TestCase, TransactionTestCase
from django.contrib.auth.models import User


from news.models import News


class NewsTest(TestCase):

    def test_feed(self):
        response = self.client.get('/feeds/news/')
        self.assertEqual(response.status_code, 200)

    def test_sitemap(self):
        response = self.client.get('/sitemap-news.xml')
        self.assertEqual(response.status_code, 200)

    def test_news_sitemap(self):
        response = self.client.get('/news-sitemap.xml')
        self.assertEqual(response.status_code, 200)

    def test_newsitem(self):
        response = self.client.get('/news/404', follow=True)
        self.assertEqual(response.status_code, 404)


class NewsCrud(TransactionTestCase):
    def setUp(self):
        password = 'test'
        self.user = User.objects.create_superuser('admin',
                                                  'admin@archlinux.org',
                                                  password)
        self.client.post('/login/', {
                                    'username': self.user.username,
                                    'password': password
        })

    def tearDown(self):
        News.objects.all().delete()
        self.user.delete()

    def create(self, title='Bash broken', content='Broken in [testing]', announce=False):
        data = {
            'title': title,
            'content': content,
        }
        if announce:
            data['send_announce'] = 'on'
        return self.client.post('/news/add/', data, follow=True)

    def testCreateItem(self):
        title = 'Bash broken'
        response = self.create(title)
        self.assertEqual(response.status_code, 200)

        news = News.objects.first()
        self.assertEqual(news.author, self.user)
        self.assertEqual(news.title, title)

    def testView(self):
        self.create()
        news = News.objects.first()

        response = self.client.get(news.get_absolute_url())
        self.assertEqual(response.status_code, 200)

    def testRedirectId(self):
        self.create()
        news = News.objects.first()

        response = self.client.get('/news/{}'.format(news.id), follow=True)
        self.assertEqual(response.status_code, 200)

    def testSendAnnounce(self):
        title = 'New glibc'
        self.create(title, announce=True)
        self.assertEqual(len(mail.outbox), 1)
        self.assertIn(title, mail.outbox[0].subject)

    def testPreview(self):
        response = self.client.post('/news/preview/', {'data': '**body**'}, follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual('<p><strong>body</strong></p>', response.content.decode())