class ResourceTestCase(URLReverseMixin, unittest.TestCase): def setUp(self): self.site = ResourceSite() self.site.register_builtin_media_types() self.user = User.objects.get_or_create(username='******', is_staff=True, is_active=True, is_superuser=True)[0] self.resource = self.register_resource() self.factory = SuperUserRequestFactory(user=self.user, HTTP_ACCEPT='text/html') self.resolver = GenericURLResolver(r'^', self.site.get_urls()) self.patch_reverse(self.resolver) def tearDown(self): self.unpatch_reverse() def get_api_request(self, **kwargs): kwargs.setdefault('site', self.site) kwargs.setdefault('user', self.user) kwargs.setdefault('params', {}) kwargs.setdefault('method', 'GET') kwargs.setdefault('payload', {}) kwargs.setdefault('request', self.factory.get('/')) api_request = InternalAPIRequest(**kwargs) api_request.generate_response = MagicMock(return_value=HttpResponse()) return api_request def register_resource(self): raise NotImplementedError
def test_install_from_admin_site(self): site = ResourceSite() admin.autodiscover() site.install_models_from_site(admin.site) self.assertTrue(site.registry)
class ResourceTestCase(unittest.TestCase): def setUp(self): self.site = ResourceSite(apirequest_class=HTTPAPIKeyRequest) self.site.register_builtin_media_types() self.user = User.objects.get_or_create(username='******', is_staff=True, is_active=True, is_superuser=True)[0] self.resource = self.register_resource() self.api_key_instance = ApiKey.objects.get_or_create(user=self.user, key='foobar')[0] self.api_key = self.api_key_instance.key self.factory = RequestFactory(HTTP_ACCEPT='text/html', API_KEY=self.api_key) self.resolver = GenericURLResolver(r'^', self.site.get_urls()) def reverse(name, *args, **kwargs): ret = self.resolver.reverse(name, *args, **kwargs) return ret self.reverse = reverse def cls_reverse(slf, name, *args, **kwargs): return self.resolver.reverse(name, *args, **kwargs) original_fork = self.site.fork def fork(**kwargs): ret = original_fork(**kwargs) ret.reverse = reverse return ret self.site.fork = fork NamespaceAPIRequest.reverse = cls_reverse RootEndpoint.reverse = cls_reverse self.site.reverse = reverse def get_api_request(self, **kwargs): kwargs.setdefault('site', self.site) #kwargs.setdefault('user', self.user) #kwargs.setdefault('params', {}) #kwargs.setdefault('method', 'GET') #kwargs.setdefault('payload', {}) kwargs.setdefault('request', self.factory.get('/')) kwargs.setdefault('url_args', []) kwargs.setdefault('url_kwargs', {}) #kwargs.setdefault('reverse', self.reverse) api_request = HTTPAPIKeyRequest(**kwargs) api_request.populate_session_data_from_request(kwargs['request']) api_request.generate_response = MagicMock(return_value=HttpResponse()) return api_request def register_resource(self): raise NotImplementedError
def setUp(self): self.site = ResourceSite() self.site.register_builtin_media_types() self.user = User.objects.get_or_create(username='******', is_staff=True, is_active=True, is_superuser=True)[0] self.resource = self.register_resource() self.factory = SuperUserRequestFactory(user=self.user, HTTP_ACCEPT='text/html') self.resolver = GenericURLResolver(r'^', self.site.get_urls()) self.patch_reverse(self.resolver)
def setUp(self): self.site = ResourceSite(apirequest_class=HTTPAPIKeyRequest) self.site.register_builtin_media_types() self.user = User.objects.get_or_create(username='******', is_staff=True, is_active=True, is_superuser=True)[0] self.resource = self.register_resource() self.api_key_instance = ApiKey.objects.get_or_create(user=self.user, key='foobar')[0] self.api_key = self.api_key_instance.key self.factory = RequestFactory(HTTP_ACCEPT='text/html', API_KEY=self.api_key) self.resolver = GenericURLResolver(r'^', self.site.get_urls()) def reverse(name, *args, **kwargs): ret = self.resolver.reverse(name, *args, **kwargs) return ret self.reverse = reverse def cls_reverse(slf, name, *args, **kwargs): return self.resolver.reverse(name, *args, **kwargs) original_fork = self.site.fork def fork(**kwargs): ret = original_fork(**kwargs) ret.reverse = reverse return ret self.site.fork = fork NamespaceAPIRequest.reverse = cls_reverse RootEndpoint.reverse = cls_reverse self.site.reverse = reverse
class ResourceTestCase(unittest.TestCase): def setUp(self): self.site = ResourceSite() self.site.register_builtin_media_types() self.user = User.objects.get_or_create(username='******', is_staff=True, is_active=True, is_superuser=True)[0] self.resource = self.register_resource() self.factory = SuperUserRequestFactory(user=self.user, HTTP_ACCEPT='application/vnd.Collection+JSON') self.resolver = GenericURLResolver(r'^', self.site.get_urls()) def reverse(name, *args, **kwargs): ret = self.resolver.reverse(name, *args, **kwargs) return ret self.site.reverse = reverse def register_resource(self): raise NotImplementedError
def setUp(self): self.site = ResourceSite() self.site.register_builtin_media_types() self.user = User.objects.get_or_create(username='******', is_staff=True, is_active=True, is_superuser=True)[0] self.resource = self.register_resource() self.factory = SuperUserRequestFactory(user=self.user, HTTP_ACCEPT='text/html') self.resolver = GenericURLResolver(r'^', self.site.get_urls()) def reverse(name, *args, **kwargs): ret = self.resolver.reverse(name, *args, **kwargs) return ret original_fork = self.site.fork def fork(**kwargs): ret = original_fork(**kwargs) ret.reverse = reverse return ret self.site.fork = fork self.site.reverse = reverse