TestBlogCategoryPage.test_blogpost_titles()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
import logging
2
3
from django.conf import settings
4
from django.test import TestCase
5
from django.test import Client
6
7
from mezzanine.blog.models import BlogCategory
8
from mezzanine.blog.models import BlogPost
9
from mezzanine.core.models import CONTENT_STATUS_PUBLISHED, CONTENT_STATUS_DRAFT
10
from mezzanine.pages.models import RichTextPage
11
12
from fullcalendar.models import Occurrence
13
14
logger = logging.getLogger(__name__)
15
16
17
class TestCaseAdminLogin(TestCase):
18
    """ Test case with client and login as admin function. """
19
20
    def setUp(self):
21
        # Needed during tests with DEBUG=False (the default)
22
        # to prevent a TemplateDoesNotExist error of a filebrowser template.
23
        # Not sure what goes wrong here, but seems to work fine in manual tests.
24
        settings.TEMPLATE_DEBUG = False
25
        self.client = Client()
26
        self.login()
27
28
    def login(self):
29
        """ Login as admin. """
30
        response = self.client.post('/admin/login/?next=/admin/', {'username': 'admin', 'password': 'admin'}, follow=True)
31
        self.assertEqual(response.status_code, 200)
32
        return response
33
34
35
class TestPage(TestCaseAdminLogin):
36
    """ Tests the basic page structure and admin. """
37
    fixtures = ['test_base.json', 'test_pages.json']
38
39
    def test_edit_richtextpage_admin_view(self):
40
        richtextpages = RichTextPage.objects.all()
41
        self.assertEqual(richtextpages.count(), 4)
42
        for page in richtextpages:
43
            response = self.client.get('/admin/pages/richtextpage/' + str(page.id) + '/', follow=True)
44
            self.assertEqual(response.status_code, 200)
45
46
    def test_richtextpage_view(self):
47
        richtextpages = RichTextPage.objects.all()
48
        for page in richtextpages:
49
            response = self.client.get(page.get_absolute_url(), follow=True)
50
            self.assertEqual(response.status_code, 200)
51
52
53
class TestPageHeaderImage(TestCaseAdminLogin):
54
    """ Tests the header image of pages. """
55
    fixtures = ['test_base.json', 'test_pages.json']
56
57
    def test_edit_header_admin_view(self):
58
        richtextpages = RichTextPage.objects.all()
59
        for page in richtextpages:
60
            response = self.client.get('/admin/pages/richtextpage/' + str(page.id) + '/', follow=True)
61
            self.assertEqual(response.status_code, 200)
62
63
    def test_header_page_view(self):
64
        richtextpages = RichTextPage.objects.all()
65
        for page in richtextpages:
66
            response = self.client.get(page.get_absolute_url(), follow=True)
67
            self.assertEqual(response.status_code, 200)
68
            page_header_image_widget = response.context['page_header']
69
            if page.id == 17:
70
                self.assertEqual(page_header_image_widget.page.id, 17)
71
                self.assertEqual(str(page_header_image_widget.image), 'uploads/site-1/headerhome.jpg')
72
            if page.id == 37:
73
                self.assertEqual(page_header_image_widget.page.id, 17)
74
                self.assertEqual(str(page_header_image_widget.image), 'uploads/site-1/headerhome.png')
75
            if page.id == 29:
76
                self.assertEqual(page_header_image_widget.page.id, 29)
77
                self.assertEqual(str(page_header_image_widget.image), 'uploads/site-1/header.jpg')
78
79
80
class TestBlogCategoryPage(TestCaseAdminLogin):
81
    """ Tests the blog category page rendering """
82
    fixtures = ['test_base.json', 'test_blog.json']
83
    blog_cat_1 = 'BlogCategory1'
84
    blog_cat_2 = 'BlogCategory2'
85
86
    def setUp(self):
87
        super().setUp()
88
        settings.BLOG_POST_PER_PAGE = 2
89
90
    def test_active_in_menu(self):
91
        """ Tests whether the page is part of the menu. """
92
        response = self.client.get('/')
93
        html = str(response.content)
94
        self.assertTrue('<a href="/blogcategory1page/">blogcategory1page</a>' in html)
95
        self.assertTrue('<a href="/blogcategory2page/">blogcategory2page</a>' in html)
96
97
    def test_blogpost_titles(self):
98
        """  Tests whether the blog post titles are shown on a blog category page. """
99
        response = self.client.get('/blogcategory1page/', follow=True)
100
        html = str(response.content)
101
        self.assertTrue('<a class="button" href="/blog/blogpost3category1/">Lees verder</a>' in html)
102
        self.assertTrue('<a class="button" href="/blog/blogpost2category1/">Lees verder</a>' in html)
103
104
    def test_blogpost_contents(self):
105
        """ Tests whether the blog post contents are shown on the page. """
106
        response = self.client.get('/blogcategory1page/', follow=True)
107
        html = str(response.content)
108
        self.assertTrue('<p>Example content 3.</p>' in html)
109
        self.assertTrue('<p>Example content 2.</p>' in html)
110
111
    def test_blogpage_pagination(self):
112
        """ Tests whether only a limited number of posts are shown on a page and pagination links are available. """
113
        response = self.client.get('/blogcategory1page/', follow=True)
114
        html = str(response.content)
115
        self.assertFalse('<a class="button" href="/blog/blogpost1category1/">Lees verder</a>' in html)
116
        blog_posts = response.context['blog_posts']
