예제 #1
0
def getJwtMannager(appInstance,
                   jwtSecret,
                   algorithm=None,
                   headerName=None,
                   headerType=None):
    if not jwtSecret:
        log.warning(
            getJwtMannager,
            f'Not possible to instanciate sessionManager{c.DOT_SPACE_CAUSE}Missing jwt secret at {ConfigurationKeyConstant.API_SESSION_SECRET}'
        )
    else:
        jwtManager = JwtManager(
            jwtSecret,
            ConverterStatic.getValueOrDefault(
                algorithm, JwtConstant.DEFAULT_JWT_SESSION_ALGORITHM),
            ConverterStatic.getValueOrDefault(
                headerName, JwtConstant.DEFAULT_JWT_SESSION_HEADER_NAME),
            ConverterStatic.getValueOrDefault(
                headerType, JwtConstant.DEFAULT_JWT_SESSION_HEADER_TYPE))
        if SettingHelper.activeEnvironmentIsLocal():
            info = {
                'secret': jwtManager.secret,
                'algorithm': jwtManager.algorithm,
                'headerName': jwtManager.headerName,
                'headerType': jwtManager.headerType
            }
            log.prettyJson(getJwtMannager,
                           f'JWT session',
                           info,
                           logLevel=log.SETTING)
        return jwtManager
예제 #2
0
def mustLogEnvironmentSettings():
    # Arrange

    # Act
    SettingHelper.logEnvironmentSettings()

    # Assert
    assert SettingHelper.LOCAL_ENVIRONMENT == EnvironmentHelper.get(
        SettingHelper.ACTIVE_ENVIRONMENT)
    assert SettingHelper.LOCAL_ENVIRONMENT == SettingHelper.getActiveEnvironment(
    )
    assert SettingHelper.activeEnvironmentIsLocal()
    assert "some value" == EnvironmentHelper.get('SOME_PARTICULAR_SETTING')
예제 #3
0
def getJwtMannager(appInstance,
                   jwtSecret,
                   algorithm=None,
                   headerName=None,
                   headerType=None):
    if ObjectHelper.isNone(jwtSecret):
        log.warning(
            getJwtMannager,
            f'Not possible to instanciate securityManager{c.DOT_SPACE_CAUSE}Missing jwt secret at {ConfigurationKeyConstant.API_SECURITY_SECRET}'
        )
    else:
        jwtMannager = JWTManager(appInstance)
        appInstance.config[JwtConstant.KW_JWT_SECRET_KEY] = jwtSecret
        appInstance.config[JwtConstant.KW_JWT_BLACKLIST_ENABLED] = True
        appInstance.config[
            JwtConstant.KW_JWT_ALGORITHM] = ConverterStatic.getValueOrDefault(
                algorithm, JwtConstant.DEFAULT_JWT_SECURITY_ALGORITHM)
        appInstance.config[
            JwtConstant.
            KW_JWT_HEADER_NAME] = ConverterStatic.getValueOrDefault(
                headerName, JwtConstant.DEFAULT_JWT_SECURITY_HEADER_NAME)
        appInstance.config[
            JwtConstant.
            KW_JWT_HEADER_TYPE] = ConverterStatic.getValueOrDefault(
                headerType, JwtConstant.DEFAULT_JWT_SECURITY_HEADER_TYPE)
        if SettingHelper.activeEnvironmentIsLocal():
            info = {
                'secret': jwtSecret,
                'algorithm': appInstance.config[JwtConstant.KW_JWT_ALGORITHM],
                'headerName':
                appInstance.config[JwtConstant.KW_JWT_HEADER_NAME],
                'headerType':
                appInstance.config[JwtConstant.KW_JWT_HEADER_TYPE]
            }
            log.prettyJson(getJwtMannager,
                           f'JWT security',
                           info,
                           logLevel=log.SETTING)
        return jwtMannager
