Example #1
0
 def test_delete(self):
     content = StringIO('Hello world!')
     s = DatabaseStorage()
     s.delete('hello.txt')
     s.save(name='hello.txt', content=content)
     s.delete('hello.txt')
     self.assertFalse(s.exists('hello.txt'))
Example #2
0
 def test_open(self):
     content = StringIO('Hello world!')
     s = DatabaseStorage()
     self.assertRaises(OSError, s.open, name='hello.txt')
     s.save(name='hello.txt', content=content)
     f = s.open(name='hello.txt')
     self.assertEqual(f.read(), content.getvalue())
     self.assertRaises(IOError, f.truncate)
     f = s.open(name='hello.txt', mode='r+b')
     self.assertEqual(f.read(), content.getvalue())
     f.truncate()
Example #3
0
 def test_listdir(self):
     content = StringIO('')
     english = StringIO('Hello world!')
     french = StringIO('Salut monde!')
     s = DatabaseStorage()
     self.assertEqual(s.listdir(''), ([], []))
     self.assertEqual(s.listdir('/'), ([], []))
     s.save(name='hello.txt', content=english)
     s.save(name='salut.txt', content=french)
     self.assertEqual(s.listdir(''), ([], ['hello.txt', 'salut.txt']))
     self.assertEqual(s.listdir('/'), ([], ['hello.txt', 'salut.txt']))
     s.save(name='hello/en.txt', content=english)
     s.save(name='hello/fr.txt', content=french)
     s.save(name='hello/docs/README', content=content)
     self.assertEqual(s.listdir(''), (['hello'],
                                      ['hello.txt', 'salut.txt']))
     self.assertEqual(s.listdir('/'), (['hello'],
                                       ['hello.txt', 'salut.txt']))
     self.assertEqual(s.listdir('hello'), (['docs'], ['en.txt', 'fr.txt']))
     self.assertEqual(s.listdir('/hello'), (['docs'], ['en.txt', 'fr.txt']))
     self.assertEqual(s.listdir('hello/docs'), ([], ['README']))
     self.assertEqual(s.listdir('/hello/docs'), ([], ['README']))
     self.assertRaises(OSError, s.listdir, 'goodbye')
     self.assertRaises(OSError, s.listdir, '/goodbye')
Example #4
0
 def test_save(self):
     content = StringIO('Hello world!')
     s = DatabaseStorage()
     self.assertRaises(OSError, s.save, name='', content=content)
     self.assertRaises(OSError, s.save, name='/', content=content)
     self.assertRaises(OSError, s.save, name='hello/', content=content)
     self.assertEqual(s.save(name='hello.txt', content=content),
                      'hello.txt')
     self.assertEqual(s.save(name='hello.txt', content=content),
                      'hello_1.txt')
     self.assertEqual(s.save(name='/hello.txt', content=content),
                      'hello_2.txt')
     # Do not allow the storage layer to retry, if not unique.
     self.assertEqual(s._save(name='hello.txt', content=content),
                      'hello_3.txt')
     self.assertEqual(s.save(name='/hello/goodbye.txt', content=content),
                      'hello/goodbye.txt')
     # Try to save hello.txt, but without uniquify_names
     s = DatabaseStorage(uniquify_names=False)
     self.assertRaises(OSError, s._save, name='hello.txt', content=content)
     try:
         s._save(name='hello.txt', content=content)
     except OSError, e:
         self.assertEqual(e.errno, errno.EEXIST)
