Skip to content
Snippets Groups Projects
Commit 5556ff3b authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

Add tests to /browse/directory route

parent f230e810
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@
import unittest
from unittest.mock import patch
from nose.tools import istest
from swh.web.ui import utils
......@@ -78,3 +79,40 @@ class UtilsTestCase(unittest.TestCase):
'endpoint': 'bar'
}
})
@patch('swh.web.ui.utils.flask')
@istest
def prepare_directory_listing(self, mock_flask):
# given
def mock_url_for(url_key, **kwds):
if url_key == 'browse_directory':
sha1_git = kwds['sha1_git']
return '/path/to/url/dir' + '/' + sha1_git
else:
sha1_git = kwds['q']
return '/path/to/url/file' + '/' + sha1_git
mock_flask.url_for.side_effect = mock_url_for
inputs = [{'type': 'dir',
'target': '123',
'name': 'some-dir-name'},
{'type': 'file',
'sha1': '654',
'name': 'some-filename'},
{'type': 'dir',
'target': '987',
'name': 'some-other-dirname'}]
expected_output = [{'link': '/path/to/url/dir/123',
'name': 'some-dir-name'},
{'link': '/path/to/url/file/654',
'name': 'some-filename'},
{'link': '/path/to/url/dir/987',
'name': 'some-other-dirname'}]
# when
actual_outputs = utils.prepare_directory_listing(inputs)
# then
self.assertEquals(actual_outputs, expected_output)
......@@ -123,7 +123,6 @@ class ViewTestCase(test_app.SWHViewTestCase):
# when
rv = self.client.get('/browse/content/sha1:sha1-unknown/raw')
print(self.templates)
self.assertEquals(rv.status_code, 200)
self.assert_template_used('display_content.html')
self.assertEqual(self.get_context_variable('message'),
......@@ -142,7 +141,6 @@ class ViewTestCase(test_app.SWHViewTestCase):
# when
rv = self.client.get('/browse/content/sha2:sha1-invalid/raw')
print(self.templates)
self.assertEquals(rv.status_code, 200)
self.assert_template_used('display_content.html')
self.assertEqual(self.get_context_variable('message'),
......@@ -150,3 +148,85 @@ class ViewTestCase(test_app.SWHViewTestCase):
self.assertEqual(self.get_context_variable('content'), None)
mock_service.lookup_content_raw.assert_called_once_with(
'sha2:sha1-invalid')
@patch('swh.web.ui.views.service')
@patch('swh.web.ui.utils')
@istest
def browse_directory_bad_input(self, mock_utils, mock_service):
# given
mock_service.lookup_directory.side_effect = BadInputExc('Invalid hash')
# when
rv = self.client.get('/browse/directory/sha2-invalid')
# then
self.assertEquals(rv.status_code, 200)
self.assert_template_used('directory.html')
self.assertEqual(self.get_context_variable('message'),
'Invalid hash')
self.assertEqual(self.get_context_variable('files'), [])
mock_service.lookup_directory.assert_called_once_with(
'sha2-invalid')
@patch('swh.web.ui.views.service')
@patch('swh.web.ui.utils')
@istest
def browse_directory_empty_result(self, mock_utils, mock_service):
# given
mock_service.lookup_directory.return_value = None
# when
rv = self.client.get('/browse/directory/some-sha1')
# then
self.assertEquals(rv.status_code, 200)
self.assert_template_used('directory.html')
self.assertEqual(self.get_context_variable('message'),
'Directory some-sha1 not found.')
self.assertEqual(self.get_context_variable('files'), [])
mock_service.lookup_directory.assert_called_once_with(
'some-sha1')
@patch('swh.web.ui.views.service')
@patch('swh.web.ui.views.utils')
@istest
def browse_directory(self, mock_utils, mock_service):
# given
stub_directory_ls = [
{'type': 'dir',
'target': '123',
'name': 'some-dir-name'},
{'type': 'file',
'sha1': '654',
'name': 'some-filename'},
{'type': 'dir',
'target': '987',
'name': 'some-other-dirname'}
]
mock_service.lookup_directory.return_value = stub_directory_ls
stub_directory_map = [
{'link': '/path/to/url/dir/123',
'name': 'some-dir-name'},
{'link': '/path/to/url/file/654',
'name': 'some-filename'},
{'link': '/path/to/url/dir/987',
'name': 'some-other-dirname'}
]
mock_utils.prepare_directory_listing.return_value = stub_directory_map
# when
rv = self.client.get('/browse/directory/some-sha1')
# then
print(self.templates)
self.assertEquals(rv.status_code, 200)
self.assert_template_used('directory.html')
self.assertEqual(self.get_context_variable('message'),
'Listing for directory some-sha1:')
self.assertEqual(self.get_context_variable('files'),
stub_directory_map)
mock_service.lookup_directory.assert_called_once_with(
'some-sha1')
mock_utils.prepare_directory_listing.assert_called_once_with(
stub_directory_ls)
......@@ -3,6 +3,8 @@
# License: GNU Affero General Public License version 3, or any later version
# See top-level LICENSE file for more information
import flask
def filter_endpoints(url_map, prefix_url_rule, blacklist=[]):
"""Filter endpoints by prefix url rule.
......@@ -29,3 +31,22 @@ def filter_endpoints(url_map, prefix_url_rule, blacklist=[]):
out[r.rule] = {'methods': sorted(map(str, r.methods)),
'endpoint': r.endpoint}
return out
def prepare_directory_listing(files):
"""Given a list of dictionary files, return a view ready dictionary.
"""
ls = []
for entry in files:
new_entry = {}
if entry['type'] == 'dir':
new_entry['link'] = flask.url_for('browse_directory',
sha1_git=entry['target'])
else:
new_entry['link'] = flask.url_for('show_content',
q=entry['sha1'])
new_entry['name'] = entry['name']
ls.append(new_entry)
return ls
......@@ -4,13 +4,13 @@
# See top-level LICENSE file for more information
from flask import render_template, flash, request, url_for
from flask import render_template, flash, request
from flask.ext.api.decorators import set_renderers
from flask.ext.api.renderers import HTMLRenderer
from swh.core.hashutil import ALGORITHMS
from swh.web.ui import service
from swh.web.ui import service, utils
from swh.web.ui.exc import BadInputExc
from swh.web.ui.main import app
......@@ -186,25 +186,6 @@ def show_content(q):
return render_template('display_content.html', **env)
def prepare_directory_listing(files):
"""Given a list of dictionary files, return a view ready dictionary.
"""
ls = []
for entry in files:
new_entry = {}
if entry['type'] == 'dir':
new_entry['link'] = url_for('browse_directory',
sha1_git=entry['target'])
else:
new_entry['link'] = url_for('show_content',
q=entry['sha1'])
new_entry['name'] = entry['name']
ls.append(new_entry)
return ls
@app.route('/browse/directory/<string:sha1_git>')
@set_renderers(HTMLRenderer)
def browse_directory(sha1_git):
......@@ -219,12 +200,12 @@ def browse_directory(sha1_git):
env = {'sha1_git': sha1_git}
try:
files = service.lookup_directory(sha1_git)
if files:
directory_files = service.lookup_directory(sha1_git)
if directory_files:
message = "Listing for directory %s:" % sha1_git
files = prepare_directory_listing(files)
files = utils.prepare_directory_listing(directory_files)
else:
message = "Directory %s was not found." % sha1_git
message = "Directory %s not found." % sha1_git
files = []
except BadInputExc as e: # do not like it but do not duplicate code
message = str(e)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment