def test_previous_compatible_apps_initially_selected(self): addon = addon_factory() appversion = AppVersion.objects.create(application=amo.ANDROID.id, version='1.0') ApplicationsVersions.objects.create( version=addon.current_version, application=amo.ANDROID.id, min=appversion, max=appversion, ) upload = FileUpload.objects.create(valid=False) data = {'upload': upload.uuid} request = req_factory_factory('/', post=True, data=data) request.user = user_factory() # Without an add-on, we only pre-select the default which is Firefox form = forms.NewUploadForm(data, request=request) assert form.fields['compatible_apps'].initial == [amo.FIREFOX.id] # with an add-on provided we pre-select the applications based on the # current version form = forms.NewUploadForm(data, request=request, addon=addon) assert form.fields['compatible_apps'].initial == [ amo.FIREFOX.id, amo.ANDROID.id, ]
def test_only_valid_uploads(self): upload = FileUpload.objects.create(valid=False) upload = FileUpload.objects.create(valid=False) data = {'upload': upload.uuid, 'compatible_apps': [amo.FIREFOX.id]} request = req_factory_factory('/', post=True, data=data) request.user = user_factory() form = forms.NewUploadForm(data, request=request) assert ('There was an error with your upload. Please try again.' in form.errors.get('__all__')), (form.errors) # Admin override makes the form ignore the brokenness with mock.patch('olympia.access.acl.action_allowed_user') as acl: # For the 'Addons:Edit' permission check. acl.return_value = True data['admin_override_validation'] = True form = forms.NewUploadForm(data, request=request) assert ('There was an error with your upload. Please try' not in form.errors.get('__all__')), form.errors upload.validation = '{"errors": 0}' upload.save() addon = Addon.objects.create() data.pop('admin_override_validation') form = forms.NewUploadForm(data, request=request, addon=addon) assert ('There was an error with your upload. Please try again.' not in form.errors.get('__all__')), form.errors
def test_only_valid_uploads(self): upload = FileUpload.objects.create(valid=False) form = forms.NewUploadForm( {'upload': upload.uuid, 'supported_platforms': [1]}, request=mock.Mock()) assert ('There was an error with your upload. Please try again.' in form.errors.get('__all__')), form.errors # Admin override makes the form ignore the brokenness with mock.patch('olympia.access.acl.action_allowed_user') as acl: # For the 'Addons:Edit' permission check. acl.return_value = True form = forms.NewUploadForm( {'upload': upload.uuid, 'supported_platforms': [1], 'admin_override_validation': True}, request=mock.Mock()) assert ('There was an error with your upload. Please try' not in form.errors.get('__all__')), form.errors upload.validation = '{"errors": 0}' upload.save() addon = Addon.objects.create() form = forms.NewUploadForm( {'upload': upload.uuid, 'supported_platforms': [1]}, addon=addon, request=mock.Mock()) assert ('There was an error with your upload. Please try again.' not in form.errors.get('__all__')), form.errors
def test_throttling(self, parse_addon_mock): upload = FileUpload.objects.create(valid=True, name='foo.xpi') data = {'upload': upload.uuid, 'compatible_apps': [amo.FIREFOX.id]} request = req_factory_factory('/', post=True, data=data) request.user = user_factory() request.META['REMOTE_ADDR'] = '5.6.7.8' with freeze_time('2019-04-08 15:16:23.42') as frozen_time: for x in range(0, 6): self._add_fake_throttling_action( view_class=VersionView, url='/', user=request.user, remote_addr=get_random_ip(), ) form = forms.NewUploadForm(data, request=request) assert not form.is_valid() assert form.errors.get('__all__') == [ 'You have submitted too many uploads recently. ' 'Please try again after some time.' ] frozen_time.tick(delta=timedelta(seconds=61)) form = forms.NewUploadForm(data, request=request) assert form.is_valid()
def test_valid_upload_from_different_user(self, parse_addon_mock): upload = FileUpload.objects.create( valid=True, name='foo.xpi', user=user_factory(), source=amo.UPLOAD_SOURCE_DEVHUB, ip_address='127.0.0.64', ) data = {'upload': upload.uuid, 'compatible_apps': [amo.FIREFOX.id]} request = req_factory_factory('/', post=True, data=data) request.user = user_factory() form = forms.NewUploadForm(data, request=request) assert not form.is_valid() assert ('There was an error with your upload. Please try again.' in form.errors.get('__all__')), (form.errors) # Admin override can bypass with mock.patch('olympia.access.acl.action_allowed_user') as acl: # For the 'Addons:Edit' permission check. acl.return_value = True data['admin_override_validation'] = True form = forms.NewUploadForm(data, request=request) assert form.is_valid() # Regular users can't override acl.return_value = False form = forms.NewUploadForm(data, request=request) assert not form.is_valid() assert ('There was an error with your upload. Please try again.' in form.errors.get('__all__')), form.errors
def test_firefox_default_selected(self): upload = FileUpload.objects.create(valid=False) data = {'upload': upload.uuid} request = req_factory_factory('/', post=True, data=data) request.user = user_factory() form = forms.NewUploadForm(data, request=request) assert form.fields['compatible_apps'].initial == [amo.FIREFOX.id]
def test_only_valid_uploads(self): upload = FileUpload.objects.create(valid=False) form = forms.NewUploadForm( {'upload': upload.uuid, 'supported_platforms': [1]}, request=mock.Mock()) assert ('There was an error with your upload. Please try again.' in form.errors.get('__all__')), form.errors upload.validation = '{"errors": 0}' upload.save() addon = Addon.objects.create() form = forms.NewUploadForm( {'upload': upload.uuid, 'supported_platforms': [1]}, addon=addon, request=mock.Mock()) assert ('There was an error with your upload. Please try again.' not in form.errors.get('__all__')), form.errors
def test_compat_apps_widget_custom_label_class_rendered(self): """We are setting a custom class at the label of the compatibility apps multi-select to correctly render images. """ user = user_factory() upload = FileUpload.objects.create( valid=False, name='foo.xpi', user=user, source=amo.UPLOAD_SOURCE_DEVHUB, ip_address='127.0.0.64', ) data = {'upload': upload.uuid} request = req_factory_factory('/', post=True, data=data) request.user = user form = forms.NewUploadForm(data, request=request) result = form.fields['compatible_apps'].widget.render( name='compatible_apps', value=amo.FIREFOX.id ) assert 'class="app firefox"' in result result = form.fields['compatible_apps'].widget.render( name='compatible_apps', value=amo.ANDROID.id ) assert 'class="app android"' in result
def test_firefox_default_selected(self): user = user_factory() upload = FileUpload.objects.create( valid=False, name='foo.xpi', user=user, source=amo.UPLOAD_SOURCE_DEVHUB, ip_address='127.0.0.64', ) data = {'upload': upload.uuid} request = req_factory_factory('/', post=True, data=data) request.user = user form = forms.NewUploadForm(data, request=request) assert form.fields['compatible_apps'].initial == [amo.FIREFOX.id]
def test_compat_apps_widget_custom_label_class_rendered(self): """We are setting a custom class at the label of the compatibility apps multi-select to correctly render images. """ upload = FileUpload.objects.create(valid=False) form = forms.NewUploadForm({'upload': upload.uuid}, request=mock.Mock()) result = form.fields['compatible_apps'].widget.render( name='compatible_apps', value=amo.FIREFOX.id) assert 'class="app firefox"' in result result = form.fields['compatible_apps'].widget.render( name='compatible_apps', value=amo.ANDROID.id) assert 'class="app android"' in result
def test_check_xpi_called(self, mock_check_xpi_info, mock_parse): """Make sure the check_xpi_info helper is called. There's some important checks made in check_xpi_info, if we ever refactor the form to not call it anymore, we need to make sure those checks are run at some point. """ mock_parse.return_value = None mock_check_xpi_info.return_value = {'name': 'foo', 'type': 2} upload = FileUpload.objects.create(valid=True) addon = Addon.objects.create() form = forms.NewUploadForm( {'upload': upload.uuid, 'supported_platforms': [1]}, addon=addon, request=mock.Mock()) form.clean() assert mock_check_xpi_info.called
def test_check_xpi_called(self, mock_check_xpi_info, mock_parse): """Make sure the check_xpi_info helper is called. There's some important checks made in check_xpi_info, if we ever refactor the form to not call it anymore, we need to make sure those checks are run at some point. """ mock_parse.return_value = None mock_check_xpi_info.return_value = {'name': 'foo', 'type': 2} upload = FileUpload.objects.create(valid=True, name='foo.xpi') addon = Addon.objects.create() data = {'upload': upload.uuid, 'compatible_apps': [amo.FIREFOX.id]} request = req_factory_factory('/', post=True, data=data) request.user = user_factory() form = forms.NewUploadForm(data, addon=addon, request=request) form.clean() assert mock_check_xpi_info.called
def test_firefox_default_selected(self): upload = FileUpload.objects.create(valid=False) form = forms.NewUploadForm({'upload': upload.uuid}, request=mock.Mock()) assert form.fields['compatible_apps'].initial == [amo.FIREFOX.id]