def test_custom_layout(self): """if a custom template is used in a template then that will be used to render the page""" # write out the test template layout_path = '%s/unittest.html' % c_settings.CUSTOM_LAYOUTS layout = open(layout_path, 'w') layout.write('hello world') layout.close() # now make the page page1 = Page() page1.title = '1' page1.slug = '1' page1.content = '1' page1.order = Decimal('1') page1.status = Page.VISIBLE page1.layout = layout_path page1.save() # get the response and it is 'hello world' request = self.rf.get(reverse('ccpages:view', args=[1])) response = view(request, page1.slug) self.assertEqual('hello world', response.content) # remove the layout and it is not equal page1.layout = None page1.save() response = view(request, page1.slug) self.assertNotEqual('hello world', response.content) # delete the custom template os.unlink(layout_path)
def test_title(self): """A title is set on a file from filename is none is supplied""" # open file test_pdf = open('%s/ccpages/test.pdf' % settings.STATIC_ROOT) # make page and attachment p1 = Page() p1.title = '1' p1.slug = '1' p1.content = '# Hello World' p1.order = Decimal('1') p1.password = '******' p1.status = Page.VISIBLE p1.save() at1 = PageAttachment() at1.page = p1 at1.src = File(test_pdf, 'ccpages/test.pdf') at1.save() # the title is 'test.pdf' self.assertEqual(at1.title, 'test.pdf') test_pdf.close() os.unlink(at1.src.path) # make another one, but this time with a title test_pdf = open('%s/ccpages/test.pdf' % settings.STATIC_ROOT) at2 = PageAttachment() at2.page = p1 at2.src = File(test_pdf, 'ccpages/test.pdf') at2.title = 'Arther' at2.save() # title is now arther self.assertEqual(at2.title, 'Arther') # delete the files test_pdf.close() os.unlink(at2.src.path)
def test_hash_if_no_password(self): """A hash is not generated on save if page has no password""" page1 = Page() page1.title = '1' page1.slug = '1' page1.content = '1' page1.order = Decimal('1') page1.status = Page.VISIBLE page1.save() # get the page p = Page.objects.get(pk=page1.pk) # we have no hash self.assertFalse(p.hash)
def test_view_200(self): """A visible page with no password returns a 200""" # make a page page1 = Page() page1.title = '1' page1.slug = '1' page1.content = '1' page1.order = Decimal('1') page1.status = Page.VISIBLE page1.save() # make request and we get a 200 request = self.rf.get(reverse('ccpages:view', args=[1])) response = view(request, page1.slug) self.assertEqual(200, response.status_code)
def test_index_200(self): """If there is a default then we get a nice 200""" # make a page page1 = Page() page1.title = '1' page1.slug = '1' page1.content = '1' page1.order = Decimal('1') page1.status = Page.VISIBLE page1.save() # make request and we get a 200 request = self.rf.get(reverse('ccpages:index')) response = index(request) self.assertEqual(302, response.status_code)
def test_content_rendered(self): """When a page is saved the content is passed through markdown and saved as content_rendered""" page1 = Page() page1.title = '1' page1.slug = '1' page1.content = '# Hello World' page1.order = Decimal('1') page1.password = '******' page1.status = Page.VISIBLE page1.save() # we now have rendered content self.assertHTMLEqual( page1.content_rendered, '<h1 id="hello-world">\nHello World\n</h1>')
def test_hash_if_password(self): """A hash is generated on save if page has password""" page1 = Page() page1.title = '1' page1.slug = '1' page1.content = '1' page1.order = Decimal('1') page1.password = '******' page1.status = Page.VISIBLE page1.save() # get the page p = Page.objects.get(pk=page1.pk) # we have a hash self.assertEqual( p.hash, 'f9fc27b9374ad1e3bf34fdbcec3a4fd632427fed')
def test_password_200(self): """The password page responds with a 200 ok""" # make a page page1 = Page() page1.title = '1' page1.slug = '1' page1.content = '1' page1.order = Decimal('1') page1.password = '******' page1.status = Page.VISIBLE page1.save() # make post request and we get a 200 request = self.rf.get( reverse('ccpages:password', args=[page1.slug])) response = password(request, page1.slug) self.assertEqual(200, response.status_code)
def test_supply_correct_password_and_form_is_valid(self): """If we supply the correct password to the form then the form will validate""" page1 = Page() page1.title = '1' page1.slug = '1' page1.content = '1' page1.order = Decimal('1') page1.password = '******' page1.status = Page.VISIBLE page1.save() # make the data data = {'password': '******'} # make the form form = PagePasswordForm(data, page=page1) # form is valid self.assertTrue(form.is_valid())
def test_password_redirects_back_if_page_has_no_password(self): """if a page has no password and the user attempts to access the password page, they are redirected back""" page1 = Page() page1.title = '1' page1.slug = '1' page1.content = '1' page1.order = Decimal('1') page1.status = Page.VISIBLE page1.save() # make request and we get a 302 request = self.rf.get(reverse('ccpages:password', args=[1])) response = password(request, page1.slug) self.assertEqual(302, response.status_code) self.assertEqual( response['Location'], reverse('ccpages:view', args=[page1.slug]))
def test_view_with_correct_hash(self): """if a user visits a page with the correct hash in their session the view returns a 200 response""" # make a page page1 = Page() page1.title = '1' page1.slug = '1' page1.content = '1' page1.order = Decimal('1') page1.password = '******' page1.status = Page.VISIBLE page1.save() # make request and we get a 200 request = self.rf.get(reverse('ccpages:view', args=[page1.slug])) hash = 'c0b0f36ffdfe68518916a0ea9d8a89cd2b4bc586' request.session['ccpage_1_hash'] = hash response = view(request, page1.slug) self.assertEqual(200, response.status_code)
def test_password_post_incorrect_password(self): """When the incorrect password for a page is sent via post the users session is updated to include a hash that allows access""" # make a page page1 = Page() page1.title = '1' page1.slug = '1' page1.content = '1' page1.order = Decimal('1') page1.password = '******' page1.status = Page.VISIBLE page1.save() # make post request and we get a 200 request = self.rf.post( reverse('ccpages:password', args=[page1.slug]), {'password': '******'}) response = password(request, page1.slug) self.assertEqual(200, response.status_code)
def test_view_password_redirects(self): """If there is a page with a password and the user isn't authed to view it then it redirects to the view view, but with a different url""" page1 = Page() page1.title = '1' page1.slug = '1' page1.content = '1' page1.order = Decimal('1') page1.status = Page.VISIBLE page1.password = '******' page1.save() # make request and we get a 302 request = self.rf.get(reverse('ccpages:view', args=[1])) response = view(request, page1.slug) self.assertEqual(302, response.status_code) self.assertEqual( response['Location'], reverse('ccpages:password', args=[page1.slug]))
def test_password_post_correct_password(self): """When the correct password for a page is sent via post the users session is updated to include a hash that allows access""" # make a page page1 = Page() page1.title = '1' page1.slug = '1' page1.content = '1' page1.order = Decimal('1') page1.password = '******' page1.status = Page.VISIBLE page1.save() # make request and we get a 302 request = self.rf.post( reverse('ccpages:password', args=[page1.slug]), {'password': '******'}) response = password(request, page1.slug) self.assertEqual(302, response.status_code) # and now the session has the key and a hash in it self.assertEqual( request.session['ccpage_1_hash'], 'c0b0f36ffdfe68518916a0ea9d8a89cd2b4bc586')