def _run(self): action, data, app_hash, opts = self.prepare_data() if action == 'scopes': return views.list_scopes() view = getattr(views, action) kwargs = {} if action in ('create', 'edit'): validator = AppFormValidator( new_item=action == 'create', data=data) validator.validate() kwargs['data'] = validator.cleaned_data() if action in ('edit', 'delete', 'detail'): app_hash_validator = HashValidator(app_hash) app_hash_validator.validate() kwargs['app_hash'] = app_hash_validator.cleaned_data() if action == 'delete': kwargs['confirm'] = opts['confirm'] # if action == 'dump': # kwargs['json'] = opts['json'] kwargs['crawler'] = self.init_crawler() prepaired_view = self.prepare_view(view) context = prepaired_view(**kwargs) return context
def test_form_validator(): with pytest.raises(YomError): AppForm() validator_from_data = AppForm(data=fully_filled_app) validator_from_args = AppForm(args=FakeArgs(fully_filled_app)) assert validator_from_data.data == validator_from_args.data assert validator_from_data.validate() is True assert AppForm(data=less_filled_app_params).validate() is True data = {'title': 'b', 'homepage': 'c'} validator = AppForm(data=data) with pytest.raises(YomValidationError): validator.validate() with pytest.raises(YomValidationError): validator.cleaned_data() assert validator.is_valid is False data = {'title': 'b', 'homepage': None, 'scopes': ['login:avatar']} validator = AppForm(data=data) is_valid = validator.validate() cleaned_data = validator.cleaned_data() assert is_valid is True assert 'homepage' not in cleaned_data with pytest.raises(YomValidationError): AppForm(args=FakeArgs()).validate() with pytest.raises(YomValidationError): AppForm(args=FakeArgs({'title': 'a'})).validate() with pytest.raises(YomValidationError): AppForm(args=FakeArgs()).validate() with pytest.raises(YomValidationError): AppForm(new_item=False, args=FakeArgs()).validate() with pytest.raises(YomValidationError): data = {'title': 'a', 'scopes': ['wrong:scope']} AppForm(new_item=False, data=data).validate()