本文整理匯總了Python中rest_framework.status.HTTP_405_METHOD_NOT_ALLOWED屬性的典型用法代碼示例。如果您正苦於以下問題:Python status.HTTP_405_METHOD_NOT_ALLOWED屬性的具體用法?Python status.HTTP_405_METHOD_NOT_ALLOWED怎麽用?Python status.HTTP_405_METHOD_NOT_ALLOWED使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類rest_framework.status
的用法示例。
在下文中一共展示了status.HTTP_405_METHOD_NOT_ALLOWED屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_forbidden_actions
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def test_forbidden_actions(self):
"""
<api root>/sites/ should not permit POST, PUT or DELETE operations
"""
url = urljoin(urlroot, 'sites/securethe.news/')
response1 = self.client.post(
url, json={'name': 'Insecure the News?',
'domain': 'insecurethe.news'})
self.assertEqual(response1.status_code,
status.HTTP_405_METHOD_NOT_ALLOWED)
response2 = self.client.delete(url)
self.assertEqual(response2.status_code,
status.HTTP_405_METHOD_NOT_ALLOWED)
url = urljoin(urlroot, 'sites/insecurethe.news/')
response3 = self.client.put(
url, json={'name': 'Insecure the News?',
'domain': 'insecurethe.news'})
self.assertEqual(response3.status_code,
status.HTTP_405_METHOD_NOT_ALLOWED)
示例2: test_delete
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def test_delete(self):
"""Test that deleting users is not a semantically valid action"""
url = "/api/users/{0!s}"
all_users = self.users
expected_results = defaultdict(lambda: {
'status': [status.HTTP_405_METHOD_NOT_ALLOWED,
status.HTTP_403_FORBIDDEN],
'reason': "Default response should be no access"})
for user in all_users:
with self.subTest(
user=user,
expected_statuses=expected_results[(user,)]['status'],
reason=expected_results[(user,)]['reason']):
user_that_exists = DataCreationUtilities.create_test_user()
response = self.clients[user].delete(
url.format(user_that_exists['id']))
logging.debug(response)
self.assertIn(
response.status_code, expected_results[(user,)]['status'])
示例3: membership
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def membership(self, request, **kwargs):
if request.method == 'POST':
email = request.data.get('email', None)
if not email:
return Response('email required', status=400)
if User.objects.filter(email=email).exists():
return Response('user already has account', status=400)
family = request.user.membership.family
family.invite_user_to_family(email=email)
return Response('user invited', status=201)
elif request.method == 'GET':
family = request.user.membership.family
members = Membership.objects \
.select_related('user') \
.filter(family=family)
data = MembershipSerializer(members, many=True).data
return Response(data)
else:
return Response('Method not allowed', status=status.HTTP_405_METHOD_NOT_ALLOWED)
示例4: follow
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def follow(self, request, pk, **kwargs):
obj = self.queryset.get(id=int(pk))
#follow, created = Follow.objects.get_or_create(request.user, obj)
can_change_follow = True
if hasattr(self, 'can_change_follow'):
can_change_follow = self.can_change_follow(request.user, obj)
if can_change_follow:
if request.method == 'DELETE':
if Follow.objects.is_following(request.user, obj):
fol = follow.utils.unfollow(request.user, obj)
signals.unfollow.send(FollowingModelViewSet, follower=request.user, followee=obj)
return Response(status=status.HTTP_205_RESET_CONTENT)
elif request.method == 'POST':
if not Follow.objects.is_following(request.user, obj):
fol = follow.utils.follow(request.user, obj)
signals.follow.send(FollowingModelViewSet, follower=request.user, followee=obj)
return Response(status=status.HTTP_201_CREATED)
else:
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
示例5: test_cannot_create_submission
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def test_cannot_create_submission(self):
v_uid = self.asset.latest_deployed_version.uid
submission = {
"q1": "a5",
"q2": "a6",
}
# Owner
response = self.client.post(self.submission_url, data=submission)
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
# Shared
self._share_with_another_user()
self._log_in_as_another_user()
response = self.client.post(self.submission_url, data=submission)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
# Anonymous
self.client.logout()
response = self.client.post(self.submission_url, data=submission)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
示例6: test_active_user
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def test_active_user(self):
'''
Try to activate a user, where only staff is allowed
'''
response = self.client.post('/api/account/register/', self.userData, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED) # Check if response is created
user = User.objects.get(username=self.userName)
#Unauthenticated
response = self.client.post('/api/account/activateUser/', { "email":user.email}, format='json')
#self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # Check if response is not authorized
#Normal user authenticated
self.client.force_authenticate(self.simpleUser)
response = self.client.post('/api/account/activateUser/', {"email": user.email}, format='json')
#self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # Check if response is not authorized
self.client.logout()
#Staff authenticated
self.client.force_authenticate(self.admin)
response = self.client.post('/api/account/activateUser/', {"email": user.email}, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK) # Check if response is ok
response = self.client.post('/api/account/activateUser/', {"email": user.email}, format='json')
#self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) # Check if response is method is not allowed
示例7: test_add_access_with_permission
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def test_add_access_with_permission(self):
test_user = "user"
u = User.objects.create_user(test_user, "", test_user)
p = Permission.objects.get(codename="add_vendor")
assert p is not None
u.user_permissions.add(p)
u.save()
assert u.has_perm("productdb.add_vendor")
client = APIClient()
client.login(username=test_user, password=test_user)
response = client.post(REST_VENDOR_LIST, data={"name": "Awesome Vendor"})
assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED, "API endpoint is always read only"
assert response.json() == {'detail': 'Method "POST" not allowed.'}
assert Vendor.objects.count() == 3, "no additional vendor is created"
示例8: test_change_access_with_permission
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def test_change_access_with_permission(self):
# create a user with permissions
test_user = "user"
u = User.objects.create_user(test_user, "", test_user)
p = Permission.objects.get(codename="change_vendor")
assert p is not None
u.user_permissions.add(p)
u.save()
assert u.has_perm("productdb.change_vendor")
client = APIClient()
client.login(username=test_user, password=test_user)
response = client.put(REST_VENDOR_DETAIL % 1, data={"name": "renamed vendor"})
assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED, "API endpoint is always read only"
assert response.json() == {'detail': 'Method "PUT" not allowed.'}
示例9: test_delete_access_with_permission
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def test_delete_access_with_permission(self):
test_user = "user"
u = User.objects.create_user(test_user, "", test_user)
p = Permission.objects.get(codename="delete_vendor")
assert p is not None
u.user_permissions.add(p)
u.save()
assert u.has_perm("productdb.delete_vendor")
client = APIClient()
client.login(username=test_user, password=test_user)
response = client.delete(REST_VENDOR_DETAIL % 1)
assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED, "API endpoint is always read only"
assert response.json() == {'detail': 'Method "DELETE" not allowed.'}
assert Vendor.objects.count() == 3, "no vendor was deleted"
示例10: test_model_creation
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def test_model_creation(admin_api_client, admin_user, model_name):
model_data = setup_model(model_name, admin_user)
response = admin_api_client.post('/collab/{}/'.format(model_name),
data=model_data,
HTTP_ACCEPT='application/json')
# Manually handle matches, where API does not allow creation or
# modification of objects, as they're read only
if model_name == "matches":
assert_response(response, status.HTTP_405_METHOD_NOT_ALLOWED)
return
assert_response(response, status.HTTP_201_CREATED)
projects_created = [response.data]
response = admin_api_client.get('/collab/{}/'.format(model_name),
HTTP_ACCEPT="application/json")
assert_response(response, status.HTTP_200_OK, projects_created)
示例11: test_post
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def test_post(self):
"""
POST requests should throw a 405 Error
"""
view = BasicObjectView.as_view()
data = {'fake': 'data'}
request = factory.post('/', data, format='json')
with self.assertNumQueries(0):
response = view(request).render()
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
self.assertEqual(response.data, {"detail": 'Method "POST" not allowed.'})
示例12: test_put
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def test_put(self):
"""
PUT requests should throw a 405 Error
"""
view = BasicObjectView.as_view()
data = {'fake': 'data'}
request = factory.put('/', data, format='json')
with self.assertNumQueries(0):
response = view(request).render()
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
self.assertEqual(response.data, {"detail": 'Method "PUT" not allowed.'})
示例13: test_delete
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def test_delete(self):
"""
DELETE requests should throw a 405 Error
"""
view = BasicObjectView.as_view()
request = factory.delete('/')
with self.assertNumQueries(0):
response = view(request).render()
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
self.assertEqual(response.data, {"detail": 'Method "DELETE" not allowed.'})
示例14: test_post
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def test_post(self):
"""
POST requests should throw a 405 Error
"""
view = BasicFlatView.as_view()
data = {'fake': 'data'}
request = factory.post('/', data, format='json')
with self.assertNumQueries(0):
response = view(request).render()
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
self.assertEqual(response.data, {"detail": 'Method "POST" not allowed.'})
示例15: test_put
# 需要導入模塊: from rest_framework import status [as 別名]
# 或者: from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED [as 別名]
def test_put(self):
"""
PUT requests should throw a 405 Error
"""
view = BasicFlatView.as_view()
data = {'fake': 'data'}
request = factory.put('/', data, format='json')
with self.assertNumQueries(0):
response = view(request).render()
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
self.assertEqual(response.data, {"detail": 'Method "PUT" not allowed.'})