123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894 |
- import io
- import pathlib
- import pytest
- import zipfile
- from datetime import datetime, timedelta
- from werkzeug.datastructures import FileStorage
- from stor import db
- from stor.auth.models import User
- from stor.repository.definitions import (
- Dsi, EcResourceStatus, EcSharingLevel, LanguageDataType,
- LicenceType, LicenceConditionType, LocalResourceStatus, LocalSharingLevel, Theme
- )
- from stor.repository.models import Resource
- @pytest.mark.parametrize(
- ( "resource_id", "expected_monolingual", "expected_bilingual", "expected_other"),
- (
- (1, 0, 0, 2),
- (2, 222, 0, 0),
- (3, 0, 0, 0),
- (4, 44444, 0, 0),
- (5, 6420, 55555, 0),
- (6, 0, 246, 2),
- ),
- )
- def test_aggregate_data_sizes(app, resource_id, expected_monolingual, expected_bilingual, expected_other):
- with app.app_context():
- resource = Resource.query.filter_by(id=resource_id).first()
- sizes = resource.aggregate_data_sizes()
- assert sizes[LanguageDataType.MONOLINGUAL_WORD] == expected_monolingual
- assert sizes[LanguageDataType.BILINGUAL_TU] == expected_bilingual
- assert sizes[LanguageDataType.OTHER_FILE] == expected_other
- @pytest.mark.parametrize(
- ("username", "message"),
- (
- ("", "Access Denied"),
- ("una", "Access Denied"),
- ("vera", "Email not verified"),
- ("ina", "Account not activated"),
- ("maya", "Access Denied"),
- ),
- )
- def test_get_invalid(client, auth, username, message):
- if username:
- auth.login(username)
- endpoints = [
- "",
- "/contribute",
- "/browse",
- "/1",
- "/1/download",
- "/1/download-licence",
- ]
- for endpoint in endpoints:
- assert bytes(message, "utf8") in client.get("/repository{0}".format(endpoint), follow_redirects=True).data
- def test_default_authorized(client, auth):
- auth.login("stan")
- response = client.get("/repository", follow_redirects=True)
- assert response.history[0].status_code == 308
- assert response.status_code == 200
- @pytest.mark.parametrize(
- ("username"),
- (
- (""),
- ("una"),
- ),
- )
- def test_contribute_post_unauthenticated_and_unauthorized(client, auth, username):
- auth.login(username)
- data = {
- "resource_title" : "Cooking with Garlic",
- "resource_description" : "How to cook with garlic",
- "licence_type" : "CC-BY-4.0",
- "resource_files" : [],
- "local_sharing_level" : "ALL",
- "ec_sharing_level" : "INTERNAL",
- "accept_tos" : True,
- }
- response = client.post("/repository/contribute", data=data)
- assert len(response.history) == 0
- assert b"Access Denied" in response.data
- def test_contribute_get_valid(client, auth):
- auth.login("stan")
- response = client.get("/repository/contribute")
- assert response.status_code == 200
- assert b"<h1>Contribute New Resource</h1>" in response.data
- assert b"I agree with the Language Resource upload Terms of Service for Data Holders" in response.data
- @pytest.mark.parametrize(
- ("licence_type", "expected_licence_filename", "ec_sharing_level", "expected_ec_status",
- "ipr_holder", "licence_conditions"),
- (
- (LicenceType.PUBLIC_DOMAIN, "", EcSharingLevel.INTERNAL,
- EcResourceStatus.NOT_RELAYED, "", []),
- (LicenceType.CC_BY_40, LicenceType.CC_BY_40.value.standard_filename, EcSharingLevel.INTERNAL,
- EcResourceStatus.NOT_RELAYED, "Dept of Thunderstorms", [LicenceConditionType.ATTRIBUTION]),
- (LicenceType.CC_BY_40, LicenceType.CC_BY_40.value.standard_filename, EcSharingLevel.INTERNAL,
- EcResourceStatus.NOT_RELAYED, "Dept of Thunderstorms", [LicenceConditionType.ATTRIBUTION]),
- (LicenceType.OTHER, "licence.pdf", EcSharingLevel.INTERNAL,
- EcResourceStatus.NOT_RELAYED, "", []),
- (LicenceType.OTHER, "licence.pdf", EcSharingLevel.NONE,
- EcResourceStatus.NOT_APPLICABLE, "", []),
- (LicenceType.CC_BY_NC_SA_40, LicenceType.CC_BY_NC_SA_40.value.standard_filename, EcSharingLevel.INTERNAL,
- EcResourceStatus.NOT_RELAYED, "Dept of Thunderstorms", [LicenceConditionType.ATTRIBUTION, LicenceConditionType.NON_COMMERCIAL, LicenceConditionType.SHARE_ALIKE]),
- ),
- )
- def test_contribute_post_valid(app, client, auth, licence_type, expected_licence_filename,
- ec_sharing_level, expected_ec_status, ipr_holder, licence_conditions):
- username = "stan"
- auth.login(username)
- previous_seen = datetime.utcnow()
- with app.app_context():
- previous_seen = User.query.filter_by(username=username).first().seen
- expected_id = 11 # 10 resources pre-populated
- resource_title = "Cooking with Garlic"
- licence_file = FileStorage(stream=io.BytesIO(b"licence information"), filename="licence.pdf") if licence_type.value.user_provided else None
- resource_files = [FileStorage(stream=io.BytesIO(b"a cookery book"), filename="garlic.txt")]
- data = {
- "resource_title" : resource_title,
- "resource_description" : "How to cook with garlic",
- "themes" : [Theme.AGRICULTURE.name],
- "licence_type" : licence_type.name,
- "licence_file" : licence_file,
- "resource_files" : resource_files,
- "local_sharing_level" : "ALL",
- "ec_sharing_level" : ec_sharing_level.name,
- "ipr_holder" : ipr_holder,
- "attribution" : "Licensed",
- "accept_tos" : True,
- }
- response = client.post("/repository/contribute", data=data, follow_redirects=True)
- assert response.status_code == 200
- assert response.history[0].status_code == 302
- assert response.history[0].headers["Location"] == "/index"
- with app.test_request_context():
- resource = Resource.query.filter_by(id=expected_id).first()
- standard_user = User.query.filter_by(username=username).first()
- assert (standard_user.seen - previous_seen) > timedelta()
- assert resource is not None
- assert len(resource.titles) == 1
- assert resource.titles[0].language == "en"
- assert resource.titles[0].content == resource_title
- assert len(resource.descriptions) == 1
- assert resource.descriptions[0].language == "en"
- assert resource.descriptions[0].content == "How to cook with garlic"
- assert resource.owner_id == 3
- assert resource.owner == standard_user
- assert resource.contact_id == 3
- assert resource.contact == standard_user
- assert len(resource.dsis) == 0
- assert len(resource.themes) == 1
- assert resource.themes[0].theme == Theme.AGRICULTURE
- assert resource.licence.type == licence_type
- assert resource.licence.name == licence_type.value.default_name
- if licence_type.value.default_text:
- assert resource.licence.texts[0].language == "en"
- assert resource.licence.texts[0].content == licence_type.value.default_text
- else:
- assert len(resource.licence.texts) == 0
- assert resource.licence.url == None
- assert resource.local_sharing_level == LocalSharingLevel.ALL
- assert resource.ec_sharing_level == ec_sharing_level
- assert resource.local_status == LocalResourceStatus.RAW
- assert resource.ec_status == expected_ec_status
- if ipr_holder:
- assert len(resource.ipr_holders) == 1
- assert resource.ipr_holders[0].organization_name == ipr_holder
- else:
- assert len(resource.ipr_holders) == 0
- assert len(resource.attributions) == 1
- assert resource.attributions[0].language == "en"
- assert resource.attributions[0].content == "Licensed"
- assert resource.funding_project == None
- assert((datetime.utcnow() - resource.created) < timedelta(seconds=2))
- assert resource.updated == None
- location = pathlib.Path(app.config["UPLOAD_FOLDER"]).resolve().joinpath(resource.uuid)
- assert location.exists()
- resource_path = location.joinpath("input", "garlic.txt")
- assert resource_path.exists()
- assert resource_path.stat().st_size > 0
- licence_dir = location.joinpath("output", "licence")
- assert licence_dir.exists()
- if licence_type.value.file_required:
- licence_path = licence_dir.joinpath(expected_licence_filename)
- assert licence_path.exists()
- assert licence_path.stat().st_size > 0
- else:
- licence_path = licence_dir.joinpath("licence_{0}.txt".format(licence_type.value.export_name))
- assert licence_path.exists()
- assert licence_path.stat().st_size > 0
- with open(licence_path) as licence_file:
- licence_lines = licence_file.read().splitlines()
- assert licence_lines[0] == licence_type.value.display_name
- assert licence_lines[1] == licence_type.value.default_text
- assert len(resource.licence.conditions) == len(licence_conditions)
- for condition in licence_conditions:
- assert any(lc.condition == condition for lc in resource.licence.conditions)
- assert len(standard_user.resources) == 1
- assert standard_user.resources[0] == resource
- assert len(standard_user.contact_for) == 1
- assert standard_user.contact_for[0] == resource
- outbox = app.extensions["mailman"].outbox
- assert len(outbox) == 1
- assert outbox[0].subject == "New resource contributed | Acmhainn nua curtha ar fáil"
- assert outbox[0].body.startswith("A new resource has been contributed.\n\nUploader: stan\nResource title: Cooking with Garlic")
- assert outbox[0].from_email == "no-reply@localhost"
- assert set(outbox[0].to) == {"reeve@test.com", "ada@test.com"}
- expected_document = {
- "get_titles_en": resource_title,
- "get_descriptions_en": "How to cook with garlic",
- "local_status": "RAW",
- "local_sharing_level": "ALL",
- "owner_id": standard_user.id,
- "funding_project_id": None,
- "get_licence_0": licence_type.name,
- "get_themes_0": Theme.AGRICULTURE.name
- }
- app.elasticsearch.index.assert_any_call(index="resources", id=expected_id, document=expected_document)
- def test_contribute_post_compressed(app, client, auth):
- auth.login("stan")
- expected_id = 11 # 10 resources pre-populated
- resource_file_info = [
- ("a.txt", io.BytesIO(b"aaaaa")),
- ("b.sh", io.BytesIO(b"bbbbb")),
- ("c.tmx", io.BytesIO(b"ccccc")),
- ("d.zip", io.BytesIO(b"ddddd")),
- ("nested/e.odt", io.BytesIO(b"eeeee")),
- ("much/further/nested/f.doc", io.BytesIO(b"fffff")),
- ("nested/g.exe", io.BytesIO(b"ggggg")),
- ]
- zip_bytes = io.BytesIO()
- with zipfile.ZipFile(zip_bytes, mode="a") as zip:
- for name, data in resource_file_info:
- zip.writestr(name, data.getvalue())
- zip_bytes.seek(0)
- resource_files = [FileStorage(stream=zip_bytes, filename="files.zip")]
- data = {
- "resource_title" : "Cooking with Garlic",
- "resource_description" : "How to cook with garlic",
- "licence_type" : LicenceType.CC0_10.name,
- "licence_file" : None,
- "resource_files" : resource_files,
- "local_sharing_level" : "ALL",
- "ec_sharing_level" : "INTERNAL",
- "accept_tos" : True,
- }
- response = client.post("/repository/contribute", data=data, follow_redirects=True)
- assert response.status_code == 200
- assert response.history[0].status_code == 302
- assert response.history[0].headers["Location"] == "/index"
- with app.app_context():
- resource = Resource.query.filter_by(id=expected_id).first()
- standard_user = User.query.filter_by(username="stan").first()
- assert resource is not None
- assert len(resource.titles) == 1
- assert resource.titles[0].language == "en"
- assert resource.titles[0].content == "Cooking with Garlic"
- assert len(resource.descriptions) == 1
- assert resource.descriptions[0].language == "en"
- assert resource.descriptions[0].content == "How to cook with garlic"
- assert resource.owner_id == 3
- assert resource.owner == standard_user
- assert resource.licence.type == LicenceType.CC0_10
- assert resource.licence.name == ""
- assert len(resource.licence.texts) == 0
- assert resource.licence.url == None
- assert resource.local_sharing_level == LocalSharingLevel.ALL
- assert resource.ec_sharing_level == EcSharingLevel.INTERNAL
- assert resource.local_status == LocalResourceStatus.RAW
- assert resource.ec_status == EcResourceStatus.NOT_RELAYED
- assert((datetime.utcnow() - resource.created) < timedelta(seconds=2))
- assert resource.updated == None
- location = pathlib.Path(app.config["UPLOAD_FOLDER"]).resolve().joinpath(resource.uuid)
- assert location.exists()
- resource_path = location.joinpath("input", "files.zip")
- assert resource_path.exists()
- assert resource_path.stat().st_size > 0
- assert location.joinpath("input", "a.txt").exists()
- assert location.joinpath("input", "c.tmx").exists()
- assert location.joinpath("input", "nested_e.odt").exists()
- assert location.joinpath("input", "much_further_nested_f.doc").exists()
- assert not location.joinpath("input", "b.sh").exists()
- assert not location.joinpath("input", "d.zip").exists()
- assert not location.joinpath("input", "nested_g.exe").exists()
- assert not location.joinpath("input", "nested").exists()
- assert not location.joinpath("input", "much", "further", "nested").exists()
- licence_dir = location.joinpath("output", "licence")
- assert licence_dir.exists()
- assert len(standard_user.resources) == 1
- assert standard_user.resources[0] == resource
- def test_contribute_post_multiple(app, client, auth):
- auth.login("stan")
- expected_id = 11 # 10 resources pre-populated
- zip_resource_file_info = [
- ("a.txt", io.BytesIO(b"aaaaa")),
- ("b.sh", io.BytesIO(b"bbbbb")),
- ("c.tmx", io.BytesIO(b"ccccc")),
- ("d.zip", io.BytesIO(b"ddddd")),
- ]
- zip_bytes = io.BytesIO()
- with zipfile.ZipFile(zip_bytes, mode="a") as zip:
- for name, data in zip_resource_file_info:
- zip.writestr(name, data.getvalue())
- zip_bytes.seek(0)
- resource_files = [
- FileStorage(stream=io.BytesIO(b"aniseed cookery book"), filename="aniseed.txt"),
- FileStorage(stream=io.BytesIO(b"basil cookery book"), filename="basil.tmx"),
- FileStorage(stream=zip_bytes, filename="files.zip"),
- ]
- data = {
- "resource_title" : "Cooking",
- "resource_description" : "How to cook",
- "licence_type" : LicenceType.CC0_10.name,
- "licence_file" : None,
- "resource_files" : resource_files,
- "local_sharing_level" : "ALL",
- "ec_sharing_level" : "INTERNAL",
- "accept_tos" : True,
- }
- response = client.post("/repository/contribute", data=data, follow_redirects=True)
- assert response.status_code == 200
- assert response.history[0].status_code == 302
- assert response.history[0].headers["Location"] == "/index"
- with app.app_context():
- resource = Resource.query.filter_by(id=expected_id).first()
- standard_user = User.query.filter_by(username="stan").first()
- assert resource is not None
- assert len(resource.titles) == 1
- assert resource.titles[0].language == "en"
- assert resource.titles[0].content == "Cooking"
- assert len(resource.descriptions) == 1
- assert resource.descriptions[0].language == "en"
- assert resource.descriptions[0].content == "How to cook"
- assert resource.owner_id == 3
- assert resource.owner == standard_user
- assert resource.licence.type == LicenceType.CC0_10
- assert resource.licence.name == ""
- assert len(resource.licence.texts) == 0
- assert resource.licence.url == None
- assert resource.local_sharing_level == LocalSharingLevel.ALL
- assert resource.ec_sharing_level == EcSharingLevel.INTERNAL
- assert resource.local_status == LocalResourceStatus.RAW
- assert resource.ec_status == EcResourceStatus.NOT_RELAYED
- assert((datetime.utcnow() - resource.created) < timedelta(seconds=2))
- assert resource.updated == None
- location = pathlib.Path(app.config["UPLOAD_FOLDER"]).resolve().joinpath(resource.uuid)
- assert location.exists()
- resource_path = location.joinpath("input", "files.zip")
- assert resource_path.exists()
- assert resource_path.stat().st_size > 0
- assert location.joinpath("input", "aniseed.txt").exists()
- assert location.joinpath("input", "basil.tmx").exists()
- assert location.joinpath("input", "a.txt").exists()
- assert location.joinpath("input", "c.tmx").exists()
- assert not location.joinpath("input", "b.sh").exists()
- assert not location.joinpath("input", "d.zip").exists()
- licence_dir = location.joinpath("output", "licence")
- assert licence_dir.exists()
- assert len(standard_user.resources) == 1
- assert standard_user.resources[0] == resource
- @pytest.mark.parametrize(
- ( "name", "description", "licence_type", "resource_filename", "local_sl", "ec_sl", "ipr_holder", "attribution", "accept", "message"),
- (
- ("", "res desc", "CC_BY_40", "resource.txt", "ALL", "INTERNAL", "I.P.R. Holder", "Licensed", True, b"Please fill in this field."),
- ("res name", "", "CC_BY_40", "resource.txt", "ALL", "INTERNAL", "I.P.R. Holder", "Licensed", True, b"Please fill in this field."),
- ("res name", "res desc", "ABC_80", "resource.txt", "ALL", "INTERNAL", "I.P.R. Holder", "Licensed", True, b"Not a valid choice."),
- ("res name", "res desc", "CC_BY_40", "resource.txt", "NONE", "INTERNAL", "I.P.R. Holder", "Licensed", True, b"Not a valid choice."),
- ("res name", "res desc", "CC_BY_40", "resource.txt", "ALL", "UNKNOWN", "I.P.R. Holder", "Licensed", True, b"Not a valid choice."),
- ("res name", "res desc", "CC_BY_40", "resource.txt", "ALL", "INTERNAL", "I.P.R. Holder", "Licensed", "", b"This box must be checked in order to continue."),
- ("res name", "res desc", "CC_BY_40", "resource.sh", "ALL", "INTERNAL", "I.P.R. Holder", "Licensed", True, b"Only the following formats are allowed:"),
- ("res-01", "res desc", "CC_BY_40", "resource.txt", "ALL", "INTERNAL", "I.P.R. Holder", "Licensed", True, b"You have previously uploaded a resource with this title."),
- ("res name", "res desc", "CC_BY_40", "resource.txt", "ALL", "INTERNAL", "", "Licensed", True, b"An IPR holder must be named for the type of licence selected."),
- ("res name", "res desc", "CC_BY_40", "resource.txt", "ALL", "INTERNAL", "I.P.R. Holder", "", True, b"Attribution text must be entered for the type of licence selected."),
- ),
- )
- def test_contribute_post_invalid(client, app, auth, name, description, licence_type, resource_filename,
- local_sl, ec_sl, ipr_holder, attribution, accept, message):
- username = "gobnait"
- auth.login(username)
- expected_id = 11 # 10 resources pre-populated
- expected_resources = 5
- resource_files = FileStorage(stream=io.BytesIO(b"a cookery book"), filename=resource_filename)
- data = {
- "resource_title" : name,
- "resource_description" : description,
- "licence_type" : licence_type,
- "resource_files" : resource_files,
- "local_sharing_level" : local_sl,
- "ec_sharing_level" : ec_sl,
- "ipr_holder" : ipr_holder,
- "attribution" : attribution,
- "accept_tos" : accept,
- }
- response = client.post("/repository/contribute", data=data, follow_redirects=True)
- assert response.status_code == 200
- assert len(response.history) == 0
- assert message in response.data
- with app.app_context():
- assert Resource.query.filter_by(id=expected_id).first() is None
- assert len(User.query.filter_by(username=username).first().resources) == expected_resources
- @pytest.mark.parametrize(
- ( "licence_type", "licence_file", "message"),
- (
- (LicenceType.OTHER, None, "A licence file must be provided for the selected licence type."),
- (LicenceType.OTHER, FileStorage(stream=io.BytesIO(b"licence info"), filename="licence.sh"), "Only the following formats are allowed:"),
- (LicenceType.CC_BY_40, FileStorage(stream=io.BytesIO(b"licence info"), filename="licence.pdf"), "No licence file is required for a standard licence type"),
- ),
- )
- def test_contribute_post_invalid_licence(client, app, auth, licence_type, licence_file, message):
- auth.login("stan")
- expected_id = 11 # 10 resources pre-populated
- resource_files = FileStorage(stream=io.BytesIO(b"a cookery book"), filename="resource.txt")
- data = {
- "resource_title" : "res name",
- "resource_description" : "res desc",
- "licence_type" : licence_type.name,
- "licence_file" : licence_file,
- "resource_files" : resource_files,
- "local_sharing_level" : LocalSharingLevel.ALL,
- "ec_sharing_level" : EcSharingLevel.NONE,
- "ipr_holder" : "I.P.R. Holder",
- "attribution" : "Licensed",
- "accept_tos" : True,
- }
- response = client.post("/repository/contribute", data=data, follow_redirects=True)
- assert response.status_code == 200
- assert len(response.history) == 0
- assert bytes(message, "utf8") in response.data
- with app.app_context():
- assert Resource.query.filter_by(id=expected_id).first() is None
- standard_user = User.query.filter_by(username="stan").first()
- assert len(standard_user.resources) == 0
- ALL_TITLES = ["res-01", "res-02", "ac-02", "cinnamon", "ac-04", "elderflower", "fennel", "fearn", "res-07", "res-08", "res-09", "res-10"]
- @pytest.mark.parametrize(
- ("username", "primary_metadata_language", "request_args", "result_count", "visible_total", "expected_titles", "expected_texts"),
- (
- ("stan", "en", "?page=1", 5, 5, ["elderflower", "res-07", "res-08"], []),
- ("stan", "en", "?page=2", 5, 5, ["res-09", "res-10"], []),
- ("stan", "en", "?page=3", 5, 5, [], []),
- ("gobnait", "en", "?page=1", 6, 6, ["elderflower", "fennel", "res-07"], []),
- ("gobnait", "en", "?page=2", 6, 6, ["res-08", "res-09", "res-10"], []),
- ("gobnait", "ga", "?page=1", 6, 6, ["elderflower", "fearn", "res-07"], []),
- ("gobnait", "ga", "?page=2", 6, 6, ["res-08", "res-09", "res-10"], []),
- ("leo", "en", "?page=1", 6, 6, ["ac-04", "elderflower", "res-07"], []),
- ("leo", "en", "?page=2", 6, 6, ["res-08", "res-09", "res-10"], []),
- ("steph", "en", "", 7, 7, ["ac-04", "elderflower", "fennel"], ["href=\"/repository/browse?page=2\""]),
- ("steph", "en", "?page=1", 7, 7, ["ac-04", "elderflower", "fennel"], ["href=\"/repository/browse?page=2\""]),
- ("steph", "en", "?page=2", 7, 7, ["res-07", "res-08", "res-09"], ["href=\"/repository/browse?page=3\""]),
- ("steph", "en", "?page=3", 7, 7, ["res-10"], ["href=\"/repository/browse?page=2\""]),
- ("steph", "en", "?page=4", 7, 7, [], ["href=\"/repository/browse?page=3\""]),
- ("reeve", "en", "?page=1", 7, 7, ["ac-04", "elderflower", "fennel"], []),
- ("reeve", "en", "?page=2", 7, 7, ["res-07", "res-08", "res-09"], []),
- ("reeve", "en", "?page=3", 7, 7, ["res-10"], []),
- ("steph", "en", "?licence=UNKNOWN", 0, 7, [], []),
- ("steph", "en", "?licence=CC_BY_40", 0, 7, [], []),
- ("steph", "en", "?licence=PUBLIC_DOMAIN", 1, 7, ["elderflower"], []),
- ("steph", "en", "?licence=CC0_10", 2, 7, ["res-07", "res-10"], []),
- ("steph", "en", "?licence=OTHER", 4, 7, ["ac-04", "fennel", "res-08"], ["href=\"/repository/browse?page=2&licence=OTHER\""]),
- ("steph", "en", "?licence=OTHER&page=2", 4, 7, ["res-09"], ["href=\"/repository/browse?page=1&licence=OTHER\""]),
- ("steph", "ga", "?licence=OTHER", 4, 7, ["ac-04", "fearn", "res-08"], ["href=\"/repository/browse?page=2&licence=OTHER\""]),
- ("steph", "ga", "?licence=OTHER&page=2", 4, 7, ["res-09"], ["href=\"/repository/browse?page=1&licence=OTHER\""]),
- ("gobnait", "en", "?licence=OTHER", 3, 6, ["fennel", "res-08", "res-09"], []),
- ("stan", "en", "?licence=OTHER", 2, 5, ["res-08", "res-09"], []),
- ("steph", "en", "?theme=UNKNOWN", 0, 7, [], []),
- ("steph", "en", "?theme=ECONOMY", 0, 7, [], []),
- ("steph", "en", "?theme=HEALTH", 1, 7, ["ac-04"], []),
- ("steph", "en", "?theme=ENERGY", 1, 7, ["elderflower"], []),
- ("steph", "en", "?theme=ENVIRONMENT", 2, 7, ["elderflower", "res-09"], []),
- ("steph", "en", "?theme=REGIONS", 3, 7, ["fennel", "res-07", "res-09"], []),
- ("gobnait", "en", "?theme=HEALTH", 0, 6, [], []),
- ("gobnait", "en", "?theme=REGIONS", 3, 6, ["fennel", "res-07", "res-09"], []),
- ("stan", "en", "?theme=HEALTH", 0, 5, [], []),
- ("stan", "en", "?theme=REGIONS", 2, 5, ["res-07", "res-09"], []),
- ("steph", "en", "?licence=PUBLIC_DOMAIN&theme=ENVIRONMENT", 1, 7, ["elderflower"], []),
- ("steph", "en", "?licence=OTHER&theme=REGIONS", 2, 7, ["fennel", "res-09"], []),
- ("steph", "en", "?licence=CC0_10&theme=REGIONS", 1, 7, ["res-07"], []),
- ("gobnait", "en", "?licence=OTHER&theme=REGIONS", 2, 6, ["fennel", "res-09"], []),
- ("gobnait", "en", "?licence=CC0_10&theme=REGIONS", 1, 6, ["res-07"], []),
- ("stan", "en", "?licence=OTHER&theme=REGIONS", 1, 5, ["res-09"], []),
- ("stan", "en", "?licence=CC0_10&theme=REGIONS", 1, 5, ["res-07"], []),
- ("steph", "en", "?dsi=UNKNOWN", 0, 7, [], []),
- ("steph", "en", "?dsi=EESSI", 0, 7, [], []),
- ("steph", "en", "?dsi=EH", 1, 7, ["ac-04"], []),
- ("steph", "en", "?dsi=EURO", 1, 7, ["elderflower"], []),
- ("steph", "en", "?dsi=ODR", 2, 7, ["fennel", "res-10"], []),
- ("steph", "en", "?dsi=EJ", 3, 7, ["elderflower", "res-08", "res-10"], []),
- ("gobnait", "en", "?dsi=EH", 0, 6, [], []),
- ("gobnait", "en", "?dsi=EURO", 1, 6, ["elderflower"], []),
- ("gobnait", "en", "?dsi=ODR", 2, 6, ["fennel", "res-10"], []),
- ("gobnait", "en", "?dsi=EJ", 3, 6, ["elderflower", "res-08", "res-10"], []),
- ("stan", "en", "?dsi=EH", 0, 5, [], []),
- ("stan", "en", "?dsi=EURO", 1, 5, ["elderflower"], []),
- ("stan", "en", "?dsi=ODR", 1, 5, ["res-10"], []),
- ("stan", "en", "?dsi=EJ", 3, 5, ["elderflower", "res-08", "res-10"], []),
- ("steph", "en", "?licence=OTHER&dsi=ODR", 1, 7, ["fennel"], []),
- ("steph", "en", "?licence=OTHER&dsi=CS", 2, 7, ["fennel", "res-09"], []),
- ("steph", "en", "?licence=OTHER&dsi=EJ", 1, 7, ["res-08"], []),
- ("gobnait", "en", "?licence=OTHER&dsi=ODR", 1, 6, ["fennel"], []),
- ("gobnait", "en", "?licence=OTHER&dsi=CS", 2, 6, ["fennel", "res-09"], []),
- ("gobnait", "en", "?licence=OTHER&dsi=EJ", 1, 6, ["res-08"], []),
- ("stan", "en", "?licence=OTHER&dsi=ODR", 0, 5, [], []),
- ("stan", "en", "?licence=OTHER&dsi=CS", 1, 5, ["res-09"], []),
- ("stan", "en", "?licence=OTHER&dsi=EJ", 1, 5, ["res-08"], []),
- ("steph", "en", "?theme=HEALTH&dsi=EH", 1, 7, ["ac-04"], []),
- ("steph", "en", "?theme=HOUSING&dsi=ODR", 1, 7, ["fennel"], []),
- ("steph", "en", "?theme=JUSTICE&dsi=EJ", 1, 7, ["res-10"], []),
- ("gobnait", "en", "?theme=HEALTH&dsi=EH", 0, 6, [], []),
- ("gobnait", "en", "?theme=HOUSING&dsi=ODR", 1, 6, ["fennel"], []),
- ("gobnait", "en", "?theme=JUSTICE&dsi=EJ", 1, 6, ["res-10"], []),
- ("stan", "en", "?theme=HEALTH&dsi=EH", 0, 5, [], []),
- ("stan", "en", "?theme=HOUSING&dsi=ODR", 0, 5, [], []),
- ("stan", "en", "?theme=JUSTICE&dsi=EJ", 1, 5, ["res-10"], []),
- ("steph", "en", "?licence=CC0_10&theme=JUSTICE&dsi=SI", 1, 7, ["res-10"], []),
- ("steph", "en", "?licence=OTHER&theme=REGIONS&dsi=CS", 2, 7, ["fennel", "res-09"], []),
- ("gobnait", "en", "?licence=CC0_10&theme=JUSTICE&dsi=SI", 1, 6, ["res-10"], []),
- ("gobnait", "en", "?licence=OTHER&theme=REGIONS&dsi=CS", 2, 6, ["fennel", "res-09"], []),
- ("stan", "en", "?licence=CC0_10&theme=JUSTICE&dsi=SI", 1, 5, ["res-10"], []),
- ("stan", "en", "?licence=OTHER&theme=REGIONS&dsi=CS", 1, 5, ["res-09"], []),
- ("steph", "en", "?project=4", 0, 7, [], []),
- ("steph", "en", "?project=2", 1, 7, ["res-07"], []),
- ("steph", "en", "?project=3", 2, 7, ["ac-04", "res-09"], []),
- ("steph", "en", "?project=1", 4, 7, ["elderflower", "fennel", "res-08"], ["href=\"/repository/browse?page=2&project=1\""]),
- ("steph", "en", "?project=1&page=2", 4, 7, ["res-10"], ["href=\"/repository/browse?page=1&project=1\""]),
- ("gobnait", "en", "?project=1", 4, 6, ["elderflower", "fennel", "res-08"], []),
- ("gobnait", "en", "?project=1&page=2", 4, 6, ["res-10"], []),
- ("stan", "en", "?project=1", 3, 5, ["elderflower", "res-08", "res-10"], []),
- ("stan", "en", "?project=1&page=2", 3, 5, [], []),
- ("steph", "en", "?licence=CC0_10&project=1", 1, 7, ["res-10"], []),
- ("steph", "en", "?licence=OTHER&project=3", 2, 7, ["ac-04", "res-09"], []),
- ("gobnait", "en", "?licence=OTHER&project=3", 1, 6, ["res-09"], []),
- ("steph", "en", "?theme=HEALTH&project=1", 0, 7, [], []),
- ("steph", "en", "?theme=HOUSING&project=1", 1, 7, ["fennel"], []),
- ("stan", "en", "?theme=HOUSING&project=1", 0, 5, [], []),
- ("steph", "en", "?dsi=ODR&project=1", 2, 7, ["fennel", "res-10"], []),
- ("gobnait", "en", "?dsi=ODR&project=1", 2, 6, ["fennel", "res-10"], []),
- ("stan", "en", "?dsi=ODR&project=1", 1, 5, ["res-10"], []),
- ("steph", "en", "?dsi=EJ&project=1", 3, 7, ["elderflower", "res-08", "res-10"], []),
- ("steph", "en", "?licence=OTHER&theme=REGIONS&dsi=CS&project=1", 1, 7, ["fennel"], []),
- ("gobnait", "en", "?licence=OTHER&theme=REGIONS&dsi=CS&project=1", 1, 6, ["fennel"], []),
- ("stan", "en", "?licence=OTHER&theme=REGIONS&dsi=CS&project=1", 0, 5, [], []),
- ("steph", "en", "?licence=OTHER&theme=REGIONS&dsi=CS&project=3", 1, 7, ["res-09"], []),
- ("stan", "en", "?licence=OTHER&theme=REGIONS&dsi=CS&project=3", 1, 5, ["res-09"], []),
- ("steph", "en", "?licence=PUBLIC_DOMAIN&theme=ENERGY&dsi=EURO&project=2", 0, 7, [], []),
- ("steph", "en", "?licence=PUBLIC_DOMAIN&theme=ENERGY&dsi=EURO&project=1", 1, 7, ["elderflower"], []),
- ("gobnait", "en", "?licence=PUBLIC_DOMAIN&theme=ENERGY&dsi=EURO&project=1", 1, 6, ["elderflower"], []),
- ("stan", "en", "?licence=PUBLIC_DOMAIN&theme=ENERGY&dsi=EURO&project=1", 1, 5, ["elderflower"], []),
- ),
- )
- def test_browse_get_valid(app, client, auth, username, primary_metadata_language, request_args, result_count,
- visible_total, expected_titles, expected_texts):
- app.config["PRIMARY_METADATA_LANGUAGE"] = primary_metadata_language
- auth.login(username)
- response = client.get("/repository/browse{0}".format(request_args))
- assert response.status_code == 200
- for title in ALL_TITLES:
- assert (bytes(title, "utf8") in response.data) == (title in expected_titles)
- for text in expected_texts:
- assert (bytes(text, "utf8") in response.data)
- if result_count == visible_total:
- assert bytes("Total resources: {0}".format(visible_total), "utf8") in response.data
- else:
- assert bytes("Approximately {0} resource(s) found (<a href=/repository/browse>of {1} total</a>)".format(result_count, visible_total), "utf8") in response.data
- @pytest.mark.parametrize(
- ("username", "user_id", "is_staff", "request_args", "es_query", "es_total", "visible_total", "es_results", "page_no", "expected_titles", "additional_filters"),
- (
- ("steph", 7, True, "zzzzz", "zzzzz", 0, 7, [], 1, [], []),
- ("steph", 7, True, "elderflower", "elderflower", 1, 7, [5], 1, ["elderflower"], []),
- ("steph", 7, True, "dill", "dill", 1, 7, [2], 1, [], []),
- ("steph", 7, True, "aniseed", "aniseed", 2, 7, [1, 7], 1, ["res-07"], []),
- ("steph", 7, True, "fennel", "fennel", 3, 7, [2, 3, 6], 1, ["fennel"], []),
- ("steph", 7, True, "ginger", "ginger", 4, 7, [3, 4, 6], 1, ["ac-04", "fennel"], []),
- ("gobnait", 4, False, "ginger", "ginger", 4, 6, [3, 4, 6], 1, ["fennel"], []),
- ("steph", 7, True, "ginger&page=2", "ginger", 4, 7, [7], 2, ["res-07"], []),
- ("gobnait", 4, False, "ginger&page=2", "ginger", 4, 6, [7], 2, ["res-07"], []),
- ("steph", 7, True, "res", "res", 6, 7, [1, 2, 7], 1, ["res-07"], []),
- ("steph", 7, True, "res&page=1", "res", 6, 7, [1, 2, 7], 1, ["res-07"], []),
- ("steph", 7, True, "res&page=2", "res", 6, 7, [8, 9, 10], 2, ["res-08", "res-09", "res-10"], []),
- ("steph", 7, True, "ginger&theme=REGIONS", "ginger", 4, 7, [3, 4, 6], 1, ["fennel"], [{"multi_match": {"query": "REGIONS","fields": ["get_themes_*"]}}]),
- ("gobnait", 4, False, "ginger&theme=REGIONS", "ginger", 4, 6, [3, 4, 6], 1, ["fennel"], [{"multi_match": {"query": "REGIONS","fields": ["get_themes_*"]}}]),
- ("steph", 7, True, "ginger&theme=REGIONS&page=2", "ginger", 4, 7, [7], 2, ["res-07"], [{"multi_match": {"query": "REGIONS","fields": ["get_themes_*"]}}]),
- ("gobnait", 4, False, "ginger&theme=REGIONS&page=2", "ginger", 4, 6, [7], 2, ["res-07"], [{"multi_match": {"query": "REGIONS","fields": ["get_themes_*"]}}]),
- ("steph", 7, True, "ginger&licence=OTHER", "ginger", 4, 7, [3, 4, 6], 1, ["ac-04", "fennel"], [{"match": {"get_licence_0": "OTHER"}}]),
- ("gobnait", 4, False, "ginger&licence=OTHER", "ginger", 4, 6, [3, 4, 6], 1, ["fennel"], [{"match": {"get_licence_0": "OTHER"}}]),
- ("steph", 7, True, "ginger&licence=OTHER&page=2", "ginger", 4, 7, [7], 2, [], [{"match": {"get_licence_0": "OTHER"}}]),
- ("gobnait", 4, False, "ginger&licence=OTHER&page=2", "ginger", 4, 6, [7], 2, [], [{"match": {"get_licence_0": "OTHER"}}]),
- ("steph", 7, True, "ginger&project=1", "ginger", 4, 7, [3, 4, 6], 1, ["fennel"], [{"term" : {"funding_project_id": 1}}]),
- ("steph", 7, True, "ginger&dsi=EH", "ginger", 4, 7, [3, 4, 6], 1, ["ac-04"], [{"multi_match" : {"query" : "EH", "fields" : ["get_dsis_*"]}}]),
- ),
- )
- def test_search_resources_repository(app, client, auth, username, user_id, is_staff, request_args, es_query, es_total,
- visible_total, es_results, page_no, expected_titles, additional_filters):
- auth.login(username)
- app.elasticsearch.search.return_value = {
- "hits": {
- "total": {"value": es_total},
- "hits": [{"_id" : r} for r in es_results]
- }
- }
- response = client.get("/repository/browse/search?q={0}".format(request_args))
- assert response.status_code == 200
- for title in ALL_TITLES:
- assert (bytes(title, "utf8") in response.data) == (title in expected_titles)
- assert bytes("Approximately {0} resource(s) found (<a href=/repository/browse>of {1} total</a>)".format(es_total, visible_total), "utf8") in response.data
- expected_search_query = {
- "bool": {
- "must": {
- "multi_match": {
- "query": es_query,
- "fields": ["get_titles_*", "get_descriptions_*"]
- }
- },
- "filter": [
- {
- "match": {
- "local_status": "PUBLISHED"
- }
- }
- ]
- }
- }
- for additional_filter in additional_filters:
- expected_search_query["bool"]["filter"].append(additional_filter)
- if not is_staff:
- expected_search_query["bool"]["filter"].append({
- "bool": {
- "should": [
- {
- "term": {
- "owner_id": user_id
- }
- },
- {
- "match": {
- "local_sharing_level": "ALL"
- }
- }
- ]
- }
- })
- page_size = app.config["RESOURCES_PER_PAGE_REPOSITORY"]
- from_ = (page_no - 1) * page_size
- app.elasticsearch.search.assert_called_once_with(index="resources", query=expected_search_query, from_=from_, size=page_size)
- @pytest.mark.parametrize(
- ("username", "resource_id", "visible", "resource_title", "expected_view_count", "contact_email", "dsis", "themes", "linguality_type", "languages", "format", "encoding", "size"),
- (
- ("una", 1, False, "res-01", 0, "gobnait@test.com", [], [], "Monolingual", ["ga"], "text/plain", "utf8", "0 Unknown"),
- ("una", 5, False, "elderflower", 0, "leo@test.com", [Dsi.EJ.value, Dsi.EURO.value], [Theme.ENERGY, Theme.ENVIRONMENT], "Bilingual", ["en", "ga"], "application/x-tmx+xml", "utf16", "55555 Translation Units"),
- ("gobnait", 1, False, "res-01", 0, "gobnait@test.com", [], [], "Monolingual", ["ga"], "text/plain", "utf8", "0 Unknown"),
- ("gobnait", 2, False, "res-02", 0, "gobnait@test.com", [], [], "Monolingual", ["ga"], "text/plain", "utf8", "0 Unknown"),
- ("gobnait", 3, False, "cinnamon", 0, "gobnait@test.com", [], [], "Monolingual", ["ga"], "text/plain", "utf8", "0 Unknown"),
- ("gobnait", 4, False, "ac-04", 2, "leo@test.com", [Dsi.EH.value], [Theme.HEALTH], "Monolingual", ["zz"], "text/plain", "ascii", "44444 Words"),
- ("gobnait", 5, True, "elderflower", 1, "leo@test.com", [Dsi.EJ.value, Dsi.EURO.value], [Theme.ENERGY, Theme.ENVIRONMENT], "Bilingual", ["en", "ga"], "application/x-tmx+xml", "utf16", "55555 Translation Units"),
- ("gobnait", 6, True, "fennel", 7, "gobnait@test.com", [], [Theme.HOUSING, Theme.REGIONS], "Multilingual", ["ga", "en", "de"], "text/csv", "utf8", "66666 Terms"),
- ("leo", 4, True, "ac-04", 3, "leo@test.com", [Dsi.EH.value], [Theme.HEALTH], "Monolingual", ["zz"], "text/plain", "ascii", "44444 Words"),
- ("leo", 5, True, "elderflower", 1, "leo@test.com", [Dsi.EJ.value, Dsi.EURO.value], [Theme.ENERGY, Theme.ENVIRONMENT], "Bilingual", ["en", "ga"], "application/x-tmx+xml", "utf16", "55555 Translation Units"),
- ("leo", 6, False, "fennel", 7, "gobnait@test.com", [], [Theme.HOUSING, Theme.REGIONS], "Multilingual", ["ga", "en", "de"], "text/csv", "utf8", "66666 Terms"),
- ("steph", 4, True, "ac-04", 2, "leo@test.com", [Dsi.EH.value], [Theme.HEALTH], "Monolingual", ["zz"], "text/plain", "ascii", "44444 Words"),
- ("steph", 5, True, "elderflower", 1, "leo@test.com", [Dsi.EJ.value, Dsi.EURO.value], [Theme.ENERGY, Theme.ENVIRONMENT], "Bilingual", ["en", "ga"], "application/x-tmx+xml", "utf16", "55555 Translation Units"),
- ("steph", 6, True, "fennel", 7, "gobnait@test.com", [], [Theme.HOUSING, Theme.REGIONS], "Multilingual", ["ga", "en", "de"], "text/csv", "utf8", "66666 Terms"),
- ("reeve", 0, False, "res-00", 0, "unknown@test.com", [], [], "Unknown", [], "OTHER", "", "0 Unknown"),
- ("reeve", 4, True, "ac-04", 3, "leo@test.com", [Dsi.EH.value], [Theme.HEALTH], "Monolingual", ["zz"], "text/plain", "ascii", "44444 Words"),
- ),
- )
- def test_detail_get_authenticated(client, app, auth, username, resource_id, visible, resource_title,
- expected_view_count, contact_email, dsis, themes, linguality_type, languages, format, encoding, size):
- auth.login(username)
- response = client.get("/repository/{0}".format(resource_id))
- assert response.status_code == 200
- assert (bytes(resource_title, "utf8") in response.data) == visible
- assert (bytes(contact_email, "utf8") in response.data) == visible
- assert (bytes(linguality_type, "utf8") in response.data) == visible
- for language in languages:
- assert (bytes("<li>{0}</li>".format(language), "utf8") in response.data) == visible
- assert (bytes(format, "utf8") in response.data) == visible
- if encoding:
- assert (bytes(encoding, "utf8") in response.data) == visible
- assert (bytes(size, "utf8") in response.data) == visible
- for dsi in dsis:
- assert (bytes(str(dsi.display_name), "utf8") in response.data) == visible
- for theme in themes:
- assert (bytes(str(theme.value.display_name), "utf8") in response.data) == visible
- if resource_id > 0:
- with app.app_context():
- assert Resource.query.filter_by(id=resource_id).first().get_view_count() == expected_view_count
- @pytest.mark.parametrize(
- ("username", "resource_id", "resource_filename", "expected_download_count"),
- (
- ("leo", 4, "resource/processed_resource4.txt", 1),
- ("leo", 5, "resource/processed_resource5.txt", 2),
- ("gobnait", 5, "resource/processed_resource5.txt", 2),
- ("gobnait", 6, "resource/processed_resource6.txt", 4),
- ("steph", 4, "resource/processed_resource4.txt", 1),
- ("steph", 5, "resource/processed_resource5.txt", 2),
- ("steph", 6, "resource/processed_resource6.txt", 4),
- ("reeve", 4, "resource/processed_resource4.txt", 1),
- ("reeve", 5, "resource/processed_resource5.txt", 1),
- ("reeve", 6, "resource/processed_resource6.txt", 5),
- ),
- )
- def test_download_valid(client, app, auth, username, resource_id, resource_filename, expected_download_count):
- auth.login(username)
- app.config["UPLOAD_FOLDER"] = "test-res/repository"
- response = client.post("/repository/{0}/download".format(resource_id), data={"accept_licence": True}, follow_redirects=True)
- assert response.status_code == 200
- with zipfile.ZipFile(io.BytesIO(response.data)) as archive:
- assert resource_filename in archive.namelist()
- with app.app_context():
- assert Resource.query.filter_by(id=resource_id).first().get_download_count() == expected_download_count
- @pytest.mark.parametrize(
- ("username", "resource_id", "expected_text"),
- (
- ("una", 1, "Access Denied"),
- ("gobnait", 1, "Access Denied"),
- ("gobnait", 2, "Access Denied"),
- ("gobnait", 3, "Access Denied"),
- ("gobnait", 4, "Access Denied"),
- ("reeve", 0, "Not Found"),
- ),
- )
- def test_download_invalid(client, app, auth, username, resource_id, expected_text):
- auth.login(username)
- response = client.post("/repository/{0}/download".format(resource_id), data={"accept_licence": True}, follow_redirects=True)
- assert response.status_code == 200
- assert bytes(expected_text, "utf8") in response.data
- with pytest.raises(zipfile.BadZipFile):
- with zipfile.ZipFile(io.BytesIO(response.data)) as archive:
- pass
- if resource_id > 0:
- with app.app_context():
- assert Resource.query.filter_by(id=resource_id).first().get_download_count() == 0
- @pytest.mark.parametrize(
- ("username", "resource_id"),
- (
- ("leo", 4),
- ("leo", 5),
- ("gobnait", 5),
- ("gobnait", 6),
- ("steph", 4),
- ("steph", 5),
- ("steph", 6),
- ("reeve", 4),
- ("reeve", 5),
- ("reeve", 6),
- ),
- )
- def test_download_licence_valid(client, app, auth, username, resource_id):
- auth.login(username)
- app.config["UPLOAD_FOLDER"] = "test-res/repository"
- response = client.get("/repository/{0}/download-licence".format(resource_id))
- assert response.status_code == 200
- assert response.data == bytes("Licence for resource #{0}\n".format(resource_id), "utf8")
- @pytest.mark.parametrize(
- ("username", "resource_id", "expected_text"),
- (
- ("una", 1, "Access Denied"),
- ("gobnait", 1, "Access Denied"),
- ("gobnait", 2, "Access Denied"),
- ("gobnait", 3, "Access Denied"),
- ("gobnait", 4, "Access Denied"),
- ("reeve", 0, "Not Found"),
- ),
- )
- def test_download_licence_invalid(client, app, auth, username, resource_id, expected_text):
- auth.login(username)
- response = client.get("/repository/{0}/download-licence".format(resource_id))
- assert response.status_code == 200
- assert bytes(expected_text, "utf8") in response.data
- def test_detail_view_counts(client, auth, app):
- resource_id = 6
- with app.app_context():
- resource = Resource.query.filter_by(id=resource_id).first()
- for stat in resource.stats:
- db.session.delete(stat)
- db.session.commit()
- views = [
- ("gobnait", 1),
- ("gobnait", 1), # Previously viewed
- ("steph", 2),
- ("reeve", 3),
- ("stan", 3), # Not authorized
- ("gobnait", 3), # Previously viewed
- ("ada", 4),
- ]
- for username, expected_view_count in views:
- auth.login(username)
- client.get("/repository/{0}".format(resource_id))
- with app.app_context():
- assert Resource.query.filter_by(id=resource_id).first().get_view_count() == expected_view_count
- def test_download_counts(client, auth, app):
- app.config["UPLOAD_FOLDER"] = "test-res/repository"
- resource_id = 6
- with app.app_context():
- resource = Resource.query.filter_by(id=resource_id).first()
- for stat in resource.stats:
- db.session.delete(stat)
- db.session.commit()
- downloads = [
- ("stan", 0), # Not authorized
- ("gobnait", 1),
- ("steph", 2),
- ("ada", 3),
- ("reeve", 4),
- ("steph", 4), # Previously downloaded
- ]
- for username, expected_download_count in downloads:
- auth.login(username)
- client.get("/repository/{0}/download".format(resource_id))
- with app.app_context():
- assert Resource.query.filter_by(id=resource_id).first().get_download_count() == expected_download_count
|