def test_gather_and_enforce_request_args_enumerated_defaults_values_for_default_cast_function_and_raise_value(self, tu_get_parm): tu_get_parm.return_value = 'ice' with current_app.test_request_context('/whatever?vanilla=ice'): fetched_value = tu.gather_and_enforce_request_args_enumerated([{'name': 'vanilla'}]) assert fetched_value == {'vanilla': 'ice'} assert tu_get_parm.called_once_with('vanilla', default=None, cast_function=None, raise_value_error=True)
def test_render_conferences_handles_unicode(): hits = Response({ 'hits': { 'hits': [ { '_type': 'conference', '_source': { 'address': [ {'original_address': 'Paris, France'}, ], 'control_number': 1351301, 'titles': [ {'title': u'Théorie de Cordes en France'}, ], }, }, ], 'total': 1, }, }).hits expected = ([ [ u'<a href="/conferences/1351301">Théorie de Cordes en France</a>', 'Paris, France', '', u' ', ], ], 1) with current_app.test_request_context(): result = render_conferences(1, hits) assert expected == result
def send_post_by_email(post_id): """Send a post to community members by email.""" with current_app.test_request_context("/send_post_by_email"): post = Post.query.get(post_id) if post is None: # deleted after task queued, but before task run return thread = post.thread community = thread.community logger.info( "Sending new post by email to members of community %r", community.name ) CHUNK_SIZE = 20 members_id = [member.id for member in community.members if member.can_login] chunk = [] for idx, member_id in enumerate(members_id): chunk.append(member_id) if idx % CHUNK_SIZE == 0: batch_send_post_to_users.apply_async((post.id, chunk)) chunk = [] if chunk: batch_send_post_to_users.apply_async((post.id, chunk))
def update_expired_embargoes(): """Release expired embargoes every midnight.""" logger = current_app.logger base_url = urlunsplit(( current_app.config.get('PREFERRED_URL_SCHEME', 'http'), current_app.config['JSONSCHEMAS_HOST'], current_app.config.get('APPLICATION_ROOT') or '', '', '' )) # The task needs to run in a request context as JSON Schema validation # will use url_for. with current_app.test_request_context('/', base_url=base_url): s = B2ShareRecordsSearch( using=current_search_client, index='records' ).query( 'query_string', query='open_access:false AND embargo_date:{{* TO {0}}}'.format( datetime.now(timezone.utc).isoformat() ), allow_leading_wildcard=False ).fields([]) record_ids = [hit.meta.id for hit in s.scan()] if record_ids: logger.info('Changing access of {} embargoed publications' ' to public.'.format(len(record_ids))) for record in Record.get_records(record_ids): logger.debug('Making embargoed publication {} public'.format( record.id)) record['open_access'] = True record.commit() db.session.commit() indexer = RecordIndexer() indexer.bulk_index(record_ids) indexer.process_bulk_queue()
def run(self, username, password, email, admin=False): # force type conversion to boolean user_type = 'administrator' if admin else 'user' userdata = { 'username': username, 'password': password, 'email': email, 'user_type': user_type, 'is_active': admin, 'needs_activation': not admin } with app.test_request_context('/users', method='POST'): if userdata.get('password', None) and not is_hashed(userdata.get('password')): userdata['password'] = get_hash(userdata.get('password'), app.config.get('BCRYPT_GENSALT_WORK_FACTOR', 12)) user = superdesk.get_resource_service('users').find_one(username=userdata.get('username'), req=None) if user: logger.info('user already exists %s' % (userdata)) else: logger.info('creating user %s' % (userdata)) superdesk.get_resource_service('users').post([userdata]) logger.info('user saved %s' % (userdata)) return userdata
def test_topic_update_read(database, user, topic): """Tests the update read method if the topic is unread/read.""" forumsread = ForumsRead.query.\ filter(ForumsRead.user_id == user.id, ForumsRead.forum_id == topic.forum_id).first() with current_app.test_request_context(): # Test with logged in user login_user(user) assert current_user.is_authenticated # Update the tracker assert topic.update_read(current_user, topic.forum, forumsread) # Because the tracker is already up-to-date, it shouldn't update it # again. assert not topic.update_read(current_user, topic.forum, forumsread) # Adding a new post - now the tracker shouldn't be up-to-date anymore. post = Post(content="Test Content") post.save(topic=topic, user=user) forumsread = ForumsRead.query.\ filter(ForumsRead.user_id == user.id, ForumsRead.forum_id == topic.forum_id).first() # Test tracker length flaskbb_config["TRACKER_LENGTH"] = 0 assert not topic.update_read(current_user, topic.forum, forumsread) flaskbb_config["TRACKER_LENGTH"] = 1 assert topic.update_read(current_user, topic.forum, forumsread) # Test with logged out user logout_user() assert not current_user.is_authenticated assert not topic.update_read(current_user, topic.forum, forumsread)
def process_v1_record(directory, indexer, base_url, logfile): """ Parse a downloaded file containing records """ with open(os.path.join(directory, '___record___.json'), 'r') as f: file_content = f.read() record_json = json.loads(file_content) recid = str(record_json.get('record_id')) if not record_json.get('domain'): click.secho('Record {} "{}" has no domain, '.format(recid, record_json.get('title')), fg='red') logfile.write("\n********************\n") logfile.write("\nERROR: record {} has no domain, is in limbo\n".format(recid)) logfile.write("\n********************\n") click.secho('Processing record {} "{}"'.format(recid, record_json.get('title'))) record = _process_record(record_json) if record is not None: user = get_or_create_user(record_json['uploaded_by']) with current_app.test_request_context('/', base_url=base_url): current_app.login_manager.reload_user(user) try: deposit = Deposit.create(record) _create_bucket(deposit, record_json, directory, logfile) deposit.publish() _, record = deposit.fetch_published() # index the record indexer.index(record) db.session.commit() except: logfile.write("\n********************") logfile.write("\nERROR while creating record {}\n".format(recid)) logfile.write(traceback.format_exc()) logfile.write("\n********************") click.secho("Finished processing {}".format(record['titles'][0]['title']))
def example(): """Example command from example plugin""" with current_app.test_request_context(): with session_language('es_ES'): print _('example plugin says hi'), current_plugin if self.settings.get('show_message'): print self.settings.get('dummy_message')
def add_patients(app_users, services): many_to_one_field_counts = { "phone_numbers": [0, 4], "household_members": [0, 5], "income_sources": [0, 4], "emergency_contacts": [0, 2], "addresses": [0, 2], "employers": [0, 2], } with current_app.test_request_context(): for _ in range(50): login_user(fake.random_element(app_users)) patient = fake.patient() for field in many_to_one_field_counts: objs = fake.some(field, many_to_one_field_counts[field]) for obj in objs: getattr(patient, field).append(obj) db.session.add(patient) db.session.commit() for _ in range(random.randint(0, 3)): login_user(fake.random_element(app_users)) possible_service_ids = [s.id for s in services if s.id != current_user.service_id] patient.referrals.append(fake.referrals(possible_service_ids)) logout_user() for _ in range(random.randint(0, 3)): login_user(fake.random_element(app_users)) possible_sliding_scale_ids = [ s.id for s in SlidingScale.query.filter(SlidingScale.service_id == current_user.service_id) ] patient.screening_results.append(fake.screening_results(possible_sliding_scale_ids)) logout_user() db.session.commit() print "Added patients with lots of data, referrals and screening results"
def records(): """Load records.""" import pkg_resources import uuid from flask_login import login_user, logout_user from dojson.contrib.marc21 import marc21 from dojson.contrib.marc21.utils import create_record, split_blob from invenio_accounts.models import User from invenio_deposit.api import Deposit users = User.query.all() # pkg resources the demodata data_path = pkg_resources.resource_filename( 'invenio_records', 'data/marc21/bibliographic.xml' ) with open(data_path) as source: with current_app.test_request_context(): indexer = RecordIndexer() with db.session.begin_nested(): for index, data in enumerate(split_blob(source.read()), start=1): login_user(users[index % len(users)]) # do translate record = marc21.do(create_record(data)) # create record indexer.index(Deposit.create(record)) logout_user() db.session.commit()
def send_daily_social_digest_task(): # a request_context is required when rendering templates with current_app.test_request_context("/send_daily_social_updates"): config = current_app.config if not config.get("PRODUCTION") or config.get("DEMO"): return send_daily_social_digest()
def test_topic_tracker_needs_update_cleared(database, user, topic): """Tests if the topicsread needs an update if the forum has been marked as cleared. """ forumsread = ForumsRead.query.\ filter(ForumsRead.user_id == user.id, ForumsRead.forum_id == topic.forum_id).first() topicsread = TopicsRead.query.\ filter(TopicsRead.user_id == user.id, TopicsRead.topic_id == topic.id).first() with current_app.test_request_context(): assert topic.tracker_needs_update(forumsread, topicsread) # Update the tracker forumsread = ForumsRead() forumsread.user_id = user.id forumsread.forum_id = topic.forum_id forumsread.last_read = datetime.utcnow() forumsread.cleared = datetime.utcnow() forumsread.save() # Now the topic should be read assert not topic.tracker_needs_update(forumsread, topicsread)
def test_category_get_forums(forum, user): category = forum.category with current_app.test_request_context(): # Test with logged in user login_user(user) assert current_user.is_authenticated cat, forums = Category.get_forums(category.id, current_user) # Check if it is a list because in a category there are normally more # than one forum in it (not in these tests) assert isinstance(forums, list) is True assert forums == [(forum, None)] assert cat == category # Test the same thing with a logged out user logout_user() assert not current_user.is_authenticated cat, forums = Category.get_forums(category.id, current_user) # Check if it is a list because in a category there are normally more # than one forum in it (not in these tests) assert isinstance(forums, list) is True assert forums == [(forum, None)] assert cat == category
def test_category_get_all(forum, user): category = forum.category with current_app.test_request_context(): # Test with logged in user login_user(user) assert current_user.is_authenticated categories = Category.get_all(current_user) # All categories are stored in a list assert isinstance(categories, list) # The forums for a category are also stored in a list assert isinstance(categories[0][1], list) assert categories == [(category, [(forum, None)])] # Test with logged out user logout_user() assert not current_user.is_authenticated categories = Category.get_all(current_user) # All categories are stored in a list assert isinstance(categories, list) # The forums for a category are also stored in a list assert isinstance(categories[0][1], list) assert categories == [(category, [(forum, None)])]
def revision_created_handler(revision, **kwargs): options = dict( db.session.query( ItemOption.name, ItemOption.value ).filter( ItemOption.item_id == revision.repository_id, ItemOption.name.in_([ 'build.branch-names', ]) ) ) if not should_build_branch(revision, options.get('build.branch-names', '*')): return data = { 'sha': revision.sha, 'repository': revision.repository.url, } with current_app.test_request_context('/api/0/builds/', method='POST', data=data): response = BuildIndexAPIView().post() if isinstance(response, (list, tuple)): response, status = response if status != 200: logger.error('Failed to create builds: %s' % (response,))
def update_sitemap_cache(urls=None, max_url_count=None): """Update the Sitemap cache.""" # We need request context to properly generate the external link # using url_for. We fix base_url as we want to simulate a # request as it looks from an external client, instead of a task. siteurl = current_app.config['THEME_SITEURL'] with current_app.test_request_context(base_url=siteurl): max_url_count = max_url_count or \ current_app.config['ZENODO_SITEMAP_MAX_URL_COUNT'] sitemap = current_app.extensions['zenodo-sitemap'] urls = iter(urls or sitemap._generate_all_urls()) url_scheme = current_app.config['ZENODO_SITEMAP_URL_SCHEME'] urls_slice = list(itertools.islice(urls, max_url_count)) page_n = 0 sitemap.clear_cache() while urls_slice: page_n += 1 page = render_template('zenodo_sitemap/sitemap.xml', urlset=filter(None, urls_slice)) sitemap.set_cache('sitemap:' + str(page_n), page) urls_slice = list(itertools.islice(urls, max_url_count)) urlset = [ { 'loc': url_for('zenodo_sitemap.sitemappage', page=pn, _external=True, _scheme=url_scheme) } for pn in range(1, page_n+1)] index_page = render_template('zenodo_sitemap/sitemapindex.xml', urlset=urlset, url_scheme=url_scheme) sitemap.set_cache('sitemap:0', index_page)
def run(self, username, password, email, admin='false'): # force type conversion to boolean user_type = 'administrator' if admin.lower() == 'true' else 'user' userdata = { 'username': username, 'password': password, 'email': email, 'user_type': user_type, app.config['LAST_UPDATED']: utcnow(), } with app.test_request_context('/users', method='POST'): if userdata.get('password', None) and not is_hashed(userdata.get('password')): userdata['password'] = get_hash(userdata.get('password'), app.config.get('BCRYPT_GENSALT_WORK_FACTOR', 12)) user = superdesk.get_resource_service('users').find_one(username=userdata.get('username'), req=None) if user: logger.info('updating user %s' % (userdata)) superdesk.get_resource_service('users').patch(user.get('_id'), userdata) return userdata else: logger.info('creating user %s' % (userdata)) userdata[app.config['DATE_CREATED']] = userdata[app.config['LAST_UPDATED']] superdesk.get_resource_service('users').post([userdata]) logger.info('user saved %s' % (userdata)) return userdata
def test_pagination(self): admin_role = AuthRole.query.filter_by(name='administrator').first() duke = AuthUser(email="*****@*****.**", username="******", password="******", confirmed=True, role=admin_role) db.session.add(duke) db.session.commit() # add one author and many books author1 = Author() db.session.add(author1) author1_details = AuthorDetail("en", "London") author1_details.first_name = "Jack" author1.details.append(author1_details) for i in range(100): lw = LiteraryWork("en") lw.creation_datestring = "1910" db.session.add(lw) lwd = LiteraryWorkDetail("en", "Burning Daylight") lw.details.append(lwd) db.session.commit() # test page 1 response = self.client.get( self.lws_ext_lnk, headers=self.generate_auth_header("*****@*****.**", "hardcore") ) self.assertTrue(response.status_code == 200) json_response = loads(response.data.decode('utf-8')) with current_app.test_request_context('/'): lw_pg1 = url_for('api.get_literary_works', page=1, _external=True) lw_pg2 = url_for('api.get_literary_works', page=2, _external=True) lw_pg3 = url_for('api.get_literary_works', page=3, _external=True) parent_pg = url_for('api.index', _external=True) self_pg = url_for('api.get_literary_works', _external=True) self.assertTrue(json_response["_meta"]["total"] == 100) self.assertTrue(json_response["_meta"]["page"] == 1) self.assertTrue(json_response["_meta"]["max_results"] == current_app.config['ELIBRARIAN_ITEMS_PER_PAGE']) self.assertTrue(json_response["_links"]["next"] == lw_pg2) self.assertTrue(json_response["_links"]["parent"]["href"] == parent_pg) self.assertTrue(json_response["_links"]["self"]["href"] == self_pg) response2 = self.client.get( self.lws_lnk_pg2, headers=self.generate_auth_header("*****@*****.**", "hardcore") ) self.assertTrue(response2.status_code == 200) json_response2 = loads(response2.data.decode('utf-8')) self.assertTrue(json_response2["_meta"]["total"] == 100) self.assertTrue(json_response2["_meta"]["page"] == 2) self.assertTrue(json_response2["_links"]["next"] == lw_pg3) self.assertTrue(json_response2["_links"]["prev"] == lw_pg1)
def batch(): """ Execute multiple requests, submitted as a batch. :statuscode 207: Multi status """ # TODO: we could probably turn off csrf protection for each requests # and only use the CSRF in the header of the parent request requests, err = RequestSchema().load(request.get_json(), many=True) responses = [] status_code = 207 for req in requests: method = req['method'] url = req['url'] body = req.get('body', None) headers = req.get('headers', {}) with current_app.app_context(): headers.setdefault('accept', 'application/json') with current_app.test_request_context(url, method=method, data=body, headers=headers): try: # Can modify flask.g here without affecting # flask.g of the root request for the batch # Pre process Request rv = current_app.preprocess_request() if rv is None: # Main Dispatch rv = current_app.dispatch_request() except Exception as e: rv = current_app.handle_user_exception(e) response = current_app.make_response(rv) # Post process Request response = current_app.process_response(response) # Response is a Flask response object. # _read_response(response) reads response.response # and returns a string. If your endpoints return JSON object, # this string would be the response as a JSON string. responses.append({ "status_code": response.status_code, "body": response.data.decode('utf-8'), "headers": [{'name': k, 'value': v} for k, v in response.headers.items()] }) # if error response if (response.status_code % 400 < 100) or (response.status_code % 400 < 100): status_code = response.status_code break return json.dumps(responses), status_code
def test_generic_save_view_handles_non_json_data(self): with current_app.test_request_context('/whatever', headers={'Content-Type':'application/not_json'}, data='{"cant_drive": "55"}'): resp_object = tv.generic_save_view(args_dict={}, document_type='careless_whisper') assert resp_object.status_code == 409 assert resp_object.data == '"problem with saving careless_whisper: content type is not application/json"'
def decorator(*args, **kwargs): base_url = urlunsplit(( current_app.config.get('PREFERRED_URL_SCHEME', 'http'), current_app.config['JSONSCHEMAS_HOST'], current_app.config.get('APPLICATION_ROOT') or '', '', '' )) with current_app.test_request_context('/', base_url=base_url): f(*args, **kwargs)
def get_populated_sitemap(self): """Populate sitemap template with current app urls.""" site_url = current_app.config['SITE_URL'] with current_app.test_request_context(base_url=site_url): urls = iter(self._generate_all_urls()) page = render_template('sitemap/sitemap.xml', urlset=filter(None, urls)) return page
def run_email_result_task(index_name, sketch_id=None): """Create email Celery task. This task is run after all sketch analyzers are done and emails the result of all analyzers to the user who imported the data. Args: index_name: An index name. sketch_id: A sketch ID (optional). Returns: Email sent status. """ # We need to get a fake request context so that url_for() will work. with current_app.test_request_context(): searchindex = SearchIndex.query.filter_by(index_name=index_name).first() sketch = None try: to_username = searchindex.user.username except AttributeError: logging.warning('No user to send email to.') return '' if sketch_id: sketch = Sketch.query.get(sketch_id) subject = 'Timesketch: [{0:s}] is ready'.format(searchindex.name) # TODO: Use jinja templates. body = 'Your timeline [{0:s}] has been imported and is ready.'.format( searchindex.name) if sketch: view_urls = sketch.get_view_urls() view_links = [] for view_url, view_name in iter(view_urls.items()): view_links.append('<a href="{0:s}">{1:s}</a>'.format( view_url, view_name)) body = body + '<br><br><b>Sketch</b><br>{0:s}'.format( sketch.external_url) analysis_results = searchindex.description.replace('\n', '<br>') body = body + '<br><br><b>Analysis</b>{0:s}'.format( analysis_results) if view_links: body = body + '<br><br><b>Views</b><br>' + '<br>'.join( view_links) try: send_email(subject, body, to_username, use_html=True) except RuntimeError as e: return repr(e) return 'Sent email to {0:s}'.format(to_username)
def test_generic_save_view_handles_abend(self, tv_save_generic): tv_save_generic.side_effect = ThermalBaseError('phenomenon') with current_app.test_request_context('/whatever', headers={'Content-Type':'application/json'}, data='{"thermo":"plastic"}'): resp_object = tv.generic_save_view(args_dict={'feed': 'bag'}, document_type='headache') assert resp_object.status_code == 400 assert resp_object.data == '"phenomenon"'
def test_flaskbbdomain_translations(default_settings): domain = current_app.extensions.get("babel").domain with current_app.test_request_context(): # no translations accessed and thus the cache is empty assert domain.get_translations_cache() == {} # load translations into cache assert isinstance(domain.get_translations(), Translations) assert len(domain.get_translations_cache()) == 1 # 'en'
def test_label_form(self, test_client): with current_app.test_request_context('/'): form = LabelForm(MultiDict({ 'title': u"Test label title", 'icon_emoji': u"🔟", 'required': False, 'restricted': False }), meta={'csrf': False}) assert form.validate()
def setUp(self): super(FrontendTestCase, self).setUp() # Push a request context self.req_ctx = app.test_request_context() self.req_ctx.push() # Create our test client self.client = app.test_client()
def authenticated_user(userinfo): """Authentication context manager.""" user = User.query.filter(User.id == userinfo.id).one() with current_app.test_request_context(): login_user(user) try: yield finally: logout_user()
def conditional_request_ctx(self): if self.with_request_ctx: kwargs = self.with_request_ctx if isinstance(kwargs, str): kwargs = {"path": kwargs} with current_app.test_request_context(**kwargs): yield else: yield
def setUp(self): self.app = create_app('testing_virtualenv') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() with current_app.test_request_context('/'): self.root_ext_lnk = url_for('main.index', _external=True) self.ui_ext_lnk = url_for('webui.index', _external=True)
def test_ac_cannot_add_police_department(mockdata, client, session): with current_app.test_request_context(): login_ac(client) form = DepartmentForm(name='Test Police Department', short_name='TPD') rv = client.post( url_for('main.add_department'), data=form.data, follow_redirects=True ) assert rv.status_code == 403
def test_admin_cannot_duplicate_police_department_during_edit(mockdata, client, session): with current_app.test_request_context(): login_admin(client) existing_dep_form = DepartmentForm(name='Existing Police Department', short_name='EPD') existing_dep_rv = client.post( url_for('main.add_department'), data=existing_dep_form.data, follow_redirects=True ) assert 'New department' in existing_dep_rv.data.decode('utf-8') new_dep_form = DepartmentForm(name='New Police Department', short_name='NPD') new_dep_rv = client.post( url_for('main.add_department'), data=new_dep_form.data, follow_redirects=True ) assert 'New department' in new_dep_rv.data.decode('utf-8') new_department = Department.query.filter_by( name='New Police Department').one() edit_form = EditDepartmentForm(name='Existing Police Department', short_name='EPD2') rv = client.post( url_for('main.edit_department', department_id=new_department.id), data=edit_form.data, follow_redirects=True ) assert 'already exists' in rv.data.decode('utf-8') # make sure original department is still here existing_department = Department.query.filter_by( name='Existing Police Department').one() assert existing_department.short_name == 'EPD' # make sure new department is left unchanged new_department = Department.query.filter_by( name='New Police Department').one() assert new_department.short_name == 'NPD'
def test_ac_can_see_officer_not_in_their_dept(mockdata, client, session): with current_app.test_request_context(): login_ac(client) officer = Officer.query.except_(Officer.query.filter_by(department_id=AC_DEPT)).first() rv = client.get( url_for('main.officer_profile', officer_id=officer.id), follow_redirects=True ) assert rv.status_code == 200 # Testing names doesn't work bc the way we display them varies assert str(officer.id) in rv.data.decode('utf-8')
def test_admin_can_add_officer_badge_number(mockdata, client, session): with current_app.test_request_context(): login_admin(client) form = AssignmentForm(star_no='1234', rank='COMMANDER') rv = client.post( url_for('main.add_assignment', officer_id=3), data=form.data, follow_redirects=True ) assert 'Added new assignment' in rv.data.decode('utf-8')
def test_admins_can_edit_incident_links_and_licenses(mockdata, client, session): with current_app.test_request_context(): login_admin(client) inc = Incident.query.options(joinedload(Incident.links), joinedload(Incident.license_plates), joinedload(Incident.officers)).first() address_form = LocationForm(street_name=inc.address.street_name, cross_street1=inc.address.cross_street1, cross_street2=inc.address.cross_street2, city=inc.address.city, state=inc.address.state, zip_code=inc.address.zip_code) old_links = inc.links old_links_forms = [ LinkForm(url=link.url, link_type=link.link_type).data for link in inc.links ] new_url = 'http://rachel.com' link_form = LinkForm(url='http://rachel.com', link_type='video') old_license_plates = inc.license_plates new_number = '453893' license_plates_form = LicensePlateForm(number=new_number, state='IA') ooid_forms = [OOIdForm(ooid=officer.id) for officer in inc.officers] form = IncidentForm(date_field=str(inc.date), time_field=str(inc.time), report_number=inc.report_number, description=inc.description, department='1', address=address_form.data, links=old_links_forms + [link_form.data], license_plates=[license_plates_form.data], officers=ooid_forms) data = process_form_data(form.data) rv = client.post(url_for('main.incident_api', obj_id=inc.id) + '/edit', data=data, follow_redirects=True) assert rv.status_code == 200 assert 'successfully updated' in rv.data.decode('utf-8') # old links are still there for link in old_links: assert link in inc.links assert new_url in [link.url for link in inc.links] # old license plates are gone assert old_license_plates not in inc.license_plates assert len(inc.license_plates) == 1 assert new_number in [lp.number for lp in inc.license_plates]
def test_user_can_add_tag(mockdata, client, session): with current_app.test_request_context(): login_user(client) form = FaceTag(officer_id=1, image_id=4, dataX=34, dataY=32, dataWidth=3, dataHeight=33) rv = client.post(url_for('main.label_data', image_id=4), data=form.data, follow_redirects=True) assert 'Tag added to database' in rv.data
def test_user_can_register_with_legit_credentials(mockdata, client, session): with current_app.test_request_context(): diceware_password = '******' form = RegistrationForm(email='*****@*****.**', username='******', password=diceware_password, password2=diceware_password) rv = client.post( url_for('auth.register'), data=form.data, follow_redirects=True ) assert 'A confirmation email has been sent to you.' in rv.data
def records(): """Load records.""" from flask_login import login_user, logout_user from invenio_accounts.models import User from invenio_deposit.api import Deposit user = User.query.one() with current_app.test_request_context(): login_user(user) with db.session.begin_nested(): Deposit.create({'title': 'Test'}) logout_user() db.session.commit()
def test_endpoint_on_one_app(self): @self.app_1.route('/') @self.autodoc.doc() def index(): """Returns a hello world message""" return 'Hello World!' with self.app_2.app_context(): with current_app.test_request_context(): response = self.autodoc.json() data = json.loads(response.data.decode('utf-8')) self.assertIn('endpoints', data) self.assertEqual(data['endpoints'], [])
def test_records_experiments_facets(inspire_app): with current_app.test_request_context(): expected_filters = {"classification", "institution"} expected_aggregations = { **experiment_inspire_classification_aggregation(order=1), **experiment_institution_aggregation(order=2), } filters = current_app.config["RECORDS_REST_FACETS"][ "records-experiments"]()["filters"].keys() aggregations = current_app.config["RECORDS_REST_FACETS"][ "records-experiments"]()["aggs"] assert filters == expected_filters assert aggregations == expected_aggregations
def test_records_conferences_facets(inspire_app): with current_app.test_request_context(): expected_filters = {"subject", "series", "start_date", "contains"} expected_aggregations = { **conf_series_aggregation(order=1), **conf_subject_aggregation(order=2), } filters = current_app.config["RECORDS_REST_FACETS"][ "records-conferences"]()["filters"].keys() aggregations = current_app.config["RECORDS_REST_FACETS"][ "records-conferences"]()["aggs"] assert filters == expected_filters assert aggregations == expected_aggregations
def url_for(*args, **kwargs): """ url_for replacement that works even when there is no request context. """ if "_external" not in kwargs: kwargs["_external"] = False reqctx = _request_ctx_stack.top if reqctx is None: if kwargs["_external"]: raise RuntimeError("Cannot generate external URLs without a " "request context.") with current_app.test_request_context(): return _url_for(*args, **kwargs) return _url_for(*args, **kwargs)
def test_forum_update_read(database, user, topic): """Test the update read method.""" forumsread = ForumsRead.query.\ filter(ForumsRead.user_id == user.id, ForumsRead.forum_id == topic.forum_id).first() topicsread = TopicsRead.query.\ filter(TopicsRead.user_id == user.id, TopicsRead.topic_id == topic.id).first() forum = topic.forum with current_app.test_request_context(): # Test with logged in user login_user(user) # Should return False because topicsread is None assert not forum.update_read(current_user, forumsread, topicsread) # This is the first time the user visits the topic topicsread = TopicsRead() topicsread.user_id = user.id topicsread.topic_id = topic.id topicsread.forum_id = topic.forum_id topicsread.last_read = datetime.utcnow() topicsread.save() # hence, we also need to create a new forumsread entry assert forum.update_read(current_user, forumsread, topicsread) forumsread = ForumsRead.query.\ filter(ForumsRead.user_id == user.id, ForumsRead.forum_id == topic.forum_id).first() # everything should be up-to-date now assert not forum.update_read(current_user, forumsread, topicsread) post = Post(content="Test Content") post.save(user=user, topic=topic) # Updating the topicsread tracker topicsread.last_read = datetime.utcnow() topicsread.save() # now the forumsread tracker should also need a update assert forum.update_read(current_user, forumsread, topicsread) logout_user() # should fail because the user is logged out assert not forum.update_read(current_user, forumsread, topicsread)
def language(lang_code): '''Force a given language''' ctx = None if not request: ctx = current_app.test_request_context() ctx.push() backup = g.get('lang_code') g.lang_code = lang_code refresh() yield g.lang_code = backup if ctx: ctx.pop() refresh()
def test_validation_unknown_fields(es, location): """Test validation error due to unknown fields.""" json_data = json.dumps({'desc': {}, '_deposit': {'id': None}}) with current_app.test_request_context('/api/deposits/video', method='PUT', data=json_data, content_type='application/json'): with pytest.raises(MarshmallowErrors) as errors: video_loader() assert '400: Bad Request' in str(errors.value) error_body = json.loads(errors.value.get_body()) assert error_body['status'] == 400 assert error_body['errors'][0]['field'] == 'desc'
def get(self): """ An OEmbed compliant API endpoint See: http://oembed.com/ Support datasets and reuses URLs """ args = oembed_parser.parse_args() if args['format'] != 'json': api.abort(501, 'Only JSON format is supported') url = args['url'] # Fix flask not detecting URL with https://domain:443/ if 'https:' in url and ':443/' in url: url = url.replace(':443/', '/') with current_app.test_request_context(url) as ctx: if not ctx.request.endpoint: api.abort(404, 'Unknown URL') endpoint = ctx.request.endpoint.replace('_redirect', '') view_args = ctx.request.view_args if endpoint not in self.ROUTES: api.abort(404, 'Unknown URL') param = self.ROUTES[endpoint] item = view_args[param] if isinstance(item, Exception): raise item width = maxwidth = 1000 height = maxheight = 200 params = { 'width': width, 'height': height, 'item': item, 'type': param } params[param] = item html = theme.render('oembed.html', **params) return { 'type': 'rich', 'version': '1.0', 'html': html, 'width': width, 'height': height, 'maxwidth': maxwidth, 'maxheight': maxheight, }
def test_user_can_get_reset_password_with_valid_token(mockdata, client, session): with current_app.test_request_context(): form = PasswordResetForm(email='*****@*****.**', password='******', password2='catdog') user = User.query.filter_by(email='*****@*****.**').one() token = user.generate_reset_token() rv = client.post(url_for('auth.password_reset', token=token), data=form.data, follow_redirects=True) assert 'Your password has been updated.' in rv.data
def test_user_cannot_register_if_passwords_dont_match(mockdata, client, session): with current_app.test_request_context(): form = RegistrationForm(email='*****@*****.**', username='******', password='******', password2='cat') rv = client.post(url_for('auth.register'), data=form.data, follow_redirects=False) # Form will return 200 only if the form does not validate assert rv.status_code == 200 assert 'Passwords must match' in rv.data
def test_user_can_change_password_if_they_match(mockdata, client, session): with current_app.test_request_context(): login_user(client) form = ChangePasswordForm(old_password='******', password='******', password2='validpasswd') rv = client.post( url_for('auth.change_password'), data=form.data, follow_redirects=True ) assert 'Your password has been updated.' in rv.data
def test_user_cannot_reset_password_with_invalid_token(mockdata, client, session): with current_app.test_request_context(): form = PasswordResetForm(email='*****@*****.**', password='******', password2='catdog') token = 'beepboopbeep' rv = client.post( url_for('auth.password_reset', token=token), data=form.data, follow_redirects=True ) assert 'Your password has been updated.' not in rv.data
def test_if_not_authenticated_redirect(self, redirect, is_authenticated, app): from flask import current_app as flask_app is_authenticated.return_value = False def view(): return "view" wrapped_view = token.login_required(view) with flask_app.test_request_context('/'): wrapped_view() redirect.assert_called_once()
def test_get_search_with_source_with_fields_query_param_and_wrong_mimetype( inspire_app): with current_app.test_request_context( "?fields=authors,ids", headers={"Accept": "application/x-bibtex"}): with pytest.raises(FieldsParamForbidden): search = LiteratureSearch() get_search_with_source(search) with current_app.test_request_context( "?fields=authors,ids", headers={"Accept": "application/vnd+inspire.latex.eu+x-latex"}, ): with pytest.raises(FieldsParamForbidden): search = LiteratureSearch() get_search_with_source(search) with current_app.test_request_context( "?fields=authors,ids", headers={"Accept": "application/vnd+inspire.latex.us+x-latex"}, ): with pytest.raises(FieldsParamForbidden): search = LiteratureSearch() get_search_with_source(search)
def test_featured_tag_replaces_others(mockdata, client, session): with current_app.test_request_context(): login_admin(client) tag1 = Face.query.first() officer = Officer.query.filter_by(id=tag1.officer_id).one() # Add second tag for officer mock = MagicMock(return_value=Image.query.filter(Image.id != tag1.img_id).first()) with patch('OpenOversight.app.main.views.crop_image', mock): image = Image.query.filter(Image.department_id == officer.department_id).first() form = FaceTag(officer_id=officer.id, image_id=image.id, dataX=34, dataY=32, dataWidth=3, dataHeight=33) rv = client.post( url_for('main.label_data', image_id=image.id), data=form.data, follow_redirects=True ) views.crop_image.assert_called_once() assert b'Tag added to database' in rv.data tag2 = Face.query.filter(Face.officer_id == tag1.officer_id).filter(Face.id != tag1.id).one_or_none() assert tag2 is not None # Set tag 1 as featured rv = client.post( url_for('main.set_featured_tag', tag_id=tag1.id), follow_redirects=True ) assert b'Successfully set this tag as featured' in rv.data tag1 = Face.query.filter(Face.id == tag1.id).one() assert tag1.featured is True # Set tag 2 as featured rv = client.post( url_for('main.set_featured_tag', tag_id=tag2.id), follow_redirects=True ) assert b'Successfully set this tag as featured' in rv.data tag1 = Face.query.filter(Face.id == tag1.id).one() tag2 = Face.query.filter(Face.id == tag2.id).one() assert tag1.featured is False assert tag2.featured is True
def test_admins_can_edit_incident_officers(mockdata, client, session): with current_app.test_request_context(): login_admin(client) inc = Incident.query.options(joinedload(Incident.links), joinedload(Incident.license_plates), joinedload(Incident.officers)).first() address_form = LocationForm(street_name=inc.address.street_name, cross_street1=inc.address.cross_street1, cross_street2=inc.address.cross_street2, city=inc.address.city, state=inc.address.state, zip_code=inc.address.zip_code) links_forms = [ LinkForm(url=link.url, link_type=link.link_type).data for link in inc.links ] license_plates_forms = [ LicensePlateForm(number=lp.number, state=lp.state).data for lp in inc.license_plates ] old_officers = inc.officers old_officer_ids = [officer.id for officer in inc.officers] old_ooid_forms = [OOIdForm(oo_id=the_id) for the_id in old_officer_ids] # get a new officer that is different from the old officers new_officer = Officer.query.except_( Officer.query.filter(Officer.id.in_(old_officer_ids))).first() new_ooid_form = OOIdForm(oo_id=new_officer.id) form = IncidentForm(date_field=str(inc.date), time_field=str(inc.time), report_number=inc.report_number, description=inc.description, department='1', address=address_form.data, links=links_forms, license_plates=license_plates_forms, officers=old_ooid_forms + [new_ooid_form]) data = process_form_data(form.data) rv = client.post(url_for('main.incident_api', obj_id=inc.id) + '/edit', data=data, follow_redirects=True) assert rv.status_code == 200 assert 'successfully updated' in rv.data.decode('utf-8') for officer in old_officers: assert officer in inc.officers assert new_officer.id in [off.id for off in inc.officers]
def test_hep_author_publications_cataloger_facets(inspire_app): author = "1111_Jones" author_recid = "1111" with current_app.test_request_context(f"?author={author}"): expected_filters = { "author", "author_count", "doc_type", "earliest_date", "citation_count", "citation_count_without_self_citations", "collaboration", "refereed", "citeable", "collection", "subject", "arxiv_categories", "self_affiliations", "affiliations", "self_author_names", "self_curated_relation", "rpp", } expected_aggregations = { **hep_earliest_date_aggregation(order=1, title="Date of paper"), **hep_author_count_aggregation(order=2), **hep_rpp(order=3), **hep_doc_type_aggregation(order=4), **hep_author_aggregation(order=5, author=author, title="Collaborators"), **hep_collaboration_aggregation(order=6), **hep_self_author_affiliations_aggregation(order=7, author_recid=author_recid), **hep_subject_aggregation(order=8), **hep_arxiv_categories_aggregation(order=9), **hep_self_author_names_aggregation(order=10, author_recid=author_recid), **hep_collection_aggregation(order=11), **hep_self_author_claimed_papers_aggregation(order=12, author_recid=author_recid), } filters = set(current_app.config["CATALOGER_RECORDS_REST_FACETS"] ["hep-author-publication"]()["filters"].keys()) aggregations = current_app.config["CATALOGER_RECORDS_REST_FACETS"][ "hep-author-publication"]()["aggs"] assert filters == expected_filters assert aggregations == expected_aggregations
def test_ac_can_edit_officer_in_their_dept(mockdata, client, session): with current_app.test_request_context(): login_ac(client) department = Department.query.filter_by(id=AC_DEPT).first() first_name = 'Testier' last_name = 'OTester' middle_initial = 'R' suffix = '' race = random.choice(RACE_CHOICES)[0] gender = random.choice(GENDER_CHOICES)[0] form = AddOfficerForm(first_name=first_name, last_name=last_name, middle_initial=middle_initial, suffix=suffix, race=race, gender=gender, star_no=666, rank='COMMANDER', department=department.id, birth_year=1990) data = process_form_data(form.data) rv = client.post(url_for('main.add_officer'), data=data, follow_redirects=True) officer = Officer.query.filter_by(last_name=last_name).one() new_last_name = 'Shiny' form = EditOfficerForm(first_name=first_name, last_name=new_last_name, suffix=suffix, race=race, gender=gender, department=department.id) data = process_form_data(form.data) rv = client.post(url_for('main.edit_officer', officer_id=officer.id), data=data, follow_redirects=True) assert 'Officer {} edited'.format(new_last_name) in rv.data.decode( 'utf-8') assert last_name not in rv.data.decode('utf-8') # Check the changes were added to the database officer = Officer.query.filter_by(id=officer.id).one() assert officer.last_name == new_last_name
def test_saved_msg_event_has_been_saved(self): """retrieves message event from database""" message_event = {'msg_id': 'AMsgId', 'event': EventsApi.SENT.value, 'date_time': ''} with self.app.app_context(): with current_app.test_request_context(): Saver().save_message(SecureMessage(msg_id='AMsgId', thread_id='AMsgId')) Saver().save_msg_event(message_event['msg_id'], message_event['event']) with self.engine.connect() as con: request = con.execute('SELECT * FROM securemessage.events') for row in request: self.assertTrue(row is not None) self.assertTrue(row[1] == message_event['event']) self.assertTrue(row[2] == message_event['msg_id']) self.assertTrue(row[3] is not None)
def test_msg_commit_exception_does_a_rollback(self): """check message commit exception clears the session""" with self.app.app_context(): self.db.drop_all() with current_app.test_request_context(): with self.assertRaises(MessageSaveException): Saver().save_message(self.test_message) self.db.create_all() Saver().save_message(self.test_message) with self.engine.connect() as con: request = con.execute('SELECT COUNT(securemessage.secure_message.id) FROM securemessage.secure_message') for row in request: self.assertTrue(row._row[0] == 1)
def test_ac_can_set_featured_tag_in_their_dept(mockdata, client, session): with current_app.test_request_context(): login_ac(client) tag = Face.query.filter(Face.officer.has(department_id=AC_DEPT)).first() tag_id = tag.id rv = client.post( url_for('main.set_featured_tag', tag_id=tag_id), follow_redirects=True ) assert b'Successfully set this tag as featured' in rv.data featured_tag = Face.query.filter(Face.officer_id == tag.officer_id).filter(Face.featured == True).one_or_none() # noqa: E712 assert featured_tag is not None
def test_search_factory_without_query(inspire_app): with current_app.test_request_context(""): search = InspireSearch() expected_query_string = "" expected_search_to_dict = { "query": { "match_all": {} }, "track_total_hits": True } query_string, search = inspire_search_factory(None, search) search_to_dict = search.to_dict() assert expected_query_string == query_string assert expected_search_to_dict == search_to_dict