Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functional tests implementation for mscolab.py file #2065

Draft
wants to merge 44 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
e06b396
remove inputs from conditions (#1808)
nilupulmanodya Jun 10, 2023
8471ba1
Setup sp and idp for the sso (#1809)
nilupulmanodya Jun 15, 2023
f2b1434
Split conf sp idp (#1811)
nilupulmanodya Jun 28, 2023
106bee5
UI changes in Qt for SSO (#1813)
nilupulmanodya Jul 21, 2023
245d64e
web browser implementation (#1814)
nilupulmanodya Jul 25, 2023
4c556a3
Configure mscolab for sso (#1818)
nilupulmanodya Aug 1, 2023
529e7f6
To do fixes #1818 (#1974)
nilupulmanodya Sep 24, 2023
198cf88
improve code for multiple Idps
nilupulmanodya Sep 24, 2023
db82eb7
conf routes for multiple conf
nilupulmanodya Oct 6, 2023
90a1c62
remove uncessary .yaml
nilupulmanodya Oct 6, 2023
df4ae37
update cmd metadata
nilupulmanodya Oct 6, 2023
8dd2c9e
update conf
nilupulmanodya Oct 6, 2023
05f3c2c
update saml handler for multiple idps
nilupulmanodya Oct 7, 2023
aa47a09
pinning of xmlschema
nilupulmanodya Oct 9, 2023
1912fd4
pin werkzeug
nilupulmanodya Oct 9, 2023
77f21d3
disable pytests for todo refactor
nilupulmanodya Oct 10, 2023
cf2a3b9
disbale whole file gsoc_testing
nilupulmanodya Oct 10, 2023
c7ab2ae
fix conf
nilupulmanodya Oct 11, 2023
34e7e33
resolve comments
nilupulmanodya Oct 12, 2023
b02854a
resolve comments
nilupulmanodya Oct 14, 2023
f94da54
Merge pull request #2043 from nilupulmanodya/implement-mscolab-for-mu…
ReimarBauer Oct 15, 2023
458a978
Merge branch 'develop' of https://github.com/nilupulmanodya/MSS into …
nilupulmanodya Oct 16, 2023
2d59aa5
manual conflict resolve ui_mscolab_connect_dialog.ui file
nilupulmanodya Oct 16, 2023
18a9e7c
resolve flake8
nilupulmanodya Oct 16, 2023
ef326c2
Merge pull request #2061 from nilupulmanodya/merge_current_develop
ReimarBauer Oct 19, 2023
3d328f9
set SSL certificate verification enablement (#2062)
nilupulmanodya Oct 19, 2023
2fa29ae
functional test cases implementation mscolab.py
nilupulmanodya Oct 24, 2023
a796365
resolove flake8
nilupulmanodya Oct 24, 2023
456b1c4
Revert "resolove flake8"
nilupulmanodya Oct 24, 2023
8cc06a1
Revert "Revert "resolove flake8""
nilupulmanodya Oct 24, 2023
b08bbdf
resolve flake8
nilupulmanodya Oct 24, 2023
d7b4e6b
recorrect commit
nilupulmanodya Oct 24, 2023
973deaf
fix flake8 test_mscolab.py
nilupulmanodya Oct 24, 2023
8ee5d0a
set fixed dir for crts keys and metadata xmls
nilupulmanodya Oct 31, 2023
2073c9b
fixes pylint
nilupulmanodya Oct 31, 2023
faa974e
Merge branch 'develop' of https://github.com/nilupulmanodya/MSS into …
nilupulmanodya Nov 11, 2023
8d9d00c
implement constants through envs
nilupulmanodya Nov 11, 2023
3e56c4d
set env through test_mscolab.py
nilupulmanodya Nov 11, 2023
35a1d4b
set abs path
nilupulmanodya Nov 11, 2023
1ce2013
Merge remote-tracking branch 'origin/develop' into functional-tests-i…
nilupulmanodya Nov 27, 2023
54db2c0
resolve pull conflicts
nilupulmanodya Nov 27, 2023
804b36c
resolve pull conflicts
nilupulmanodya Nov 27, 2023
0bb9e34
set env TESTING_MSCOLAB_SSO_DIR idp_conf.py
nilupulmanodya Nov 30, 2023
6868c0c
test with sys.path
nilupulmanodya Dec 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mslib/mscolab/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ class setup_saml2_backend:
# }
# },
]
# Setting up the path from environment variables to settings is only for testing purposes
mscolab_settings.MSCOLAB_SSO_DIR = os.getenv("TESTING_MSCOLAB_SSO_DIR", mscolab_settings.MSCOLAB_SSO_DIR)
mscolab_settings.USE_SAML2 = bool(os.getenv("TESTING_USE_SAML2", mscolab_settings.USE_SAML2))

if os.path.exists(f"{mscolab_settings.MSCOLAB_SSO_DIR}/mss_saml2_backend.yaml"):
with open(f"{mscolab_settings.MSCOLAB_SSO_DIR}/mss_saml2_backend.yaml", encoding="utf-8") as fobj:
yaml_data = yaml.safe_load(fobj)
Expand Down
14 changes: 11 additions & 3 deletions mslib/mscolab/mscolab.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@
from mslib.mscolab.utils import create_files
from mslib.utils import setup_logging
from mslib.utils.qt import Worker, Updater
from mslib.mscolab import mscolab
from mslib.msidp import idp_conf


# ToDo: refactor after testing this is a work around just for testing
import sys
sys.path.append("../../")

def handle_start(args):
from mslib.mscolab.server import APP, initialize_managers, start_server
setup_logging(args)
Expand Down Expand Up @@ -271,8 +277,10 @@ def handle_mscolab_metadata_init(repo_exists):
print('generating metadata file for the mscolab server')

try:
command = ["python", os.path.join("mslib", "mscolab", "mscolab.py"),
"start"] if repo_exists else ["mscolab", "start"]
import sys
print(sys.path)

command = ["python", mscolab.__file__, "start"] if repo_exists else ["mscolab", "start"]
process = subprocess.Popen(command)
cmd_curl = ["curl", "--retry", "5", "--retry-connrefused", "--retry-delay", "3",
"http://localhost:8083/metadata/localhost_test_idp",
Expand All @@ -294,7 +302,7 @@ def handle_local_idp_metadata_init(repo_exists):
if os.path.exists(os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "idp.xml")):
os.remove(os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "idp.xml"))

idp_conf_path = os.path.join("mslib", "msidp", "idp_conf.py")
idp_conf_path = idp_conf.__file__

if not repo_exists:
import site
Expand Down
3 changes: 2 additions & 1 deletion mslib/msidp/idp_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@
from saml2.saml import NAME_FORMAT_URI
from saml2.saml import NAMEID_FORMAT_PERSISTENT
from saml2.saml import NAMEID_FORMAT_TRANSIENT
from tests import constants

XMLSEC_PATH = os.path.join(os.environ["CONDA_PREFIX"], "bin", "xmlsec1")

# CRTs and metadata files can be generated through the mscolab server.
# if configured that way CRTs DIRs should be same in both IDP and mscolab server.
BASE_DIR = os.path.expanduser("~")
DATA_DIR = os.path.join(BASE_DIR, "colabdata")
MSCOLAB_SSO_DIR = os.path.join(DATA_DIR, 'datasso')
MSCOLAB_SSO_DIR = os.getenv("TESTING_MSCOLAB_SSO_DIR", os.path.join(DATA_DIR, 'datasso'))

BASEDIR = os.path.abspath(os.path.dirname(__file__))

Expand Down
111 changes: 109 additions & 2 deletions tests/_test_mscolab/test_mscolab.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,22 @@
limitations under the License.
"""
import os
import argparse
import pytest
import mock
import argparse
from flask_testing import TestCase

from mslib.mscolab.conf import mscolab_settings
from mslib.mscolab.models import Operation, User, Permission
from mslib.mscolab.mscolab import handle_db_reset, handle_db_seed, confirm_action, main
from mslib.mscolab.mscolab import (handle_db_reset, handle_db_seed, confirm_action, main,
handle_mscolab_certificate_init, handle_local_idp_certificate_init,
handle_mscolab_backend_yaml_init, handle_mscolab_metadata_init,
handle_local_idp_metadata_init)
from mslib.mscolab.server import APP
from mslib.mscolab.seed import add_operation
from tests import constants

mscolab_settings.MSCOLAB_SSO_DIR = constants.MSCOLAB_SSO_DIR


def test_confirm_action():
Expand Down Expand Up @@ -114,3 +120,104 @@ def test_handle_db_seed(self):
assert len(all_users) == 10
all_permissions = Permission.query.all()
assert len(all_permissions) == 17

def test_handle_mscolab_certificate_init(self):
"""
Test the initialization of the MSColab server certificate files.
This function tests the initialization process of the MSColab server certificate files
by calling the initialization function and checking if the generated key and
certificate files contain the expected content.
"""
handle_mscolab_certificate_init()
file_key = os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, 'key_mscolab.key')
key_content = ''
with open(file_key, 'r', encoding='utf-8') as file:
key_content = file.read()
assert "-----BEGIN PRIVATE KEY-----" in key_content
assert "-----END PRIVATE KEY-----" in key_content
file_cert = os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, 'crt_mscolab.crt')
crt_content = ''
with open(file_cert, 'r', encoding='utf-8') as file:
crt_content = file.read()
assert "-----BEGIN CERTIFICATE-----" in crt_content
assert "-----END CERTIFICATE-----" in crt_content

def test_handle_local_idp_certificate_init(self):
"""
Test the initialization of the local Identity Provider (IDP) certificate files.
This function tests the initialization process of the local IDP certificate files
by calling the initialization function and checking if the generated key and
certificate files contain the expected content.
"""

handle_local_idp_certificate_init()
file_key = os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, 'key_local_idp.key')
key_content = ''
with open(file_key, 'r', encoding='utf-8') as file:
key_content = file.read()
assert "-----BEGIN PRIVATE KEY-----" in key_content
assert "-----END PRIVATE KEY-----" in key_content
file_crt = os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, 'crt_local_idp.crt')
crt_content = ''
with open(file_crt, 'r', encoding='utf-8') as file:
crt_content = file.read()
assert "-----BEGIN CERTIFICATE-----" in crt_content
assert "-----END CERTIFICATE-----" in crt_content

def test_handle_mscolab_backend_yaml_init(self):
"""
Test the initialization of MScolab backend YAML configuration.
This function tests the initialization process of the MScolab backend YAML
"""

handle_mscolab_backend_yaml_init()
file_yaml = os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, 'mss_saml2_backend.yaml')
mss_saml2_backend_content = ''
with open(file_yaml, 'r', encoding='utf-8') as file:
mss_saml2_backend_content = file.read()
assert "localhost_test_idp" in mss_saml2_backend_content
assert "entityid_endpoint" in mss_saml2_backend_content

def test_handle_mscolab_metadata_init(self):
"""
Test the initialization of MSColab server metadata.
This function tests the initialization process of MSColab server metadata
by calling several initialization functions and checking if the expected
content is present in the generated metadata XML file.
"""
# set TESTING_USE_SAML2 and MSCOLAB_SSO_DIRthrough envs
os.environ['TESTING_MSCOLAB_SSO_DIR'] = mscolab_settings.MSCOLAB_SSO_DIR
os.environ['TESTING_USE_SAML2'] = "True"

handle_mscolab_certificate_init()
handle_mscolab_backend_yaml_init()
mscolab_settings.USE_SAML2 = True
assert handle_mscolab_metadata_init(True) is True
Copy link
Member

@ReimarBauer ReimarBauer Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for using the command you assume you are in the correct dir, this can make problems for handle_mscolab_metadata_init

You can get the path of mscolab.py by importing it and looking on mscolab.__file__

Tests sometimes shows that also the code needs to become improved

metadata_xml = os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, 'metadata_sp.xml')
metadata_content = ''
with open(metadata_xml, 'r', encoding='utf-8') as file:
metadata_content = file.read()
assert "urn:oasis:names:tc:SAML:2.0:metadata" in metadata_content

def test_handle_local_idp_metadata_init(self):
"""
Test the initialization of local Identity Provider (IDP) metadata.
This function tests the initialization process of local IDP metadata
by calling several initialization functions and checking if the expected
content is present in the generated metadata XML file.
"""
# set TESTING_USE_SAML2 and MSCOLAB_SSO_DIRthrough envs
os.environ['TESTING_MSCOLAB_SSO_DIR'] = mscolab_settings.MSCOLAB_SSO_DIR
os.environ['TESTING_USE_SAML2'] = "True"

handle_local_idp_certificate_init()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you want
assert handle_local_idp_certificate_init() is True

you can also debug tests, this is an example by pycharm (full version, the free version can do this too)

debugging_tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are also "print" outputs which can be compared, if they are the expected ones, e.g. https://pavolkutaj.medium.com/how-to-test-printed-output-in-python-with-pytest-and-its-capsys-fixture-161010cfc5ad

handle_mscolab_backend_yaml_init()
handle_mscolab_certificate_init()
mscolab_settings.USE_SAML2 = True
handle_mscolab_metadata_init(True)
assert handle_local_idp_metadata_init(True) is True
idp_xml = os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, 'metadata_sp.xml')
idp_content = ''
with open(idp_xml, 'r', encoding='utf-8') as file:
idp_content = file.read()
assert "urn:oasis:names:tc:SAML:2.0:metadata" in idp_content
4 changes: 4 additions & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
# we keep DATA_DIR until we move netCDF4 files to pyfilesystem2
DATA_DIR = DATA_FS.getsyspath("")

# set MSCOLAB_SSO_DIR through envs
MSCOLAB_SSO_DIR = os.path.join(os.path.expanduser("~"), 'testingdatasso')


# deployed mscolab url
MSCOLAB_URL = "http://localhost:8083"
# mscolab test server's url
Expand Down