123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- # -*- coding: utf-8 -*-
- """
- (c) 2021 - Copyright Red Hat Inc
- Authors:
- Pierre-Yves Chibon <pingou@pingoured.fr>
- """
- from __future__ import unicode_literals, absolute_import
- import arrow
- import copy
- import datetime
- import unittest
- import shutil
- import sys
- import time
- import os
- import flask
- import json
- import munch
- from mock import ANY, patch, MagicMock
- from sqlalchemy.exc import SQLAlchemyError
- sys.path.insert(
- 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
- )
- import pagure.lib.query
- import tests
- class PagureFlaskApiProjectHascommitTests(tests.SimplePagureTest):
- """Tests for the flask API of pagure for listing contributors of a project"""
- maxDiff = None
- @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureFlaskApiProjectHascommitTests, self).setUp()
- tests.create_projects(self.session)
- tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
- def test_private_project(self):
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- project.private = True
- self.session.add(project)
- self.session.commit()
- output = self.app.get("/api/0/test/hascommit?user=pingou")
- self.assertEqual(output.status_code, 404)
- expected_rv = {
- "error": "Project not found",
- "error_code": "ENOPROJECT",
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
- def test_missing_branch(self):
- output = self.app.get("/api/0/test/hascommit?user=pingou")
- self.assertEqual(output.status_code, 400)
- expected_rv = {
- "error": "Invalid or incomplete input submitted",
- "error_code": "EINVALIDREQ",
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
- def test_missing_user(self):
- output = self.app.get("/api/0/test/hascommit?branch=user")
- self.assertEqual(output.status_code, 400)
- expected_rv = {
- "error": "Invalid or incomplete input submitted",
- "error_code": "EINVALIDREQ",
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
- def test_just_main_admin(self):
- output = self.app.get("/api/0/test/hascommit?user=pingou&branch=main")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "args": {
- "branch": "main",
- "project": {
- "access_groups": {
- "admin": [],
- "collaborator": [],
- "commit": [],
- "ticket": [],
- },
- "access_users": {
- "admin": [],
- "collaborator": [],
- "commit": [],
- "owner": ["pingou"],
- "ticket": [],
- },
- "close_status": [
- "Invalid",
- "Insufficient data",
- "Fixed",
- "Duplicate",
- ],
- "custom_keys": [],
- "date_created": ANY,
- "date_modified": ANY,
- "description": "test project #1",
- "full_url": "http://localhost.localdomain/test",
- "fullname": "test",
- "id": 1,
- "milestones": {},
- "name": "test",
- "namespace": None,
- "parent": None,
- "priorities": {},
- "tags": [],
- "url_path": "test",
- "user": {
- "full_url": "http://localhost.localdomain/user/pingou",
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- },
- "user": "pingou",
- },
- "hascommit": True,
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
- def test_admin_user(self):
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- msg = pagure.lib.query.add_user_to_project(
- session=self.session,
- project=project,
- new_user="foo",
- user="pingou",
- access="admin",
- branches=None,
- required_groups=None,
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- output = self.app.get("/api/0/test/hascommit?user=foo&branch=main")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "args": {
- "branch": "main",
- "project": {
- "access_groups": {
- "admin": [],
- "collaborator": [],
- "commit": [],
- "ticket": [],
- },
- "access_users": {
- "admin": ["foo"],
- "collaborator": [],
- "commit": [],
- "owner": ["pingou"],
- "ticket": [],
- },
- "close_status": [
- "Invalid",
- "Insufficient data",
- "Fixed",
- "Duplicate",
- ],
- "custom_keys": [],
- "date_created": ANY,
- "date_modified": ANY,
- "description": "test project #1",
- "full_url": "http://localhost.localdomain/test",
- "fullname": "test",
- "id": 1,
- "milestones": {},
- "name": "test",
- "namespace": None,
- "parent": None,
- "priorities": {},
- "tags": [],
- "url_path": "test",
- "user": {
- "full_url": "http://localhost.localdomain/user/pingou",
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- },
- "user": "foo",
- },
- "hascommit": True,
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
- def test_admin_user_and_commit(self):
- tests.create_user(self.session, "baz", "foo baz", ["baz@bar.com"])
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- msg = pagure.lib.query.add_user_to_project(
- session=self.session,
- project=project,
- new_user="foo",
- user="pingou",
- access="admin",
- branches=None,
- required_groups=None,
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- msg = pagure.lib.query.add_user_to_project(
- session=self.session,
- project=project,
- new_user="baz",
- user="pingou",
- access="commit",
- branches=None,
- required_groups=None,
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- output = self.app.get("/api/0/test/hascommit?user=baz&branch=main")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "args": {
- "branch": "main",
- "project": {
- "access_groups": {
- "admin": [],
- "collaborator": [],
- "commit": [],
- "ticket": [],
- },
- "access_users": {
- "admin": ["foo"],
- "collaborator": [],
- "commit": ["baz"],
- "owner": ["pingou"],
- "ticket": [],
- },
- "close_status": [
- "Invalid",
- "Insufficient data",
- "Fixed",
- "Duplicate",
- ],
- "custom_keys": [],
- "date_created": ANY,
- "date_modified": ANY,
- "description": "test project #1",
- "full_url": "http://localhost.localdomain/test",
- "fullname": "test",
- "id": 1,
- "milestones": {},
- "name": "test",
- "namespace": None,
- "parent": None,
- "priorities": {},
- "tags": [],
- "url_path": "test",
- "user": {
- "full_url": "http://localhost.localdomain/user/pingou",
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- },
- "user": "baz",
- },
- "hascommit": True,
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
- def test_admin_user_and_commit_and_ticket(self):
- tests.create_user(self.session, "baz", "foo baz", ["baz@bar.com"])
- tests.create_user(
- self.session, "alex", "Alex Ander", ["alex@ander.com"]
- )
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- msg = pagure.lib.query.add_user_to_project(
- session=self.session,
- project=project,
- new_user="foo",
- user="pingou",
- access="admin",
- branches=None,
- required_groups=None,
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- msg = pagure.lib.query.add_user_to_project(
- session=self.session,
- project=project,
- new_user="baz",
- user="pingou",
- access="commit",
- branches=None,
- required_groups=None,
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- msg = pagure.lib.query.add_user_to_project(
- session=self.session,
- project=project,
- new_user="alex",
- user="pingou",
- access="ticket",
- branches=None,
- required_groups=None,
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- output = self.app.get("/api/0/test/hascommit?user=alex&branch=main")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "args": {
- "branch": "main",
- "project": {
- "access_groups": {
- "admin": [],
- "collaborator": [],
- "commit": [],
- "ticket": [],
- },
- "access_users": {
- "admin": ["foo"],
- "collaborator": [],
- "commit": ["baz"],
- "owner": ["pingou"],
- "ticket": ["alex"],
- },
- "close_status": [
- "Invalid",
- "Insufficient data",
- "Fixed",
- "Duplicate",
- ],
- "custom_keys": [],
- "date_created": ANY,
- "date_modified": ANY,
- "description": "test project #1",
- "full_url": "http://localhost.localdomain/test",
- "fullname": "test",
- "id": 1,
- "milestones": {},
- "name": "test",
- "namespace": None,
- "parent": None,
- "priorities": {},
- "tags": [],
- "url_path": "test",
- "user": {
- "full_url": "http://localhost.localdomain/user/pingou",
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- },
- "user": "alex",
- },
- "hascommit": False,
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
- def test_admin_user_and_commit_and_ticket_and_contributors(self):
- tests.create_user(self.session, "baz", "foo baz", ["baz@bar.com"])
- tests.create_user(
- self.session, "alex", "Alex Ander", ["alex@ander.com"]
- )
- tests.create_user(self.session, "ralph", "Ralph B.", ["ralph@b.com"])
- tests.create_user(self.session, "kevin", "Kevin F.", ["kevin@f.com"])
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- msg = pagure.lib.query.add_user_to_project(
- session=self.session,
- project=project,
- new_user="foo",
- user="pingou",
- access="admin",
- branches=None,
- required_groups=None,
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- msg = pagure.lib.query.add_user_to_project(
- session=self.session,
- project=project,
- new_user="baz",
- user="pingou",
- access="commit",
- branches=None,
- required_groups=None,
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- msg = pagure.lib.query.add_user_to_project(
- session=self.session,
- project=project,
- new_user="alex",
- user="pingou",
- access="ticket",
- branches=None,
- required_groups=None,
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- msg = pagure.lib.query.add_user_to_project(
- session=self.session,
- project=project,
- new_user="ralph",
- user="pingou",
- access="collaborator",
- branches="epel*",
- required_groups=None,
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- msg = pagure.lib.query.add_user_to_project(
- session=self.session,
- project=project,
- new_user="kevin",
- user="pingou",
- access="collaborator",
- branches="f*",
- required_groups=None,
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- output = self.app.get("/api/0/test/hascommit?user=kevin&branch=main")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "args": {
- "branch": "main",
- "project": {
- "access_groups": {
- "admin": [],
- "collaborator": [],
- "commit": [],
- "ticket": [],
- },
- "access_users": {
- "admin": ["foo"],
- "collaborator": ["kevin", "ralph"],
- "commit": ["baz"],
- "owner": ["pingou"],
- "ticket": ["alex"],
- },
- "close_status": [
- "Invalid",
- "Insufficient data",
- "Fixed",
- "Duplicate",
- ],
- "custom_keys": [],
- "date_created": ANY,
- "date_modified": ANY,
- "description": "test project #1",
- "full_url": "http://localhost.localdomain/test",
- "fullname": "test",
- "id": 1,
- "milestones": {},
- "name": "test",
- "namespace": None,
- "parent": None,
- "priorities": {},
- "tags": [],
- "url_path": "test",
- "user": {
- "full_url": "http://localhost.localdomain/user/pingou",
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- },
- "user": "kevin",
- },
- "hascommit": False,
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
- output = self.app.get("/api/0/test/hascommit?user=kevin&branch=f33")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "args": {
- "branch": "f33",
- "project": {
- "access_groups": {
- "admin": [],
- "collaborator": [],
- "commit": [],
- "ticket": [],
- },
- "access_users": {
- "admin": ["foo"],
- "collaborator": ["kevin", "ralph"],
- "commit": ["baz"],
- "owner": ["pingou"],
- "ticket": ["alex"],
- },
- "close_status": [
- "Invalid",
- "Insufficient data",
- "Fixed",
- "Duplicate",
- ],
- "custom_keys": [],
- "date_created": ANY,
- "date_modified": ANY,
- "description": "test project #1",
- "full_url": "http://localhost.localdomain/test",
- "fullname": "test",
- "id": 1,
- "milestones": {},
- "name": "test",
- "namespace": None,
- "parent": None,
- "priorities": {},
- "tags": [],
- "url_path": "test",
- "user": {
- "full_url": "http://localhost.localdomain/user/pingou",
- "fullname": "PY C",
- "name": "pingou",
- "url_path": "user/pingou",
- },
- },
- "user": "kevin",
- },
- "hascommit": True,
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
|