commit 163f57d0cffc454efe333f473c2331474432ed1f
Author: David Friesecký <frieseck@students.zcu.cz>
Date:   Tue Apr 13 01:01:51 2021 +0200

    Re #8578 - Remaked tests files
    - created tests for new functions
         - set_certificate_revoked, clear_certificate_revocation, get_all_revoked_by, get_all_issued_by

diff --git a/src/constants.py b/src/constants.py
index 43fdfb6..ec6fbce 100644
--- a/src/constants.py
+++ b/src/constants.py
@@ -4,6 +4,8 @@ DATABASE_FILE = "db/database_sqlite.db"
 DATABASE_FILE_LOCATION = FileAnchor("aswi2021jmsd", DATABASE_FILE)
 DATETIME_FORMAT = "%d.%m.%Y %H:%M:%S"
 
+REV_REASON_UNSPECIFIED = "unspecified"
+
 # Types of certificates
 ROOT_CA_ID = 1
 INTERMEDIATE_CA_ID = 2
diff --git a/tests/integration_tests/dao/certificate_repository/__init__.py b/tests/integration_tests/dao/certificate_repository/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/tests/integration_tests/dao/certificate_repository/conftest.py b/tests/integration_tests/dao/certificate_repository/conftest.py
deleted file mode 100644
index 45e2561..0000000
--- a/tests/integration_tests/dao/certificate_repository/conftest.py
+++ /dev/null
@@ -1,13 +0,0 @@
-import pytest
-import sqlite3
-from sqlite3 import Connection, Cursor
-
-from src.dao.certificate_repository import CertificateRepository
-from src.constants import DATABASE_FILE
-
-
-@pytest.fixture
-def repository():
-    connection: Connection = sqlite3.connect(DATABASE_FILE)
-    cursor: Cursor = connection.cursor()
-    return CertificateRepository(connection, cursor)
diff --git a/tests/integration_tests/dao/certificate_repository/create_certificate_test.py b/tests/integration_tests/dao/certificate_repository/create_certificate_test.py
deleted file mode 100644
index a2264f6..0000000
--- a/tests/integration_tests/dao/certificate_repository/create_certificate_test.py
+++ /dev/null
@@ -1,13 +0,0 @@
-import pytest
-from src.constants import *
-
-
-def test_connection(repository):
-    sql = f"SELECT * FROM {TAB_CERTIFICATE_TYPES}"
-    repository.cursor.execute(sql)
-    selected_rows = repository.cursor.fetchall()
-
-    assert len(selected_rows) > 1
-    assert selected_rows[ROOT_CA_ID - 1][1] == "ROOT_CA"
-    assert selected_rows[INTERMEDIATE_CA_ID - 1][1] == "INTERMEDIATE_CA"
-    assert selected_rows[CERTIFICATE_ID - 1][1] == "CERTIFICATE"
diff --git a/tests/integration_tests/dao/certificate_repository_test.py b/tests/integration_tests/dao/certificate_repository_test.py
new file mode 100644
index 0000000..e4fad84
--- /dev/null
+++ b/tests/integration_tests/dao/certificate_repository_test.py
@@ -0,0 +1,160 @@
+import pytest
+from src.constants import *
+from src.model.certificate import Certificate
+from src.model.private_key import PrivateKey
+
+# type=CA => usage={CA:True}
+
+
+def test_set_certificate_revoked(certificate_repository, private_key_repository):
+    private_key = PrivateKey(private_key_id=-1, private_key="test_pk", password="test_password")
+    private_key_id = private_key_repository.create(private_key)
+
+    assert type(private_key_id) is int
+
+    certificate = Certificate(certificate_id=-1, common_name="test_common_name", valid_from="test_from",
+                              valid_to="test_to", pem_data="test_pem_data", private_key_id=private_key_id,
+                              type_id=ROOT_CA_ID, parent_id=-1, usages={CA_ID: True})
+    certificate_id = certificate_repository.create(certificate)
+
+    assert type(certificate_id) is int
+
+    certificate = certificate_repository.read(certificate_id)
+
+    assert certificate.parent_id == certificate_id
+    assert certificate.revocation_date == ""
+    assert certificate.revocation_reason == ""
+
+    check_revoked = certificate_repository.set_certificate_revoked(
+        certificate_id=certificate_id, revocation_date="test_rev_date_1")
+
+    assert check_revoked is True
+
+    certificate = certificate_repository.read(certificate_id)
+
+    assert certificate.revocation_date == "test_rev_date_1"
+    assert certificate.revocation_reason == REV_REASON_UNSPECIFIED
+
+    check_revoked = certificate_repository.set_certificate_revoked(
+        certificate_id=certificate_id, revocation_date="test_rev_date_2")
+
+    assert check_revoked is False
+
+    certificate = certificate_repository.read(certificate_id)
+
+    assert certificate.revocation_date != "test_rev_date_2"
+    assert certificate.revocation_date == "test_rev_date_1"
+    assert certificate.revocation_reason == REV_REASON_UNSPECIFIED
+
+
+def test_clear_certificate_revocation(certificate_repository, private_key_repository):
+    private_key = PrivateKey(private_key_id=-1, private_key="test_pk", password="test_password")
+    private_key_id = private_key_repository.create(private_key=private_key)
+
+    assert type(private_key_id) is int
+
+    certificate = Certificate(certificate_id=-1, common_name="test_common_name", valid_from="test_from",
+                              valid_to="test_to", pem_data="test_pem_data", private_key_id=private_key_id,
+                              type_id=ROOT_CA_ID, parent_id=-1, usages={CA_ID: True})
+    certificate_id = certificate_repository.create(certificate=certificate)
+
+    assert type(certificate_id) is int
+
+    certificate = certificate_repository.read(certificate_id=certificate_id)
+
+    assert certificate.parent_id == certificate_id
+    assert certificate.revocation_date == ""
+    assert certificate.revocation_reason == ""
+
+    check_revoked = certificate_repository.set_certificate_revoked(
+        certificate_id=certificate_id, revocation_date="test_rev_date_1")
+
+    assert check_revoked is True
+
+    certificate = certificate_repository.read(certificate_id=certificate_id)
+
+    assert certificate.revocation_date == "test_rev_date_1"
+    assert certificate.revocation_reason == REV_REASON_UNSPECIFIED
+
+    check_cleared = certificate_repository.clear_certificate_revocation(certificate_id=certificate_id)
+
+    assert check_cleared is True
+
+    certificate = certificate_repository.read(certificate_id=certificate_id)
+
+    assert certificate.revocation_date == ""
+    assert certificate.revocation_reason == ""
+
+
+def test_get_all_revoked_by(certificate_repository, private_key_repository):
+    private_key = PrivateKey(private_key_id=-1, private_key="test_pk", password="test_password")
+    private_key_id = private_key_repository.create(private_key=private_key)
+
+    assert type(private_key_id) is int
+
+    certificate = Certificate(certificate_id=-1, common_name="test_common_name", valid_from="test_from",
+                              valid_to="test_to", pem_data="test_pem_data", private_key_id=private_key_id,
+                              type_id=ROOT_CA_ID, parent_id=-1, usages={CA_ID: True})
+    certificate_id = certificate_repository.create(certificate=certificate)
+
+    assert type(certificate_id) is int
+
+    certificate.type_id = INTERMEDIATE_CA_ID
+    certificate.parent_id = certificate_id
+
+    certificate.common_name = "child_1"
+    child_1_id = certificate_repository.create(certificate=certificate)
+
+    certificate.common_name = "child_2"
+    child_2_id = certificate_repository.create(certificate=certificate)
+
+    certificate.common_name = "child_3"
+    child_3_id = certificate_repository.create(certificate=certificate)
+
+    certificate_repository.set_certificate_revoked(
+        certificate_id=child_1_id, revocation_date="test_revoke_child_1", revocation_reason="test_parent")
+    certificate_repository.set_certificate_revoked(
+        certificate_id=child_3_id, revocation_date="test_revoke_child_3", revocation_reason="test_parent")
+
+    certificates = certificate_repository.get_all_revoked_by(certificate_id=certificate_id)
+
+    assert len(certificates) == 2
+    assert certificates[0].certificate_id == child_1_id and certificates[1].certificate_id == child_3_id
+
+
+def test_get_all_issued_by(certificate_repository, private_key_repository):
+    private_key = PrivateKey(private_key_id=-1, private_key="test_pk", password="test_password")
+    private_key_id = private_key_repository.create(private_key=private_key)
+
+    assert type(private_key_id) is int
+
+    certificate = Certificate(certificate_id=-1, common_name="test_common_name", valid_from="test_from",
+                              valid_to="test_to", pem_data="test_pem_data", private_key_id=private_key_id,
+                              type_id=ROOT_CA_ID, parent_id=-1, usages={CA_ID: True})
+    certificate_id = certificate_repository.create(certificate=certificate)
+
+    assert type(certificate_id) is int
+
+    certificate.type_id = INTERMEDIATE_CA_ID
+    certificate.parent_id = certificate_id
+
+    certificate.common_name = "child_1"
+    child_1_id = certificate_repository.create(certificate=certificate)
+
+    certificate.common_name = "child_2"
+    child_2_id = certificate_repository.create(certificate=certificate)
+
+    certificate.common_name = "child_3"
+    child_3_id = certificate_repository.create(certificate=certificate)
+
+    certificate_repository.set_certificate_revoked(
+        certificate_id=child_1_id, revocation_date="test_revoke_child_1", revocation_reason="test_parent")
+    certificate_repository.set_certificate_revoked(
+        certificate_id=child_3_id, revocation_date="test_revoke_child_3", revocation_reason="test_parent")
+
+    certificates = certificate_repository.get_all_issued_by(certificate_id=certificate_id)
+
+    assert len(certificates) == 3
+    assert certificates[0].certificate_id == child_1_id and \
+           certificates[1].certificate_id == child_2_id and \
+           certificates[2].certificate_id == child_3_id
diff --git a/tests/integration_tests/dao/conftest.py b/tests/integration_tests/dao/conftest.py
new file mode 100644
index 0000000..042a8cd
--- /dev/null
+++ b/tests/integration_tests/dao/conftest.py
@@ -0,0 +1,38 @@
+import pytest
+import sqlite3
+from sqlite3 import Connection, Cursor
+
+from src.dao.certificate_repository import CertificateRepository
+from src.dao.private_key_repository import PrivateKeyRepository
+from src.db.init_queries import SCHEMA_SQL, DEFAULT_VALUES_SQL
+
+
+# scope="module" means that this fixture is run once per module
+@pytest.fixture(scope="module")
+def connection():
+    print("Creating a new SQLITE connection to the test DB")
+    connection: Connection = sqlite3.connect(':memory:')
+
+    # yield the created connection
+    yield connection
+
+
+@pytest.fixture(scope="module")
+def cursor(connection):
+    cursor = connection.cursor()
+
+    # execute db initialisation script
+    cursor.executescript(SCHEMA_SQL)
+
+    # insert default values
+    cursor.executescript(DEFAULT_VALUES_SQL)
+
+    return cursor
+
+@pytest.fixture
+def certificate_repository(connection, cursor):
+    return CertificateRepository(connection, cursor)
+
+@pytest.fixture
+def private_key_repository(connection, cursor):
+    return PrivateKeyRepository(connection, cursor)