117
        self.assertEqual(len(blog_posts), 2)
118
        self.assertTrue('Pagina 1 van 2' in html)
119
120
121
class TestBlogListView(TestCaseAdminLogin):
122
    """ Tests the blog post list view. """
123
    fixtures = ['test_base.json', 'test_blog.json']
124
    blog_cat_1 = 'BlogCategory1'
125
    blog_cat_2 = 'BlogCategory2'
126
    posts_per_page = 2
127
128
    def setUp(self):
129
        super().setUp()
130
        settings.BLOG_POST_PER_PAGE = TestBlogListView.posts_per_page
131
132
    def test_blogpost_titles(self):
133
        """ Tests whether the titles of the last 2 blog posts are shown on the page. """
134
        blog_categories = BlogCategory.objects.all()
135
        for category in blog_categories:
136
            url = category.get_absolute_url()
137
            print(url)
138
            response = self.client.get(url)
139
            html = str(response.content)
140
            posts = BlogPost.objects.filter(categories=category)
141
            counter = 0
142
            for post in posts:
143
                if counter < TestBlogListView.posts_per_page:
144
                    self.assertTrue(post.get_absolute_url() in html)
145
                else:
146
                    self.assertFalse(post.get_absolute_url() in html)
147
                counter += 1
148
149
150
class TestEvent(object):
151
    """
152
    Tests the integration with the fullcalendar app.
153
    Tests the events column and sidebar widget, and the individual occurrence page.
154
    Tests whether the events from the chosen (in the admin) sites are shown,
155
     * Events from all sites in column element
156
     * Events from this site in column element
157
     * Events from this site and main site in column element
158
    """
159
160
    def get_html(self, url):
161
        response = self.client.get(url, follow=True)
162
        self.assertEqual(response.status_code, 200)
163
        return str(response.content)
164
165
    def test_all_site_events_visibility__user(self):
166
        """
167
        Tests whether the agenda sidebar set to show events from all sites,
168
        actually shows these events,and whether the draft status of events is respected and thus not shown,
169
        """
170
        url = '/'
171
        html = self.get_html(url)
172
        occurrences = Occurrence.objects.all()
173
        self.check_occurrence_visibility(occurrences, html, self.is_admin())
174
175
    def test_this_site_events_visibility_user(self):
176
        """
177
        Tests whether the agenda sidebar is set to show events from this site only,
178
        actually shows only these events, and whether the draft status of events is respected and thus not shown,
179
        """
180
        url = '/eventsthissite/'
181
        html = self.get_html(url)
182
        occurrences = Occurrence.site_related.all()
183
        self.check_occurrence_visibility(occurrences, html, self.is_admin())
184
185
    def test_this_site_and_main_events_visibility_user(self):
186
        """
187
        Tests whether the events column elements, that is set to show events from this and main site,
188
        actually shows only these events, and whether the draft status of events is respected and thus not shown,
189
        """
190
        settings.SITE_ID = 2  # set to a department site
191
        url = '/'
192
        html = self.get_html(url)
193
        sites = {1, 2}
194
        occurrences = Occurrence.site_related.filter(site_id__in=sites)
195
        self.check_occurrence_visibility(occurrences, html, self.is_admin())
196
        occurrences_site_3 = Occurrence.objects.filter(site_id=3)
197
        for occurrence in occurrences_site_3:
198
            self.assertFalse(str(occurrence.event.title) in html)
199
        settings.SITE_ID = 1
200
201
    def check_occurrence_visibility(self, occurrences, html, is_admin):
202
        """
203
        Tests that draft occurrences are not shown in agenda sidebar, and that their pages are hidden.
204
        :param occurrences: the occurrences to check for visibility based on published status
205
        :param html: the html of the page
206
        """
207
        for occurrence in occurrences:
208
            if occurrence.status == CONTENT_STATUS_DRAFT and not is_admin:
209
                self.assertFalse(str(occurrence.event.title) in html)
210
                response = self.client.get(occurrence.get_absolute_url(), follow=True)
211
                self.assertEqual(response.status_code, 404)
212
            elif occurrence.status == CONTENT_STATUS_PUBLISHED:
213
                self.assertTrue(str(occurrence.event.title) in html)
214
                response = self.client.get(occurrence.get_absolute_url())
215
                self.assertEqual(response.status_code, 200)
216
217
218
class TestEventAdmin(TestCase, TestEvent):
219
    """
220
    Tests the draft/published status visibility in sidebar and the occurrence page, for a normal user (draft hidden).
221
    see TestEvent for actual tests
222
    """
223
    fixtures = ['test_base.json', 'test_pages.json', 'test_events.json']
224
225
    def setUp(self):
226
        self.client = Client()
227
        response = self.client.post('/admin/login/?next=/admin/', {'username': 'admin', 'password': 'admin'}, follow=True)
228
        self.assertEqual(response.status_code, 200)
229
230
    def tearDown(self):
231
        settings.SITE_ID = 1
232
233
    def is_admin(self):
234
        return True
235
236
237
class TestEventUser(TestCase, TestEvent):
238
    """
239
    Tests the draft/published status visibility in sidebar and occurrence page, for an admin (draft visible)
240
    see TestEvent for actual tests
241
    """
242
    fixtures = ['test_base.json', 'test_pages.json', 'test_events.json']
243
244
    def setUp(self):
245
        self.client = Client()
246
247
    def tearDown(self):
248
        settings.SITE_ID = 1
249
250
    def is_admin(self):
251
        return False
252