def test_voucher_is_available(self):
        voucher_code = 'test_voucher_remove_code'
        voucher_availability = 'test_voucher_remove_availability'
        certification_level = 'test_voucher_remove_level'

        #given
        self.voucher_table.put_item(Item={'code': voucher_code, 'certification_level': certification_level, 'availability': voucher_availability})
        voucher = Voucher(voucher_code, certification_level, voucher_availability)

        #when
        response = voucher.isAvailable()

        #then
        assert response == True
Beispiel #2
0
    def getVoucher(self):
        payload = None
        user = User.get(self.user_id)
        if user:
            for user_certification in user.user_certifications:
                if not user_certification.voucher_code:
                    try:
                        voucher = Voucher.getAvailable(
                            int(user_certification.level_id))
                        user_certification.attribuateVoucher(voucher)
                    except Exception as e:
                        self.logger.warn(e)

            payload = {
                "blocks": [{
                    "type": "section",
                    "block_id": "section001",
                    "text": {
                        "type": "mrkdwn",
                        "text": "Hi! Here are your personal voucher codes"
                    }
                }, {
                    "type": "section",
                    "block_id": "section002",
                    "fields": user.formatSlackFields()
                }]
            }
        else:
            payload = {
                "text":
                "Hi! Unfortunately we couldn't find your personal voucher code in our data base. Please get back to <@UBRJ09SBE>."
            }

        return payload
    def test_voucher_remove_available(self):
        voucher_code = 'test_voucher_remove_code'
        voucher_availability = 'test_voucher_remove_availability'
        certification_level = 'test_voucher_remove_level'

        #given
        self.voucher_table.put_item(Item={'code': voucher_code, 'certification_level': certification_level, 'availability': voucher_availability})
        voucher = Voucher(voucher_code, certification_level, voucher_availability)

        #when
        response = voucher.remove()

        #then
        assert response == True
        vouchers = self.voucher_table.scan(FilterExpression=Attr('code').eq(voucher_code))['Items']
        assert len(vouchers) == 0
    def test_voucher_is_unavailable(self):
        voucher_code = 'test_voucher_remove_code'
        voucher_availability = 'test_voucher_remove_availability'
        certification_level = 'test_voucher_remove_level'
        user_id = 'test_voucher_remove_user_id'
        attribuated_date = 'test_voucher_remove_attribuated_date'

        #given
        self.voucher_table.put_item(Item={'code': voucher_code, 'certification_level': certification_level, 'availability': voucher_availability})
        self.user_table.put_item(Item={'user_id': user_id, 'certification_level': certification_level, 'voucher_code': voucher_code, 'attribuated_date': attribuated_date})
        voucher = Voucher(voucher_code, certification_level, voucher_availability)

        #when
        response = voucher.isAvailable()

        #then
        assert response == False
    def test_voucher_add(self):
        voucher_code = 'test_voucher_add_code'
        voucher_availability = 'test_voucher_add_availability'
        certification_level = 'test_voucher_add_level'

        #given
        voucher = Voucher(voucher_code, certification_level, voucher_availability)

        #when
        response = voucher.add()

        #then
        assert response == True
        vouchers = self.voucher_table.scan(FilterExpression=Attr('code').eq(voucher_code))['Items']
        assert len(vouchers) == 1
        assert vouchers[0]['code'] == voucher_code
        assert vouchers[0]['certification_level'] == certification_level
        assert vouchers[0]['availability'] == voucher_availability
    def test_voucher_remove_unavailable(self):
        voucher_code = 'test_voucher_remove_code'
        voucher_availability = 'test_voucher_remove_availability'
        certification_level = 'test_voucher_remove_level'
        user_id = 'test_voucher_remove_user_id'
        attribuated_date = 'test_voucher_remove_attribuated_date'

        #given
        self.voucher_table.put_item(Item={'code': voucher_code, 'certification_level': certification_level, 'availability': voucher_availability})
        self.user_table.put_item(Item={'user_id': user_id, 'certification_level': certification_level, 'voucher_code': voucher_code, 'attribuated_date': attribuated_date})
        voucher = Voucher(voucher_code, certification_level, voucher_availability)

        #when
        response = voucher.remove()

        #then
        assert response == False
        vouchers = self.voucher_table.scan(FilterExpression=Attr('code').eq(voucher_code))['Items']
        assert len(vouchers) == 1
    def test_voucher_get_nonexistent(self):
        voucher_code = 'test_voucher_get_code'

        #given

        #when
        voucher = Voucher.get(voucher_code)

        #then
        assert voucher == None
    def test_voucher_str_available(self):
        voucher_code = 'test_voucher_str_available_id'
        voucher_availability = 'test_voucher_str_available_availability'
        certification_level = 'test_voucher_str_available_level'

        #given
        self.voucher_table.put_item(Item={'code': voucher_code, 'certification_level': certification_level, 'availability': voucher_availability})
        voucher = Voucher(voucher_code, certification_level, voucher_availability)

        #when
        response = str(voucher)

        #then
        assert response != None
    def test_voucher_get(self):
        voucher_code = 'test_voucher_get_code'
        voucher_availability = 'test_voucher_get_availability'
        certification_level = 'test_voucher_get_level'

        #given
        self.voucher_table.put_item(Item={'code': voucher_code, 'certification_level': certification_level, 'availability': voucher_availability})

        #when
        voucher = Voucher.get(voucher_code)

        #then
        assert voucher.code == voucher_code
        assert voucher.availability == voucher_availability
        assert voucher.certification_level == certification_level
    def test_voucher_get_available_only_available(self):
        voucher_A_code = 'test_voucher_A_get_code'
        voucher_B_code = 'test_voucher_B_get_code'
        voucher_availability = 'test_voucher_get_availability'
        certification_level = 'test_voucher_get_level'

        #given
        self.voucher_table.put_item(Item={'code': voucher_A_code, 'certification_level': certification_level, 'availability': voucher_availability})
        self.voucher_table.put_item(Item={'code': voucher_B_code, 'certification_level': certification_level, 'availability': voucher_availability})

        #when
        voucher = Voucher.getAvailable(certification_level)

        #then
        assert voucher != None
        assert voucher.code in [voucher_A_code, voucher_B_code]
    def test_voucher_get_available_other_level(self):
        voucher_A_code = 'test_voucher_A_get_code'
        voucher_B_code = 'test_voucher_B_get_code'
        voucher_availability = 'test_voucher_get_availability'
        certification_level_A = 'test_voucher_get_level_A'
        certification_level_B = 'test_voucher_get_level_B'

        #given
        self.voucher_table.put_item(Item={'code': voucher_A_code, 'certification_level': certification_level_A, 'availability': voucher_availability})
        self.voucher_table.put_item(Item={'code': voucher_B_code, 'certification_level': certification_level_B, 'availability': voucher_availability})

        #when
        voucher = Voucher.getAvailable(certification_level_A)

        #then
        assert voucher != None
        assert voucher.code == voucher_A_code
    def test_voucher_get_available_unavailable(self):
        voucher_A_code = 'test_voucher_A_get_code'
        voucher_B_code = 'test_voucher_B_get_code'
        voucher_availability = 'test_voucher_get_availability'
        certification_level_A = 'test_voucher_get_level_A'
        certification_level_B = 'test_voucher_get_level_B'
        user_id = 'test_voucher_remove_user_id'
        attribuated_date = 'test_voucher_remove_attribuated_date'

        #given
        self.voucher_table.put_item(Item={'code': voucher_A_code, 'certification_level': certification_level_A, 'availability': voucher_availability})
        self.voucher_table.put_item(Item={'code': voucher_B_code, 'certification_level': certification_level_B, 'availability': voucher_availability})
        self.user_table.put_item(Item={'user_id': user_id, 'certification_level': certification_level_A, 'voucher_code': voucher_A_code, 'attribuated_date': attribuated_date})

        #when
        voucher = Voucher.getAvailable(certification_level_A)

        #then
        assert voucher == None
    def test_user_attribuateVoucher_wrong_level(self):
        user_id = 'test_user_get_id'
        certification_level = 'test_user_get_level'
        voucher_code = 'test_user_get_voucher_code'
        voucher_level = 'test_user_get_voucher_level'
        voucher_availability = '01/01/2019'

        #given
        self.user_table.put_item(Item={'user_id': user_id, 'certification_level': certification_level})
        self.voucher_table.put_item(Item={'code': voucher_code, 'certification_level': voucher_level})
        user = User(user_id, certification_level)
        voucher = Voucher(voucher_code, voucher_level, voucher_availability)

        #when
        response = user.attribuateVoucher(voucher)

        #then
        assert response == False
        assert user.voucher_code == None
    def test_user_attribuateVoucher(self):
        user_id = 'test_user_get_id'
        certification_level = 'test_user_get_level'
        voucher_code = 'test_user_get_voucher_code'
        voucher_availability = '01/01/2019'

        #given
        self.user_table.put_item(Item={'user_id': user_id, 'certification_level': certification_level})
        self.voucher_table.put_item(Item={'code': voucher_code, 'certification_level': certification_level})
        user = User(user_id, certification_level)
        voucher = Voucher(voucher_code, certification_level, voucher_availability)

        #when
        response = user.attribuateVoucher(voucher)

        #then
        assert response == True
        assert user.voucher_code == voucher_code
        assert user.attribuated_date == time.strftime('%d/%m/%Y',time.localtime())
