def test_slug_created(self): post = Post() post.title = 'Test' post.description = 'Test' post.save() # we now have a date self.assertEqual(post.slug, 'test')
def test_edit_url(self): """edit url returns correctly""" p = Post() p.title = 'This is a test post' p.slug = 'this-is-test-post' p.source_content = 'Hm, ha ha.' p.save() self.assertEqual(p.edit_url(), '/blog/edit/1/')
def handle(self, *args, **kwargs): # # # kill all Post.objects.all().delete() Tag.objects.all().delete() # # # now import for mdfile in os.listdir(MDFILES): if mdfile == '.DS_Store': continue f = '%s/%s' % (MDFILES, mdfile) with codecs.open(f, encoding='UTF-8') as md: contents = md.read() parts = contents.split('---') header = yaml.load(parts[0]) # build the post try: # make post p = Post() # created p.created = header['created'] # title p.title = header['title'] # url slug = mdfile.split('/').pop().split('.')[0] if slug.startswith('201'): slug = slug[11:] p.slug = slug # description p.description = header['description'] # contents p.source_content = parts[1] # status p.status = p.PUBLISHED # save it p.save() # tags try: for tag in header['tags']: tag, created = Tag.objects.get_or_create(tag=tag) if created: tag.slug = slugify(tag) tag.save() p.tags.add(tag) except KeyError: pass except TypeError: # is an scrappy idea # make post if mdfile == 'index.html': continue p = Post() p.title = mdfile[11:] p.slug = slugify(p.title) p.contents = contents p.save()
def test_markdown_rendering(self): """The source is rendered into markdown on saving""" post = Post() post.title = 'Test' post.slug = 'test' post.source_content = "# Hello" post.save() # we now have markdown self.assertHTMLEqual('<h1 id="hello">Hello</h1>', post.rendered_content)
def test_date_if_blank(self): """If no date is supplied for a post then datetime.now() is applied""" post = Post() post.title = 'Test' post.slug = 'test' post.source_content = "#Hello" post.save() # we now have a date self.assertTrue(post.created)
def test_use_date_if_supplied(self): """If a date is supplied at the time of creation then it will be used when saving""" post_date = datetime.now() post = Post() post.title = 'Test' post.slug = 'test' post.source_content = "#Hello" post.created = post_date post.save() # we now have a date self.assertEqual(post_date, post.created)
class ManagerTestCases(TestCase): def setUp(self): self.p1 = Post( title='test post', source_content='test', slug='test1') self.p1.save() def tearDown(self): self.p1.status = Post.HIDDEN self.p1.save() def test_published(self): """manager method returns only published blog posts""" # none self.assertEqual(0, Post.objects.published().count()) # set to published self.p1.status = Post.PUBLISHED self.p1.save() # now one self.assertEqual(1, Post.objects.published().count()) def test_hidden(self): """manager method returns only hidden blog posts""" # none self.assertEqual(1, Post.objects.hidden().count()) # set to published self.p1.status = Post.HIDDEN self.p1.save() # now one self.assertEqual(1, Post.objects.hidden().count())
def test_pygments(self): """Pygments works as expected""" post = Post() post.title = 'Test' post.slug = 'test' post.source_content = '# Hello \n\n <code lang="jibberish">boom</code>' post.save() self.assertHTMLEqual( """<h1 id="hello"> Hello </h1><p> <div class="highlight"> <pre> boom </pre> </div>""", post.rendered_content)
def test_video_tag(self): """The video tag allows a html5 video tag to be outputted using {% video http://h264.url http://webm.url http://poster.url %} """ p = Post() p.title = 'test post ' p.source_content = '{% video http://w.video.mp4 http://w.video.webm http://w.poster.jpg %}' p.save() html = """<p><video controls="controls" poster="http://w.poster.jpg" preload> <source src="http://w.video.mp4" type='video/mp4; codecs="avc1.42E01E,mp4a.40.2"'> <source src="http://w.video.webm" type='video/webm; codecs="vp8, vorbis"'> <object id="flashvideo" width="720" height="540" data="http://releases.flowplayer.org/swf/flowplayer-3.2.10.swf" type="application/x-shockwave-flash"> <param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.10.swf"> <param name="allowfullscreen" value="true"> <param name="allowscriptaccess" value="always"> <param name="flashvars" value='config={"clip":{"url":"http://w.video.mp4}}'> </object> </video></p>""" self.assertHTMLEqual(p.rendered_content, html)
def test_cache_page_month_cache_enabled_false(self): """if no cache is enabled in the settings then no caching takes place""" # make the date d = datetime( year=2012, month=6, day=18, minute=2, hour=3, second=10) # request the page request = self.rf.get( reverse('omblog:month', args=[d.year, d.month])) response = views.month(request, d.year, d.month) self.assertNotContains(response, 'THISISATESTPOST') # now create a blog post p = Post() p.title = 'THISISATESTPOST' p.slug = 'slug' p.source_content = 'This is a test post' p.status = p.PUBLISHED p.description = 'test post' p.created = d p.save() # now it contains the content response = views.month(request, d.year, d.month) self.assertContains(response, 'THISISATESTPOST') # enabled it again settings.OMBLOG_CACHE_ENABLED = True reload(o_settings) # change the post to hidden and get view again p.status = p.HIDDEN p.save() # not contains response = views.month(request, d.year, d.month) self.assertNotContains(response, 'THISISATESTPOST') # change post status again and save p.status = p.PUBLISHED p.save() # not contains, because the page is cached response = views.month(request, d.year, d.month) self.assertNotContains(response, 'THISISATESTPOST')
def test_cache_page_index_cache_enabled_false(self): """if no cache is enabled in the settings then no caching takes place""" # request the page request = self.rf.get(reverse('omblog:index')) response = views.index(request) self.assertNotContains(response, 'THISISATESTPOST') # now create a blog post p = Post() p.title = 'THISISATESTPOST' p.slug = 'slug' p.source_content = 'This is a test post' p.status = p.PUBLISHED p.description = 'test post' p.save() # now it contains the content response = views.index(request) self.assertContains(response, 'THISISATESTPOST') # enabled it again settings.OMBLOG_CACHE_ENABLED = True reload(o_settings) # change the post to hidden and get view again p.status = p.HIDDEN p.save() # not contains response = views.index(request) self.assertNotContains(response, 'THISISATESTPOST') # change post status again and save p.status = p.PUBLISHED p.save() # not contains, because the page is cached response = views.index(request) self.assertNotContains(response, 'THISISATESTPOST')
class ViewTestCases(TestCase): def setUp(self): # make a post and attachment self.post = Post() self.post.title = 'Blog Post' self.post.slug = 'blog-post' self.post.source_content = 'Blog Post' self.post.status = Post.PUBLISHED self.post.save() # now user for the auth'd views self.user = User() self.user.username = '******' self.user.set_password('test') self.user.email = '*****@*****.**' self.user.is_active = True self.user.save() def test_multiple_tags(self): """Multiple tags don't cause 500 on views""" # make tags t1 = Tag(slug='a') t1.save() t2 = Tag(slug='a') t2.save() # get view and it is 200 r = self.client.get(reverse('omblog:tag', args=[t2.slug])) self.assertEqual(200, r.status_code) def test_feed(self): """The feed view works as expected""" r = self.client.get(reverse('omblog:feed')) self.assertEqual(r.status_code, 200) def test_login(self): """The login view works as expected""" # get is a 200 r = self.client.get(reverse('omblog:login')) self.assertEqual(r.status_code, 200) # post is a 200 r = self.client.post(reverse('omblog:login'), {}) self.assertEqual(r.status_code, 200) def test_attach_delete_requires_login(self): """attach_delete requires login to access""" # attach delete is a 302 r = self.client.post(reverse('omblog:attach_delete'), {}) self.assertEqual(302, r.status_code) def test_attach_delete(self): """Attach delete works as expected""" # create image image = PostImage(post=self.post) image.title = 'Blog Post Image' image.src = 'fake/path/a.png' image.save() # login self.assertTrue( self.client.login(username='******', password='******')) # we have one post Image self.assertEqual(1, PostImage.objects.count()) # post is a 200 r = self.client.post( reverse('omblog:attach_delete'), {'pk': image.pk}) self.assertEqual(200, r.status_code) # we have no post Images self.assertEqual(0, PostImage.objects.count()) # Django 1.4 raises Http404 so just ignore these if DJANGO_MINOR > 4: # do a non existant one and we nicely 404 r = self.client.post( reverse('omblog:attach_delete'), {'pk': 1230}) self.assertEqual(404, r.status_code) # send junk and it fails nicely 404 r = self.client.post( reverse('omblog:attach_delete'), {'pk': None}) self.assertEqual(404, r.status_code) def test_attach_requires_login(self): """attach requires login to access""" r = self.client.post(reverse('omblog:attach'), {}) self.assertEqual(302, r.status_code) def test_attach(self): """attach works as expected""" # login self.assertTrue( self.client.login(username='******', password='******')) # prep the data i = Image.new('RGB', (800,800)) i.save('omblog-test.jpg','JPEG') # send data with open('omblog-test.jpg') as fp: r = self.client.post( reverse('omblog:attach'), {'post': self.post.pk, 'phile': fp}) self.assertEqual(200, r.status_code) # cleanup the file unlink('omblog-test.jpg') # Django 1.4 raises Http404 so just ignore these if DJANGO_MINOR > 4: # send junk for post and we get 404 r = self.client.post( reverse('omblog:attach'), {'post': 12321321}) self.assertEqual(404, r.status_code) def test_create_requires_login(self): """create requires login to access""" r = self.client.post(reverse('omblog:create'), {}) self.assertEqual(302, r.status_code) def test_create_works_as_expected(self): """create creates!""" self.assertTrue( self.client.login(username='******', password='******')) # send garbage and we still have one # we got a 200 r = self.client.post(reverse('omblog:create'), { 'title': '', 'description': '', }) # we have one post self.assertEqual(1, Post.objects.count()) # we got a 200 r = self.client.post(reverse('omblog:create'), { 'title': 'the title', 'description': 'the description', }) self.assertEqual(200, r.status_code) # we have two posts self.assertEqual(2, Post.objects.count()) def test_edit_requires_login(self): """edit requires login to access""" r = self.client.get(reverse('omblog:edit', args=[self.post.pk])) self.assertEqual(302, r.status_code) @skipIf( DJANGO_MINOR <= 4, 'Skipping test as Django version is 1.4 or less') def test_edit_works_as_expected(self): """edit edits!""" self.assertTrue( self.client.login(username='******', password='******')) # we got a 200 on GET r = self.client.get(reverse('omblog:edit', args=[self.post.pk])) self.assertEqual(200, r.status_code) # we got a 200 on POST r = self.client.post(reverse('omblog:edit', args=[self.post.pk]), {'title': 'The title', 'status': 4, 'slug': 'the-new-slug', 'description': 'The description', 'source_content': 'The content'}) self.assertEqual(200, r.status_code) # everything was saved p = Post.objects.get(pk=self.post.pk) self.assertEqual(p.title, 'The title') self.assertEqual(p.description, 'The description') self.assertEqual(p.status, 4) self.assertEqual(p.slug, 'the-new-slug') self.assertEqual(p.source_content, 'The content') def test_tag(self): """tag yields 200""" # make tag and we get a 200 tag = Tag(slug='t') tag.save() r = self.client.get(reverse('omblog:tag', args=[tag.slug])) self.assertEqual(200, r.status_code) def test_index(self): """index yields 200""" r = self.client.get(reverse('omblog:index')) self.assertEqual(200, r.status_code) def test_month(self): """month yields 200""" r = self.client.get( reverse('omblog:month', args=[ self.post.created.year, self.post.created.month])) self.assertEqual(200, r.status_code) def test_post(self): """post yields 200""" r = self.client.get(reverse('omblog:post', args=[self.post.slug])) self.assertEqual(200, r.status_code) def test_post_404(self): """non existant post is 404""" # Django 1.4 raises Http404 so just ignore these if DJANGO_MINOR > 4: r = self.client.get(reverse('omblog:post', args=['shoes'])) self.assertEqual(404, r.status_code) def test_redirect_edit_requires_login(self): """redirect_edit yields 302""" r = self.client.get( reverse('omblog:edit_redirect', args=[self.post.slug])) url = '{}?next=/blog/{}/edit/'.format( reverse('omblog:login'), self.post.slug) self.assertRedirects(r, url) @skipIf( DJANGO_MINOR <= 4, 'Skipping test as Django version is 1.4 or less') def test_redirect_edit_redirects_when_logged_in(self): """redirect_edit redirects correctly""" self.assertTrue( self.client.login(username='******', password='******')) r = self.client.get( reverse('omblog:edit_redirect', args=[self.post.slug])) self.assertRedirects(r, reverse('omblog:edit', args=[self.post.pk]))