Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Files
F9124920
test_directory.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Subscribers
None
test_directory.py
View Options
# Copyright (C) 2015-2017 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU Affero General Public License version 3, or any later version
# See top-level LICENSE file for more information
from
nose.tools
import
istest
from
unittest.mock
import
patch
from
..swh_api_testcase
import
SWHApiTestCase
class
DirectoryApiTestCase
(
SWHApiTestCase
):
@patch
(
'swh.web.api.views.directory.service'
)
@istest
def
api_directory
(
self
,
mock_service
):
# given
stub_directories
=
[
{
'sha1_git'
:
'18d8be353ed3480476f032475e7c233eff7371d5'
,
'type'
:
'file'
,
'target'
:
'4568be353ed3480476f032475e7c233eff737123'
,
},
{
'sha1_git'
:
'1d518d8be353ed3480476f032475e7c233eff737'
,
'type'
:
'dir'
,
'target'
:
'8be353ed3480476f032475e7c233eff737123456'
,
}]
expected_directories
=
[
{
'sha1_git'
:
'18d8be353ed3480476f032475e7c233eff7371d5'
,
'type'
:
'file'
,
'target'
:
'4568be353ed3480476f032475e7c233eff737123'
,
'target_url'
:
'/api/1/content/'
'sha1_git:4568be353ed3480476f032475e7c233eff737123/'
,
},
{
'sha1_git'
:
'1d518d8be353ed3480476f032475e7c233eff737'
,
'type'
:
'dir'
,
'target'
:
'8be353ed3480476f032475e7c233eff737123456'
,
'target_url'
:
'/api/1/directory/8be353ed3480476f032475e7c233eff737123456/'
,
}]
mock_service
.
lookup_directory
.
return_value
=
stub_directories
# when
rv
=
self
.
client
.
get
(
'/api/1/directory/'
'18d8be353ed3480476f032475e7c233eff7371d5/'
)
# then
self
.
assertEquals
(
rv
.
status_code
,
200
)
self
.
assertEquals
(
rv
[
'Content-Type'
],
'application/json'
)
self
.
assertEquals
(
rv
.
data
,
expected_directories
)
mock_service
.
lookup_directory
.
assert_called_once_with
(
'18d8be353ed3480476f032475e7c233eff7371d5'
)
@patch
(
'swh.web.api.views.directory.service'
)
@istest
def
api_directory_not_found
(
self
,
mock_service
):
# given
mock_service
.
lookup_directory
.
return_value
=
[]
# when
rv
=
self
.
client
.
get
(
'/api/1/directory/'
'66618d8be353ed3480476f032475e7c233eff737/'
)
# then
self
.
assertEquals
(
rv
.
status_code
,
404
)
self
.
assertEquals
(
rv
[
'Content-Type'
],
'application/json'
)
self
.
assertEquals
(
rv
.
data
,
{
'exception'
:
'NotFoundExc'
,
'reason'
:
'Directory with sha1_git '
'66618d8be353ed3480476f032475e7c233eff737 not found.'
})
@patch
(
'swh.web.api.views.directory.service'
)
@istest
def
api_directory_with_path_found
(
self
,
mock_service
):
# given
expected_dir
=
{
'sha1_git'
:
'18d8be353ed3480476f032475e7c233eff7371d5'
,
'type'
:
'file'
,
'name'
:
'bla'
,
'target'
:
'4568be353ed3480476f032475e7c233eff737123'
,
'target_url'
:
'/api/1/content/'
'sha1_git:4568be353ed3480476f032475e7c233eff737123/'
,
}
mock_service
.
lookup_directory_with_path
.
return_value
=
expected_dir
# when
rv
=
self
.
client
.
get
(
'/api/1/directory/'
'18d8be353ed3480476f032475e7c233eff7371d5/bla/'
)
# then
self
.
assertEquals
(
rv
.
status_code
,
200
)
self
.
assertEquals
(
rv
[
'Content-Type'
],
'application/json'
)
self
.
assertEquals
(
rv
.
data
,
expected_dir
)
mock_service
.
lookup_directory_with_path
.
assert_called_once_with
(
'18d8be353ed3480476f032475e7c233eff7371d5'
,
'bla'
)
@patch
(
'swh.web.api.views.directory.service'
)
@istest
def
api_directory_with_path_not_found
(
self
,
mock_service
):
# given
mock_service
.
lookup_directory_with_path
.
return_value
=
None
path
=
'some/path/to/dir/'
# when
rv
=
self
.
client
.
get
((
'/api/1/directory/'
'66618d8be353ed3480476f032475e7c233eff737/
%s
'
)
%
path
)
path
=
path
.
strip
(
'/'
)
# Path stripped of lead/trail separators
# then
self
.
assertEquals
(
rv
.
status_code
,
404
)
self
.
assertEquals
(
rv
[
'Content-Type'
],
'application/json'
)
self
.
assertEquals
(
rv
.
data
,
{
'exception'
:
'NotFoundExc'
,
'reason'
:
((
'Entry with path
%s
relative to '
'directory with sha1_git '
'66618d8be353ed3480476f032475e7c233eff737 not found.'
)
%
path
)})
File Metadata
Details
Attached
Mime Type
text/x-python
Expires
Sat, Jun 21, 7:44 PM (2 w, 6 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3340834
Attached To
rDWAPPS Web applications
Event Timeline
Log In to Comment