Exemple #1
0
class BaseTestCase(TestCase):
    def setUp(self):
        self.db = init_db()
        self.user = User()
        self.user.username = '******'
        self.user.chat_id = '339847919'
        self.user.gitlab_user = '******'
        self.user.gitlab_user_id = '4047441'
        self.user.access_token = "123456"
        self.user.save()
        self.project = Project()
        self.project_name = 'ada-gitlab'
        self.project_id = '12532279'
        self.project.save_webhook_infos(self.user, self.project_name,
                                        self.project_id)
        self.user.save_gitlab_repo_data(self.project)
        self.GITLAB_API_TOKEN = "12345"

        self.mocked_404_response = Response()
        self.mocked_404_response.status_code = 404
        self.response_unauthorized = Response()
        self.response_unauthorized.status_code = 401

    def create_app(self):
        app = create_app()
        app.config.from_object("gitlab.config.TestingConfig")
        return app

    def tearDown(self):
        self.db.drop_database('api')
Exemple #2
0
def get_user_infos(chat_id):
    dict_user = {"username": 0, "repository": 0}
    user = User.objects(chat_id=chat_id).first()
    if user:
        dict_user["username"] = user.gitlab_user
        dict_user["repository"] = user.project.name
    return jsonify(dict_user), 200
Exemple #3
0
    def register_repo(self, repo_data):
        project_fullname = repo_data["project_name"]
        project_fullname_splited = project_fullname.split('/')
        project_name = project_fullname_splited[-1]
        project_id = repo_data["project_id"]

        user = User.objects(chat_id=self.chat_id).first()
        try:
            project = Project()
            if user.project:
                to_delete_project = user.project
                self.delete_webhook(project_id)
                self.delete_webhook(to_delete_project.project_id)
                project = user.project
                project.update_webhook_infos(project_name, project_id)
            else:
                project.save_webhook_infos(user, project_name, project_id)
            user.save_gitlab_repo_data(project)
        except AttributeError:
            dict_error = {
                "message":
                "Tive um erro tentando cadastrar seu repositório. "
                "Mais tarde você tenta. Ok?"
            }
            raise AttributeError(json.dumps(dict_error))
Exemple #4
0
 def register_user(self, user_data):
     user = User()
     gitlab_user = user_data["gitlab_user"]
     chat_id = user_data["chat_id"]
     gitlab_user_id = user_data["gitlab_user_id"]
     existing_user = User.objects(chat_id=chat_id).first()
     if existing_user:
         dict_error = {
             "message":
             "Eu vi aqui que você já cadastrou o usuário "
             "do GitLab. Sinto muitos, mas no momento não "
             "é possível cadastrar um novo usuário do GitLab "
             "ou alterá-lo."
         }
         raise HTTPError(json.dumps(dict_error))
     user.save_gitlab_user_data(gitlab_user, chat_id, gitlab_user_id)
Exemple #5
0
def webhook_repository(chat_id, project_id):
    if request.is_json:
        content = request.get_json()
        if content["object_kind"] == "pipeline" and "finished_at" in \
           content["object_attributes"]:
            webhook = Webhook(chat_id)
            pipeline_id = content["object_attributes"]["id"]
            jobs = webhook.get_pipeline_infos(project_id, pipeline_id)
            messages = webhook.build_message(jobs)
            status_message = webhook.build_status_message(content, jobs)
            project = Project.objects(project_id=project_id).first()
            user = User.objects(project=project.id).first()
            bot = Bot(token=ACCESS_TOKEN)
            if status_message:
                bot.send_message(chat_id=user.chat_id,
                                 text=status_message,
                                 parse_mode='Markdown',
                                 disable_web_page_preview=True)
            if content["object_attributes"]["status"] == "failed":
                rerunpipeline = RerunPipeline(user.chat_id)
                buttons = rerunpipeline.build_buttons(pipeline_id)
                reply_markup = telegram.InlineKeyboardMarkup(buttons)
                bot.send_message(chat_id=user.chat_id,
                                 text=messages["jobs_message"])
                bot.send_message(chat_id=user.chat_id,
                                 text="Se você quiser reiniciar essa pipeline,"
                                 " é só clicar nesse botão",
                                 reply_markup=reply_markup)
            return "OK"
        else:
            return "OK"
    else:
        return "OK"
