From dde60081f996bd04b2471bbb322ab820199539c0 Mon Sep 17 00:00:00 2001 From: Stefan Kasberger Date: Tue, 27 Apr 2021 00:21:56 +0200 Subject: [PATCH] re-factor default unit tests --- .gitignore | 1 - setup.cfg | 1 + src/dvtests/config.py | 1 + src/dvtests/testing/conftest.py | 83 ++++++++++- .../system/testdata_authentication.json | 21 +++ .../default/unit/testdata_api.json | 38 +++++ .../default/unit/testdata_authentication.json | 22 +++ .../default/unit/testdata_installation.json | 25 ++++ .../default/unit/testdata_oaipmh.json | 51 +++++++ .../default/unit/testdata_robots-txt.json | 10 ++ .../default/unit/testdata_sitemap.json | 10 ++ .../default/unit/testdata_user.json | 9 ++ src/dvtests/testing/default/conftest.py | 2 + .../end2end/test_user_authentication.py | 98 ------------- .../default/{end2end => system}/__init__.py | 0 .../default/system/test_authentication.py | 79 ++++++++++ .../{end2end => system}/test_datafiles.py | 0 .../{end2end => system}/test_datasets.py | 0 .../{end2end => system}/test_dataverses.py | 0 .../{end2end => system}/test_search.py | 0 src/dvtests/testing/default/unit/test_api.py | 138 +++++------------- .../default/unit/test_authentication.py | 44 ++++++ .../testing/default/unit/test_installation.py | 70 +++++++++ .../testing/default/unit/test_oaipmh.py | 48 +++--- .../testing/default/unit/test_resources.py | 32 ---- .../testing/default/unit/test_robots_txt.py | 49 ++++--- .../testing/default/unit/test_sitemap.py | 46 ++++-- src/dvtests/testing/default/unit/test_user.py | 47 ++++++ .../testing/default/unit/test_user_profile.py | 40 ----- 29 files changed, 636 insertions(+), 329 deletions(-) create mode 100644 src/dvtests/testing/data/dataverse_production/default/system/testdata_authentication.json create mode 100644 src/dvtests/testing/data/dataverse_production/default/unit/testdata_api.json create mode 100644 src/dvtests/testing/data/dataverse_production/default/unit/testdata_authentication.json create mode 100644 src/dvtests/testing/data/dataverse_production/default/unit/testdata_installation.json create mode 100644 src/dvtests/testing/data/dataverse_production/default/unit/testdata_oaipmh.json create mode 100644 src/dvtests/testing/data/dataverse_production/default/unit/testdata_robots-txt.json create mode 100644 src/dvtests/testing/data/dataverse_production/default/unit/testdata_sitemap.json create mode 100644 src/dvtests/testing/data/dataverse_production/default/unit/testdata_user.json delete mode 100644 src/dvtests/testing/default/end2end/test_user_authentication.py rename src/dvtests/testing/default/{end2end => system}/__init__.py (100%) create mode 100644 src/dvtests/testing/default/system/test_authentication.py rename src/dvtests/testing/default/{end2end => system}/test_datafiles.py (100%) rename src/dvtests/testing/default/{end2end => system}/test_datasets.py (100%) rename src/dvtests/testing/default/{end2end => system}/test_dataverses.py (100%) rename src/dvtests/testing/default/{end2end => system}/test_search.py (100%) create mode 100644 src/dvtests/testing/default/unit/test_authentication.py create mode 100644 src/dvtests/testing/default/unit/test_installation.py delete mode 100644 src/dvtests/testing/default/unit/test_resources.py create mode 100644 src/dvtests/testing/default/unit/test_user.py delete mode 100644 src/dvtests/testing/default/unit/test_user_profile.py diff --git a/.gitignore b/.gitignore index 75d1c85..b03ca76 100644 --- a/.gitignore +++ b/.gitignore @@ -143,7 +143,6 @@ __pycache__/ internal Pipfile.lock browserengines -src/dvtests/testing/data src/dvtests/data env-config/ dendron.yml diff --git a/setup.cfg b/setup.cfg index 8494f2c..8633da7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -8,6 +8,7 @@ markers = v4_18_1: mark test as working for Dataverse v4.18.1 v4_20: mark test as working for Dataverse v4.20 selenium: mark test as using selenium frontend testing + smoke: mark test as smoke tests [flake8] max-line-length = 88 diff --git a/src/dvtests/config.py b/src/dvtests/config.py index 9117d9c..ef71c31 100755 --- a/src/dvtests/config.py +++ b/src/dvtests/config.py @@ -16,6 +16,7 @@ class UtilsConfig(BaseSettings): class TestingConfig(BaseSettings): INSTANCE: str + BASE_URL: str = None API_TOKEN: str = "" USER_AGENT: str = None HEADLESS: bool = True diff --git a/src/dvtests/testing/conftest.py b/src/dvtests/testing/conftest.py index e3c1a62..d66fa6c 100644 --- a/src/dvtests/testing/conftest.py +++ b/src/dvtests/testing/conftest.py @@ -3,6 +3,8 @@ from time import sleep import pytest +import requests +from pyDataverse.api import NativeApi from selenium import webdriver from selenium.webdriver.common.by import By @@ -16,20 +18,64 @@ @pytest.fixture def config(): + # Arrange + # Act + # Assert + # Cleanup if os.getenv("ENV_FILE"): return TestingConfig(_env_file=os.getenv("ENV_FILE")) else: return TestingConfig() +INSTANCE = ( + TestingConfig(_env_file=os.getenv("ENV_FILE")).INSTANCE + if os.getenv("ENV_FILE") + else TestingConfig().INSTANCE +) +BASE_URL = ( + TestingConfig(_env_file=os.getenv("ENV_FILE")).BASE_URL + if os.getenv("ENV_FILE") + else TestingConfig().BASE_URL +) + + +@pytest.fixture +def session(config): + # Arrange + # Act + # Assert + # Cleanup + s = requests.Session() + s.headers.update({"User-Agent": config.USER_AGENT}) + return s + + +@pytest.fixture +def native_api(config): + # Arrange + # Act + # Assert + # Cleanup + return NativeApi(BASE_URL) + + @pytest.fixture def test_config(config): + # Arrange + # Act + # Assert + # Cleanup instance_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data") return read_json(os.path.join(instance_dir, config.INSTANCE + ".json")) @pytest.fixture def firefox_options(firefox_options, config): + # Arrange + # Act + # Assert + # Cleanup firefox_options.add_argument("-foreground") if config.HEADLESS: firefox_options.headless = True @@ -42,6 +88,10 @@ def firefox_options(firefox_options, config): @pytest.fixture def chrome_options(chrome_options, config): + # Arrange + # Act + # Assert + # Cleanup if config.HEADLESS: chrome_options.add_argument("--headless") if config.USER_AGENT: @@ -54,12 +104,11 @@ def chrome_options(chrome_options, config): return chrome_options -def login_normal_user(driver, test_config, config, user, password): - base_url = test_config["instance"]["base-url"] - driver.get(f"{base_url}/loginpage.xhtml") - driver.set_window_size(config.WINDOW_WIDTH, config.WINDOW_HEIGHT) +def login_normal_user(driver, login_mode, window_width, window_height, user, password): + driver.get(f"{BASE_URL}/loginpage.xhtml") + driver.set_window_size(window_width, window_height) sleep(5) - if test_config["tests"]["login"]["login-page"] == "normal-user-and-shibboleth": + if login_mode == "normal-user-and-shibboleth": driver.find_element(By.LINK_TEXT, "Username/Email").click() sleep(3) driver.find_element(By.ID, "loginForm:credentialsContainer:0:credValue").send_keys( @@ -135,6 +184,10 @@ def read_json(filename: str, mode: str = "r", encoding: str = "utf-8") -> dict: @pytest.fixture def dataverse_upload_full_01(): + # Arrange + # Act + # Assert + # Cleanup return read_json( os.path.join( ROOT_DIR, @@ -145,6 +198,10 @@ def dataverse_upload_full_01(): @pytest.fixture def dataverse_upload_min_01(): + # Arrange + # Act + # Assert + # Cleanup return read_json( os.path.join( ROOT_DIR, @@ -155,6 +212,10 @@ def dataverse_upload_min_01(): @pytest.fixture def dataset_upload_default_full_01(): + # Arrange + # Act + # Assert + # Cleanup return read_json( os.path.join( ROOT_DIR, @@ -165,6 +226,10 @@ def dataset_upload_default_full_01(): @pytest.fixture def dataset_upload_default_min_02(): + # Arrange + # Act + # Assert + # Cleanup return read_json( os.path.join( ROOT_DIR, @@ -175,6 +240,10 @@ def dataset_upload_default_min_02(): @pytest.fixture def datafile_upload_full_01(): + # Arrange + # Act + # Assert + # Cleanup return read_json( os.path.join( ROOT_DIR, @@ -185,6 +254,10 @@ def datafile_upload_full_01(): @pytest.fixture def datafile_upload_min_01(): + # Arrange + # Act + # Assert + # Cleanup return read_json( os.path.join( ROOT_DIR, diff --git a/src/dvtests/testing/data/dataverse_production/default/system/testdata_authentication.json b/src/dvtests/testing/data/dataverse_production/default/system/testdata_authentication.json new file mode 100644 index 0000000..1e0689e --- /dev/null +++ b/src/dvtests/testing/data/dataverse_production/default/system/testdata_authentication.json @@ -0,0 +1,21 @@ +{ + "shibboleth": { + "login-valid": [ + { + "title": "AUSSDA" + } + ] + }, + "normal-login": { + "login-valid": [ + [ + { + "login-mode": "normal-user-and-shibboleth" + }, + { + "title": "AUSSDA" + } + ] + ] + } +} diff --git a/src/dvtests/testing/data/dataverse_production/default/unit/testdata_api.json b/src/dvtests/testing/data/dataverse_production/default/unit/testdata_api.json new file mode 100644 index 0000000..498c660 --- /dev/null +++ b/src/dvtests/testing/data/dataverse_production/default/unit/testdata_api.json @@ -0,0 +1,38 @@ +{ + "dataverse": { + "valid": [ + [ + { + "alias": ":root" + }, + { + "alias": "AUSSDA", + "name": "AUSSDA", + "affiliation": "AUSSDA", + "emails": [ + "info@aussda.at" + ], + "tagline": "We make social science data accessible, creating opportunities for research and data reuse, benefitting science and society.", + "link-url": "https://aussda.at", + "url": "https://data.aussda.at/api/v1/dataverses/:root?User-Agent=pydataverse" + } + ], + [ + { + "alias": "AUSSDA" + }, + { + "alias": "AUSSDA", + "name": "AUSSDA", + "affiliation": "AUSSDA", + "emails": [ + "info@aussda.at" + ], + "tagline": "We make social science data accessible, creating opportunities for research and data reuse, benefitting science and society.", + "link-url": "https://aussda.at", + "url": "https://data.aussda.at/api/v1/dataverses/AUSSDA?User-Agent=pydataverse" + } + ] + ] + } +} diff --git a/src/dvtests/testing/data/dataverse_production/default/unit/testdata_authentication.json b/src/dvtests/testing/data/dataverse_production/default/unit/testdata_authentication.json new file mode 100644 index 0000000..0a26d0b --- /dev/null +++ b/src/dvtests/testing/data/dataverse_production/default/unit/testdata_authentication.json @@ -0,0 +1,22 @@ +{ + "shibboleth": { + "interface-valid": [ + [ + { + "url": "/Shibboleth.sso/DiscoFeed" + }, + { + "content-type": "application/json; charset=UTF-8" + } + ], + [ + { + "url": "/Shibboleth.sso/Metadata" + }, + { + "content-type": "application/samlmetadata+xml" + } + ] + ] + } +} diff --git a/src/dvtests/testing/data/dataverse_production/default/unit/testdata_installation.json b/src/dvtests/testing/data/dataverse_production/default/unit/testdata_installation.json new file mode 100644 index 0000000..7749f12 --- /dev/null +++ b/src/dvtests/testing/data/dataverse_production/default/unit/testdata_installation.json @@ -0,0 +1,25 @@ +{ + "version": { + "valid": [ + { + "version": "4.20", + "build": "413-4e07b62" + } + ] + }, + "server": { + "api-valid": [ + { + "url": "data.aussda.at" + } + ], + "request-valid": [ + { + "server": "Apache/2.4.41 (Ubuntu)", + "content-encoding": "gzip", + "keep-alive": "timeout=5, max=100", + "connection": "Keep-Alive" + } + ] + } +} diff --git a/src/dvtests/testing/data/dataverse_production/default/unit/testdata_oaipmh.json b/src/dvtests/testing/data/dataverse_production/default/unit/testdata_oaipmh.json new file mode 100644 index 0000000..88d9105 --- /dev/null +++ b/src/dvtests/testing/data/dataverse_production/default/unit/testdata_oaipmh.json @@ -0,0 +1,51 @@ +{ + "endpoint": { + "valid": [ + [ + { + "url": "https://data.aussda.at/oai" + }, + { + "encoding": "UTF-8", + "content-type": "text/xml;charset=UTF-8" + } + ], + [ + { + "url": "https://data.aussda.at/oai?verb=ListIdentifiers&metadataPrefix=oai_ddi&set=all_published" + }, + { + "encoding": "UTF-8", + "content-type": "text/xml;charset=UTF-8" + } + ], + [ + { + "url": "https://data2.aussda.at/oai?verb=ListIdentifiers&metadataPrefix=ddi" + }, + { + "encoding": "UTF-8", + "content-type": "text/xml;charset=UTF-8" + } + ], + [ + { + "url": "https://data2.aussda.at/oai?verb=ListRecords&metadataPrefix=ddi&set=all_published" + }, + { + "encoding": "UTF-8", + "content-type": "text/xml;charset=UTF-8" + } + ], + [ + { + "url": "https://data2.aussda.at/oai?verb=GetRecord&metadataPrefix=oai_ddi&identifier=doi:10.11587/BQVSOW" + }, + { + "encoding": "UTF-8", + "content-type": "text/xml;charset=UTF-8" + } + ] + ] + } +} diff --git a/src/dvtests/testing/data/dataverse_production/default/unit/testdata_robots-txt.json b/src/dvtests/testing/data/dataverse_production/default/unit/testdata_robots-txt.json new file mode 100644 index 0000000..e9e0b69 --- /dev/null +++ b/src/dvtests/testing/data/dataverse_production/default/unit/testdata_robots-txt.json @@ -0,0 +1,10 @@ +{ + "robots-txt": { + "valid": [ + { + "encoding": "ISO-8859-1", + "content-type": "text/plain" + } + ] + } +} diff --git a/src/dvtests/testing/data/dataverse_production/default/unit/testdata_sitemap.json b/src/dvtests/testing/data/dataverse_production/default/unit/testdata_sitemap.json new file mode 100644 index 0000000..f9b6874 --- /dev/null +++ b/src/dvtests/testing/data/dataverse_production/default/unit/testdata_sitemap.json @@ -0,0 +1,10 @@ +{ + "sitemap": { + "valid": [ + { + "encoding": "ISO-8859-1", + "content-type": "application/xml" + } + ] + } +} diff --git a/src/dvtests/testing/data/dataverse_production/default/unit/testdata_user.json b/src/dvtests/testing/data/dataverse_production/default/unit/testdata_user.json new file mode 100644 index 0000000..0f04d04 --- /dev/null +++ b/src/dvtests/testing/data/dataverse_production/default/unit/testdata_user.json @@ -0,0 +1,9 @@ +{ + "api": { + "valid": [ + { + "url": "data.aussda.at" + } + ] + } +} diff --git a/src/dvtests/testing/default/conftest.py b/src/dvtests/testing/default/conftest.py index 137c6d9..cbedc48 100644 --- a/src/dvtests/testing/default/conftest.py +++ b/src/dvtests/testing/default/conftest.py @@ -1,4 +1,6 @@ +from ..conftest import BASE_URL from ..conftest import click_cookie_rollbar +from ..conftest import INSTANCE from ..conftest import login_normal_user from ..conftest import login_shibboleth_user from ..conftest import read_json diff --git a/src/dvtests/testing/default/end2end/test_user_authentication.py b/src/dvtests/testing/default/end2end/test_user_authentication.py deleted file mode 100644 index 7387942..0000000 --- a/src/dvtests/testing/default/end2end/test_user_authentication.py +++ /dev/null @@ -1,98 +0,0 @@ -from time import sleep - -import pytest -import requests -from selenium.webdriver.common.by import By - -from ..conftest import login_normal_user -from ..conftest import login_shibboleth_user - - -class TestShibboleth: - @pytest.mark.v4_18_1 - def test_interfaces(self, test_config): - """ - - Input - * base url - - Expected result - * status code - * encoding - * content type - - """ - instance_cfg = test_config["instance"] - - base_url = instance_cfg["base-url"] - url = f"{base_url}/Shibboleth.sso/DiscoFeed" - resp = requests.get(url) - sleep(3) - assert resp.status_code == 200 - assert resp.url == url - assert resp.encoding == "UTF-8" - assert "application/json" in requests.head(url).headers["Content-Type"] - - url = f"{base_url}/Shibboleth.sso/Metadata" - resp = requests.get(url) - sleep(3) - assert resp.status_code == 200 - assert resp.url == url - - @pytest.mark.v4_18_1 - @pytest.mark.selenium - def test_login(self, test_config, config, selenium): - """ - - Input - * base url - * user(s) - - Expected result - * user - * name - - """ - base_url = test_config["instance"]["base-url"] - instance_cfg = test_config["instance"] - - selenium = login_shibboleth_user( - selenium, - base_url, - config.WINDOW_WIDTH, - config.WINDOW_HEIGHT, - config.USER_SHIBBOLETH, - config.USER_SHIBBOLETH_PWD, - config.USER_SHIBBOLETH_NAME, - ) - assert instance_cfg["title"] == selenium.title - assert ( - selenium.find_element(By.ID, "userDisplayInfoTitle").text - == config.USER_SHIBBOLETH_NAME - ) - - -class TestNormalLogin: - @pytest.mark.v4_18_1 - @pytest.mark.selenium - def test_login(self, test_config, config, selenium): - """ - - Input - * base url - - Expected result - * user - * name - - """ - instance_cfg = test_config["instance"] - - selenium = login_normal_user( - selenium, test_config, config, config.USER_NORMAL, config.USER_NORMAL_PWD, - ) - assert instance_cfg["title"] == selenium.title - assert ( - selenium.find_element(By.ID, "userDisplayInfoTitle").text - == config.USER_NORMAL_NAME - ) diff --git a/src/dvtests/testing/default/end2end/__init__.py b/src/dvtests/testing/default/system/__init__.py similarity index 100% rename from src/dvtests/testing/default/end2end/__init__.py rename to src/dvtests/testing/default/system/__init__.py diff --git a/src/dvtests/testing/default/system/test_authentication.py b/src/dvtests/testing/default/system/test_authentication.py new file mode 100644 index 0000000..4957beb --- /dev/null +++ b/src/dvtests/testing/default/system/test_authentication.py @@ -0,0 +1,79 @@ +import json +import os + +import pytest +from selenium.webdriver.common.by import By + +from ..conftest import BASE_URL +from ..conftest import INSTANCE +from ..conftest import login_normal_user +from ..conftest import login_shibboleth_user +from ..conftest import ROOT_DIR + + +with open( + os.path.join( + ROOT_DIR, + "src/dvtests/testing/data", + INSTANCE, + "default/system/testdata_authentication.json", + ) +) as json_file: + testdata = json.load(json_file) + + +class TestShibboleth: + @pytest.mark.v4_18_1 + @pytest.mark.v4_20 + @pytest.mark.selenium + @pytest.mark.parametrize("expected", testdata["shibboleth"]["login-valid"]) + def test_login_valid(self, config, selenium, expected): + """Test Shibboleth login procedure.""" + # Arrange + # Act + selenium = login_shibboleth_user( + selenium, + BASE_URL, + config.WINDOW_WIDTH, + config.WINDOW_HEIGHT, + config.USER_SHIBBOLETH, + config.USER_SHIBBOLETH_PWD, + config.USER_SHIBBOLETH_NAME, + ) + + # Assert + assert selenium.title == expected["title"] + assert ( + selenium.find_element(By.ID, "userDisplayInfoTitle").text + == config.USER_SHIBBOLETH_NAME + ) + # Cleanup + + +class TestNormalLogin: + @pytest.mark.v4_18_1 + @pytest.mark.v4_20 + @pytest.mark.selenium + @pytest.mark.parametrize( + "test_input,expected", testdata["normal-login"]["login-valid"] + ) + def test_login(self, config, selenium, test_input, expected): + """Test normal login procedure.""" + # Arrange + # Act + selenium = login_normal_user( + selenium, + test_input["login-mode"], + config.WINDOW_WIDTH, + config.WINDOW_HEIGHT, + config.USER_NORMAL, + config.USER_NORMAL_PWD, + ) + + # Assert + assert selenium.title == expected["title"] + assert ( + selenium.find_element(By.ID, "userDisplayInfoTitle").text + == config.USER_NORMAL_NAME + ) + # Cleanup diff --git a/src/dvtests/testing/default/end2end/test_datafiles.py b/src/dvtests/testing/default/system/test_datafiles.py similarity index 100% rename from src/dvtests/testing/default/end2end/test_datafiles.py rename to src/dvtests/testing/default/system/test_datafiles.py diff --git a/src/dvtests/testing/default/end2end/test_datasets.py b/src/dvtests/testing/default/system/test_datasets.py similarity index 100% rename from src/dvtests/testing/default/end2end/test_datasets.py rename to src/dvtests/testing/default/system/test_datasets.py diff --git a/src/dvtests/testing/default/end2end/test_dataverses.py b/src/dvtests/testing/default/system/test_dataverses.py similarity index 100% rename from src/dvtests/testing/default/end2end/test_dataverses.py rename to src/dvtests/testing/default/system/test_dataverses.py diff --git a/src/dvtests/testing/default/end2end/test_search.py b/src/dvtests/testing/default/system/test_search.py similarity index 100% rename from src/dvtests/testing/default/end2end/test_search.py rename to src/dvtests/testing/default/system/test_search.py diff --git a/src/dvtests/testing/default/unit/test_api.py b/src/dvtests/testing/default/unit/test_api.py index 8eef0be..a2e5317 100644 --- a/src/dvtests/testing/default/unit/test_api.py +++ b/src/dvtests/testing/default/unit/test_api.py @@ -1,110 +1,42 @@ -import pytest -import requests -from pyDataverse.api import NativeApi - - -class TestApi: - @pytest.mark.v4_18_1 - def test_api_dataverses(self, test_config): - """ - - Input - * base url - * dataverse aliases - - Expected result - * dataverse - * status code - * url - * content type - * alias - * affiliation - * tagline - * link url - * contact email +import json +import os - """ - base_url = test_config["instance"]["base-url"] - - for dv in test_config["dataverses"]: - url = f"{base_url}/api/dataverses/{dv}" - resp = requests.get(url) - - assert resp.status_code == 200 - assert "application/json" in resp.headers["Content-Type"] - assert resp.json()["data"]["name"] == test_config["dataverses"][dv]["name"] - assert ( - resp.json()["data"]["alias"] == test_config["dataverses"][dv]["alias"] - ) - assert ( - resp.json()["data"]["affiliation"] - == test_config["dataverses"][dv]["affiliation"] - ) - assert ( - resp.json()["data"]["theme"]["tagline"] - == test_config["dataverses"][dv]["tagline"] - ) - assert ( - resp.json()["data"]["theme"]["linkUrl"] - == test_config["dataverses"][dv]["url"] - ) - for contact in resp.json()["data"]["dataverseContacts"]: - assert ( - contact["contactEmail"] in test_config["dataverses"][dv]["emails"] - ) - - @pytest.mark.v4_18_1 - def test_dataverse_version(self, test_config): - """ +import pytest - Input - * base url +from ..conftest import INSTANCE +from ..conftest import ROOT_DIR - Expected result - * version - * build - """ - base_url = test_config["instance"]["base-url"] +with open( + os.path.join( + ROOT_DIR, "src/dvtests/testing/data", INSTANCE, "default/unit/testdata_api.json" + ) +) as json_file: + testdata = json.load(json_file) - api = NativeApi(base_url) - resp = api.get_info_version() - assert resp.json()["data"]["version"] == test_config["instance"]["version"] +class TestDataverse: @pytest.mark.v4_18_1 - def test_dataverse_server(self, test_config): - """ - - Input - * base url - - Expected result - * base url - - """ - base_url = test_config["instance"]["base-url"] - - api = NativeApi(base_url) - resp = api.get_info_server() - assert resp.json()["data"]["message"] == test_config["instance"]["base-url"] - - def test_user(self, test_config, config): - """ - - Input - * base url - - Expected result - * base url - - """ - """Test user endpoint. - - Does not work below Dataverse 5.3 or 5.2 - """ - base_url = test_config["instance"]["base-url"] - api_token = config.API_TOKEN - - api = NativeApi(base_url, api_token) - resp = api.get_user() - assert resp.json()["data"]["message"] == test_config["instance"]["base-url"] + @pytest.mark.v4_20 + @pytest.mark.parametrize("test_input,expected", testdata["dataverse"]["valid"]) + def test_valid(self, config, native_api, test_input, expected): + """Test important Dataverses.""" + # Arrange + # Act + resp = native_api.get_dataverse(test_input["alias"]) + r_data = resp.json()["data"] + + # Assert + assert r_data["alias"] == expected["alias"] + assert r_data["name"] == expected["name"] + assert r_data["affiliation"] == expected["affiliation"] + for c in r_data["dataverseContacts"]: + assert c["contactEmail"] in expected["emails"] + assert len(r_data["dataverseContacts"]) == len(expected["emails"]) + assert r_data["theme"]["tagline"] == expected["tagline"] + assert r_data["theme"]["linkUrl"] == expected["link-url"] + assert resp.status_code == 200 + assert resp.headers["Content-Type"] == "application/json" + assert resp.url == expected["url"] + + # Cleanup diff --git a/src/dvtests/testing/default/unit/test_authentication.py b/src/dvtests/testing/default/unit/test_authentication.py new file mode 100644 index 0000000..3fc17ba --- /dev/null +++ b/src/dvtests/testing/default/unit/test_authentication.py @@ -0,0 +1,44 @@ +import json +import os +from time import sleep + +import pytest +from selenium.webdriver.common.by import By + +from ..conftest import BASE_URL +from ..conftest import INSTANCE +from ..conftest import login_normal_user +from ..conftest import login_shibboleth_user +from ..conftest import ROOT_DIR + + +with open( + os.path.join( + ROOT_DIR, + "src/dvtests/testing/data", + INSTANCE, + "default/unit/testdata_authentication.json", + ) +) as json_file: + testdata = json.load(json_file) + + +class TestShibboleth: + @pytest.mark.v4_18_1 + @pytest.mark.v4_20 + @pytest.mark.parametrize( + "test_input,expected", testdata["shibboleth"]["interface-valid"] + ) + def test_interface_valid(self, session, test_input, expected): + """Test Shibboleth interface.""" + # Arrange + url = f'{BASE_URL}{test_input["url"]}' + + # Act + resp = session.get(url) + + # Assert + assert resp.url == url + assert resp.status_code == 200 + assert resp.headers["Content-Type"] == expected["content-type"] + # Cleanup diff --git a/src/dvtests/testing/default/unit/test_installation.py b/src/dvtests/testing/default/unit/test_installation.py new file mode 100644 index 0000000..99df1ab --- /dev/null +++ b/src/dvtests/testing/default/unit/test_installation.py @@ -0,0 +1,70 @@ +import json +import os + +import pytest + +from ..conftest import BASE_URL +from ..conftest import INSTANCE +from ..conftest import ROOT_DIR + + +with open( + os.path.join( + ROOT_DIR, + "src/dvtests/testing/data", + INSTANCE, + "default/unit/testdata_installation.json", + ) +) as json_file: + testdata = json.load(json_file) + + +class TestVersion: + @pytest.mark.v4_18_1 + @pytest.mark.v4_20 + @pytest.mark.parametrize("expected", testdata["version"]["valid"]) + def test_valid(self, native_api, expected): + """Test Dataverse version.""" + # Arrange + # Act + resp = native_api.get_info_version() + r_data = resp.json()["data"] + + # Assert + assert r_data["version"] == expected["version"] + assert r_data["build"] == expected["build"] + # Cleanup + + +class TestServer: + @pytest.mark.v4_18_1 + @pytest.mark.v4_20 + @pytest.mark.parametrize("expected", testdata["server"]["api-valid"]) + def test_api_valid(self, native_api, expected): + """Test Dataverse server.""" + # Arrange + # Act + resp = native_api.get_info_server() + r_data = resp.json()["data"] + + # Assert + assert r_data["message"] == expected["url"] + + # Cleanup + + @pytest.mark.v4_18_1 + @pytest.mark.v4_20 + @pytest.mark.parametrize("expected", testdata["server"]["request-valid"]) + def test_request_valid(self, session, expected): + """Test Dataverse server.""" + # Arrange + # Act + resp = session.get(BASE_URL) + + # Assert + assert resp.headers["Server"] == expected["server"] + assert resp.headers["Content-Encoding"] == expected["content-encoding"] + assert resp.headers["Keep-Alive"] == expected["keep-alive"] + assert resp.headers["Connection"] == expected["connection"] + + # Cleanup diff --git a/src/dvtests/testing/default/unit/test_oaipmh.py b/src/dvtests/testing/default/unit/test_oaipmh.py index ce84864..4263520 100644 --- a/src/dvtests/testing/default/unit/test_oaipmh.py +++ b/src/dvtests/testing/default/unit/test_oaipmh.py @@ -1,23 +1,37 @@ +import json +import os + import pytest -import requests +from ..conftest import INSTANCE +from ..conftest import ROOT_DIR -class TestMetadataServer: - @pytest.mark.v4_18_1 - def test_endpoints(self, test_config): - """ - Input - * url +with open( + os.path.join( + ROOT_DIR, + "src/dvtests/testing/data", + INSTANCE, + "default/unit/testdata_oaipmh.json", + ) +) as json_file: + testdata = json.load(json_file) - Expected result - * status code - * encoding - * content type - """ - for ep in test_config["metadata-server"]["endpoints"]: - resp = requests.get(ep["url"], allow_redirects=True) - assert resp.status_code == 200 - assert resp.encoding == "UTF-8" - assert "text/xml" in resp.headers["Content-Type"] +class TestEndpoint: + @pytest.mark.v4_18_1 + @pytest.mark.v4_20 + @pytest.mark.parametrize("test_input,expected", testdata["endpoint"]["valid"]) + def test_valid(self, session, test_input, expected): + """Test OAI-PMH endpoints.""" + # Arrange + # Act + resp = session.get(test_input["url"]) + + # Assert + assert resp.text + assert resp.url == test_input["url"] + assert resp.status_code == 200 + assert resp.encoding == expected["encoding"] + assert resp.headers["Content-Type"] == expected["content-type"] + # Cleanup diff --git a/src/dvtests/testing/default/unit/test_resources.py b/src/dvtests/testing/default/unit/test_resources.py deleted file mode 100644 index 1dd8c91..0000000 --- a/src/dvtests/testing/default/unit/test_resources.py +++ /dev/null @@ -1,32 +0,0 @@ -import pytest -import requests - - -class TestResources: - @pytest.mark.v4_18_1 - @pytest.mark.selenium - def test_urls(self, test_config, selenium): - """ - - TODO - * no selenium necessary - - Input - * url - - Expected result - * url - * title - - """ - for res in test_config["resources"]: - resp = requests.get(res["url_start"], allow_redirects=True) - assert resp.status_code == 200 - assert resp.encoding == "UTF-8" - if "url_end" in res: - assert resp.url == res["url_end"] - else: - assert resp.url == res["url_start"] - if "title" in res: - selenium.get(res["url"]) - assert res["title"] == selenium.title diff --git a/src/dvtests/testing/default/unit/test_robots_txt.py b/src/dvtests/testing/default/unit/test_robots_txt.py index 67f3fd3..d8cea34 100644 --- a/src/dvtests/testing/default/unit/test_robots_txt.py +++ b/src/dvtests/testing/default/unit/test_robots_txt.py @@ -1,27 +1,40 @@ +import json +import os + import pytest -import requests +from ..conftest import BASE_URL +from ..conftest import INSTANCE +from ..conftest import ROOT_DIR -class TestRobotsTxt: - @pytest.mark.v4_18_1 - def test_robots_txt(self, test_config): - """ - Input - * base url +with open( + os.path.join( + ROOT_DIR, + "src/dvtests/testing/data", + INSTANCE, + "default/unit/testdata_robots-txt.json", + ) +) as json_file: + testdata = json.load(json_file) - Expected result - * status code - * content type - * encoding - """ - test_cfg = test_config["tests"]["robots.txt"] +class TestRobotsTxt: + @pytest.mark.v4_18_1 + @pytest.mark.v4_20 + @pytest.mark.parametrize("expected", testdata["robots-txt"]["valid"]) + def test_valid(self, session, expected): + """Test robots.txt.""" + # Arrange + url = f"{BASE_URL}/robots.txt" - base_url = test_config["instance"]["base-url"] + # Act + resp = session.get(url) - url = f"{base_url}/robots.txt" - resp = requests.get(url) + # Assert + assert resp.url == url assert resp.status_code == 200 - assert resp.encoding == test_cfg["encoding"] - assert "text/plain" in requests.head(url).headers["Content-Type"] + assert resp.encoding == expected["encoding"] + assert resp.headers["Content-Type"] == expected["content-type"] + + # Cleanup diff --git a/src/dvtests/testing/default/unit/test_sitemap.py b/src/dvtests/testing/default/unit/test_sitemap.py index ad7f285..0bae204 100644 --- a/src/dvtests/testing/default/unit/test_sitemap.py +++ b/src/dvtests/testing/default/unit/test_sitemap.py @@ -1,23 +1,39 @@ +import json +import os + import pytest -import requests + +from ..conftest import BASE_URL +from ..conftest import INSTANCE +from ..conftest import ROOT_DIR + + +with open( + os.path.join( + ROOT_DIR, + "src/dvtests/testing/data", + INSTANCE, + "default/unit/testdata_sitemap.json", + ) +) as json_file: + testdata = json.load(json_file) class TestSitemap: @pytest.mark.v4_18_1 - def test_sitemap(self, test_config): - """ - - Input - * base url + @pytest.mark.v4_20 + @pytest.mark.parametrize("expected", testdata["sitemap"]["valid"]) + def test_valid(self, session, expected): + """Test sitemap.""" + # Arrange + url = f"{BASE_URL}/sitemap.xml" - Expected result - * content type - * status code - * url + # Act + resp = session.get(url) - """ - base_url = test_config["instance"]["base-url"] - url = f"{base_url}/sitemap.xml" + # Assert + assert resp.status_code == 200 + # assert resp.encoding == expected["encoding"] + assert resp.headers["Content-Type"] == expected["content-type"] - assert requests.get(url).status_code == 200 - assert "application/xml" in requests.head(url).headers["Content-Type"] + # Cleanup diff --git a/src/dvtests/testing/default/unit/test_user.py b/src/dvtests/testing/default/unit/test_user.py new file mode 100644 index 0000000..54b7293 --- /dev/null +++ b/src/dvtests/testing/default/unit/test_user.py @@ -0,0 +1,47 @@ +import json +import os + +import pytest + +from ..conftest import INSTANCE +from ..conftest import ROOT_DIR + + +with open( + os.path.join( + ROOT_DIR, + "src/dvtests/testing/data", + INSTANCE, + "default/unit/testdata_user.json", + ) +) as json_file: + testdata = json.load(json_file) + + +class TestApi: + @pytest.mark.v5_2 + @pytest.mark.parametrize("expected", testdata["api"]["valid"]) + def test_valid(self, native_api, expected): + """ + + Input + * base url + + Expected result + * base url + + """ + """Test user endpoint. + + Does not work below Dataverse 5.3 or 5.2 + """ + # Arrange + # Act + resp = native_api.get_user() + print(resp) + r_data = resp.json()["data"] + + # Assert + assert r_data["message"] == expected["url"] + + # Cleanup diff --git a/src/dvtests/testing/default/unit/test_user_profile.py b/src/dvtests/testing/default/unit/test_user_profile.py deleted file mode 100644 index c3c7681..0000000 --- a/src/dvtests/testing/default/unit/test_user_profile.py +++ /dev/null @@ -1,40 +0,0 @@ -from time import sleep - -import pytest -from selenium.webdriver.common.by import By - -from ..conftest import login_normal_user - - -class TestUserProfile: - @pytest.mark.v4_18_1 - @pytest.mark.selenium - def test_navigate(self, test_config, config, selenium): - """ - - Input - * base url - - Expected result - * title - - """ - test_cfg = test_config["tests"]["user-profile-navigate"] - instance_cfg = test_config["instance"] - - selenium = login_normal_user( - selenium, test_config, config, config.USER_NORMAL, config.USER_NORMAL_PWD, - ) - - assert instance_cfg["title"] == selenium.title - selenium.find_element(By.ID, "userDisplayInfoTitle").click() - sleep(3) - selenium.find_element(By.LINK_TEXT, "My Data").click() - sleep(3) - assert test_cfg["title"] == selenium.title - selenium.find_element(By.LINK_TEXT, "Notifications").click() - sleep(3) - selenium.find_element(By.LINK_TEXT, "Account Information").click() - sleep(3) - selenium.find_element(By.LINK_TEXT, "API Token").click() - sleep(3)