Skip to content

Commit f790d58

Browse files
committed
Merge pull request #33 from atin65536/master
fix GET 405 to GET 404 on unexisting folder.
2 parents 43e9caf + d755206 commit f790d58

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

.travis.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ python:
44
- "2.6"
55
env:
66
- DJANGO_VERSION=1.6.11
7-
- DJANGO_VERSION=1.5.12
87
- DJANGO_VERSION=1.7.7
98
matrix:
109
exclude:
1110
- python: "2.6"
1211
env: DJANGO_VERSION=1.7.7
13-
install: pip install -q Django==$DJANGO_VERSION djangorestframework
12+
install: pip install -q Django==$DJANGO_VERSION djangorestframework==3.2.5
1413
script: python setup.py test

djangodav/auth/rest.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
from django.utils.decorators import method_decorator
2323
from django.views.decorators.csrf import csrf_exempt
2424
from djangodav.responses import HttpResponseUnAuthorized
25-
from rest_framework.exceptions import APIException
25+
26+
try:
27+
import rest_framework
28+
from rest_framework.exceptions import APIException
29+
except ImportError:
30+
rest_framework = None
2631

2732

2833
class RequestWrapper(object):
@@ -38,6 +43,7 @@ class RestAuthViewMixIn(object):
3843

3944
@method_decorator(csrf_exempt)
4045
def dispatch(self, request, *args, **kwargs):
46+
assert rest_framework is not None, "django rest framework is not installed."
4147
if request.method.lower() != 'options':
4248
user_auth_tuple = None
4349
for auth in self.authentications:

djangodav/auth/tests.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@
33

44
from django.test import TestCase
55
from django.test.client import RequestFactory
6+
try:
7+
from django.utils.unittest import skipUnless
8+
except ImportError:
9+
from unittest import skipUnless
610

711
from djangodav.views import DavView
812
from djangodav.fs.resources import DummyReadFSDavResource
913
from djangodav.auth.rest import RestAuthViewMixIn
1014

11-
from rest_framework.authentication import SessionAuthentication as RestSessionAuthentication, \
15+
try:
16+
import rest_framework
17+
from rest_framework.authentication import SessionAuthentication as RestSessionAuthentication, \
1218
BasicAuthentication as RestBasicAuthentication
19+
except ImportError:
20+
rest_framework = None
21+
1322
from djangodav.responses import HttpResponseUnAuthorized
1423
from django.contrib.auth import get_user_model
1524
from djangodav.locks import DummyLock
@@ -35,16 +44,20 @@ def setUp(self):
3544
self.user.set_password('test')
3645
self.user.save()
3746

47+
@skipUnless(rest_framework, "required Django Rest Framework")
3848
def assertIsAuthorized(self, response):
3949
self.assertIsInstance(response, HttpResponse)
4050
self.assertNotIsInstance(response, HttpResponseUnAuthorized)
4151

52+
@skipUnless(rest_framework, "required Django Rest Framework")
4253
def assertIsNotAuthorized(self, response):
4354
self.assertIsInstance(response, HttpResponseUnAuthorized)
4455

56+
@skipUnless(rest_framework, "required Django Rest Framework")
4557
def assertHasAuthenticateHeader(self, response):
4658
self.assertEqual(response['WWW-Authenticate'], 'Basic realm="api"')
4759

60+
@skipUnless(rest_framework, "required Django Rest Framework")
4861
def test_auth_session(self):
4962
""" test whether we can authenticate through Django session """
5063

@@ -66,6 +79,7 @@ class RestAuthDavView(TestDAVView):
6679
response = v(request, '/')
6780
self.assertIsAuthorized(response)
6881

82+
@skipUnless(rest_framework, "required Django Rest Framework")
6983
def test_auth_basic(self):
7084
""" test whether we can authenticate through Basic auth """
7185

@@ -90,6 +104,7 @@ class RestAuthDavView(TestDAVView):
90104
response = v(request, '/')
91105
self.assertIsAuthorized(response)
92106

107+
@skipUnless(rest_framework, "required Django Rest Framework")
93108
def test_auth_multiple(self):
94109
""" test whether we can authenticate through either Session or Basic auth """
95110

djangodav/views/tests.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -262,24 +262,24 @@ def test_dispatch(self):
262262
def test_allowed_object(self):
263263
v = DavView()
264264
v.__dict__['resource'] = self.sub_object
265-
self.assertListEqual(v._allowed_methods(), ['HEAD', 'OPTIONS', 'PROPFIND', 'LOCK', 'UNLOCK', 'GET', 'DELETE', 'PROPPATCH', 'COPY', 'MOVE', 'PUT'])
265+
self.assertListEqual(v._allowed_methods(), ['HEAD', 'OPTIONS', 'PROPFIND', 'LOCK', 'UNLOCK', 'GET', 'DELETE', 'PROPPATCH', 'COPY', 'MOVE', 'PUT', 'MKCOL'])
266266

