Esempio n. 1
0
    def setup_method(self, test_method):
        original_db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "shecodes_user_manager",
                                        "she_codes_account_manager_db_v1.db")

        self.test_db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                    "she_codes_account_manager_db_v1_tests.db")

        shutil.copy(original_db_path, self.test_db_path)
        self.db_manager = DbManager(self.test_db_path)
Esempio n. 2
0
 def __init__(self):
     print(
         '==>To update an exist volunteer - enter volunteer id + first&last name and fill in the desired fields. \n '
         '(Enter 0 to keep field unchanged). \n ==>in order to view existing volunteer type only volunteer id. \n'
     )
     self._volunteer_id = input("Insert volunteer ID:")
     self._mail = input("Insert mail:")
     self._first_name = input("First name:")
     self._last_name = input("Last name:")
     self._area = input("Insert area:")
     self._district = input("Insert district:")
     self._branch = input("Insert branch:")
     self._role = input("Insert role:")
     self._db_manager = DbManager()
     self.slack_api_helper = SlackApiHelper.create()
def main():
    manager = DbManager()
    slack_api_helper = SlackApiHelper.create()
    logging_manager.init_logger("she_codes_Polling.log")
    while True:
        polling_ui_mutex = PollingUiMutex("polling")
        with polling_ui_mutex:
            polling_check_data(manager, slack_api_helper)
        logging_manager.logger.info("polling...")
        time.sleep(5)
Esempio n. 4
0
def main():
    # slack_invitation_mail = WorkspaceInvitation()
    # slack_invitation_mail.send_invitation_mail()
    with open("shecodes_user_manager\slack_token.json", "rb") as fp:
        my_tokens = json.load(fp)
    slack_api_helper = SlackApiHelper(my_tokens["slack_signing_secret"], my_tokens["slack_bot_token"],
                                       my_tokens["slack_user_token"])
    '''some function connected to kafka topic and retrieve volunteer_id '''
    # slack_api_helper.get_channel_user_to_add()
    # Once we have our event listeners configured, we can start the
    # Flask server with the default `/events` endpoint on port 3000
    db_manager = DbManager()

    slack_api_helper.start_server()
 def is_mail_in_workspace(self, mail, volunteer_id):
     try:
         db_manager = DbManager()
         value_return_from_api = self.slack_client_for_user.api_call(
             "users.lookupByEmail", params={'email': mail})
         self.user_name_retrieve = value_return_from_api["user"]["id"]
         print(self.user_name_retrieve)
         logging_manager.logger.info(
             "volunteer mail check succeed - volunteer in workspace")
         return True
     except errors.SlackApiError as e:
         if e.response['error'] == "users_not_found":
             logging_manager.logger.error(
                 "The following mail isn't a member in the workspace:" +
                 mail)
         return False
class TestDbManager:
    @classmethod
    def setup_class(cls):
        init_logger("test_db_manager.log")

    def setup_method(self, test_method):
        original_db_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)), "..",
            "shecodes_user_manager", "she_codes_account_manager_db_v1.db")

        self.test_db_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "she_codes_account_manager_db_v1_tests.db")

        shutil.copy(original_db_path, self.test_db_path)
        self.manager = DbManager(self.test_db_path)

    def teardown_method(self, test_method):
        # self.db_manager.close_db()
        os.unlink(self.test_db_path)

    def test_get_channels_for_volunteer(self):
        with self.manager:
            sandy_the_dog = Volunteer(43, "*****@*****.**", "Sandy", "Raskin",
                                      "haifa", "north", "technion",
                                      "district_manager")
            self.manager.insert_volunteer(sandy_the_dog)
            volunteer_data_id = self.manager.get_volunteer_data_id()
            channels = self.manager.get_slack_channels_for_volunteer(
                volunteer_data_id)
            assert (len(channels) > 0)

    def test_insert_volunteer_illegal_location(self):
        with self.manager:
            sandy_the_dog = Volunteer(42, "*****@*****.**", "Sandy", "Raskin",
                                      "North", "Harish", "Smokey",
                                      "district_manager")
            result = self.manager.insert_volunteer(sandy_the_dog)
            assert not result

    def test_insert_volunteer_illegal_role(self):
        with self.manager:
            sandy_the_dog = Volunteer(42, "*****@*****.**", "Sandy", "Raskin",
                                      "haifa", "north", "technion", "Doctor")
            result = self.manager.insert_volunteer(sandy_the_dog)
            assert not result

    def test_entering_exist_volunteer_twice(self):
        with self.manager:
            sandy_the_dog = Volunteer(42, "*****@*****.**", "Sandy", "Raskin",
                                      "haifa", "north", "technion",
                                      "district_manager")
            result = self.manager.insert_volunteer(sandy_the_dog)
            with pytest.raises(DbManagerException):
                result = self.manager.insert_volunteer(sandy_the_dog)

    def test_view_volunteer_data(self):
        with self.manager:
            sandy_the_dog = Volunteer(24, "0", "0", "0", "0", "0", "0", "0")
            view_data_dict = self.manager.view_volunteer_data(sandy_the_dog)
            for key in view_data_dict:
                if (type(view_data_dict[key]) == int):
                    assert (view_data_dict[key] != 0)
                else:
                    assert (len(view_data_dict[key]) > 0)

    def test_view_not_exist_volunteer_data(self):
        with self.manager:
            sandy_the_dog = Volunteer(100, "0", "0", "0", "0", "0", "0", "0")
            with pytest.raises(DbManagerException):
                view_data_dict = self.manager.view_volunteer_data(
                    sandy_the_dog)