예제 #4
0
def mustLogWithColors():
    # Arrange
    noExceptionThrown = 'exception not thrown'
    someLogMessage = 'some log message'
    someExceptionMessage = 'some exception message'
    someInnerExceptionMessage = 'some inner exception message'
    exception = None
    someExceptionMessageWithStackTrace = f'{someExceptionMessage} with stacktrace'
    someExceptionMessageWithoutStackTrace = f'{someExceptionMessage} without stacktrace'

    def controlableException(logType, muteStackTrace=False):
        try:
            raise Exception(
                someExceptionMessageWithoutStackTrace
                if muteStackTrace else someExceptionMessageWithStackTrace)
        except Exception as exception:
            if logType in OPTIONAL_EXCEPTION_LOG_TYPES:
                logType(logType,
                        someLogMessage,
                        exception=exception,
                        muteStackTrace=muteStackTrace)
            else:
                logType(logType,
                        someLogMessage,
                        exception,
                        muteStackTrace=muteStackTrace)

    # Act
    log.success(log.success, someLogMessage)
    log.setting(log.setting, someLogMessage)
    log.debug(log.debug, someLogMessage)
    log.warning(log.warning, someLogMessage)

    controlableException(log.log)
    controlableException(log.debug)
    controlableException(log.warning)
    controlableException(log.wraper)
    controlableException(log.failure)
    controlableException(log.error)
    controlableException(log.test)

    controlableException(log.log, muteStackTrace=True)
    controlableException(log.debug, muteStackTrace=True)
    controlableException(log.warning, muteStackTrace=True)
    controlableException(log.wraper, muteStackTrace=True)
    controlableException(log.failure, muteStackTrace=True)
    controlableException(log.error, muteStackTrace=True)
    controlableException(log.test, muteStackTrace=True)

    log.log(log.log, someLogMessage, None)
    log.debug(log.debug, someLogMessage, None)
    log.warning(log.warning, someLogMessage, None)
    log.wraper(log.wraper, noExceptionThrown, None)
    log.failure(log.failure, noExceptionThrown, None)
    log.error(log.error, noExceptionThrown, None)
    log.test(log.test, someLogMessage, None)

    # Assert
    assert SettingHelper.LOCAL_ENVIRONMENT == EnvironmentHelper.get(
        SettingHelper.ACTIVE_ENVIRONMENT)
    assert SettingHelper.activeEnvironmentIsLocal()
