def test_create_a_new_paste_without_filename_or_description(self): self.assertIsNone(Paste.get_by_id(1)) data = { 'filename': '', 'description': '', 'content': 'foo bar baz', } url = reverse('paste_create') with freeze_time('2016-12-25'): response = self.client.post(url, data) detail_url = reverse('paste_detail', args=[1]) self.assertRedirects(response, detail_url) paste = Paste.get_by_id(1) self.assertEqual( paste.to_dict(), { 'author': u'', 'created': datetime.datetime(2016, 12, 25), 'description': u'', 'filename': u'untitled.txt', 'files': [ { 'content_type': 'text/plain', 'created': datetime.datetime(2016, 12, 25), 'filename': 'untitled.txt', 'num_lines': 1, 'path': u'pasty/2016/12/25/1/1/untitled.txt', 'relative_path': '1/untitled.txt', }, ], 'fork': None, 'id': 1, 'num_files': 1, 'num_lines': 1, 'preview': ('<div class="highlight highlight__autumn">' '<pre><span></span>foo bar baz\n</pre></div>\n'), 'url': '/1/', }, )
def test_create_a_new_paste_on_post(self): self.assertIsNone(Paste.get_by_id(1)) data = { 'description': 'Foo', 'filename': 'example.txt', 'content': 'foo bar baz', } url = reverse('paste_create') with freeze_time('2016-12-25'): response = self.client.post(url, data) self.assertEqual(response.status_code, 302) self.assertEqual(response['Location'], '/1/') paste = Paste.get_by_id(1) self.assertEqual( paste.to_dict(), { 'author': u'', 'created': datetime.datetime(2016, 12, 25), 'description': 'Foo', 'filename': 'example.txt', 'files': [ { 'content_type': 'text/plain', 'created': datetime.datetime(2016, 12, 25), 'filename': 'example.txt', 'num_lines': 1, 'path': 'pasty/2016/12/25/1/1/example.txt', 'relative_path': '1/example.txt', }, ], 'fork': None, 'id': 1, 'num_files': 1, 'num_lines': 1, 'preview': ('<div class="highlight highlight__autumn">' '<pre><span></span>foo bar baz\n</pre></div>\n'), 'url': '/1/', }, )
def test_star_a_paste_creates_star(self): url = reverse('api_star_create') paste = Paste(id=1234) paste.put() data = {'paste': paste.key.id()} self.login('*****@*****.**') response = self.client.post(url, data) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-type'], 'application/json') self.assertEqual( sorted(response.json()), ['author', 'id', 'paste', 'stars'], )
def test_shows_detail_for_paste_without_filename_or_description(self): paste = Paste.create_with_files(id=1234, files=[('', 'foo bar baz')]) url = reverse('paste_detail', args=[paste.key.id()]) response = self.client.get(url) self.assertContains(response, 'foo bar baz', status_code=200)
def test_highlight_content_with_custom_lexer_config(self): config = LexerConfig.get() config.lexers = [{'extension': 'sass', 'language': 'CSS'}] config.put() # Same content, but different filenames. files = [ ('example.sass', 'body { font-family: serif; }'), ('example.txt', 'body { font-family: serif; }'), ] paste = Paste.create_with_files(files=files) css_expected = ( u'<div class="highlight highlight__autumn"><pre><span></span>' u'<span class="nt">body</span> <span class="p">{</span>' u' <span class="nb">font-family</span><span class="o">:</span>' u' <span class="nb">serif</span><span class="p">;</span>' u' <span class="p">}</span>\n</pre></div>\n') txt_expected = ( u'<div class="highlight highlight__autumn"><pre><span></span>' u'body { font-family: serif; }\n</pre></div>\n') self.assertEqual(paste.preview, css_expected) # The sass file was highlighted as CSS. self.assertEqual(paste.files[0].content_highlight(), css_expected) self.assertEqual(paste.files[1].content_highlight(), txt_expected)
def test_returns_404_for_bogus_filename(self): paste = Paste.create_with_files(files=[('image.jpg', 'example')]) url = reverse('paste_raw', args=[paste.key.id(), '1/bogus.jpg']) response = self.client.get(url) self.assertEqual(response.status_code, 404)
def test_serves_raw_file(self): paste = Paste.create_with_files(files=[('image.jpg', 'example')]) url = reverse('paste_raw', args=[paste.key.id(), '1/image.jpg']) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-type'], 'image/jpeg')
def test_download_paste_files_as_zip(self): paste = Paste.create_with_files(id=1234, files=[('example.txt', 'foo')]) url = reverse('paste_download', args=[paste.key.id()]) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Disposition'], 'attachment; filename="example.txt.zip"') self.assertEqual(response['Content-type'], 'application/zip')
def test_unstar_a_paste_removes_star(self): user_email = '*****@*****.**' self.login(user_email) paste = Paste(id=1234) paste.put() starred = paste.create_star_for_author(user_email) self.assertEqual(get_starred_pastes(user_email), [paste]) data = {'paste': paste.key.id()} url = reverse('api_star_delete') response = self.client.post(url, data) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-type'], 'application/json') self.assertEqual(response.json(), { 'id': starred.key.id(), 'stars': [], })
def test_shows_starred_pastes(self): user_email = u'*****@*****.**' paste = Paste(id=1234, created=datetime.datetime(2016, 12, 25)) paste.put() paste.create_star_for_author(user_email) url = reverse('api_star_list') self.login(user_email) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-type'], 'application/json') self.assertEqual( response.json(), { u'stars': [ { u'author': None, u'created': u'2016-12-25T00:00:00', u'description': None, u'filename': 'untitled.txt', u'files': [], u'fork': None, u'id': 1234, u'num_files': 0, u'num_lines': 0, u'preview': None, u'url': u'/1234/', }, ], }, )
def test_error_for_non_existent_paste(self): url = reverse('api_paste_detail', args=('1234', )) self.assertIsNone(Paste.get_by_id(1234)) response = self.client.get(url) self.assertEqual(response.status_code, 404) self.assertEqual(response['Content-type'], 'application/json') self.assertEqual( response.json(), {u'error': u'Paste does not exist'}, )
def test_unstar_non_existent_paste(self): url = reverse('api_star_delete') data = {'paste': '1234'} self.assertIsNone(Paste.get_by_id(1234)) self.login('*****@*****.**') response = self.client.post(url, data) self.assertEqual(response.status_code, 400) self.assertEqual(response['Content-type'], 'application/json') self.assertEqual( response.json(), {'error': 'Does not exist'}, )
def test_shows_detail_for_paste(self): paste = Paste.create_with_files(id=1234, files=[('example.txt', 'foo')]) url = reverse('paste_detail', args=[paste.key.id()]) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual( response.context_data, { 'page_title': 'example.txt', 'paste': paste, 'starred': None, }, )
def test_shows_list_of_pastes(self): for n in range(11): fname = 'example-%s.txt' % n fcontent = 'foo %s' % n paste = Paste.create_with_files(files=[(fname, fcontent)]) index.add_paste(paste) url = reverse('paste_list') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.template_name, 'pasty/paste_list.html') self.assertEqual( sorted(response.context_data), ['page_title', 'pastes', 'section', 'terms'], ) self.assertEqual(response.context_data['pastes'].count, 11)
def test_get_paste_with_one_content_file(self): xmas = datetime.datetime(2016, 12, 25) with freeze_time('2016-12-25'): files = [('example.txt', 'foo')] paste = Paste.create_with_files(id=1234, created=xmas, files=files) url = reverse('api_paste_detail', args=(paste.key.id(), )) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-type'], 'application/json') self.assertEqual( response.json(), { u'author': None, u'created': u'2016-12-25T00:00:00', u'description': None, u'filename': u'example.txt', u'files': [{ u'content_type': u'text/plain', u'created': u'2016-12-25T00:00:00', u'filename': u'example.txt', u'num_lines': 1, u'path': u'pasty/2016/12/25/1234/1/example.txt', u'relative_path': u'1/example.txt', }], u'fork': None, u'id': 1234, u'num_files': 1, u'num_lines': 1, u'preview': u'<div class="highlight highlight__autumn"><pre><span></span>foo\n</pre></div>\n', u'url': u'/1234/', })
def test_forking_from_paste_pre_fills_form(self): files = [('example.txt', 'foo bar baz')] paste = Paste.create_with_files(id=1234, description='Foo', files=files) url = reverse('paste_create') response = self.client.get(url, {'fork': paste.key.id()}) self.assertEqual(response.status_code, 200) self.assertEqual( sorted(response.context_data), ['form', 'page_title', 'section'], ) self.assertEqual(response.context_data['page_title'], u'New paste') self.assertEqual(response.context_data['section'], 'paste_create') self.assertEqual( response.context_data['form'].initial, { 'content': 'foo bar baz', 'description': 'Foo', 'filename': 'example.txt', }, )
def test_to_dict_for_forked_paste(self): xmas = datetime.datetime(2016, 12, 25) orig_key = Paste(id=1234, created=xmas).put() fork = Paste(id=5678, created=xmas, fork=orig_key) fork.put() result = fork.to_dict() self.assertEqual( result, { u'author': None, u'created': xmas, u'description': None, u'filename': 'untitled.txt', u'files': [], u'fork': 1234, u'id': 5678, u'num_files': 0, u'num_lines': 0, u'preview': None, u'url': u'/5678/', }, )
def test_unicode(self): obj = Paste(author='*****@*****.**', filename='example.txt') self.assertEqual(unicode(obj), u'[email protected] / example.txt')
def test_get_or_404_returns_paste_for_valid_string_id(self): Paste(id=1234).put() obj = Paste.get_or_404('1234') self.assertEqual(obj.key.id(), 1234)
def test_get_or_404_returns_paste_for_valid_integer_id(self): Paste(id=1234).put() obj = Paste.get_or_404(1234) self.assertEqual(obj.key.id(), 1234)
def test_get_or_404_with_unknown_id(self): self.assertIsNone(Paste.get_by_id(1234)) with self.assertRaises(Http404): Paste.get_or_404(1234)
def test_get_or_404_with_bad_id(self): with self.assertRaises(Http404): Paste.get_or_404('bogus')
def test_get_or_404_with_none_id(self): with self.assertRaises(Http404): Paste.get_or_404(None)
def test_unicode_for_anonymous_author(self): obj = Paste(filename='example.txt') self.assertEqual(unicode(obj), u'anonymous / example.txt')