Exemple #6
0
    def test_create_current_pipeline(self):
        CurrentPipeline.drop_collection()
        Project.drop_collection()
        User.drop_collection()

        user = User()
        user.username = "******"
        user.save()

        project = Project()
        project.user_id = user.id
        project.description = "Test project current pipeline"
        project.name = "Test project current pipeline"
        project.web_url = "https://currentpipeline.com"
        project.branches = ["branch1", "branch2"]
        project.save()

        current_pipeline = CurrentPipeline()
        name = "Test current"
        pipeline_jobs = [{"Teste": "Testando"}]
        current_pipeline.create_current_pipeline(name, pipeline_jobs, project)

        current_pipeline2 = CurrentPipeline.objects(name=name).first()

        self.assertEqual(current_pipeline, current_pipeline2)
Exemple #7
0
    def test_get_current_pipeline(self):
        CurrentPipeline.drop_collection()
        Project.drop_collection()
        User.drop_collection()

        user = User()
        user.username = "******"
        user.save()

        project = Project()
        project.user_id = user.id
        project.description = "Test project current pipeline"
        project.name = "Test project current pipeline"
        project.web_url = "https://currentpipeline.com"
        project.branches = ["branch1", "branch2"]
        project.save()

        currentpipeline = CurrentPipeline()
        currentpipeline.project = project
        currentpipeline.name = "Test current"
        currentpipeline.pipeline_jobs = [{"Teste": "Testando"}]
        currentpipeline.save()

        generalinfo = GeneralInformationPipelines()
        generalinfo.project = project
        generalinfo.number_of_pipelines = 10
        generalinfo.successful_pipelines = 5
        generalinfo.save()

        pipelines_in_db = CurrentPipeline.get_current_pipeline(project)
        for pipeline in pipelines_in_db:
            self.assertEqual(currentpipeline, pipeline)
Exemple #8
0
    def test_register_repo(self):
        user = self.user
        user.project = None
        user.save()
        user = User.objects(chat_id=self.user.chat_id).first()
        self.assertEqual(self.user.project, None)
        project_name = "ada-gitlab"
        project_id = "12532279"
        repo_data = {
            "project_name": project_name,
            "chat_id": "339847919",
            "project_id": project_id
        }
        self.webhook.register_repo(repo_data)

        user = User.objects(chat_id=self.user.chat_id).first()
        self.assertEqual(user.project.name, project_name)
        self.assertEqual(user.project.project_id, project_id)
Exemple #9
0
    def test_create_user(self):
        User.drop_collection()
        user = User()
        username = "******"
        user.create_user(username)

        user2 = User.objects(username=username).first()
        self.assertEqual(user, user2)
Exemple #10
0
    def test_save_gitlab_user_data(self):
        gitlab_user = '******'
        chat_id = 'id'
        gitlab_user_id = 'git_id'
        username = '******'
        user = User()
        user.username = username
        user.save()
        user.save_gitlab_user_data(gitlab_user, chat_id, gitlab_user_id)

        user_db = User.objects(username=username).first()
        self.assertEqual(user, user_db)
Exemple #11
0
    def setUp(self):
        self.db = init_db()
        self.user = User()
        self.user.username = '******'
        self.user.chat_id = '339847919'
        self.user.gitlab_user = '******'
        self.user.gitlab_user_id = '4047441'
        self.user.access_token = "123456"
        self.user.save()
        self.project = Project()
        self.project_name = 'ada-gitlab'
        self.project_id = '12532279'
        self.project.save_webhook_infos(self.user, self.project_name,
                                        self.project_id)
        self.user.save_gitlab_repo_data(self.project)
        self.GITLAB_API_TOKEN = "12345"

        self.mocked_404_response = Response()
        self.mocked_404_response.status_code = 404
        self.response_unauthorized = Response()
        self.response_unauthorized.status_code = 401
