def setUp(self): self.search_pop_thm = Shelf.objects.create( title='Populâr themes', endpoint='search', criteria='?sort=users&type=statictheme', footer_text='See more populâr themes') self.search_hol_thm = Shelf.objects.create( title='Holidây themes', endpoint='search', criteria=('?category=holiday&sort=recommended%2Cusers' + '&type=statictheme&app=firefox'), footer_text='See more holidây themes') self.search_rec_thm = Shelf.objects.create( title='Recommended themes', endpoint='search', criteria='?promoted=recommended&sort=random&type=statictheme', footer_text='See more recommended themes') self.collections_shelf = Shelf.objects.create( title='Enhanced privacy extensions', endpoint='collections', criteria='privacy-matters', footer_text='See more enhanced privacy extensions') # Set up the request to support drf_reverse api_version = api_settings.DEFAULT_VERSION self.request = APIRequestFactory().get('/api/%s/' % api_version) self.request.versioning_scheme = ( api_settings.DEFAULT_VERSIONING_CLASS()) self.request.version = api_version self.request.user = AnonymousUser()
def calculate_etag(instance: models.Model) -> str: """ Calculate the MD5 hash of a resource representation in the API. The serializer for the model class is retrieved, and then used to construct the representation of the instance. Then, the representation is rendered to camelCase JSON, after which the MD5 hash is calculated of this result. """ model_class = type(instance) serializer_class = _get_serializer_for_models()[model_class] # build a dummy request with the configured domain, since we're doing STRONG # comparison. Required as context for hyperlinked serializers request = Request(StaticRequest()) request.version = api_settings.DEFAULT_VERSION request.versioning_scheme = api_settings.DEFAULT_VERSIONING_CLASS() serializer = serializer_class(instance=instance, context={"request": request}) # render the output to json, which is used as hash input renderer = CamelCaseJSONRenderer() rendered = renderer.render(serializer.data, "application/json") # calculate md5 hash return hashlib.md5(rendered).hexdigest()
def setUp(self): self.search_pop_thm = Shelf.objects.create( title='Populâr themes', endpoint='search', addon_type=amo.ADDON_STATICTHEME, criteria='?sort=users&type=statictheme', footer_text='See more populâr themes', footer_pathname='/themes/', ) self.search_rec_ext = Shelf.objects.create( title='Recommended extensions', endpoint='search', addon_type=amo.ADDON_EXTENSION, criteria='?promoted=recommended&sort=random&type=extension', footer_text='See more recommended extensions', ) self.collections_shelf = Shelf.objects.create( title='Enhanced privacy extensions', endpoint='collections', addon_type=amo.ADDON_EXTENSION, criteria='privacy-matters', footer_text='See more enhanced privacy extensions', footer_pathname='https://blog.mozilla.org/addons', ) # Set up the request to support drf_reverse api_version = api_settings.DEFAULT_VERSION self.request = APIRequestFactory().get('/api/%s/' % api_version) self.request.versioning_scheme = api_settings.DEFAULT_VERSIONING_CLASS( ) self.request.version = api_version self.request.user = AnonymousUser() self.request = DRFRequest(self.request)
def get_request(self, path, data=None, **extra): api_version = 'v5' # choose v5 to ignore 'l10n_flat_input_output' gate request = APIRequestFactory().get(f'/api/{api_version}{path}', data, **extra) request.versioning_scheme = (api_settings.DEFAULT_VERSIONING_CLASS()) request.version = api_version return request
def get_serializer(self, obj, **extra_context): api_version = api_settings.DEFAULT_VERSION request = APIRequestFactory().get('/api/%s/' % api_version) request.versioning_scheme = api_settings.DEFAULT_VERSIONING_CLASS() request.version = api_version extra_context.setdefault('request', request) return FileEntriesSerializer(instance=obj, context=extra_context)
def drf_url(context, viewname, *args, **kwargs): """Helper for DjangoRestFramework's ``reverse`` in templates.""" request = context.get('request') if request: if not hasattr(request, 'versioning_scheme'): request.versioning_scheme = api_settings.DEFAULT_VERSIONING_CLASS() request.version = request.versioning_scheme.determine_version( request, *args, **kwargs) return drf_reverse(viewname, request=request, args=args, kwargs=kwargs)
def get_serializer(self, **extra_context): api_version = api_settings.DEFAULT_VERSION request = APIRequestFactory().get('/api/%s/' % api_version) request.versioning_scheme = api_settings.DEFAULT_VERSIONING_CLASS() request.version = api_version extra_context.setdefault('request', request) return AddonBrowseVersionSerializerFileOnly(instance=self.version, context=extra_context)
def setUp(self): self.criteria_sea = '?promoted=recommended&sort=random&type=extension' self.criteria_col = 'password-managers' self.criteria_col_404 = 'passwordmanagers' self.criteria_not_200 = '?sort=user&type=extension' self.criteria_empty = '?sort=users&type=theme' api_version = api_settings.DEFAULT_VERSION self.request = APIRequestFactory().get('/api/%s/' % api_version) self.request.versioning_scheme = api_settings.DEFAULT_VERSIONING_CLASS( ) self.request.version = api_version self.request.user = AnonymousUser() responses.add( responses.GET, reverse_ns('addon-search') + self.criteria_sea, status=200, json={'count': 103}, ) responses.add( responses.GET, reverse_ns( 'collection-addon-list', kwargs={ 'user_pk': settings.TASK_USER_ID, 'collection_slug': self.criteria_col, }, ), status=200, json={'count': 1}, ) responses.add( responses.GET, reverse_ns( 'collection-addon-list', kwargs={ 'user_pk': settings.TASK_USER_ID, 'collection_slug': self.criteria_col_404, }, ), status=404, json={'detail': 'Not found.'}, ), responses.add( responses.GET, reverse_ns('addon-search') + self.criteria_not_200, status=400, json=['Invalid "sort" parameter.'], ) responses.add( responses.GET, reverse_ns('addon-search') + self.criteria_empty, status=200, json={'count': 0}, )
def get_applicatie_serializer(applicatie: Applicatie, request: HttpRequest) -> ApplicatieSerializer: """ Wrap the request into a DRF-suitable request. """ request = Request(request) scheme = api_settings.DEFAULT_VERSIONING_CLASS() request.version, request.versioning_scheme = ( scheme.determine_version(request, version=api_settings.DEFAULT_VERSION), scheme, ) serializer = ApplicatieSerializer(instance=applicatie, context={"request": request}) return serializer
def setUp(self): super(TestAddonBrowseVersionSerializer, self).setUp() license = License.objects.create( name={ 'en-US': u'My License', 'fr': u'Mä Licence', }, text={ 'en-US': u'Lorem ipsum dolor sit amet, has nemore patrioqué', }, url='http://license.example.com/' ) self.addon = addon_factory( file_kw={ 'hash': 'fakehash', 'is_mozilla_signed_extension': True, 'platform': amo.PLATFORM_ALL.id, 'size': 42, 'filename': 'notify-link-clicks-i18n.xpi', 'is_webextension': True }, version_kw={ 'license': license, 'min_app_version': '50.0', 'max_app_version': '*', 'release_notes': { 'en-US': u'Release notes in english', 'fr': u'Notes de version en français', }, 'reviewed': self.days_ago(0), } ) extract_version_to_git(self.addon.current_version.pk) self.addon.current_version.reload() assert self.addon.current_version.release_notes self.version = self.addon.current_version # Set up the request to support drf_reverse api_version = api_settings.DEFAULT_VERSION self.request = APIRequestFactory().get('/api/%s/' % api_version) self.request.versioning_scheme = ( api_settings.DEFAULT_VERSIONING_CLASS() ) self.request.version = api_version
def setUp(self): super(TestAddonCompareVersionSerializer, self).setUp() self.addon = addon_factory( name='My Addôn', slug='my-addon', file_kw={'filename': 'webextension_no_id.xpi', 'is_webextension': True}, ) extract_version_to_git(self.addon.current_version.pk) self.addon.current_version.refresh_from_db() self.version = self.addon.current_version # Set up the request to support drf_reverse api_version = api_settings.DEFAULT_VERSION self.request = APIRequestFactory().get('/api/%s/' % api_version) self.request.versioning_scheme = api_settings.DEFAULT_VERSIONING_CLASS() self.request.version = api_version
def setUp(self): self.search_shelf = Shelf.objects.create( title='Populâr themes', endpoint='search', criteria='?sort=users&type=statictheme', footer_text='See more populâr themes') self.collections_shelf = Shelf.objects.create( title='Enhanced privacy extensions', endpoint='collections', criteria='privacy-matters', footer_text='See more enhanced privacy extensions') # Set up the request to support drf_reverse api_version = api_settings.DEFAULT_VERSION self.request = APIRequestFactory().get('/api/%s/' % api_version) self.request.versioning_scheme = ( api_settings.DEFAULT_VERSIONING_CLASS()) self.request.version = api_version
def reverse_ns(viewname, api_version=None, args=None, kwargs=None, **extra): """An API namespace aware reverse to be used in DRF API based tests. It works by creating a fake request from the API version you need, and then setting the version so the un-namespaced viewname from DRF is resolved into the namespaced viewname used interally by django. Unless overriden with the api_version parameter, the API version used is the DEFAULT_VERSION in settings. e.g. reverse_ns('addon-detail') is resolved to reverse('v4:addon-detail') if the api version is 'v4'. """ api_version = api_version or api_settings.DEFAULT_VERSION request = req_factory_factory('/api/%s/' % api_version) request.versioning_scheme = api_settings.DEFAULT_VERSIONING_CLASS() request.version = api_version return drf_reverse( viewname, args=args or [], kwargs=kwargs or {}, request=request, **extra)
def setUp(self): super().setUp() self.addon = addon_factory( file_kw={'filename': 'notify-link-clicks-i18n.xpi'}) extract_version_to_git(self.addon.current_version.pk) self.addon.current_version.refresh_from_db() extract_version_to_git(self.addon.current_version.pk) self.addon.current_version.reload() assert self.addon.current_version.file.filename == 'notify-link-clicks-i18n.xpi' self.version = self.addon.current_version self.file = self.addon.current_version.file # Set up the request to support drf_reverse api_version = api_settings.DEFAULT_VERSION self.request = APIRequestFactory().get('/api/%s/' % api_version) self.request.versioning_scheme = api_settings.DEFAULT_VERSIONING_CLASS( ) self.request.version = api_version
def versioned_reverse(view, version='v1', **kwargs): factory = APIRequestFactory() request = factory.options('/') request.versioning_scheme = api_settings.DEFAULT_VERSIONING_CLASS() request.version = version return reverse(view, request=request, **kwargs)
def setUp(self): api_version = api_settings.DEFAULT_VERSION self.request = APIRequestFactory().get('/api/%s/' % api_version) self.request.versioning_scheme = api_settings.DEFAULT_VERSIONING_CLASS( ) self.request.version = api_version