Esempio n. 7
0
class TestSlackUserPolling(object):

    @classmethod
    def setup_class(cls):
        init_logger("test_slack_user_polling.log")

    def setup_method(self, test_method):
        original_db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "shecodes_user_manager",
                                        "she_codes_account_manager_db_v1.db")

        self.test_db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                    "she_codes_account_manager_db_v1_tests.db")

        shutil.copy(original_db_path, self.test_db_path)
        self.db_manager = DbManager(self.test_db_path)

    def teardown_method(self, test_method):
        # self.db_manager.close_db()
        os.unlink(self.test_db_path)

    def test_insert_SlackPollingStatus(self):
        with self.db_manager:
            volunteer_data = Volunteer(43, "*****@*****.**", "Sandy", "Raskin", "haifa", "north", "technion", "district_manager")
            self.db_manager.insert_into_volunteer_data_table(volunteer_data)
            volunteer_data_id = self.db_manager.get_volunteer_data_id()
            self.db_manager.insert_new_volunteer_to_slack_user_polling_table(volunteer_data_id)
            cur = self.db_manager.conn.cursor()
            sql = 'SELECT * FROM SlackPollingStatus'
            cur.execute(sql)
            db_result = cur.fetchall()
            # below we use -1 for the most recent created volunteer
            assert (db_result[-1][0] == volunteer_data_id)

    def test_entering_exist_user_SlackPollingStatus(self):
        with self.db_manager:
            volunteer_data_id = 2
            self.db_manager.insert_new_volunteer_to_slack_user_polling_table(volunteer_data_id)
            with pytest.raises(DbManagerException):
                self.db_manager.insert_new_volunteer_to_slack_user_polling_table(volunteer_data_id)

    def test_SlackPollingStatus_change_status(self):
        with self.db_manager:
            volunteer_data_id = 1
            self.db_manager.insert_new_volunteer_to_slack_user_polling_table(volunteer_data_id)
            self.db_manager.update_slack_user_status([volunteer_data_id], "YOHOO")
            cur = self.db_manager.conn.cursor()
            sql = 'SELECT * FROM SlackPollingStatus'
            cur.execute(sql)
            db_result = cur.fetchall()
            assert (db_result[-1][1] == "YOHOO")