Exemple #12
0
    def setUp(self):
        super().setUp()
        GeneralInformationPipelines.drop_collection()
        Project.drop_collection()
        self.user = User()
        self.user.username = "******"
        self.user.save()

        self.project = Project()
        self.project.user_id = self.user.id
        self.project.description = "Test project current pipeline"
        self.project.name = "Test project current pipeline"
        self.project.web_url = "https://currentpipeline.com"
        self.project.branches = ["branch1", "branch2"]
        self.project.save()

        self.general_information_pipeline = GeneralInformationPipelines()
        self.number_of_pipelines = 10
        self.successful_pipelines = 5
        self.general_information_pipeline.create_general_information_pipeline(
            self.project, self.number_of_pipelines, self.successful_pipelines)
Exemple #13
0
def save_user_domain(chat_id):
    try:
        post_json = request.json
        domain = post_json["domain"]
        user = User.objects(chat_id=chat_id).first()
        user.update(domain=domain)
    except HTTPError as http_error:
        return user.error_message(http_error)
    except AttributeError:
        return jsonify(NOT_FOUND), 404
    else:
        return jsonify({"status": "success"}), 200
Exemple #14
0
def rerun_pipeline(chat_id, pipeline_id):
    try:
        user = User.objects(chat_id=chat_id).first()
        project = user.get_user_project()
        rerunpipeline = RerunPipeline(chat_id)
        restarted_pipeline = rerunpipeline.rerun_pipeline(
            project.project_id, pipeline_id)
    except HTTPError as http_error:
        return rerunpipeline.error_message(http_error)
    except AttributeError:
        return jsonify(NOT_FOUND), 404
    else:
        return jsonify(restarted_pipeline), 200
Exemple #15
0
 def return_project(self, chat_id, check_project_exists, object_type):
     user = User.objects(chat_id=chat_id).first()
     project = user.get_user_project()
     try:
         if self.get_class_type(object_type) == "Report":
             util = self.check_project_exists(project, object_type, user)
         else:
             util = self.check_project_exists(project, object_type, None)
     except HTTPError as http_error:
         raise HTTPError(
             self.exception_json(http_error.response.status_code))
     else:
         return util
Exemple #16
0
 def test_register_repo_validation(self, mocked_get, mocked_delete,
                                   mocked_os):
     user = self.user
     user.save()
     user = User.objects(chat_id=self.user.chat_id).first()
     project_name = "ada-gitlab"
     project_id = "12532279"
     repo_data = {
         "project_name": project_name,
         "chat_id": "339847919",
         "project_id": project_id
     }
     self.webhook.register_repo(repo_data)
     acess_token = "32y324798798732"
     webhook_url = "https://gitlab.adachatops.com/"
     mocked_os.getenv.return_value = acess_token
     mocked_os.getenv.return_value = webhook_url
     delete_hook = Response()
     delete_hook.status_code = 200
     mocked_get.return_value = self.mocked_get_hooks_response
     mocked_delete.return_value = delete_hook
     user = User.objects(chat_id=self.user.chat_id).first()
     self.assertEqual(user.project.name, project_name)
     self.assertEqual(user.project.project_id, project_id)
Exemple #17
0
def stable_deploy(chat_id):
    try:
        user = User.objects(chat_id=chat_id).first()
        project = user.project
        stable_deploy = StableDeploy(chat_id)
        pipeline_id = stable_deploy.find_latest_stable_version(
            project.project_id)
        deploy_status = stable_deploy.run_stable_deploy(
            project.project_id, pipeline_id)
    except HTTPError as http_error:
        return stable_deploy.error_message(http_error)
    except AttributeError:
        return jsonify(NOT_FOUND), 404
    else:
        return jsonify(deploy_status), 200
Exemple #18
0
 def get_user_domain(self):
     user = User.objects(chat_id=self.chat_id).first()
     return user.domain
Exemple #19
0
    def test_update_webhook_infos(self):
        User.drop_collection()
        user = User()
        username = "******"
        user.create_user(username)
        user.save()

        Project.drop_collection()
        project = Project()
        project.user_id = user.id
        project.description = "Test user add project"
        project.name = "Test user add project"
        project.web_url = "https://useraddProject.com"
        project.branches = ["branch1", "branch2"]
        project.save()

        user.save_gitlab_repo_data(project)
        project.update_webhook_infos(str(project.name), str(project.user_id))

        project_user = User.objects(project=project).first()
        self.assertEqual(user, project_user)