267267
def test_allowed_collection(self):
268268
v = DavView()
269269
v.__dict__['resource'] = self.top_collection
270-
self.assertListEqual(v._allowed_methods(), ['HEAD', 'OPTIONS', 'PROPFIND', 'LOCK', 'UNLOCK', 'GET', 'DELETE', 'PROPPATCH', 'COPY', 'MOVE'])
270+
self.assertListEqual(v._allowed_methods(), ['HEAD', 'OPTIONS', 'PROPFIND', 'LOCK', 'UNLOCK', 'GET', 'DELETE', 'PROPPATCH', 'COPY', 'MOVE', 'PUT', 'MKCOL'])
271271

272272
def test_allowed_missing_collection(self):
273273
v = DavView()
274274
parent = MockCollection('/path/to/obj')
275275
v.__dict__['resource'] = MissingMockCollection('/path/', get_parent=Mock(return_value=parent))
276-
self.assertListEqual(v._allowed_methods(), ['HEAD', 'OPTIONS', 'PROPFIND', 'LOCK', 'UNLOCK', 'GET', 'PUT', 'MKCOL'])
276+
self.assertListEqual(v._allowed_methods(), ['HEAD', 'OPTIONS', 'PROPFIND', 'LOCK', 'UNLOCK', 'GET', 'DELETE', 'PROPPATCH', 'COPY', 'MOVE', 'PUT', 'MKCOL'])
277277

278278
def test_allowed_missing_parent(self):
279279
v = DavView()
280280
parent = MissingMockCollection('/path/to/obj')
281281
v.__dict__['resource'] = MissingMockCollection('/path/', get_parent=Mock(return_value=parent))
282-
self.assertEqual(v._allowed_methods(), [])
282+
self.assertListEqual(v._allowed_methods(), ['HEAD', 'OPTIONS', 'PROPFIND', 'LOCK', 'UNLOCK', 'GET', 'DELETE', 'PROPPATCH', 'COPY', 'MOVE', 'PUT', 'MKCOL'])
283283

284284
def test_options_root(self):
285285
path = '/'
@@ -384,7 +384,7 @@ def test_put_collection(self):
384384
request = HttpRequest()
385385
resp = v.put(request, path)
386386
self.assertFalse(self.sub_collection.write.called)
387-
self.assertEqual(403, resp.status_code)
387+
self.assertEqual(405, resp.status_code)
388388

389389
def test_mkcol_new(self):
390390
path = '/collection/missing_sub_collection'

djangodav/views/views.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
PATTERN_IF_DELIMITER = re.compile(r'(<([^>]+)>)|(\(([^\)]+)\))')
2727

28-
2928
class DavView(View):
3029
resource_class = None
3130
lock_class = None
@@ -95,15 +94,11 @@ def options(self, request, path, *args, **kwargs):
9594
return response
9695

9796
def _allowed_methods(self):
98-
allowed = ['HEAD', 'OPTIONS', 'PROPFIND', 'LOCK', 'UNLOCK']
99-
if not self.resource.exists:
100-
parent = self.resource.get_parent()
101-
if not (parent.is_collection and parent.exists):
102-
return []
103-
return allowed + ['GET', 'PUT', 'MKCOL']
104-
allowed += ['GET', 'DELETE', 'PROPPATCH', 'COPY', 'MOVE']
105-
if self.resource.is_object:
106-
allowed += ['PUT']
97+
allowed = [
98+
'HEAD', 'OPTIONS', 'PROPFIND', 'LOCK', 'UNLOCK',
99+
'GET', 'DELETE', 'PROPPATCH', 'COPY', 'MOVE', 'PUT', 'MKCOL',
100+
]
101+
107102
return allowed
108103

109104
def get_access(self, resource):
@@ -207,9 +202,9 @@ def head(self, request, path, *args, **kwargs):
207202
def put(self, request, path, *args, **kwargs):
208203
parent = self.resource.get_parent()
209204
if not parent.exists:
210-
raise Http404("Resource doesn't exists")
205+
return HttpResponseConflict("Resource doesn't exists")
211206
if self.resource.is_collection:
212-
return self.no_access()
207+
return HttpResponseNotAllowed(list(set(self._allowed_methods()) - set(['MKCOL', 'PUT'])))
213208
if not self.resource.exists and not self.has_access(parent, 'write'):
214209
return self.no_access()
215210
if self.resource.exists and not self.has_access(self.resource, 'write'):
@@ -235,7 +230,7 @@ def delete(self, request, path, *args, **kwargs):
235230

236231
def mkcol(self, request, path, *args, **kwargs):
237232
if self.resource.exists:
238-
return HttpResponseNotAllowed(self._allowed_methods())
233+
return HttpResponseNotAllowed(list(set(self._allowed_methods()) - set(['MKCOL', 'PUT'])))
239234
if not self.resource.get_parent().exists:
240235
return HttpResponseConflict()
241236
length = request.META.get('CONTENT_LENGTH', 0)

0 commit comments

Comments
 (0)