Esempio n. 8
0
class UI(object):
    def __init__(self):
        print(
            '==>To update an exist volunteer - enter volunteer id + first&last name and fill in the desired fields. \n '
            '(Enter 0 to keep field unchanged). \n ==>in order to view existing volunteer type only volunteer id. \n'
        )
        self._volunteer_id = input("Insert volunteer ID:")
        self._mail = input("Insert mail:")
        self._first_name = input("First name:")
        self._last_name = input("Last name:")
        self._area = input("Insert area:")
        self._district = input("Insert district:")
        self._branch = input("Insert branch:")
        self._role = input("Insert role:")
        self._db_manager = DbManager()
        self.slack_api_helper = SlackApiHelper.create()

    def insert_new_volunteer(self):
        volunteer = Volunteer(volunteer_id=self._volunteer_id,
                              mail=self._mail,
                              first_name=self._first_name,
                              last_name=self._last_name,
                              area=self._area,
                              district=self._district,
                              branch=self._branch,
                              role=self._role)
        return self._db_manager.insert_volunteer(volunteer)

    def update_volunteer_data(self):
        volunteer = Volunteer(volunteer_id=self._volunteer_id,
                              mail=self._mail,
                              first_name=self._first_name,
                              last_name=self._last_name,
                              area=self._area,
                              district=self._district,
                              branch=self._branch,
                              role=self._role)
        return self._db_manager.update_volunteer_data(volunteer)

    def view_volunteer_data(self):
        volunteer = Volunteer(volunteer_id=self._volunteer_id,
                              mail=self._mail,
                              first_name=self._first_name,
                              last_name=self._last_name,
                              area=self._area,
                              district=self._district,
                              branch=self._branch,
                              role=self._role)
        return self._db_manager.view_volunteer_data(volunteer)

    def is_user_registered_to_slack_workspace(self, mail, volunteer_id):
        response = self.slack_api_helper.is_mail_in_workspace(
            mail, volunteer_id)
        return response

    def newly_added_volunteer_to_slack(self, time_period):
        polling_volunteer_id_list = self._db_manager.get_polling_status_counter(
            time_period)
        all_volunteer_id_in_period_list = self._db_manager.get_joined_volunteer_counter(
            time_period)
        newly_added_list = []
        for volunteer_id in all_volunteer_id_in_period_list:
            counter = 1
            for polling_volunteer_id in polling_volunteer_id_list:
                if volunteer_id == polling_volunteer_id:
                    counter = 0
                    break
            if counter == 1:
                newly_added_list.append(volunteer_id)
        return newly_added_list

    def add_user_to_channels(self):
        self.slack_channels_for_volunteer = self._db_manager.get_slack_channels_for_volunteer(
            self.volunteer_id_update_or_inserted)
        self.slack_api_helper.adding_member_to_channel(
            self.slack_channels_for_volunteer)

    def user_choose_table(self):
        choices = {
            '1':
            'Insert new volunteer',
            '2':
            'Update volunteer data',
            '3':
            'View volunteer data',
            '4':
            'Check how many volunteers added to slack in period of time YYYY-MM-DD'
        }
        choices_to_functions = {
            '1': self.insert_new_volunteer,
            '2': self.update_volunteer_data,
            '3': self.view_volunteer_data,
            '4': self.newly_added_volunteer_to_slack
        }
        print(choices)
        self._choice = input('Insert choice number:')
        with self._db_manager:
            if self._choice == '3':
                data_to_view = choices_to_functions[self._choice]()
                if data_to_view is None:
                    logging_manager.logger.error(
                        "volunteer data ID insert isn't in system please check"
                    )
                else:
                    for item in data_to_view:
                        print("{} :  {}".format(item, data_to_view[item]))
            if self._choice == '4':
                self._start_time_for_analysis = input(
                    "For maintenance only use - Insert data to know how many volunteer "
                    "joined slack in period of time YYYY-MM-DD")
                data_to_view = choices_to_functions[self._choice](
                    self._start_time_for_analysis)
                print("How many volunteer joined slack workspace from {} : {}".
                      format(self._start_time_for_analysis, len(data_to_view)))
            if self._choice != '4' and self._choice != '3':
                self.polling_ui_mutex = PollingUiMutex("UI")
                with self.polling_ui_mutex:
                    commit_result = choices_to_functions[self._choice]()
                    if commit_result:
                        print(choices[self._choice] + " succeed")
                        logging_manager.logger.info(choices[self._choice] +
                                                    " succeed")
                        self.volunteer_id_update_or_inserted = self._db_manager.get_volunteer_data_id(
                        )
                        mail_in_workspace_respond = self.is_user_registered_to_slack_workspace(
                            self._mail, self.volunteer_id_update_or_inserted)
                        if mail_in_workspace_respond:
                            self.add_user_to_channels()
                        else:
                            print("Sending invitation...")
                            logging_manager.logger.info(
                                "Sending invitation...")
                            workspace_invitation = WorkspaceInvitation(
                                self._mail)
                            workspace_invitation.send_invitation_mail()
                            volunteer_exist_in_polling_table = self._db_manager.volunteer_in_polling_table_check(
                                self.volunteer_id_update_or_inserted)
                            if not volunteer_exist_in_polling_table:
                                self._db_manager.insert_new_volunteer_to_slack_user_polling_table(
                                    self.volunteer_id_update_or_inserted)
                            else:
                                logging_manager.logger.info(
                                    "volunteer already in polling table")
                    else:
                        logging_manager.logger.error(
                            choices[self._choice] +
                            " failed - please check logs to verify the problem"
                        )
                        print(
                            choices[self._choice] +
                            " failed - please check logs to verify the problem"
                        )