Exemple #20
0
class TestGeneralInformationPipeline(BaseTestCase):

    def setUp(self):
        super().setUp()
        GeneralInformationPipelines.drop_collection()
        Project.drop_collection()
        self.user = User()
        self.user.username = "******"
        self.user.save()

        self.project = Project()
        self.project.user_id = self.user.id
        self.project.description = "Test project current pipeline"
        self.project.name = "Test project current pipeline"
        self.project.web_url = "https://currentpipeline.com"
        self.project.branches = ["branch1", "branch2"]
        self.project.save()

        self.general_information_pipeline = GeneralInformationPipelines()
        self.number_of_pipelines = 10
        self.successful_pipelines = 5
        self.general_information_pipeline.create_general_information_pipeline(
            self.project, self.number_of_pipelines, self.successful_pipelines)

    def test_create_general_information_pipeline(self):
        general_info_db = GeneralInformationPipelines.objects(
            project=self.project).first()
        self.assertEqual(self.general_information_pipeline, general_info_db)

    def get_test_general_information_pipeline(self):
        general_info = GeneralInformationPipelines.\
            get_general_information_pipeline(self.project)
        self.assertEqual(self.general_information_pipeline, general_info)

    def test_add_pipeline_fail(self):
        current_pipeline = CurrentPipeline()
        name = "Test current2"
        pipeline_jobs = [{"duration": 9.8,
                          'date': '03/04/2019',
                          'name': 'nome',
                          'stage': 'flake8',
                          'status': False,
                          'web_url': 'http://teste.com'}]
        current_pipeline.create_current_pipeline(name,
                                                 pipeline_jobs, self.project)

        self.general_information_pipeline.add_pipeline(current_pipeline,
                                                       self.project)
        general_info = GeneralInformationPipelines.\
            get_general_information_pipeline(self.project)
        self.assertEqual(self.number_of_pipelines + 1,
                         general_info.number_of_pipelines)
        self.assertEqual(self.successful_pipelines,
                         general_info.successful_pipelines)

    def test_add_pipeline_sucess(self):
        current_pipeline = CurrentPipeline()
        name = "Test current"
        pipeline_jobs = [{'duration': 9.8,
                          'date': '03/04/2019',
                          'name': 'nome',
                          'stage': 'flake8',
                          'status': True,
                          'web_url': 'http://teste.com'}]
        current_pipeline.create_current_pipeline(name,
                                                 pipeline_jobs, self.project)

        self.general_information_pipeline.add_pipeline(current_pipeline,
                                                       self.project)
        general_info = GeneralInformationPipelines.\
            get_general_information_pipeline(self.project)
        self.assertEqual(self.number_of_pipelines + 1,
                         general_info.number_of_pipelines)
        self.assertEqual(self.successful_pipelines + 1,
                         general_info.successful_pipelines)
Exemple #21
0
 def setup(self):
     super().setUp()
     User.drop_collection()
     Project.drop_collection()
Exemple #22
0
 def get_access_token(self, chat_id):
     user = User.objects(chat_id=chat_id).first()
     return user.access_token
Exemple #23
0
def get_access_token():
    code = request.args.get('code')
    chat_id = request.args.get('state')
    send_message(ACCESS_TOKEN, chat_id)
    existing_user = User.objects(chat_id=chat_id).first()
    if not existing_user:
        GITLAB_TOKEN = authenticate_access_token(code)
        db_user = User()
        db_user.access_token = GITLAB_TOKEN
        db_user.chat_id = str(chat_id)
        db_user.save()
        user = UserUtils(chat_id)
        user_infos = user.get_own_user_data()
        db_user.gitlab_user = user_infos["gitlab_username"]
        db_user.gitlab_user_id = str(user_infos["gitlab_user_id"])
        db_user.save()
        user.send_button_message(user_infos, chat_id)
    redirect_uri = "https://t.me/{bot_name}".format(bot_name=BOT_NAME)
    return redirect(redirect_uri, code=302)
Exemple #24
0
 def test_register_user(self):
     self.webhook.register_user(self.user_data)
     user = User.objects(gitlab_user=self.gitlab_user).first()
     self.assertEqual(user.gitlab_user_id, self.gitlab_user_id)