Completed
Pull Request — master (#155)
by
unknown
41s
created

TestSidebar.test_edit_sidebar_admin_view()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
import logging
2
logger = logging.getLogger(__name__)
3
4
from django.conf import settings
5
from django.contrib.contenttypes.models import ContentType
6
from django.test import TestCase
7
from django.test import Client
8
9
from mezzanine.blog.models import BlogCategory
10
from mezzanine.blog.models import BlogPost
11
from mezzanine.core.models import CONTENT_STATUS_PUBLISHED, CONTENT_STATUS_DRAFT
12
from mezzanine.pages.models import RichTextPage
13
14
from fullcalendar.models import Occurrence
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_pages.json']
38
39
    def test_edit_richtextpage_admin_view(self):
40
        richtextpages = RichTextPage.objects.all()
41
        self.assertEqual(len(richtextpages), 6)
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_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 == 4:
70
                self.assertEqual(page_header_image_widget.page.id, 2)
71
                self.assertEqual(str(page_header_image_widget.image), 'uploads/site-1/example_header.jpg')
72
            if page.id == 3:
73
                self.assertEqual(page_header_image_widget.page.id, 2)
74
                self.assertEqual(str(page_header_image_widget.image), 'uploads/site-1/example_header.jpg')
75
            if page.id == 8:
76
                self.assertEqual(page_header_image_widget.page.id, 8)
77
                self.assertEqual(str(page_header_image_widget.image), 'uploads/site-1/example_header_subpage.jpg')
78
79
80
class TestBlogCategoryPage(TestCaseAdminLogin):
81
    """ Tests the blog category page rendering """
82
    fixtures = ['test_blog.json']
83
    blog_cat_1 = 'BlogCategory1'
84
    blog_cat_2 = 'BlogCategory2'
85
86
    def test_active_in_menu(self):
87
        """ Tests whether the page is part of the menu. """
88
        response = self.client.get('/')
89
        html = str(response.content)
90
        self.assertTrue('<a href="/blogcategory1page/">BlogCategory1Page</a>' in html)
91
        self.assertTrue('<a href="/blogcategory2page/">BlogCategory2Page</a>' in html)
92
93
    def test_blogpost_titles(self):
94
        """  Tests whether the blog post titles are shown on a blog category page. """
95
        response = self.client.get('/blogcategory1page/', follow=True)
96
        html = str(response.content)
97
        self.assertTrue('<a href="/blog/blogpost3category1/">BlogPost3Category1</a>' in html)
98
        self.assertTrue('<a href="/blog/blogpost2category1/">BlogPost2Category1</a>' in html)
99
100
    def test_blogpost_contents(self):
101
        """ Tests whether the blog post contents are shown on the page. """
102
        response = self.client.get('/blogcategory1page/', follow=True)
103
        html = str(response.content)
104
        self.assertTrue('<p>Example content 3.</p>' in html)
105
        self.assertTrue('<p>Example content 2.</p>' in html)
106
107
    def test_blogpage_pagination(self):
108
        """ Tests whether only a limited number of posts are shown on a page and pagination links are available. """
109
        response = self.client.get('/blogcategory1page/', follow=True)
110
        html = str(response.content)
111
        # the number of posts per pages is set to 2 in the fixtures
112
        self.assertFalse('<a href="/blog/blogpost1category1/">BlogPost1Category1</a>' in html)
113
        blog_posts = response.context['blog_posts']
114
        self.assertEqual(len(blog_posts), 2)
115
        self.assertTrue('<span>Page 1 of 2</span>' in html)
116
117
118
class TestBlogListView(TestCaseAdminLogin):
119
    """ Tests the blog post list view. """
120
    fixtures = ['test_blog.json']
121
    blog_cat_1 = 'BlogCategory1'
122
    blog_cat_2 = 'BlogCategory2'
123
    posts_per_page = 2
124
125
    def test_blogpost_titles(self):
126
        """ Tests whether the titles of the last 2 blog posts are shown on the page. """
127
        blog_categories = BlogCategory.objects.all()
128
        for category in blog_categories:
129
            url = category.get_absolute_url()
130
            response = self.client.get(url)
131
            html = str(response.content)
132
            posts = BlogPost.objects.filter(categories=category)
133
            counter = 0
134
            for post in posts:
135
                post_title_html = '<a href="' + post.get_absolute_url() + '">' + post.title + '</a>'
136
                if counter < self.posts_per_page:
137
                    self.assertTrue(post_title_html in html)
138
                else:
139
                    self.assertFalse(post_title_html in html)
140
                counter += 1
141
142
143
class TestEvent(object):
144
    """
145
    Tests the integration with the fullcalendar app.
146
    Tests the events column and sidebar widget, and the individual occurrence page.
147
    Tests whether the events from the chosen (in the admin) sites are shown,
148
     * Events from all sites in column element
149
     * Events from this site in column element
150
     * Events from this site and main site in column element
151
    """
152
153
    def get_html(self, url):
154
        response = self.client.get(url)
155
        self.assertEqual(response.status_code, 200)
156
        return str(response.content)
157
158
    def test_all_site_events_visibility__user(self):
159
        """
160
        Tests whether the events column elements, that is set to show events from all sites,
161
        actually shows these events,and whether the draft status of events is respected and thus not shown,
162
        """
163
        url = '/'
164
        html = self.get_html(url)
165
        occurrences = Occurrence.objects.all()
166
        self.check_occurrence_visibility(occurrences, html, self.is_admin())
167
168
    def test_this_site_events_visibility_user(self):
169
        """
170
        Tests whether the events column elements, that is set to show events from this site only,
171
        actually shows only these events, and whether the draft status of events is respected and thus not shown,
172
        """
173
        url = '/eventsthissite/'
174
        html = self.get_html(url)
175
        occurrences = Occurrence.site_related.all()
176
        self.check_occurrence_visibility(occurrences, html, self.is_admin())
177
178
    def test_this_site_and_main_events_visibility_user(self):
179
        """
180
        Tests whether the events column elements, that is set to show events from this and main site,
181
        actually shows only these events, and whether the draft status of events is respected and thus not shown,
182
        """
183
        settings.SITE_ID = 2  # set to a department site
184
        url = '/'
185
        html = self.get_html(url)
186
        sites = {1, 2}
187
        occurrences = Occurrence.site_related.filter(site_id__in=sites)
188
        self.check_occurrence_visibility(occurrences, html, self.is_admin())
189
        occurrences_site_3 = Occurrence.objects.filter(site_id=3)
190
        for occurrence in occurrences_site_3:
191
            self.assertFalse(str(occurrence.event.title) in html)
192
        settings.SITE_ID = 1  # back to main site
193
194
    def check_occurrence_visibility(self, occurrences, html, is_admin):
195
        """
196
        Tests that draft occurrences are not shown in columns, and that their pages are hidden.
197
        :param occurrences: the occurrences to check for visibility based on published status
198
        :param html: the html of the page
199
        """
200
        for occurrence in occurrences:
201
            if occurrence.status == CONTENT_STATUS_DRAFT and not is_admin:
202
                self.assertFalse(str(occurrence.event.title) in html)
203
                response = self.client.get(occurrence.get_absolute_url(), follow=True)
204
                self.assertEqual(response.status_code, 404)
205
            elif occurrence.status == CONTENT_STATUS_PUBLISHED:
206
                self.assertTrue(str(occurrence.event.title) in html)
207
                response = self.client.get(occurrence.get_absolute_url())
208
                self.assertEqual(response.status_code, 200)
209
210
211
class TestEventAdmin(TestCase, TestEvent):
212
    """
213
    Tests the draft/published status visibility in widgets and the occurrence page, for a normal user (draft hidden).
214
    see TestEvent for actual tests
215
    """
216
    fixtures = ['test_events.json']
217
218
    def setUp(self):
219
        self.client = Client()
220
        response = self.client.post('/admin/login/?next=/admin/', {'username': 'admin', 'password': 'admin'}, follow=True)
221
        self.assertEqual(response.status_code, 200)
222
223
    def is_admin(self):
224
        return True
225
226
227
class TestEventUser(TestCase, TestEvent):
228
    """
229
    Tests the draft/published status visibility in widgets and occurrence page, for an admin (draft visible)
230
    see TestEvent for actual tests
231
    """
232
    fixtures = ['test_events.json']
233
234
    def setUp(self):
235
        self.client = Client()
236
237
    def is_admin(self):
238
        return False
239