commit c36d3299a385137f9a66e6dd376c60400b2cd4c0
Author: David Friesecký <frieseck@students.zcu.cz>
Date:   Wed Apr 28 10:08:31 2021 +0200

    Re #8670 - Covered certificate_repository by the CRUD tests

diff --git a/src/dao/certificate_repository.py b/src/dao/certificate_repository.py
index 0130a91..ecdc760 100644
--- a/src/dao/certificate_repository.py
+++ b/src/dao/certificate_repository.py
@@ -196,8 +196,6 @@ class CertificateRepository:
                       certificate.parent_id,
                       certificate_id]
 
-            print(f"Parent: {certificate.parent_id}")
-
             self.cursor.execute(sql, values)
             self.connection.commit()
 
@@ -207,6 +205,8 @@ class CertificateRepository:
             self.cursor.execute(sql, values)
             self.connection.commit()
 
+            check_updated = self.cursor.rowcount > 0
+
             # iterate over usage pairs
             for usage_id, usage_value in certificate.usages.items():
                 if usage_value:
@@ -220,7 +220,7 @@ class CertificateRepository:
         except Error as e:
             raise DatabaseException(e)
 
-        return self.cursor.rowcount > 0
+        return check_updated
 
     def delete(self, certificate_id: int):
         """
diff --git a/tests/integration_tests/dao/certificate_repository_test.py b/tests/integration_tests/dao/certificate_repository_test.py
index ddaaf16..f9d4e55 100644
--- a/tests/integration_tests/dao/certificate_repository_test.py
+++ b/tests/integration_tests/dao/certificate_repository_test.py
@@ -4,6 +4,236 @@ from src.model.certificate import Certificate
 from src.model.private_key import PrivateKey
 
 
+def test_create(certificate_repository, private_key_repository_unique):
+    private_key = PrivateKey(private_key_id=-1, private_key="test_pk", password="test_password")
+    private_key_id = private_key_repository_unique.create(private_key=private_key)
+
+    assert type(private_key_id) is int
+
+    certificate_1 = Certificate(certificate_id=-1, common_name="test_create_1", valid_from="test_from_1",
+                                valid_to="test_to_1", pem_data="test_pem_data_1", private_key_id=private_key_id,
+                                type_id=ROOT_CA_ID, parent_id=-1, usages={CA_ID: True})
+    certificate_id_1 = certificate_repository.create(certificate=certificate_1)
+
+    certificate_2 = Certificate(certificate_id=-1, common_name="test_create_2", valid_from="test_from_2",
+                                valid_to="test_to_2", pem_data="test_pem_data_2", private_key_id=private_key_id,
+                                type_id=INTERMEDIATE_CA_ID, parent_id=certificate_id_1,
+                                usages={CA_ID: True, SSL_ID: True})
+    certificate_id_2 = certificate_repository.create(certificate=certificate_2)
+
+    assert type(certificate_id_1) is int
+    assert type(certificate_id_2) is int
+
+    certificate_1 = certificate_repository.read(certificate_id=certificate_id_1)
+    certificate_2 = certificate_repository.read(certificate_id=certificate_id_2)
+
+    assert certificate_1.certificate_id == certificate_id_1
+    assert certificate_1.common_name == "test_create_1"
+    assert certificate_1.valid_from == "test_from_1"
+    assert certificate_1.valid_to == "test_to_1"
+    assert certificate_1.private_key_id == private_key_id
+    assert certificate_1.type_id == ROOT_CA_ID
+    assert certificate_1.parent_id == certificate_id_1
+    assert certificate_1.usages[CA_ID]
+    assert not certificate_1.usages[SSL_ID]
+    assert not certificate_1.usages[SIGNATURE_ID]
+    assert not certificate_1.usages[AUTHENTICATION_ID]
+    assert certificate_1.revocation_date == ""
+    assert certificate_1.revocation_reason == ""
+
+    assert certificate_2.certificate_id == certificate_id_2
+    assert certificate_2.common_name == "test_create_2"
+    assert certificate_2.valid_from == "test_from_2"
+    assert certificate_2.valid_to == "test_to_2"
+    assert certificate_2.private_key_id == private_key_id
+    assert certificate_2.type_id == INTERMEDIATE_CA_ID
+    assert certificate_2.parent_id == certificate_id_1
+    assert certificate_2.usages[CA_ID]
+    assert certificate_2.usages[SSL_ID]
+    assert not certificate_2.usages[SIGNATURE_ID]
+    assert not certificate_2.usages[AUTHENTICATION_ID]
+    assert certificate_2.revocation_date == ""
+    assert certificate_2.revocation_reason == ""
+
+
+def test_read(certificate_repository, private_key_repository_unique):
+    private_key = PrivateKey(private_key_id=-1, private_key="test_pk", password="test_password")
+    private_key_id = private_key_repository_unique.create(private_key=private_key)
+
+    assert type(private_key_id) is int
+
+    certificate = Certificate(certificate_id=-1, common_name="test_read", 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.certificate_id == certificate_id
+    assert certificate.common_name == "test_read"
+    assert certificate.valid_from == "test_from"
+    assert certificate.valid_to == "test_to"
+    assert certificate.private_key_id == private_key_id
+    assert certificate.type_id == ROOT_CA_ID
+    assert certificate.parent_id == certificate_id
+    assert certificate.usages[CA_ID]
+    assert not certificate.usages[SSL_ID]
+    assert not certificate.usages[SIGNATURE_ID]
+    assert not certificate.usages[AUTHENTICATION_ID]
+    assert certificate.revocation_date == ""
+    assert certificate.revocation_reason == ""
+
+
+def test_read_invalid_id(certificate_repository, private_key_repository_unique):
+    none_id = 100
+    certificate = certificate_repository.read(certificate_id=none_id)
+
+    assert certificate is None
+
+
+def test_read_all(certificate_repository, private_key_repository_unique):
+    certificates = certificate_repository.read_all()
+
+    assert len(certificates) == 3
+
+    assert certificates[0].common_name == "test_create_1"
+    assert certificates[1].common_name == "test_create_2"
+    assert certificates[2].common_name == "test_read"
+
+def test_update(certificate_repository, private_key_repository_unique):
+    private_key = PrivateKey(private_key_id=-1, private_key="test_pk", password="test_password")
+    private_key_id = private_key_repository_unique.create(private_key=private_key)
+
+    assert type(private_key_id) is int
+
+    certificate = Certificate(certificate_id=-1, common_name="test_update_before", 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.certificate_id == certificate_id
+    assert certificate.common_name == "test_update_before"
+    assert certificate.valid_from == "test_from"
+    assert certificate.valid_to == "test_to"
+    assert certificate.private_key_id == private_key_id
+    assert certificate.type_id == ROOT_CA_ID
+    assert certificate.parent_id == certificate_id
+    assert certificate.usages[CA_ID]
+    assert not certificate.usages[SSL_ID]
+    assert not certificate.usages[SIGNATURE_ID]
+    assert not certificate.usages[AUTHENTICATION_ID]
+    assert certificate.revocation_date == ""
+    assert certificate.revocation_reason == ""
+
+    certificate.common_name = "test_update_after"
+    certificate.usages[SSL_ID] = True
+
+    check_updated = certificate_repository.update(certificate_id=certificate_id, certificate=certificate)
+
+    assert check_updated is True
+
+    certificate = certificate_repository.read(certificate_id=certificate_id)
+
+    assert certificate.certificate_id == certificate_id
+    assert certificate.common_name == "test_update_after"
+    assert certificate.valid_from == "test_from"
+    assert certificate.valid_to == "test_to"
+    assert certificate.private_key_id == private_key_id
+    assert certificate.type_id == ROOT_CA_ID
+    assert certificate.parent_id == certificate_id
+    assert certificate.usages[CA_ID]
+    assert certificate.usages[SSL_ID]
+    assert not certificate.usages[SIGNATURE_ID]
+    assert not certificate.usages[AUTHENTICATION_ID]
+    assert certificate.revocation_date == ""
+    assert certificate.revocation_reason == ""
+
+
+def test_update_invalid_id(certificate_repository, private_key_repository_unique):
+    private_key = PrivateKey(private_key_id=-1, private_key="test_pk", password="test_password")
+    private_key_id = private_key_repository_unique.create(private_key=private_key)
+
+    assert type(private_key_id) is int
+
+    certificate = Certificate(certificate_id=-1, common_name="test_update_before", 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.certificate_id == certificate_id
+    assert certificate.common_name == "test_update_before"
+    assert certificate.valid_from == "test_from"
+    assert certificate.valid_to == "test_to"
+    assert certificate.private_key_id == private_key_id
+    assert certificate.type_id == ROOT_CA_ID
+    assert certificate.parent_id == certificate_id
+    assert certificate.usages[CA_ID]
+    assert not certificate.usages[SSL_ID]
+    assert not certificate.usages[SIGNATURE_ID]
+    assert not certificate.usages[AUTHENTICATION_ID]
+    assert certificate.revocation_date == ""
+    assert certificate.revocation_reason == ""
+
+    certificate.common_name = "test_update_after"
+    certificate.usages[SSL_ID] = True
+
+    none_id = 100
+    check_updated = certificate_repository.update(certificate_id=none_id, certificate=certificate)
+
+    assert check_updated is False
+
+
+def test_delete(certificate_repository, private_key_repository_unique):
+    private_key = PrivateKey(private_key_id=-1, private_key="test_pk", password="test_password")
+    private_key_id = private_key_repository_unique.create(private_key=private_key)
+
+    assert type(private_key_id) is int
+
+    certificate_1 = Certificate(certificate_id=-1, common_name="test_delete_1", 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_1 = certificate_repository.create(certificate=certificate_1)
+
+    certificate_2 = Certificate(certificate_id=-1, common_name="test_delete_2", 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_2 = certificate_repository.create(certificate=certificate_2)
+
+    assert type(certificate_id_1) is int
+    assert type(certificate_id_2) is int
+
+    certificate_1 = certificate_repository.read(certificate_id=certificate_id_1)
+    certificate_2 = certificate_repository.read(certificate_id=certificate_id_2)
+
+    assert certificate_1.certificate_id == certificate_id_1
+    assert certificate_1.common_name == "test_delete_1"
+
+    assert certificate_2.certificate_id == certificate_id_2
+    assert certificate_2.common_name == "test_delete_2"
+
+    check_deleted = certificate_repository.delete(certificate_id=certificate_id_1)
+
+    assert check_deleted is True
+
+    certificate_1 = certificate_repository.read(certificate_id=certificate_id_1)
+    certificate_2 = certificate_repository.read(certificate_id=certificate_id_2)
+
+    assert certificate_1 is None
+
+    assert certificate_2.certificate_id == certificate_id_2
+    assert certificate_2.common_name == "test_delete_2"
+
+
 def test_set_certificate_revoked(certificate_repository, private_key_repository_unique):
     private_key = PrivateKey(private_key_id=-1, private_key="test_pk", password="test_password")
     private_key_id = private_key_repository_unique.create(private_key)
diff --git a/tests/integration_tests/dao/private_key_repository_test.py b/tests/integration_tests/dao/private_key_repository_test.py
index 0d65399..6c149ff 100644
--- a/tests/integration_tests/dao/private_key_repository_test.py
+++ b/tests/integration_tests/dao/private_key_repository_test.py
@@ -128,6 +128,7 @@ def test_update_invalid_id(private_key_repository_unique):
 
     assert check_updated is False
 
+
 def test_delete(private_key_repository_unique):
     private_key_1 = PrivateKey(private_key_id=-1, private_key="test_private_key_1", password="test_password_1")
     private_key_2 = PrivateKey(private_key_id=-1, private_key="test_private_key_2")
@@ -152,6 +153,8 @@ def test_delete(private_key_repository_unique):
 
     check_deleted = private_key_repository_unique.delete(private_key_id=private_key_1_id)
 
+    assert check_deleted is True
+
     private_keys = private_key_repository_unique.read_all()
 
     assert len(private_keys) == 1