Beispiel #15
0
    def addVouchers(self, voucher_codes, availability_date,
                    certification_level_id):
        response = ""

        success_list = list()
        error_list = list()
        for voucher_code in voucher_codes:
            try:
                if Voucher(voucher_code, certification_level_id,
                           availability_date).add():
                    success_list.append(voucher_code)
                else:
                    error_list.append(voucher_code)
            except:
                error_list.append(voucher_code)

        message = str(len(success_list)) + " voucher codes added with success."
        if error_list and len(error_list) > 0:
            message += "\n" + str(
                len(error_list)
            ) + " voucher codes not imported:\n" + '\n'.join(error_list)
        response = {"text": message}

        return response
    def launch(self, reportType):

        start_time = time.time()
        levels = Level.getAll()
        vouchers = Voucher.getAll()
        milestones = Milestone.getAll()

        users = UserCertification.getAll()
        users_with_voucher = [user for user in users if user.voucher_code]
        users_with_profile = [
            user for user in users if user.profile_update_date
        ]
        users_without_gift = [
            user for user in users_with_profile if not user.gift_sent_date
        ]

        end_time = time.time()

        global_message = "*" + str(len(users)) + "* users in the challenge\n" \
            + "*" + str(len(users_with_voucher)) + "* already claimed a voucher code and " \
            + "*" + str(len(users_with_profile)) + "* have updated their profile"

        if len(milestones) > 0:
            global_message += "\n"
            for milestone in milestones:
                users_involved = [
                    user for user in users_with_profile
                    if user.profile_update_date <= milestone.date
                ]

                stars_earned = 0
                for user in users_involved:
                    stars_earned += next(
                        (level.stars
                         for level in levels if level.id == user.level_id),
                        None)

                users_involved_count = len(users_involved)
                global_message += "\n*Milestone #" + str(
                    milestone.id) + "* for _" + milestone.date.strftime(
                        '%d/%m/%Y') + "_ - " + str(stars_earned) + "/" + str(
                            milestone.goal) + " :star:"
                if stars_earned > 0:
                    global_message += " _(" + str(
                        users_involved_count) + " people involved)_"

        if reportType == 'user':
            # Users report
            user_message = global_message \
                + "\nDon't know how to get your certification? Go to https://kugawana.atlassian.net/wiki/x/DYAkAg"

            footer = "(compute time: " + \
                str(round(end_time - start_time, 3)) + ")"

            if self.config.post_to_slack:
                self.kugawana_tool.post_notification_to_kugawana_slack(
                    slack_channel=self.config.slack_channel,
                    title="Latest AWS certification report!",
                    title_link="https://aws.amazon.com/fr/certification/",
                    message=user_message,
                    footer=footer,
                    level="good")
            else:
                print(user_message)
                print(footer)

        elif reportType == 'admin':
            # Admin report
            admin_message = "*" + str(len(levels)) + "* certification levels\n" \
                + "*" + str(len(vouchers)) + "* voucher codes\n" \
                + global_message
            users_details = "\n".join(
                ["<@" + user.user_id + ">" for user in users_without_gift])

            # Check profile synch of all users with voucher code
            users_out_of_sync = list()
            users_profile_voucher_not_matched = list()
            for user in users_with_voucher:
                if not user.profile_update_date:
                    try:
                        profile = self.sc.api_call(
                            method="users.profile.get", user=user.user_id
                        )['profile']['fields']['XfELFP2WL9']['value']
                        if profile:
                            user_level_name = re.search(
                                ' \((.+?) level\)', profile.lower()).group(1)
                            certification_level = [
                                level for level in levels
                                if level.name == user_level_name
                            ][0]
                            if user.level_id == certification_level:
                                users_out_of_sync.append(user)
                            else:
                                users_profile_voucher_not_matched.append(user)
                    except Exception as e:
                        self.logger.warn(e)

            users_out_of_sync_report = "\n".join(
                ["<@" + user.user_id + ">" for user in users_out_of_sync])
            users_profile_voucher_not_matched_report = "\n".join([
                "<@" + user.user_id + ">"
                for user in users_profile_voucher_not_matched
            ])

            # Check profile of all users from Slack
            external_users_out_of_sync = list()
            slack_users_id = self.sc.api_call(
                method="channels.info",
                channel=self.config.users_slack_channel)['channel']['members']
            for slack_user_id in slack_users_id:
                if not slack_user_id in [user.user_id for user in users]:
                    # We check only users not in the initiative (for whom we do not have an entry is the database)
                    try:
                        profile = self.sc.api_call(
                            method="users.profile.get", user=slack_user_id
                        )['profile']['fields']['XfELFP2WL9']['value']
                        if profile:
                            user_level_name = re.search(
                                ' \((.+?) level\)', profile.lower()).group(1)
                            certification_level = [
                                level for level in levels
                                if level.name == user_level_name
                            ][0]
                            external_users_out_of_sync.append(
                                slack_user_id + "-" + certification_level.name)
                    except Exception as e:
                        self.logger.warn(e)
            external_users_out_of_sync_report = "\n".join([
                "<@" + external_user_out_of_sync.split('-')[0] + "> (*" +
                external_user_out_of_sync.split('-')[1] + "*)"
                for external_user_out_of_sync in external_users_out_of_sync
            ])

            footer = "(compute time: " + \
                str(round(end_time - start_time, 3)) + ")"

            if self.config.post_to_slack:
                self.kugawana_tool.post_notification_to_kugawana_slack(
                    slack_channel=self.config.admin_slack_channel,
                    title="Today's AWS certification report!",
                    title_link="https://aws.amazon.com/fr/certification/",
                    message=admin_message,
                    footer=footer,
                    level="good")
                if len(users_details) > 0:
                    self.kugawana_tool.post_notification_to_kugawana_slack(
                        slack_channel=self.config.admin_slack_channel,
                        title="List of users who did not receive their gift",
                        message=users_details,
                        level="0576b9")
                if len(users_out_of_sync_report) > 0:
                    self.kugawana_tool.post_notification_to_kugawana_slack(
                        slack_channel=self.config.admin_slack_channel,
                        title="List of users with their profile out of sync",
                        message=users_out_of_sync_report,
                        level="danger")
                if len(users_profile_voucher_not_matched_report) > 0:
                    self.kugawana_tool.post_notification_to_kugawana_slack(
                        slack_channel=self.config.admin_slack_channel,
                        title=
                        "List of users with their profile not matching their voucher level",
                        message=users_profile_voucher_not_matched_report,
                        level="warning")
                if len(external_users_out_of_sync_report) > 0:
                    self.kugawana_tool.post_notification_to_kugawana_slack(
                        slack_channel=self.config.admin_slack_channel,
                        title=
                        "List of external users with AWS certification on their profile",
                        message=external_users_out_of_sync_report)
            else:
                print("admin_message:\n" + admin_message)
                print("users_details:\n" + users_details)
                print("users_out_of_sync_report:\n" + users_out_of_sync_report)
                print("users_profile_voucher_not_matched_report:\n" +
                      users_profile_voucher_not_matched_report)
                print("external_users_out_of_sync_report:\n" +
                      external_users_out_of_sync_report)