def apiKeyManager_worksProperly(): # arrange SECRET = 'abcd' SESSION_DURATION = 10 + 360 ALGORITHM = 'HS256' HEADER_NAME = 'Context' HEADER_TYPE = 'ApiKey ' IDENTITY = RandomHelper.string(minimum=100, maximum=150) CONTEXT = 'ABCD' CONTEXT_LIST = [CONTEXT] DATA = {'personal': 'data'} deltaMinutes = DateTimeHelper.timeDelta(minutes=SESSION_DURATION) apiKeyManager = ApiKeyManager.JwtManager(SECRET, ALGORITHM, HEADER_NAME, HEADER_TYPE) timeNow = DateTimeHelper.dateTimeNow() payload = { JwtConstant.KW_IAT: timeNow, JwtConstant.KW_NFB: timeNow, JwtConstant.KW_JTI: f"{int(f'{time.time()}'.replace('.', ''))+int(f'{time.time()}'.replace('.', ''))}", JwtConstant.KW_EXPIRATION: timeNow + deltaMinutes, JwtConstant.KW_IDENTITY: IDENTITY, JwtConstant.KW_FRESH: False, JwtConstant.KW_TYPE: JwtConstant.ACCESS_VALUE_TYPE, JwtConstant.KW_CLAIMS: { JwtConstant.KW_CONTEXT: CONTEXT_LIST, JwtConstant.KW_DATA: DATA } } # act totalRuns = 10000 lines = 3 initTime = time.time() for i in range(totalRuns): encodedPayload = apiKeyManager.encode(payload) decodedPayload = apiKeyManager.decode(encodedPayload) accessException = TestHelper.getRaisedException( apiKeyManager.validateAccessApiKey, rawJwt=decodedPayload) refreshException = TestHelper.getRaisedException( apiKeyManager.validateRefreshApiKey, rawJwt=decodedPayload) endTime = time.time() - initTime # assert assert lines * .0001 > endTime / totalRuns, (lines * .0001, endTime / totalRuns) assert ObjectHelper.equals(payload, decodedPayload), (payload, decodedPayload) assert ObjectHelper.isNone(accessException), accessException assert ObjectHelper.isNotNone(refreshException), refreshException assert ObjectHelper.equals( GlobalException.__name__, type(refreshException).__name__), (GlobalException.__name__, type(refreshException).__name__, refreshException) assert ObjectHelper.equals(401, refreshException.status) assert ObjectHelper.equals('Invalid apiKey', refreshException.message) assert ObjectHelper.equals( 'Refresh apiKey should have type refresh, but it is access', refreshException.logMessage)
def buildModelFromMessageScanResponseDto(self, messageScanDto, conversationKey) : errorList = messageScanDto['errorList'] soup = SoupUtil.getSoupFromHtml(messageScanDto['html']) ownerInfo = SoupUtil.getText( SoupUtil.getValue( SoupUtil.findByPartialAttributeValue(soup, 'div', 'class', 'copyable-text'), 'data-pre-plain-text' ) ) ownerKey = ownerInfo.split(']')[-1].strip() ownerKey = ownerKey if not ownerKey.endswith(':') else ownerKey[:-1] now = DateTimeHelper.dateTimeNow() postedAt = ownerInfo.split(']')[0].split('[')[-1].strip().split() postedAt = f'{StringHelper.join(postedAt[-1].split("/")[::-1], character="-")} {postedAt[0]}:{str(now).split(":")[-1]}' return Message.Message( key = messageScanDto['key'], conversationKey = conversationKey, ownerKey = ownerKey, postedAt = postedAt, ownerInfo = ownerInfo, scannedAt = now, text = SoupUtil.getText(SoupUtil.findByPartialAttributeValue(soup, 'span', 'class', 'selectable-text copyable-text')), originalAsText = SoupUtil.getText(soup), originalAsHtml = messageScanDto['html'], isPoolerMessage = 'message-out' in SoupUtil.getValue(SoupUtil.findTag(soup, 'div'), 'class'), poolingStatus = PoolingStatus.SUCCESS, errorCount = len(errorList), errorListAsJson = Serializer.jsonifyIt(errorList) )
def getStatus(self): try: actuatorHealthList = self.repository.actuatorHealth.findAll() if ObjectHelper.isList(actuatorHealthList) and 1 == len( actuatorHealthList): model = actuatorHealthList[0] else: try: model = self.repository.actuatorHealth.save( ActuatorHealth.ActuatorHealth( status=ActuatorHealthStatus.UP)) except Exception as exception: log.error(self.getStatus, 'Api cannot reach database', exception) model = ActuatorHealth.ActuatorHealth() model.laskCheck = DateTimeHelper.dateTimeNow() self.repository.actuatorHealth.save(model) except Exception as exception: model = ActuatorHealth.ActuatorHealth( status=ActuatorHealthStatus.DOWN, laskCheck=DateTimeHelper.dateTimeNow()) log.error(self.getStatus, f'Api is {model.status}', exception) return self.converter.actuatorHealth.fromModelToResponseDto(model)
def overrideDateData(model): now = DateTimeHelper.dateTimeNow() model.createdAt = getValueOrDefault(model.createdAt, now) model.updatedAt = now
def overrideDateData(self, model): now = DateTimeHelper.dateTimeNow() model.createdAt = ConverterStatic.getValueOrDefault( model.createdAt, now) model.updatedAt = now
def updateAll(self, modelList) : dateTimeNow = DateTimeHelper.dateTimeNow() for model in modelList : model.updatedAt = dateTimeNow return self.repository.message.saveAll(modelList)