class BaseTestCase(TestCase): def setUp(self): self.db = init_db() self.user = User() self.user.username = '******' self.user.chat_id = '123456789' self.user.github_user = '******' self.user.github_user_id = '46005310' self.user.access_token = os.getenv("GITHUB_API_TOKEN", "") self.project = Project() self.project.name = 'apitest' self.project_id = '185474581' self.project.save() self.user.project = self.project self.user.save() self.GITHUB_API_TOKEN = os.getenv("GITHUB_API_TOKEN", "") self.response_not_found = Response() self.response_not_found.status_code = 404 self.response_unauthorized = Response() self.response_unauthorized.status_code = 401 def create_app(self): app = create_app() app.config.from_object("github.config.TestingConfig") return app def tearDown(self): self.db.drop_database('api')
def test_save_github_user_data(self): User.drop_collection() github_user = '******' chat_id = 'id' github_user_id = 'git_id' user = User() user.github_user = github_user user.save() user.save_github_user_data(github_user, chat_id, github_user_id) user_db = User.objects(github_user=github_user).first() self.assertEqual(user, user_db)
def register_repo(self, repo_json): project_name = repo_json["repository_name"] owner = repo_json["owner"] chat_id = repo_json["chat_id"] user = User.objects(chat_id=chat_id).first() try: project = Project() if user.project: webhook = Webhook(chat_id) webhook.delete_hook(owner, project_name) webhook.delete_hook(user.github_user, user.project.name) user.github_user = owner user.save() project = user.project project.update_repository_infos(str(project_name)) else: webhook = Webhook(chat_id) webhook.delete_hook(owner, project_name) user.github_user = owner user.save() project.save_repository_infos(user, str(project_name)) user.save_github_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))
def test_create_project(self): project = Project() description = "Test project" name = "Test Project" web_url = "https://cakaca.com" branches = ["branch1", "branch2"] project_id = "2113" user = User() user.username = "******" user.save() project.create_project(user, description, name, web_url, branches, project_id) project_db = Project.objects(name=name).first() self.assertEqual(project, project_db)
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.github_user dict_user["repository"] = user.project.name return jsonify(dict_user), 200
def setUp(self): self.db = init_db() self.user = User() self.user.username = '******' self.user.chat_id = '123456789' self.user.github_user = '******' self.user.github_user_id = '46005310' self.user.access_token = os.getenv("GITHUB_API_TOKEN", "") self.project = Project() self.project.name = 'apitest' self.project_id = '185474581' self.project.save() self.user.project = self.project self.user.save() self.GITHUB_API_TOKEN = os.getenv("GITHUB_API_TOKEN", "") self.response_not_found = Response() self.response_not_found.status_code = 404 self.response_unauthorized = Response() self.response_unauthorized.status_code = 401
def get_releases(chat_id): try: user = User.objects(chat_id=chat_id).first() project_release = user.project project_release = project_release.name.split("/") release = Release(chat_id, user.github_user, project_release[-1]) release_data = release.get_last_release() except HTTPError as http_error: return release.error_message(http_error) except AttributeError: return jsonify(NOT_FOUND), 404 else: return jsonify(release_data), 200
def test_save_github_repo_data(self): user = User() username = "******" user.create_user(username) project = Project() project.user_id = user.id project.save() user.save_github_repo_data(project) project_user = User.objects(project=project).first() self.assertEqual(user, project_user)
def get_contributor_issues(chat_id, contributor_username): try: user = User.objects(chat_id=chat_id).first() contributor_issues = ContributorIssues(chat_id) project = user.project project_collaborator = FindProjectCollaborators(chat_id) full_name = project_collaborator.get_project(project.name) issues = contributor_issues.\ get_contributor_issues(full_name, contributor_username) except HTTPError as http_error: return contributor_issues.error_message(http_error) except AttributeError: return jsonify(NOT_FOUND), 404 else: return jsonify(issues), 200
def get_pull_request(chat_id): try: user = User.objects(chat_id=chat_id).first() project_pr = user.project project_pr = project_pr.name.split("/") pull_request = PullRequest(chat_id) pull_request_data = pull_request.get_pull_requests( user.github_user, project_pr[-1]) except HTTPError as http_error: return pull_request.error_message(http_error) except AttributeError: return jsonify(NOT_FOUND), 404 else: return jsonify(pull_request_data), 200
def find_collaborators(chat_id): try: user = User.objects(chat_id=chat_id).first() project = user.project project_name = project.name collab = FindProjectCollaborators(chat_id) owner_and_repo = collab.get_project(project_name) contributors_names = collab.get_collaborators(str(owner_and_repo)) except HTTPError as http_error: return collab.error_message(http_error) except AttributeError: return jsonify(NOT_FOUND), 404 else: return jsonify({"collaborators": contributors_names}), 200
def comment_issue(chat_id): try: response = request.get_json() body = response['body'] issue_number = response['issue_number'] user = User.objects(chat_id=chat_id).first() project = Project() project = user.project project = project.name.split("/") issue = Issue(chat_id) comment_issue = issue.comment_issue(project[-1], user.github_user, issue_number, body) except HTTPError as http_error: return issue.error_message(http_error) except AttributeError: return jsonify(NOT_FOUND), 404 else: return jsonify({"body": comment_issue["body"]}), 200
def create_issue(chat_id): try: response = request.get_json() title = response['title'] body = response['body'] user = User.objects(chat_id=chat_id).first() project = Project() project = user.project project = project.name.split("/") issue = Issue(chat_id) create_issue = issue.create_issue(project[-1], user.github_user, title, body) except HTTPError as http_error: return issue.error_message(http_error) except AttributeError: return jsonify(NOT_FOUND), 404 else: return jsonify({ "title": create_issue["title"], "body": create_issue["body"], "html_url": create_issue["html_url"] }), 200
def get_access_token(chat_id): code = request.args.get('code') existing_user = User.objects(chat_id=chat_id).first() send_message(ACCESS_TOKEN, chat_id) if not existing_user: GITHUB_TOKEN = authenticate_access_token(code) db_user = User() db_user.access_token = GITHUB_TOKEN db_user.chat_id = str(chat_id) db_user.save() user = UserInfo(chat_id) user_infos = user.get_own_user_data() db_user.github_user = user_infos["github_username"] db_user.github_user_id = str(user_infos["github_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)
def get_access_token(self, chat_id): user = User.objects(chat_id=chat_id).first() return user.access_token
def setup(self): super().setUp() User.drop_collection()
def test_create_user(self): user = User() username = "******" user.create_user(username) user2 = User.objects(github_user=username).first() self.assertEqual(user, user2)