import pytest from datetime import timedelta from unittest import mock from werkzeug.security import generate_password_hash from stor import create_app, db from stor.auth.models import Group, User from stor.repository.definitions import ( Dsi, EcResourceStatus, EcSharingLevel, FundingType, LicenceConditionType, LicenceType, LingualityType, LocalResourceStatus, LocalSharingLevel, MultilingualityType, SizeUnit, Theme ) from stor.repository.models import ( FundingProject, Licence, LicenceCondition, LicenceText, Resource, ResourceAttribution, ResourceDescription, ResourceDsi, ResourceFileInfo, ResourceFileLanguage, ResourceIprHolder, ResourceStat, ResourceTheme, ResourceTitle ) PASS = "Password123!" PASS_HASH = generate_password_hash(PASS) class TestingConfig(): CONTACT_EMAIL = "stor@test.com" DOC_URL = "http://127.0.0.1:5001/doc" ELASTICSEARCH_URL = None EMAIL_VERIFICATION_TOKEN_EXPIRY = 3 LANGUAGES = {"en" : "English", "ga" : "Gaeilge"} PASSWORD_RESET_TOKEN_EXPIRY = 3 PERMANENT_SESSION_LIFETIME = timedelta(minutes=2) PLATFORM_NAME = "STÓR" PRIMARY_METADATA_LANGUAGE = "en" PROJECTS_PER_PAGE = 12 RESOURCES_PER_PAGE_EDITOR = 4 RESOURCES_PER_PAGE_REPOSITORY = 3 SECRET_KEY = "test" SQLALCHEMY_DATABASE_URI = "sqlite:///:memory:" SQLALCHEMY_TRACK_MODIFICATIONS = False TESTING = True TM_URL = "http://127.0.0.1:5001/tm" USERS_PER_PAGE = 4 WTF_CSRF_ENABLED = False MAIL_SERVER = "localhost" MAIL_PORT = 8025 MAIL_USERNAME = "user" MAIL_PASSWORD = "pass" MAIL_DEFAULT_SENDER = "no-reply@localhost" MAIL_BACKEND = "locmem" @pytest.fixture def app(tmp_path): app = create_app(TestingConfig) app.config["UPLOAD_FOLDER"] = tmp_path with app.app_context(): db.create_all() admin_group = Group(id=1, name="admin") reviewer_group = Group(id=2, name="reviewer") staff_group = Group(id=3, name="staff") standard_group = Group(id=4, name="standard") db.session.add_all([admin_group, reviewer_group, standard_group]) db.session.add(User( id=1, username="ada", email="ada@test.com", email_verified=True, activated=True, password_rotated=True, first_name="Ada", last_name="Min", organization_name="University of Rain", _password=PASS_HASH, groups=[standard_group, staff_group, reviewer_group, admin_group], )) db.session.add(User( id=2, username="reeve", email="reeve@test.com", email_verified=True, activated=True, password_rotated=True, first_name="Reeve", last_name="Ewing", organization_name="University of Rain", _password=PASS_HASH, groups=[standard_group, staff_group, reviewer_group], )) db.session.add(User( id=3, username="stan", email="stan@test.com", email_verified=True, activated=True, password_rotated=True, first_name="Stan", last_name="Ford", organization_name="Dept of Sunshine", _password=PASS_HASH, groups=[standard_group], )) db.session.add(User( id=4, username="gobnait", email="gobnait@test.com", email_verified=True, activated=True, password_rotated=True, first_name="Gobnait", last_name="Ní Mhurchú", organization_name="Dept of Sunshine", organization_address="123 Department Street", organization_phone="(01)1234567", position_in_organization="Officer", _password=PASS_HASH, groups=[standard_group], )) db.session.add(User( id=5, username="leo", email="leo@test.com", email_verified=True, activated=True, password_rotated=True, first_name="Leo", last_name="Cat", organization_name="Dept of Sunshine", _password=PASS_HASH, groups=[standard_group], )) db.session.add(User( id=6, username="una", email="una@test.com", email_verified=True, activated=True, password_rotated=True, first_name="Úna", last_name="Ssigned", organization_name="Dept of Sunshine", _password=PASS_HASH, )) db.session.add(User( id=7, username="steph", email="steph@test.com", email_verified=True, activated=True, password_rotated=True, first_name="Steph", last_name="Anie", organization_name="Dept of Sunshine", _password=PASS_HASH, groups=[standard_group, staff_group], )) db.session.add(User( id=8, username="vera", email="vera@test.com", password_rotated=True, first_name="Vera", last_name="Fication-Pending", organization_name="Dept of Sunshine", _password=PASS_HASH, )) db.session.add(User( id=9, username="ina", email="ina@test.com", email_verified=True, password_rotated=True, first_name="Ina", last_name="Ctive", organization_name="Dept of Sunshine", _password=PASS_HASH, )) db.session.add(User( id=10, username="maya", email="maya@test.com", email_verified=True, activated=True, first_name="Maya", last_name="Grated-User", organization_name="Dept of Sunshine", _password=PASS_HASH, groups=[standard_group], )) db.session.add(FundingProject(id=1, name="Funding Project A", short_name="FPA", funding_type=FundingType.NATIONAL_FUNDS, funding_country="IE")) db.session.add(FundingProject(id=2, name="Funding Project B", short_name="FPB", funding_type=FundingType.OTHER)) db.session.add(FundingProject(id=3, name="Funding Project C", short_name="FPC", funding_type=FundingType.EU_FUNDS, funding_country="EU")) db.session.add(FundingProject(id=4, name="Funding Project D", short_name="FPD", funding_type=FundingType.OWN_FUNDS)) db.session.add(Resource( id=1, uuid="uuid1", owner_id=4, contact_id=7, local_sharing_level=LocalSharingLevel.ALL, ec_sharing_level=EcSharingLevel.NONE, local_status=LocalResourceStatus.RAW, ec_status=EcResourceStatus.NOT_APPLICABLE, metadata_creator_id=2, )) db.session.add(Resource( id=2, uuid="uuid2", owner_id=4, contact_id=4, local_sharing_level=LocalSharingLevel.ALL, ec_sharing_level=EcSharingLevel.INTERNAL, local_status=LocalResourceStatus.PROCESSED, ec_status=EcResourceStatus.NOT_RELAYED, metadata_creator_id=2, funding_project_id=1, )) db.session.add(Resource( id=3, uuid="uuid3", owner_id=4, contact_id=4, local_sharing_level=LocalSharingLevel.INTERNAL, ec_sharing_level=EcSharingLevel.NONE, local_status=LocalResourceStatus.VALIDATED, ec_status=EcResourceStatus.NOT_APPLICABLE, metadata_creator_id=2, personal_data_included=True, funding_project_id=1, )) db.session.add(Resource( id=4, uuid="uuid4", owner_id=5, contact_id=5, local_sharing_level=LocalSharingLevel.INTERNAL, ec_sharing_level=EcSharingLevel.INTERNAL, local_status=LocalResourceStatus.PUBLISHED, ec_status=EcResourceStatus.NOT_RELAYED, funding_project_id=3, )) db.session.add(Resource( id=5, uuid="uuid5", owner_id=5, contact_id=5, local_sharing_level=LocalSharingLevel.ALL, ec_sharing_level=EcSharingLevel.ALL, local_status=LocalResourceStatus.PUBLISHED, ec_status=EcResourceStatus.RELAYED, metadata_creator_id=2, sensitive_data_included=True, funding_project_id=1, )) db.session.add(Resource( id=6, uuid="uuid6", owner_id=4, contact_id=4, metadata_creator_id=2, local_sharing_level=LocalSharingLevel.INTERNAL, ec_sharing_level=EcSharingLevel.INTERNAL, local_status=LocalResourceStatus.PUBLISHED, ec_status=EcResourceStatus.NOT_RELAYED, sensitive_data_included=True, sensitive_data_additional="Trade secrets", anonymized=True, anonymized_additional="regex-based", funding_project_id=1, )) db.session.add(Resource( id=7, uuid="uuid7", owner_id=7, contact_id=7, metadata_creator_id=2, local_sharing_level=LocalSharingLevel.ALL, ec_sharing_level=EcSharingLevel.INTERNAL, local_status=LocalResourceStatus.PUBLISHED, ec_status=EcResourceStatus.NOT_RELAYED, funding_project_id=2, )) db.session.add(Resource( id=8, uuid="uuid8", owner_id=4, contact_id=4, metadata_creator_id=2, local_sharing_level=LocalSharingLevel.ALL, ec_sharing_level=EcSharingLevel.INTERNAL, local_status=LocalResourceStatus.PUBLISHED, ec_status=EcResourceStatus.NOT_RELAYED, funding_project_id=1, )) db.session.add(Resource( id=9, uuid="uuid9", owner_id=7, contact_id=7, metadata_creator_id=1, local_sharing_level=LocalSharingLevel.ALL, ec_sharing_level=EcSharingLevel.INTERNAL, local_status=LocalResourceStatus.PUBLISHED, ec_status=EcResourceStatus.NOT_RELAYED, funding_project_id=3, )) db.session.add(Resource( id=10, uuid="uuid10", owner_id=7, contact_id=7, metadata_creator_id=2, local_sharing_level=LocalSharingLevel.ALL, ec_sharing_level=EcSharingLevel.INTERNAL, local_status=LocalResourceStatus.PUBLISHED, ec_status=EcResourceStatus.NOT_RELAYED, funding_project_id=1, )) db.session.add(ResourceTitle(resource_id=1, language="en", content="res-01")) db.session.add(ResourceTitle(resource_id=2, language="en", content="res-02")) db.session.add(ResourceTitle(resource_id=2, language="ga", content="ac-02")) db.session.add(ResourceTitle(resource_id=3, language="en", content="cinnamon")) db.session.add(ResourceTitle(resource_id=4, language="ga", content="ac-04")) db.session.add(ResourceTitle(resource_id=5, language="en", content="elderflower")) db.session.add(ResourceTitle(resource_id=6, language="en", content="fennel")) db.session.add(ResourceTitle(resource_id=6, language="ga", content="fearn")) db.session.add(ResourceTitle(resource_id=7, language="en", content="res-07")) db.session.add(ResourceTitle(resource_id=8, language="en", content="res-08")) db.session.add(ResourceTitle(resource_id=9, language="en", content="res-09")) db.session.add(ResourceTitle(resource_id=10, language="en", content="res-10")) db.session.add(ResourceDescription(resource_id=1, language="en", content="aniseed, basil")) db.session.add(ResourceDescription(resource_id=2, language="en", content="dill, fennel, lime")) db.session.add(ResourceDescription(resource_id=3, language="en", content="fennel, ginger")) db.session.add(ResourceDescription(resource_id=4, language="en", content="ginger")) db.session.add(ResourceDescription(resource_id=5, language="ga", content="acmhainn eile")) db.session.add(ResourceDescription(resource_id=6, language="en", content="lime, basil, ginger")) db.session.add(ResourceDescription(resource_id=6, language="ga", content="luis, beith, gort")) db.session.add(ResourceDescription(resource_id=7, language="en", content="aniseed, ginger")) db.session.add(ResourceDescription(resource_id=8, language="en", content="resource #08")) db.session.add(ResourceDescription(resource_id=9, language="en", content="resource #09")) db.session.add(ResourceDescription(resource_id=10, language="en", content="resource #10")) db.session.add(ResourceDsi(resource_id=4, dsi=Dsi.EH)) db.session.add(ResourceDsi(resource_id=5, dsi=Dsi.EJ)) db.session.add(ResourceDsi(resource_id=5, dsi=Dsi.EURO)) db.session.add(ResourceDsi(resource_id=6, dsi=Dsi.ODR)) db.session.add(ResourceDsi(resource_id=6, dsi=Dsi.CS)) db.session.add(ResourceDsi(resource_id=8, dsi=Dsi.EJ)) db.session.add(ResourceDsi(resource_id=9, dsi=Dsi.CS)) db.session.add(ResourceDsi(resource_id=10, dsi=Dsi.EJ)) db.session.add(ResourceDsi(resource_id=10, dsi=Dsi.ODR)) db.session.add(ResourceDsi(resource_id=10, dsi=Dsi.SI)) db.session.add(ResourceTheme(resource_id=4, theme=Theme.HEALTH)) db.session.add(ResourceTheme(resource_id=5, theme=Theme.ENERGY)) db.session.add(ResourceTheme(resource_id=5, theme=Theme.ENVIRONMENT)) db.session.add(ResourceTheme(resource_id=6, theme=Theme.HOUSING)) db.session.add(ResourceTheme(resource_id=6, theme=Theme.REGIONS)) db.session.add(ResourceTheme(resource_id=7, theme=Theme.REGIONS)) db.session.add(ResourceTheme(resource_id=9, theme=Theme.REGIONS)) db.session.add(ResourceTheme(resource_id=9, theme=Theme.TRANSPORT)) db.session.add(ResourceTheme(resource_id=9, theme=Theme.ENVIRONMENT)) db.session.add(ResourceTheme(resource_id=10, theme=Theme.JUSTICE)) db.session.add(Licence(id=1, resource_id=1, type=LicenceType.CC_BY_SA_40)) db.session.add(Licence(id=2, resource_id=2, type=LicenceType.PSI, name="Terms for PSI-compliant resources")) db.session.add(Licence(id=3, resource_id=3, type=LicenceType.CC_BY_40)) db.session.add(Licence(id=4, resource_id=4, type=LicenceType.OTHER)) db.session.add(Licence(id=5, resource_id=5, type=LicenceType.PUBLIC_DOMAIN, name="Terms for Public Domain resources")) db.session.add(Licence(id=6, resource_id=6, type=LicenceType.OTHER, name="Special licence")) db.session.add(Licence(id=7, resource_id=7, type=LicenceType.CC0_10)) db.session.add(Licence(id=8, resource_id=8, type=LicenceType.OTHER)) db.session.add(Licence(id=9, resource_id=9, type=LicenceType.OTHER)) db.session.add(Licence(id=10, resource_id=10, type=LicenceType.CC0_10)) db.session.add(LicenceText(licence_id=2, language="en", content="PSI Text")) db.session.add(LicenceText(licence_id=5, language="en", content="The resource is free of all known legal restrictions.")) db.session.add(LicenceText(licence_id=5, language="ga", content="Tá an acmhainn glan ar gach srian dlí atá aitheanta.")) db.session.add(LicenceText(licence_id=6, language="en", content="Special terms")) db.session.add(LicenceText(licence_id=6, language="ga", content="Téarmaí speisialta")) db.session.add(LicenceCondition(licence_id=1, condition=LicenceConditionType.ATTRIBUTION)) db.session.add(LicenceCondition(licence_id=1, condition=LicenceConditionType.SHARE_ALIKE)) db.session.add(LicenceCondition(licence_id=3, condition=LicenceConditionType.ATTRIBUTION)) db.session.add(LicenceCondition(licence_id=6, condition=LicenceConditionType.ATTRIBUTION)) db.session.add(LicenceCondition(licence_id=6, condition=LicenceConditionType.NON_COMMERCIAL)) db.session.add(LicenceCondition(licence_id=6, condition=LicenceConditionType.NO_DERIVATIVES)) db.session.add(ResourceAttribution(resource_id=1, language="en", content="Resource 1 attribution")) db.session.add(ResourceAttribution(resource_id=6, language="en", content="Details")) db.session.add(ResourceAttribution(resource_id=6, language="ga", content="Sonraí")) db.session.add(ResourceIprHolder(resource_id=3)) db.session.add(ResourceIprHolder( resource_id=6, person_first_name="I.P.R.", person_last_name="Holden", organization_name="Dept of Thunderstorms", email="ipr.holden@test.com", )) db.session.add(ResourceIprHolder( resource_id=6, organization_name="Dept of Mysteries", email="mysteries@test.com", address="123 Mystery Street", phone="(01) 123 4567", )) db.session.add(ResourceIprHolder( resource_id=6, person_last_name="Surname", email="surname@test.com", )) db.session.add(ResourceFileInfo( id=1, resource_id=1, linguality_type=LingualityType.MONOLINGUAL, multilinguality_type=MultilingualityType.NOT_APPLICABLE, format="", character_encoding="utf8", size=0, size_unit=SizeUnit.OTHER, )) db.session.add(ResourceFileInfo( id=2, resource_id=2, linguality_type=LingualityType.MONOLINGUAL, multilinguality_type=MultilingualityType.NOT_APPLICABLE, format="text/plain", character_encoding="utf8", size=222, size_unit=SizeUnit.WORDS, )) db.session.add(ResourceFileInfo( id=4, resource_id=4, linguality_type=LingualityType.MONOLINGUAL, multilinguality_type=MultilingualityType.NOT_APPLICABLE, format="text/plain", character_encoding="ascii", size=44444, size_unit=SizeUnit.WORDS, )) db.session.add(ResourceFileInfo( id=5, resource_id=5, linguality_type=LingualityType.BILINGUAL, multilinguality_type=MultilingualityType.PARALLEL, format="application/x-tmx+xml", character_encoding="utf16", size=55555, size_unit=SizeUnit.TRANSLATION_UNITS, )) db.session.add(ResourceFileInfo( id=6, resource_id=6, linguality_type=LingualityType.MULTILINGUAL, multilinguality_type=MultilingualityType.COMPARABLE, format="text/csv", character_encoding="utf8", size=66666, size_unit=SizeUnit.TERMS, )) db.session.add(ResourceFileInfo( id=7, resource_id=6, linguality_type=LingualityType.BILINGUAL, multilinguality_type=MultilingualityType.PARALLEL, format="application/x-tmx+xml", character_encoding="utf8", size=246, size_unit=SizeUnit.TRANSLATION_UNITS, )) db.session.add(ResourceFileInfo( id=8, resource_id=6, linguality_type=LingualityType.BILINGUAL, multilinguality_type=MultilingualityType.OTHER, format="text/plain", character_encoding="big5solaris", size=664422, size_unit=SizeUnit.SENTENCES, )) db.session.add(ResourceFileInfo( id=9, resource_id=1, linguality_type=LingualityType.UNKNOWN, multilinguality_type=MultilingualityType.UNKNOWN, format="text/plain", character_encoding="utf8", size=654321, size_unit=SizeUnit.OTHER, )) db.session.add(ResourceFileInfo( id=10, resource_id=5, linguality_type=LingualityType.MONOLINGUAL, multilinguality_type=MultilingualityType.PARALLEL, format="text/plain", character_encoding="utf8", size=6420, size_unit=SizeUnit.WORDS, )) db.session.add(ResourceFileInfo( id=11, resource_id=7, linguality_type=LingualityType.BILINGUAL, multilinguality_type=MultilingualityType.PARALLEL, format="application/x-tmx+xml", character_encoding="utf8", size=711, size_unit=SizeUnit.TRANSLATION_UNITS, )) db.session.add(ResourceFileInfo( id=12, resource_id=8, linguality_type=LingualityType.BILINGUAL, multilinguality_type=MultilingualityType.PARALLEL, format="application/x-tmx+xml", character_encoding="utf8", size=812, size_unit=SizeUnit.TRANSLATION_UNITS, )) db.session.add(ResourceFileInfo( id=13, resource_id=9, linguality_type=LingualityType.BILINGUAL, multilinguality_type=MultilingualityType.PARALLEL, format="application/x-tmx+xml", character_encoding="utf8", size=913, size_unit=SizeUnit.TRANSLATION_UNITS, )) db.session.add(ResourceFileInfo( id=14, resource_id=10, linguality_type=LingualityType.BILINGUAL, multilinguality_type=MultilingualityType.PARALLEL, format="application/x-tmx+xml", character_encoding="utf8", size=1014, size_unit=SizeUnit.TRANSLATION_UNITS, )) db.session.add(ResourceFileLanguage(resource_file_info_id=2, language="ga")) db.session.add(ResourceFileLanguage(resource_file_info_id=4, language="zz")) db.session.add(ResourceFileLanguage(resource_file_info_id=5, language="en")) db.session.add(ResourceFileLanguage(resource_file_info_id=5, language="ga")) db.session.add(ResourceFileLanguage(resource_file_info_id=6, language="ga")) db.session.add(ResourceFileLanguage(resource_file_info_id=6, language="en")) db.session.add(ResourceFileLanguage(resource_file_info_id=6, language="de")) db.session.add(ResourceFileLanguage(resource_file_info_id=7, language="ga")) db.session.add(ResourceFileLanguage(resource_file_info_id=7, language="en")) db.session.add(ResourceFileLanguage(resource_file_info_id=8, language="ga")) db.session.add(ResourceFileLanguage(resource_file_info_id=8, language="en")) db.session.add(ResourceFileLanguage(resource_file_info_id=11, language="ga")) db.session.add(ResourceFileLanguage(resource_file_info_id=11, language="en")) db.session.add(ResourceFileLanguage(resource_file_info_id=12, language="ga")) db.session.add(ResourceFileLanguage(resource_file_info_id=12, language="en")) db.session.add(ResourceFileLanguage(resource_file_info_id=13, language="ga")) db.session.add(ResourceFileLanguage(resource_file_info_id=13, language="en")) db.session.add(ResourceFileLanguage(resource_file_info_id=14, language="ga")) db.session.add(ResourceFileLanguage(resource_file_info_id=14, language="en")) db.session.add(ResourceStat(resource_id=4, user_id=7, action='v')) db.session.add(ResourceStat(resource_id=4, user_id=3, action='v')) db.session.add(ResourceStat(resource_id=5, user_id=2, action='d')) db.session.add(ResourceStat(resource_id=6, user_id=1, action='v')) db.session.add(ResourceStat(resource_id=6, user_id=2, action='v')) db.session.add(ResourceStat(resource_id=6, user_id=3, action='v')) db.session.add(ResourceStat(resource_id=6, user_id=4, action='v')) db.session.add(ResourceStat(resource_id=6, user_id=5, action='v')) db.session.add(ResourceStat(resource_id=6, user_id=6, action='v')) db.session.add(ResourceStat(resource_id=6, user_id=7, action='v')) db.session.add(ResourceStat(resource_id=6, user_id=1, action='d')) db.session.add(ResourceStat(resource_id=6, user_id=3, action='d')) db.session.add(ResourceStat(resource_id=6, user_id=4, action='d')) db.session.add(ResourceStat(resource_id=6, user_id=7, action='d')) db.session.commit() app.elasticsearch = mock.Mock() yield app @pytest.fixture def client(app): return app.test_client() class AuthActions: def __init__(self, client): self._client = client def login(self, username): return self._client.post( "/auth/login", data={"username" : username, "password" : PASS}, ) def logout(self): return self._client.get("/auth/logout") @pytest.fixture def auth(client): return AuthActions(client)