def test_level_remove_nonexistent(self):
        level_id = 'test_level_remove_id'
        level_name = 'test_level_remove_name'
        level_stars = 1

        #given
        level = Level(level_id, level_name, level_stars)

        #when
        response = level.remove()

        #then
        assert response == True
        certifs = self.level_table.query(KeyConditionExpression=Key('id').eq(level_id))['Items']
        assert len(certifs) == 0
    def test_level_remove_used_by_user(self):
        level_id = 'test_level_remove_id'
        level_name = 'test_level_remove_name'
        level_stars = 1
        user_id = 'test_level_remove_user_id'

        #given
        self.level_table.put_item(Item={'id': level_id, 'name': level_name})
        self.user_table.put_item(Item={'user_id': user_id, 'certification_level': level_id})
        level = Level(level_id, level_name, level_stars)

        #when
        response = level.remove()

        #then
        assert response == False
        certifs = self.level_table.query(KeyConditionExpression=Key('id').eq(level_id))['Items']
        assert len(certifs) == 1
    def test_level_add(self):
        level_id = 'test_level_add_id'
        level_name = 'test_level_add_name'
        level_stars = 1

        #given
        level = Level(level_id, level_name, level_stars)

        #when
        response = level.add()

        #then
        assert response == True
        certifs = self.level_table.query(KeyConditionExpression=Key('id').eq(level_id))['Items']
        assert len(certifs) == 1
        assert certifs[0]['id'] == level_id
        assert certifs[0]['name'] == level_name
        assert certifs[0]['stars'] == level_stars
    def test_level_get_nonexistent(self):
        level_id = 'test_level_get_id'

        #given

        #when
        level = Level.get(level_id)

        #then
        assert level == None
    def test_level_get(self):
        level_id = 'test_level_get_id'
        level_name = 'test_level_get_name'

        #given
        self.level_table.put_item(Item={'id': level_id, 'name': level_name})

        #when
        level = Level.get(level_id)

        #then
        assert level.id == level_id
        assert level.name == level_name
    def test_level_str(self):
        level_name = 'test_level_str_name'
        certification_level = 'test_level_str_level'
        level_stars = 1

        #given
        level = Level(certification_level, level_name, level_stars)

        #when
        response = str(level)

        #then
        assert response != None
    def test_user_passesCertification_without_voucher(self):
        user_id = 'test_user_passes_id'
        level_name = 'test_user_passes_level_name'
        certification_level = 'test_user_passes_certification_level'
        level_stars = 1

        #given
        user = User(user_id, certification_level)
        level = Level(certification_level, level_name, level_stars)

        #when
        response = user.passesCertification(level)

        #then
        assert response == False
        assert user.profile_update_date == None
    def test_user_passesCertification_already_saved(self):
        user_id = 'test_user_passes_id'
        level_name = 'test_user_passes_level_name'
        certification_level = 'test_user_passes_certification_level'
        level_stars = 1
        voucher_code = 'test_user_passes_voucher_code'
        attribuated_date = 'test_user_passes_attribuated_date'
        profile_update_date = 'test_user_passes_profile_update_date'

        #given
        user = User(user_id, certification_level, voucher_code, attribuated_date, profile_update_date)
        level = Level(certification_level, level_name, level_stars)

        #when
        response = user.passesCertification(level)

        #then
        assert response == False
        assert user.profile_update_date == profile_update_date
    def test_user_passesCertification(self):
        user_id = 'test_user_passes_id'
        level_id = 'test_level_passes_id'
        level_name = 'test_user_passes_level_name'
        level_stars = 1
        voucher_code = 'test_user_passes_voucher_code'

        #given
        self.user_table.put_item(Item={'user_id': user_id, 'certification_level': level_id, 'voucher_code': voucher_code})
        self.level_table.put_item(Item={'id': level_id, 'name': level_name})
        user = User(user_id, level_id, voucher_code)
        level = Level(level_id, level_name, level_stars)

        #when
        response = user.passesCertification(level)

        #then
        assert response == True
        assert user.profile_update_date == time.strftime('%d/%m/%Y',time.localtime())
Пример #10
0
    def addUser(self, user_id, certification_level_id):
        response = ""

        user = User.get(user_id)
        levels = Level.getAll()
        certification_level = [
            level.name for level in levels
            if level.id == certification_level_id
        ][0]

        if user:
            user_certification = [
                user_certification.level_id
                for user_certification in user.user_certifications
                if user_certification.level_id == certification_level_id
            ]
            if user_certification:
                return {
                    "text":
                    "<@" + self.form['user_id'] +
                    "> already get a subscription for certification level *" +
                    certification_level + "*"
                }

        # New user or existing user with a new certification level, so we add him/her
        if UserCertification(user_id, certification_level_id).add():
            response = {
                "text":
                "<@" + self.form['user_id'] +
                "> added with certification level *" + certification_level +
                "*"
            }
        else:
            response = {
                "text":
                "There is an error while adding <@" + self.form['user_id'] +
                ">"
            }

        return response
    def launch(self, event):
        # Manage 'challenge' from Slack to validate the lambda.
        if "challenge" in event:
            return event["challenge"]

        slack_event = event['event']

        # Ignore message from bot.
        if not "bot_id" in slack_event \
           and slack_event['type'] == 'user_change' \
           and 'XfELFP2WL9' in slack_event['user']['profile']['fields']:

            # Application configuration
            config = Configuration(self.logger, self.environment)

            # Check input token
            if not event['token'] in config.slack_event_token:
                return "403 Forbidden"

            self.logger.info(slack_event['user']['real_name'] + " gets " +
                             slack_event['user']['profile']['fields']
                             ['XfELFP2WL9']['value'] + " certification!")

            user_udid = slack_event['user']['id']
            user_level_name = re.search(
                ' \((.+?) level\)', slack_event['user']['profile']['fields']
                ['XfELFP2WL9']['value'].lower()).group(1)

            user = User.get(user_udid)
            level = Level.getByName(user_level_name)

            if user and level:
                for user_certification in user.user_certifications:
                    user_certification.passesCertification(level)

        return "200 OK"
    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)