From fc443013d4a004d69c53be3286e33dd30921879e Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Thu, 4 Apr 2019 19:21:10 -0700 Subject: improve test coverage --- python/fatcat_tools/transforms/elasticsearch.py | 1 + python/fatcat_web/routes.py | 15 +++-- python/tests/tools_api.py | 6 +- python/tests/web_auth.py | 4 ++ python/tests/web_citation_csl.py | 2 + python/tests/web_editor.py | 28 +++++++++ python/tests/web_routes.py | 1 + python/tests/web_search.py | 82 ++++++++++++++++++++++++- 8 files changed, 128 insertions(+), 11 deletions(-) create mode 100644 python/tests/web_editor.py diff --git a/python/fatcat_tools/transforms/elasticsearch.py b/python/fatcat_tools/transforms/elasticsearch.py index 0c2c5e46..1d221ebc 100644 --- a/python/fatcat_tools/transforms/elasticsearch.py +++ b/python/fatcat_tools/transforms/elasticsearch.py @@ -14,6 +14,7 @@ def check_kbart(year, archive): def test_check_kbart(): + assert check_kbart(1990, dict()) == None assert check_kbart(1990, dict(year_spans=[[2000, 2000]])) == False assert check_kbart(2000, dict(year_spans=[[2000, 2000]])) == True assert check_kbart(1950, dict(year_spans=[[1900, 1920], [1990, 2000]])) == False diff --git a/python/fatcat_web/routes.py b/python/fatcat_web/routes.py index 44216809..18c51d43 100644 --- a/python/fatcat_web/routes.py +++ b/python/fatcat_web/routes.py @@ -343,7 +343,8 @@ def editgroup_view(ident): @app.route('/editgroup//annotation', methods=['POST']) @login_required def editgroup_create_annotation(ident): - app.csrf.protect() + if not app.testing: + app.csrf.protect() comment_markdown = request.form.get('comment_markdown') if not comment_markdown: app.log.info("empty comment field") @@ -368,7 +369,8 @@ def editgroup_create_annotation(ident): @app.route('/editgroup//accept', methods=['POST']) @login_required def editgroup_accept(ident): - app.csrf.protect() + if not app.testing: + app.csrf.protect() # on behalf of user... user_api = auth_api(session['api_token']) try: @@ -385,7 +387,8 @@ def editgroup_accept(ident): @app.route('/editgroup//unsubmit', methods=['POST']) @login_required def editgroup_unsubmit(ident): - app.csrf.protect() + if not app.testing: + app.csrf.protect() # on behalf of user... user_api = auth_api(session['api_token']) try: @@ -402,7 +405,8 @@ def editgroup_unsubmit(ident): @app.route('/editgroup//submit', methods=['POST']) @login_required def editgroup_submit(ident): - app.csrf.protect() + if not app.testing: + app.csrf.protect() # on behalf of user... print("submitting...") user_api = auth_api(session['api_token']) @@ -618,7 +622,8 @@ def token_login(): @app.route('/auth/change_username', methods=['POST']) @login_required def change_username(): - app.csrf.protect() + if not app.testing: + app.csrf.protect() # show the user a list of login options if not 'username' in request.form: abort(400) diff --git a/python/tests/tools_api.py b/python/tests/tools_api.py index ac75a73a..933135a3 100644 --- a/python/tests/tools_api.py +++ b/python/tests/tools_api.py @@ -9,8 +9,7 @@ from fatcat_tools import public_api, authenticated_api def test_authenticated_api(): api = authenticated_api("http://localhost:9411/v0") api.get_changelog() - api.create_editgroup_annotation("aaaaaaaaaaaabo53aaaaaaaaa4", - EditgroupAnnotation(comment_markdown="bloop test thingie")) + api.auth_check() def test_public_api(): api = public_api("http://localhost:9411/v0") @@ -19,5 +18,4 @@ def test_public_api(): # authenticated. Maybe the DefaultAPI thing? pytest.skip("public_api() client not isolated from authenticated") with pytest.raises(ApiException): - api.create_editgroup_annotation("aaaaaaaaaaaabo53aaaaaaaaa4", - EditgroupAnnotation(comment_markdown="bloop unauth test thingie")) + api.auth_check() diff --git a/python/tests/web_auth.py b/python/tests/web_auth.py index 81986eec..2fa9363b 100644 --- a/python/tests/web_auth.py +++ b/python/tests/web_auth.py @@ -26,6 +26,10 @@ def test_ia_xauth(full_app): # successful login with full_app.test_client() as app: + + rv = app.get('/auth/token_login') + assert rv.status_code == 200 + responses.add(responses.POST, full_app.config['IA_XAUTH_URI'] + "?op=authenticate", status=200, json={'success': True}) responses.add(responses.POST, full_app.config['IA_XAUTH_URI'] + "?op=info", diff --git a/python/tests/web_citation_csl.py b/python/tests/web_citation_csl.py index ae9bca55..cf50f3c3 100644 --- a/python/tests/web_citation_csl.py +++ b/python/tests/web_citation_csl.py @@ -15,6 +15,8 @@ def test_release_bibtex(app): rv = app.get('/release/aaaaaaaaaaaaarceaaaaaaaaam.bib') assert rv.status_code == 200 assert b'@article{' in rv.data + rv = app.get('/release/ccccccccccccccccccccccccca.bib') + assert rv.status_code == 404 rv = app.get('/release/aaaaaaaaaaaaarceaaaaaaaaam/citeproc?style=csl-json') assert rv.status_code == 200 # could also rv.get_json() here diff --git a/python/tests/web_editor.py b/python/tests/web_editor.py new file mode 100644 index 00000000..de094488 --- /dev/null +++ b/python/tests/web_editor.py @@ -0,0 +1,28 @@ + +import json +import pytest +from fatcat_client.rest import ApiException +from fixtures import * + + +def test_change_username(app_admin): + + # these tests aren't supposed to mutate database + rv = app_admin.post('/auth/change_username', data={'username': 'admin-tmp'}, + follow_redirects=True) + assert rv.status_code == 200 + rv = app_admin.get('/auth/account') + assert b'admin-tmp' in rv.data + + rv = app_admin.post('/auth/change_username', data={'username': 'claire'}, + follow_redirects=True) + assert rv.status_code == 400 + rv = app_admin.get('/auth/account') + assert b'admin-tmp' in rv.data + + rv = app_admin.post('/auth/change_username', data={'username': 'admin'}, + follow_redirects=True) + assert rv.status_code == 200 + rv = app_admin.get('/auth/account') + assert b'admin-tmp' not in rv.data + diff --git a/python/tests/web_routes.py b/python/tests/web_routes.py index 889bdd90..3af5369b 100644 --- a/python/tests/web_routes.py +++ b/python/tests/web_routes.py @@ -11,5 +11,6 @@ def test_static_routes(app): rv = app.get(route) assert rv.status_code == 200 + assert app.get("/search").status_code == 302 assert app.get("/static/bogus/route").status_code == 404 diff --git a/python/tests/web_search.py b/python/tests/web_search.py index 4e7cb2e0..43be9f01 100644 --- a/python/tests/web_search.py +++ b/python/tests/web_search.py @@ -33,8 +33,86 @@ def test_container_search(app): assert b"European Instructional Course Lectures" in rv.data assert b"British Editorial Society of Bone and Joint Surger" in rv.data -# TODO: entity stats +elastic_resp1 = { + 'timed_out': False, + 'aggregations': { + 'release_ref_count': {'value': 8031459}}, + 'hits': {'total': 80578584, 'hits': [], 'max_score': 0.0}, + '_shards': {'successful': 5, 'total': 5, 'skipped': 0, 'failed': 0}, + 'took': 0 +} +elastic_resp2 = { + 'timed_out': False, + 'aggregations': { + 'paper_like': {'buckets': { + 'is_oa': {'doc_count': 8031459}, + 'in_kbart': {'doc_count': 51594200}, + 'in_web': {'doc_count': 10925092}, + 'in_web_not_kbart': {'doc_count': 5160359}}}}, + 'hits': {'total': 80578584, 'hits': [], 'max_score': 0.0}, + '_shards': {'successful': 5, 'total': 5, 'skipped': 0, 'failed': 0}, + 'took': 0 +} +elastic_resp3 = { + 'timed_out': False, + 'hits': {'total': 80578584, 'hits': [], 'max_score': 0.0}, + '_shards': {'successful': 5, 'total': 5, 'skipped': 0, 'failed': 0}, + 'took': 0 +} + +@responses.activate +def test_stats(app): + + responses.add(responses.GET, + 'http://localhost:9200/fatcat_release/_search?request_cache=true', + json=elastic_resp1.copy(), status=200) + responses.add(responses.GET, + 'http://localhost:9200/fatcat_release/_search?request_cache=true', + json=elastic_resp2.copy(), status=200) + responses.add(responses.GET, + 'http://localhost:9200/fatcat_container/_search?request_cache=true', + json=elastic_resp3.copy(), status=200) + rv = app.get('/stats') + assert rv.status_code == 200 + # TODO: probe these reponses better + +@responses.activate +def test_stats_json(app): + + responses.add(responses.GET, + 'http://localhost:9200/fatcat_release/_search?request_cache=true', + json=elastic_resp1.copy(), status=200) + responses.add(responses.GET, + 'http://localhost:9200/fatcat_release/_search?request_cache=true', + json=elastic_resp2.copy(), status=200) + responses.add(responses.GET, + 'http://localhost:9200/fatcat_container/_search?request_cache=true', + json=elastic_resp3.copy(), status=200) + rv = app.get('/stats.json') + assert rv.status_code == 200 + +@responses.activate +def test_container_stats(app): + + elastic_resp = { + 'timed_out': False, + 'aggregations': { + 'container_stats': {'buckets': { + 'is_preserved': {'doc_count': 461939}, + 'in_web': {'doc_count': 2797}}}}, + 'hits': {'total': 461939, 'hits': [], 'max_score': 0.0}, + '_shards': {'successful': 5, 'total': 5, 'skipped': 0, 'failed': 0}, + 'took': 50 + } + + responses.add(responses.GET, + 'http://localhost:9200/fatcat_release/_search?request_cache=true', + json=elastic_resp, status=200) + rv = app.get('/container/issnl/1234-5678/stats.json') + assert rv.status_code == 200 + # TODO: probe this reponse better + # TODO: container stats # TODO: container ISSN-L query # TODO: release DOI query -# TODO: release fulltext query +# TODO: release fulltext (filter) query -- cgit v1.2.3