예제 #5
0
def mustPrintMessageLog_withColors():
    # Arrange
    mustLogWithNewLine = 'must log with new line'
    mustNotLogWithNewLine = 'must not log with new line'
    mustLogWithoutNewLine = 'must log without new line'
    mustNotLogWithoutNewLine = 'must not log without new line'
    mustLogWithNewLineWithException = 'must log with new line with exception'
    mustNotLogWithNewLineWithException = 'must not log with new line with exception'
    mustLogWithoutNewLineWithException = 'must log without new line with exception'
    mustNotLogWithoutNewLineWithException = 'must not log without new line with exception'
    someExceptionMessage = 'some exception message'
    thrownException = None
    try:
        raise Exception(someExceptionMessage)
    except Exception as exception:
        thrownException = exception

    # Act
    log.printLog(mustLogWithNewLine, condition=True, newLine=True)
    log.printSuccess(mustLogWithNewLine, condition=True, newLine=True)
    log.printSetting(mustLogWithNewLine, condition=True, newLine=True)
    log.printDebug(mustLogWithNewLine,
                   condition=True,
                   newLine=True,
                   exception=None)
    log.printWarning(mustLogWithNewLine,
                     condition=True,
                     newLine=True,
                     exception=None)
    log.printWarper(mustLogWithNewLine,
                    condition=True,
                    newLine=True,
                    exception=None)
    log.printFailure(mustLogWithNewLine,
                     condition=True,
                     newLine=True,
                     exception=None)
    log.printError(mustLogWithNewLine,
                   condition=True,
                   newLine=True,
                   exception=None)
    log.printTest(mustLogWithNewLine,
                  condition=True,
                  newLine=True,
                  exception=None)

    log.printLog(mustNotLogWithNewLine, condition=False, newLine=True)
    log.printSuccess(mustNotLogWithNewLine, condition=False, newLine=True)
    log.printSetting(mustNotLogWithNewLine, condition=False, newLine=True)
    log.printDebug(mustNotLogWithNewLine,
                   condition=False,
                   newLine=True,
                   exception=None)
    log.printWarning(mustNotLogWithNewLine,
                     condition=False,
                     newLine=True,
                     exception=None)
    log.printWarper(mustNotLogWithNewLine,
                    condition=False,
                    newLine=True,
                    exception=None)
    log.printFailure(mustNotLogWithNewLine,
                     condition=False,
                     newLine=True,
                     exception=None)
    log.printError(mustNotLogWithNewLine,
                   condition=False,
                   newLine=True,
                   exception=None)
    log.printTest(mustNotLogWithNewLine,
                  condition=False,
                  newLine=True,
                  exception=None)

    log.printLog(mustLogWithoutNewLine, condition=True, newLine=False)
    log.printSuccess(mustLogWithoutNewLine, condition=True, newLine=False)
    log.printSetting(mustLogWithoutNewLine, condition=True, newLine=False)
    log.printDebug(mustLogWithoutNewLine,
                   condition=True,
                   newLine=False,
                   exception=None)
    log.printWarning(mustLogWithoutNewLine,
                     condition=True,
                     newLine=False,
                     exception=None)
    log.printWarper(mustLogWithoutNewLine,
                    condition=True,
                    newLine=False,
                    exception=None)
    log.printFailure(mustLogWithoutNewLine,
                     condition=True,
                     newLine=False,
                     exception=None)
    log.printError(mustLogWithoutNewLine,
                   condition=True,
                   newLine=False,
                   exception=None)
    log.printTest(mustLogWithoutNewLine,
                  condition=True,
                  newLine=False,
                  exception=None)

    log.printLog(mustNotLogWithoutNewLine, condition=False, newLine=False)
    log.printSuccess(mustNotLogWithoutNewLine, condition=False, newLine=False)
    log.printSetting(mustNotLogWithoutNewLine, condition=False, newLine=False)
    log.printDebug(mustNotLogWithoutNewLine,
                   condition=False,
                   newLine=False,
                   exception=None)
    log.printWarning(mustNotLogWithoutNewLine,
                     condition=False,
                     newLine=False,
                     exception=None)
    log.printWarper(mustNotLogWithoutNewLine,
                    condition=False,
                    newLine=False,
                    exception=None)
    log.printFailure(mustNotLogWithoutNewLine,
                     condition=False,
                     newLine=False,
                     exception=None)
    log.printError(mustNotLogWithoutNewLine,
                   condition=False,
                   newLine=False,
                   exception=None)
    log.printTest(mustNotLogWithoutNewLine,
                  condition=False,
                  newLine=False,
                  exception=None)

    log.printLog(mustLogWithNewLineWithException,
                 condition=True,
                 newLine=True,
                 exception=thrownException)
    log.printDebug(mustLogWithNewLineWithException,
                   condition=True,
                   newLine=True,
                   exception=thrownException)
    log.printWarning(mustLogWithNewLineWithException,
                     condition=True,
                     newLine=True,
                     exception=thrownException)
    log.printWarper(mustLogWithNewLineWithException,
                    condition=True,
                    newLine=True,
                    exception=thrownException)
    log.printFailure(mustLogWithNewLineWithException,
                     condition=True,
                     newLine=True,
                     exception=thrownException)
    log.printError(mustLogWithNewLineWithException,
                   condition=True,
                   newLine=True,
                   exception=thrownException)
    log.printTest(mustLogWithNewLineWithException,
                  condition=True,
                  newLine=True,
                  exception=thrownException)

    log.printLog(mustLogWithoutNewLineWithException,
                 condition=True,
                 newLine=False,
                 exception=thrownException)
    log.printDebug(mustLogWithoutNewLineWithException,
                   condition=True,
                   newLine=False,
                   exception=thrownException)
    log.printWarning(mustLogWithoutNewLineWithException,
                     condition=True,
                     newLine=False,
                     exception=thrownException)
    log.printWarper(mustLogWithoutNewLineWithException,
                    condition=True,
                    newLine=False,
                    exception=thrownException)
    log.printFailure(mustLogWithoutNewLineWithException,
                     condition=True,
                     newLine=False,
                     exception=thrownException)
    log.printError(mustLogWithoutNewLineWithException,
                   condition=True,
                   newLine=False,
                   exception=thrownException)
    log.printTest(mustLogWithoutNewLineWithException,
                  condition=True,
                  newLine=False,
                  exception=thrownException)

    log.printLog(mustNotLogWithNewLineWithException,
                 condition=False,
                 newLine=True,
                 exception=thrownException)
    log.printDebug(mustNotLogWithNewLineWithException,
                   condition=False,
                   newLine=True,
                   exception=thrownException)
    log.printWarning(mustNotLogWithNewLineWithException,
                     condition=False,
                     newLine=True,
                     exception=thrownException)
    log.printWarper(mustNotLogWithNewLineWithException,
                    condition=False,
                    newLine=True,
                    exception=thrownException)
    log.printFailure(mustNotLogWithNewLineWithException,
                     condition=False,
                     newLine=True,
                     exception=thrownException)
    log.printError(mustNotLogWithNewLineWithException,
                   condition=False,
                   newLine=True,
                   exception=thrownException)
    log.printTest(mustNotLogWithNewLineWithException,
                  condition=False,
                  newLine=True,
                  exception=thrownException)

    log.printLog(mustNotLogWithoutNewLineWithException,
                 condition=False,
                 newLine=False,
                 exception=thrownException)
    log.printDebug(mustNotLogWithoutNewLineWithException,
                   condition=False,
                   newLine=False,
                   exception=thrownException)
    log.printWarning(mustNotLogWithoutNewLineWithException,
                     condition=False,
                     newLine=False,
                     exception=thrownException)
    log.printWarper(mustNotLogWithoutNewLineWithException,
                    condition=False,
                    newLine=False,
                    exception=thrownException)
    log.printFailure(mustNotLogWithoutNewLineWithException,
                     condition=False,
                     newLine=False,
                     exception=thrownException)
    log.printError(mustNotLogWithoutNewLineWithException,
                   condition=False,
                   newLine=False,
                   exception=thrownException)
    log.printTest(mustNotLogWithoutNewLineWithException,
                  condition=False,
                  newLine=False,
                  exception=thrownException)

    # Assert
    assert True == SettingHelper.activeEnvironmentIsLocal()
    assert SettingHelper.LOCAL_ENVIRONMENT == EnvironmentHelper.get(
        SettingHelper.ACTIVE_ENVIRONMENT)
    assert SettingHelper.LOCAL_ENVIRONMENT == SettingHelper.getActiveEnvironment(
    )