Example #5
0
class ViewsTestCase(TestCase):
    urls = 'django_dbstorage.test_urls'

    def create(self, filename, content):
        f = StringIO(content)
        self.storage = DatabaseStorage()
        self.storage.save(name=filename, content=f)

    def assertRedirect(self, response, location):
        self.assertEqual(response.status_code, 301, response.content)
        self.assertEqual(response['Location'], location)

    def test_is_dir(self):
        self.assertTrue(views.is_dir(''))
        self.assertTrue(views.is_dir('.'))
        self.assertTrue(views.is_dir('..'))
        self.assertTrue(views.is_dir('/'))
        self.assertTrue(views.is_dir('dir/'))
        self.assertFalse(views.is_dir('file'))

    def _validate_path(self, path):
        class request:
            pass
        request.path = '/root/' + path
        return views.validate_path(request, path)

    def test_validate_path(self):
        self.assertEqual(self._validate_path(''), '')
        self.assertEqual(self._validate_path('hello%20world'), 'hello world')
        self.assertRedirect(self._validate_path('./hello%20world'),
                            '/root/hello%20world')
        self.assertRedirect(self._validate_path('.'), '/root/')
        self.assertRedirect(self._validate_path('/'), '/root/')
        self.assertRedirect(self._validate_path('./'), '/root/')
        self.assertEqual(self._validate_path('dir/'), 'dir/')
        self.assertEqual(self._validate_path('file'), 'file')
        self.assertRedirect(self._validate_path('dir/.'), '/root/dir/')
        self.assertRedirect(self._validate_path('dir/../DIR/'), '/root/DIR/')
        self.assertRedirect(self._validate_path('dir/../dir/..'), '/root/')
        self.assertRedirect(self._validate_path('../dir/'), '/dir/')
        self.assertRedirect(self._validate_path('../../dir/'), '/dir/')

    def test_urls(self):
        self.create('hello.txt', 'Hello')
        response = self.client.get('/urls/hello.txt')
        self.assertEqual(response.status_code, 200, response.content)
        self.assertEqual(response.content, 'Hello')
        self.assertEqual(response['Content-Type'], 'text/plain')

    def test_urls_redirect(self):
        self.create('hello.txt', 'Hello')
        response = self.client.get('/urls/dir/../hello.txt')
        self.assertEqual(response.status_code, 301, response.content)
        self.assertEqual(response['Location'],
                         'http://testserver/urls/hello.txt')

    def test_urls_404(self):
        response = self.client.get('/urls/404')
        self.assertEqual(response.status_code, 404, response.content)

    def test_urls_index(self):
        response = self.client.get('/urls/')
        self.assertEqual(response.status_code, 404, response.content)

    def test_serve(self):
        self.create('hello.txt', 'Hello')
        response = self.client.get('/serve/hello.txt')
        self.assertEqual(response.status_code, 200, response.content)
        self.assertEqual(response.content, 'Hello')
        self.assertEqual(response['Content-Type'], 'text/plain')

    def test_serve_404(self):
        response = self.client.get('/serve/404')
        self.assertEqual(response.status_code, 404, response.content)

    def test_serve_index(self):
        response = self.client.get('/serve/')
        self.assertEqual(response.status_code, 404, response.content)

    def test_indexes(self):
        self.create('hello.txt', 'Hello')
        with disable_templates():
            response = self.client.get('/indexes/')
        self.assertEqual(response.status_code, 200, response.content)
        self.assertContains(response, 'hello.txt')

    def test_indexes_subdir(self):
        self.create('hello/world.txt', 'Hello world')
        with disable_templates():
            response = self.client.get('/indexes/')
        self.assertEqual(response.status_code, 200, response.content)
        self.assertContains(response, 'hello')
        response = self.client.get('/indexes/hello')
        self.assertEqual(response.status_code, 404, response.content)
        with disable_templates():
            response = self.client.get('/indexes/hello/')
        self.assertEqual(response.status_code, 200, response.content)
        self.assertContains(response, 'world.txt')

    def test_indexes_404(self):
        response = self.client.get('/indexes/hello/')
        self.assertEqual(response.status_code, 404, response.content)

    def test_root_exists(self):
        self.create('root/hello.txt', 'Hello')
        response = self.client.get('/root/hello.txt')
        self.assertEqual(response.status_code, 200, response.content)
        self.assertEqual(response.content, 'Hello')
        self.assertEqual(response['Content-Type'], 'text/plain')

    def test_root_404(self):
        self.create('hello.txt', 'Hello')
        response = self.client.get('/root/hello.txt')
        self.assertEqual(response.status_code, 404, response.content)

    def test_root_indexes(self):
        self.create('root/hello.txt', 'Hello')
        with disable_templates():
            response = self.client.get('/root/')
        self.assertEqual(response.status_code, 200, response.content)
        self.assertContains(response, 'hello.txt')
Example #6
0
 def test_size(self):
     content = StringIO('Hello world!')
     s = DatabaseStorage()
     self.assertRaises(OSError, s.size, name='hello.txt')
     s.save(name='hello.txt', content=content)
     self.assertEqual(s.size(name='hello.txt'), len(content.getvalue()))
Example #7
0
 def test_get_available_name(self):
     content = StringIO('Hello world!')
     s = DatabaseStorage()
     self.assertEqual(s.get_available_name('hello.txt'), 'hello.txt')
     s.save(name='hello.txt', content=content)
     self.assertEqual(s.get_available_name('hello.txt'), 'hello_1.txt')