Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_directory.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from nose.tools import istest | |||||
from rest_framework.test import APITestCase | from rest_framework.test import APITestCase | ||||
from unittest.mock import patch | from unittest.mock import patch | ||||
from swh.web.tests.testcase import SWHWebTestCase | from swh.web.tests.testcase import SWHWebTestCase | ||||
class DirectoryApiTestCase(SWHWebTestCase, APITestCase): | class DirectoryApiTestCase(SWHWebTestCase, APITestCase): | ||||
@patch('swh.web.api.views.directory.service') | @patch('swh.web.api.views.directory.service') | ||||
@istest | def test_api_directory(self, mock_service): | ||||
def api_directory(self, mock_service): | |||||
# given | # given | ||||
stub_directories = [ | stub_directories = [ | ||||
{ | { | ||||
'sha1_git': '18d8be353ed3480476f032475e7c233eff7371d5', | 'sha1_git': '18d8be353ed3480476f032475e7c233eff7371d5', | ||||
'type': 'file', | 'type': 'file', | ||||
'target': '4568be353ed3480476f032475e7c233eff737123', | 'target': '4568be353ed3480476f032475e7c233eff737123', | ||||
}, | }, | ||||
{ | { | ||||
Show All 28 Lines | def test_api_directory(self, mock_service): | ||||
self.assertEquals(rv.status_code, 200) | self.assertEquals(rv.status_code, 200) | ||||
self.assertEquals(rv['Content-Type'], 'application/json') | self.assertEquals(rv['Content-Type'], 'application/json') | ||||
self.assertEquals(rv.data, expected_directories) | self.assertEquals(rv.data, expected_directories) | ||||
mock_service.lookup_directory.assert_called_once_with( | mock_service.lookup_directory.assert_called_once_with( | ||||
'18d8be353ed3480476f032475e7c233eff7371d5') | '18d8be353ed3480476f032475e7c233eff7371d5') | ||||
@patch('swh.web.api.views.directory.service') | @patch('swh.web.api.views.directory.service') | ||||
@istest | def test_api_directory_not_found(self, mock_service): | ||||
def api_directory_not_found(self, mock_service): | |||||
# given | # given | ||||
mock_service.lookup_directory.return_value = [] | mock_service.lookup_directory.return_value = [] | ||||
# when | # when | ||||
rv = self.client.get('/api/1/directory/' | rv = self.client.get('/api/1/directory/' | ||||
'66618d8be353ed3480476f032475e7c233eff737/') | '66618d8be353ed3480476f032475e7c233eff737/') | ||||
# then | # then | ||||
self.assertEquals(rv.status_code, 404) | self.assertEquals(rv.status_code, 404) | ||||
self.assertEquals(rv['Content-Type'], 'application/json') | self.assertEquals(rv['Content-Type'], 'application/json') | ||||
self.assertEquals(rv.data, { | self.assertEquals(rv.data, { | ||||
'exception': 'NotFoundExc', | 'exception': 'NotFoundExc', | ||||
'reason': 'Directory with sha1_git ' | 'reason': 'Directory with sha1_git ' | ||||
'66618d8be353ed3480476f032475e7c233eff737 not found.'}) | '66618d8be353ed3480476f032475e7c233eff737 not found.'}) | ||||
@patch('swh.web.api.views.directory.service') | @patch('swh.web.api.views.directory.service') | ||||
@istest | def test_api_directory_with_path_found(self, mock_service): | ||||
def api_directory_with_path_found(self, mock_service): | |||||
# given | # given | ||||
expected_dir = { | expected_dir = { | ||||
'sha1_git': '18d8be353ed3480476f032475e7c233eff7371d5', | 'sha1_git': '18d8be353ed3480476f032475e7c233eff7371d5', | ||||
'type': 'file', | 'type': 'file', | ||||
'name': 'bla', | 'name': 'bla', | ||||
'target': '4568be353ed3480476f032475e7c233eff737123', | 'target': '4568be353ed3480476f032475e7c233eff737123', | ||||
'target_url': '/api/1/content/' | 'target_url': '/api/1/content/' | ||||
'sha1_git:4568be353ed3480476f032475e7c233eff737123/', | 'sha1_git:4568be353ed3480476f032475e7c233eff737123/', | ||||
Show All 9 Lines | def test_api_directory_with_path_found(self, mock_service): | ||||
self.assertEquals(rv.status_code, 200) | self.assertEquals(rv.status_code, 200) | ||||
self.assertEquals(rv['Content-Type'], 'application/json') | self.assertEquals(rv['Content-Type'], 'application/json') | ||||
self.assertEquals(rv.data, expected_dir) | self.assertEquals(rv.data, expected_dir) | ||||
mock_service.lookup_directory_with_path.assert_called_once_with( | mock_service.lookup_directory_with_path.assert_called_once_with( | ||||
'18d8be353ed3480476f032475e7c233eff7371d5', 'bla') | '18d8be353ed3480476f032475e7c233eff7371d5', 'bla') | ||||
@patch('swh.web.api.views.directory.service') | @patch('swh.web.api.views.directory.service') | ||||
@istest | def test_api_directory_with_path_not_found(self, mock_service): | ||||
def api_directory_with_path_not_found(self, mock_service): | |||||
# given | # given | ||||
mock_service.lookup_directory_with_path.return_value = None | mock_service.lookup_directory_with_path.return_value = None | ||||
path = 'some/path/to/dir/' | path = 'some/path/to/dir/' | ||||
# when | # when | ||||
rv = self.client.get(('/api/1/directory/' | rv = self.client.get(('/api/1/directory/' | ||||
'66618d8be353ed3480476f032475e7c233eff737/%s') | '66618d8be353ed3480476f032475e7c233eff737/%s') | ||||
% path) | % path) | ||||
Show All 11 Lines |