예제 #6
0
 def getUrl(self, dialect):
     log.log(self.getUrl, 'Loading repository configuration')
     url = EnvironmentHelper.get(self.ENV_DATABASE_URL)
     if isNeitherNoneNorBlank(url):
         dialect = None
         driver = None
         database = None
         username = None
         password = None
         host = None
         port = None
         schema = None
         log.log(
             self.getUrl,
             f'Prioritising repository url in {self.ENV_DATABASE_URL} environment variable'
         )
     else:
         url = self.globals.getSetting(
             f'{self.KW_API}{c.DOT}{self.KW_DATABASE}{c.DOT}{self.KW_REPOSITORY_URL}'
         )
         if isNeitherNoneNorBlank(url):
             dialect = None
             driver = None
             database = None
             username = None
             password = None
             host = None
             port = None
             schema = None
             log.log(self.getUrl,
                     f'Prioritising repository url in yamel configuration')
         else:
             url = c.NOTHING
             driver = self.globals.getSetting(
                 f'{self.KW_API}{c.DOT}{self.KW_DATABASE}{c.DOT}{self.KW_REPOSITORY_DRIVER}'
             )
             database = self.globals.getSetting(
                 f'{self.KW_API}{c.DOT}{self.KW_DATABASE}{c.DOT}{self.KW_REPOSITORY_DATABASE}'
             )
             username = self.globals.getSetting(
                 f'{self.KW_API}{c.DOT}{self.KW_DATABASE}{c.DOT}{self.KW_REPOSITORY_USERNAME}'
             )
             password = self.globals.getSetting(
                 f'{self.KW_API}{c.DOT}{self.KW_DATABASE}{c.DOT}{self.KW_REPOSITORY_PASSWORD}'
             )
             host = self.globals.getSetting(
                 f'{self.KW_API}{c.DOT}{self.KW_DATABASE}{c.DOT}{self.KW_REPOSITORY_HOST}'
             )
             port = self.globals.getSetting(
                 f'{self.KW_API}{c.DOT}{self.KW_DATABASE}{c.DOT}{self.KW_REPOSITORY_PORT}'
             )
             schema = self.globals.getSetting(
                 f'{self.KW_API}{c.DOT}{self.KW_DATABASE}{c.DOT}{self.KW_REPOSITORY_SCHEMA}'
             )
             if isNeitherNoneNorBlank(username) and isNeitherNoneNorBlank(
                     password):
                 url += f'{username}{c.COLON}{password}'
             if isNeitherNoneNorBlank(host) and isNeitherNoneNorBlank(port):
                 url += f'{c.ARROBA}{host}{c.COLON}{port}'
             url += c.SLASH
             database = f'{database}' if isNeitherNoneNorBlank(
                 database
             ) else f'{self.DEFAULT_LOCAL_STORAGE_NAME if ObjectHelper.isNone(self.globals.apiName) else self.globals.apiName}{c.DOT}{self.EXTENSION}'
             if not isNeitherNoneNorBlank(dialect):
                 dialect = self.DEFAULT_DIALECT
             plusDriverOrNothing = f'{c.PLUS}{driver}' if isNeitherNoneNorBlank(
                 driver) else c.NOTHING
             dialectAndDriver = f'''{dialect}{plusDriverOrNothing}'''
             url = f'{dialectAndDriver}{c.COLON}{c.DOUBLE_SLASH}{url}{database}'
             log.log(self.getUrl,
                     'Prioritising repository yamel configuration')
     if SettingHelper.activeEnvironmentIsLocal():
         log.prettyJson(
             self.getUrl,
             'Repository configuations', {
                 **self.globals.getSetting(f'{self.KW_API}{c.DOT}{self.KW_DATABASE}'),
                 **{
                     'dialect': dialect,
                     'driver': driver,
                     'database': database,
                     'username': username,
                     'password': password,
                     'host': host,
                     'port': port,
                     'schema': schema,
                     'url': url
                 }
             },
             logLevel=log.SETTING)
     # log.prettyPython(self.getUrl, 'url', url, logLevel=log.LOG)
     return url