Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 2166bb21

Přidáno uživatelem Stanislav Král před téměř 4 roky(ů)

Added a global error handler for DatabaseException errors.
Covered the added global DatabaseException error handler in a test.

Zobrazit rozdíly:

app.py
9 9
from src.config.connection_provider import ConnectionProvider
10 10
from src.controllers.certificates_controller import CertController
11 11
from src.controllers.crl_ocsp_controller import CrlOcspController
12
from src.exceptions.database_exception import DatabaseException
12 13
from src.services.cryptography import CryptographyService, CryptographyException
13 14
from src.utils.logger import Logger
14 15

  
......
95 96
    return certificate_controller.handle_cryptography_error(e)
96 97

  
97 98

  
99
@app.errorhandler(DatabaseException)
100
def database_error(e, certificate_controller: CertController):
101
    return certificate_controller.handle_database_error(e)
102

  
103

  
98 104
def initialize_app(application) -> bool:
99 105
    """
100 106
    Initializes the application
src/controllers/certificates_controller.py
66 66
                                                  "It may be caused by wrong format of extensions."}
67 67

  
68 68
E_UNHANDLED_CRYPTOGRAPHY_ERROR = {"success": False, "data": "An unknown error has happened in the cryptography library."}
69
E_UNHANDLED_DATABASE_ERROR = {"success": False, "data": "An unknown database error has happened."}
69 70

  
70 71

  
71 72
class CertController:
......
775 776
    def handle_cryptography_error(e):
776 777
        Logger.error(f"An unhandled CryptographyException has been raised: {str(e)}")
777 778
        return E_UNHANDLED_CRYPTOGRAPHY_ERROR, C_INTERNAL_SERVER_ERROR
779

  
780
    @staticmethod
781
    def handle_database_error(e):
782
        Logger.error(f"An unhandled DatabaseException has been raised: {str(e)}")
783
        return E_UNHANDLED_DATABASE_ERROR, C_INTERNAL_SERVER_ERROR
tests/integration_tests/rest_api/certificates_test.py
3 3
# 2->3->4->5->6->7
4 4
# 2->8->9
5 5
from src.controllers.certificates_controller import E_IDENTITY_NAME_NOT_SPECIFIED, E_IDENTITY_PASSWORD_NOT_SPECIFIED, \
6
    E_NO_CERTIFICATES_FOUND, E_UNHANDLED_CRYPTOGRAPHY_ERROR
6
    E_NO_CERTIFICATES_FOUND, E_UNHANDLED_CRYPTOGRAPHY_ERROR, E_UNHANDLED_DATABASE_ERROR
7
from src.exceptions.database_exception import DatabaseException
8
from src.model.certificate import Certificate
7 9
from src.services.cryptography import CryptographyException
8 10

  
9 11

  
......
1311 1313
    ret = make_root_ca(server)
1312 1314
    assert ret.status_code == 500
1313 1315
    assert ret.json == E_UNHANDLED_CRYPTOGRAPHY_ERROR
1316

  
1317

  
1318
def test_database_error_handler(server, mocker):
1319
    def mock_raises_database_error(self, certificate: Certificate):
1320
        raise DatabaseException("Could not create a certificate.")
1321

  
1322
    mocker.patch(
1323
        # patch create method of the CertificateRepository  in such way that a DatabaseException is raised
1324
        'src.dao.certificate_repository.CertificateRepository.create',
1325
        mock_raises_database_error
1326
    )
1327

  
1328
    ret = make_root_ca(server)
1329
    assert ret.status_code == 500
1330
    assert ret.json == E_UNHANDLED_DATABASE_ERROR

Také k dispozici: Unified diff