def test_sns_publish_notification_with_boto_exception(self):
        with patch.object(aws_service_wrapper.SNS_CLIENT, 'publish',
                          side_effect=botocore.exceptions.BotoCoreError()):

            with self.assertRaises(AWSError):
                aws_service_wrapper.sns_publish_notification(
                    "topic_arn", "subject", "message")
def notify_new_recommendation(recommendation_set: object, app_ns: str):
    '''
        Sends an SNS notification indicating that a new recommendation has been generated

        Parameters
        ----------
        recommendation_set: object
            the SecurityRecommendationSet object repr
        app_ns: str
            The application namespace supplied to the command line
            used to identify the appropriate CloudFormation exports
    '''

    recommnedation_month = parser.parse(recommendation_set.model['valid_to'])

    formatted_ticker_message = ""
    for security in recommendation_set.model['securities_set']:
        formatted_ticker_message += "Ticker Symbol: %s\n" % security[
            'ticker_symbol']

    sns_topic_arn = aws_service_wrapper.cf_read_export_value(
        constants.sns_app_notifications_topic_arn(app_ns))
    subject = "New Stock Recommendation Available"
    message = "A New Stock Recommendation is available for the month of %s\n" % recommnedation_month.strftime(
        "%B, %Y")
    message += "\n\n"
    message += formatted_ticker_message

    log.info("Publishing Recommendation set to SNS topic: %s" % sns_topic_arn)
    aws_service_wrapper.sns_publish_notification(sns_topic_arn, subject,
                                                 message)
def publish_current_returns(updated_portfolio: object, updated: bool, app_ns: str):
    '''
        publishes current returns as a SNS notifcation, given an updated portfolio
    '''

    sns_topic_arn = aws_service_wrapper.cf_read_export_value(
        constants.sns_app_notifications_topic_arn(app_ns))
    subject = "Portfolio Update - "

    if updated == True:
        subject = "Stock Advisor Update - New Portfolio was created"
    else:
        subject = "Stock Advisor Update - Portfolio Returns"

    creation_date = parser.parse(updated_portfolio.model['creation_date'])
    price_date = parser.parse(updated_portfolio.model['price_date'])

    message = "Portfolio was created on: %s\n" % datetime.strftime(
        creation_date.astimezone(get_localzone()), '%Y/%m/%d %I:%M %p (%Z)')
    message += "Price date is: %s\n\n" % datetime.strftime(
        price_date, '%Y/%m/%d')
    for security in updated_portfolio.model['current_portfolio']['securities']:
        message += "Symbol: %s\n" % security['ticker_symbol']
        message += "Purchase Price: %.2f\n" % security[
            'purchase_price']
        message += "Current Price: %.2f (%+d%%)\n" % (
            security['current_price'], round(security['current_returns'] * 100))
        message += "\n"

    log.info("Publishing portfolio update to SNS topic: %s" % sns_topic_arn)
    aws_service_wrapper.sns_publish_notification(
        sns_topic_arn, subject, message)
def notify_new_recommendation(notification_list: list, app_ns: str):
    '''
        Sends an SNS notification indicating that a new recommendation has been generated

        Parameters
        ----------
        notification_list: list
            List of new SecurityRecommendationSet objects that require notifications
        app_ns: str
            The application namespace supplied to the command line
            used to identify the appropriate CloudFormation exports
    '''
    if notification_list == None or len(notification_list) == 0:
        return

    message = ""
    sns_topic_arn = aws_service_wrapper.cf_read_export_value(
        constants.sns_app_notifications_topic_arn(app_ns))
    subject = "New Stock Recommendation Available"

    for recommendation_set in notification_list:
        formatted_ticker_message = ""

        for security in recommendation_set.model['securities_set']:
            formatted_ticker_message += "Ticker Symbol: %s\n" % security[
                'ticker_symbol']

        message += "A New Stock Recommendation is available for the following trading strategy %s\n" % recommendation_set.model[
            'strategy_name']
        message += "\n"
        message += formatted_ticker_message

        message += "\n\n"

    log.info("Publishing Recommendation set to SNS topic: %s" % sns_topic_arn)
    aws_service_wrapper.sns_publish_notification(sns_topic_arn, subject,
                                                 message)