我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用rest_framework.status.is_success()。
def check_permission(self, perm, url, method): user = UserFactory() self.client.force_authenticate(user) r = getattr(self.client, method)(url) self.assertEqual( r.status_code, status.HTTP_403_FORBIDDEN, prettify_response('Request should be forbidden', r) ) user.delete() user_with_perm = UserFactory(permissions=[perm]) self.client.force_authenticate(user_with_perm) r = getattr(self.client, method)(url) self.assertTrue( status.is_success(r.status_code), prettify_response('Request should be successful', r) ) user_with_perm.delete()
def check_context_processor(self, url): response = self.client.get(url) self.assertTrue(status.is_success(response.status_code), response.data) html = response.content.decode('utf-8') token = self.extract_token(html) expected_html = """ <html> <p>conditional is true</p> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> <p>user is authenticated</p> <p>csrf is {csrf_token}</p> <p>username is testuser</p> <p>username is testuser</p> </html> """.format(csrf_token=token) self.assertHTMLEqual(expected_html, html)
def post(self, request, *args, **kwargs): responses = [] requests_factory = BatchRequestsFactory(request) for current_request in requests_factory: handler = BaseHandler() handler.load_middleware() response = handler.get_response(current_request) result = { 'code': response.status_code, 'headers': [ {'name': key, 'value': value} for key, value in response._headers.values() ], 'body': response.content.decode('utf-8'), } if is_success(response.status_code): result['_data'] = json.loads(result['body']) if not is_success(response.status_code) or \ is_success(response.status_code) and not current_request.omit_response_on_success: result['return_body'] = True if current_request.name: requests_factory.named_responses[current_request.name] = result responses.append({k: v for k, v in result.items() if not k.startswith('_')}) if not is_success(response.status_code): # todo: handle requests dependencies break return self.finalize_response(request, Response(responses))
def test_list_endpoint(self): url = reverse(self.base_name + self.list_suffix) self.client.force_authenticate(self.user) if self.check_view_perm: self.check_permission(self.get_permission('view'), url, 'get') r = self.client.get(url) self.assertTrue( status.is_success(r.status_code), prettify_response('Request should be successful', r) ) self.compare_objects(self.model_cls.objects.all(), r.data)
def test_retrieve_endpoint(self): lookup = getattr(self.object, self.lookup_field) url = reverse(self.base_name + self.detail_suffix, args=(lookup,)) self.client.force_authenticate(self.user) if self.check_view_perm: self.check_permission(self.get_permission('view'), url, 'get') r = self.client.get(url) self.assertTrue( status.is_success(r.status_code), prettify_response('Request should be successful', r) ) self.compare_object(self.object, r.data)
def test_list_endpoint(self): url = reverse(self.base_name + self.list_suffix) self.client.force_authenticate(self.user) r = self.client.get(url) self.assertTrue( status.is_success(r.status_code), prettify_response('Request should be successful', r) ) self.compare_objects(self.model_cls.objects.filter(is_superuser=False), r.data)
def __bool__(self): return self.is_success()
def is_success(self): return drf_status.is_success(self.status)
def _get(category, cls, request, *args): boss = cls() boss.request = request # needed for check_role() to work resp = boss.get(request, *args) if not status.is_success(resp.status_code): category_name = args[-1] if len(args) > 0 else None return (None, error_response(request, resp, category, category_name)) return (resp.data, None)
def _del(category, cls, request, *args): boss = cls() boss.request = request # needed for check_role() to work resp = boss.delete(request, *args) if not status.is_success(resp.status_code): return error_message(resp) return None
def _post(category, cls, request, data, *args): boss = cls() boss.request = request # needed for check_role() to work if data: boss.request.data = data # simulate the DRF request object resp = boss.post(request, *args) if not status.is_success(resp.status_code): return error_message(resp) return None
def _put(category, cls, request, data, *args): boss = cls() boss.request = request # needed for check_role() to work if data: boss.request.data = data # simulate the DRF request object resp = boss.put(request, *args) if not status.is_success(resp.status_code): return error_message(resp) return None
def _patch(category, cls, request, data, *args): boss = cls() boss.request = request # needed for check_role() to work if data: boss.request.data = data # simulate the DRF request object resp = boss.patch(request, *args) if not status.is_success(resp.status_code): return error_message(resp) return None
def check_html(self, url, expected_html): response = self.client.get(url) self.assertTrue(status.is_success(response.status_code), response.data) html = response.content.decode('utf-8') self.assertHTMLEqual(expected_html, html)