123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722 |
- # -*- coding: utf-8 -*-
- """
- (c) 2020 - 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 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 PagureFlaskApiProjectContributorsTests(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(PagureFlaskApiProjectContributorsTests, 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/contributors")
- 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_just_main_admin(self):
- output = self.app.get("/api/0/test/contributors")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "groups": {
- "admin": [],
- "collaborators": [],
- "commit": [],
- "ticket": [],
- },
- "users": {
- "admin": ["pingou"],
- "collaborators": [],
- "commit": [],
- "ticket": [],
- },
- }
- 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/contributors")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "groups": {
- "admin": [],
- "collaborators": [],
- "commit": [],
- "ticket": [],
- },
- "users": {
- "admin": ["foo", "pingou"],
- "collaborators": [],
- "commit": [],
- "ticket": [],
- },
- }
- 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/contributors")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "groups": {
- "admin": [],
- "collaborators": [],
- "commit": [],
- "ticket": [],
- },
- "users": {
- "admin": ["foo", "pingou"],
- "collaborators": [],
- "commit": ["baz"],
- "ticket": [],
- },
- }
- 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/contributors")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "groups": {
- "admin": [],
- "collaborators": [],
- "commit": [],
- "ticket": [],
- },
- "users": {
- "admin": ["foo", "pingou"],
- "collaborators": [],
- "commit": ["baz"],
- "ticket": ["alex"],
- },
- }
- 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/contributors")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "groups": {
- "admin": [],
- "collaborators": [],
- "commit": [],
- "ticket": [],
- },
- "users": {
- "admin": ["foo", "pingou"],
- "collaborators": [
- {"branches": "f*", "user": "kevin"},
- {"branches": "epel*", "user": "ralph"},
- ],
- "commit": ["baz"],
- "ticket": ["alex"],
- },
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
- def test_admin_group(self):
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="admin_groups",
- user="pingou",
- access="admin",
- branches=None,
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- output = self.app.get("/api/0/test/contributors")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "groups": {
- "admin": ["admin_groups"],
- "collaborators": [],
- "commit": [],
- "ticket": [],
- },
- "users": {
- "admin": ["pingou"],
- "collaborators": [],
- "commit": [],
- "ticket": [],
- },
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
- def test_admin_and_commit_groups(self):
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="admin_groups",
- user="pingou",
- access="admin",
- branches=None,
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="commit_group",
- user="pingou",
- access="commit",
- branches=None,
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- output = self.app.get("/api/0/test/contributors")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "groups": {
- "admin": ["admin_groups"],
- "collaborators": [],
- "commit": ["commit_group"],
- "ticket": [],
- },
- "users": {
- "admin": ["pingou"],
- "collaborators": [],
- "commit": [],
- "ticket": [],
- },
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
- def test_admin_and_commit_and_ticket_groups(self):
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="admin_groups",
- user="pingou",
- access="admin",
- branches=None,
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="commit_group",
- user="pingou",
- access="commit",
- branches=None,
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="ticket_group",
- user="pingou",
- access="ticket",
- branches=None,
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- output = self.app.get("/api/0/test/contributors")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "groups": {
- "admin": ["admin_groups"],
- "collaborators": [],
- "commit": ["commit_group"],
- "ticket": ["ticket_group"],
- },
- "users": {
- "admin": ["pingou"],
- "collaborators": [],
- "commit": [],
- "ticket": [],
- },
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
- def test_admin_and_commit_and_ticket_and_collaborators_groups(self):
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="admin_groups",
- user="pingou",
- access="admin",
- branches=None,
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="commit_group",
- user="pingou",
- access="commit",
- branches=None,
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="ticket_group",
- user="pingou",
- access="ticket",
- branches=None,
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="epel_group",
- user="pingou",
- access="collaborator",
- branches="epel*",
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="fedora_group",
- user="pingou",
- access="collaborator",
- branches="f*",
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- output = self.app.get("/api/0/test/contributors")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "groups": {
- "admin": ["admin_groups"],
- "collaborators": [
- {"branches": "epel*", "user": "epel_group"},
- {"branches": "f*", "user": "fedora_group"},
- ],
- "commit": ["commit_group"],
- "ticket": ["ticket_group"],
- },
- "users": {
- "admin": ["pingou"],
- "collaborators": [],
- "commit": [],
- "ticket": [],
- },
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
- def test_full(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")
- # Add users for all kinds of access
- 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")
- # Create groups for all kinds of access
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="admin_groups",
- user="pingou",
- access="admin",
- branches=None,
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="commit_group",
- user="pingou",
- access="commit",
- branches=None,
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="ticket_group",
- user="pingou",
- access="ticket",
- branches=None,
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="epel_group",
- user="pingou",
- access="collaborator",
- branches="epel*",
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="fedora_group",
- user="pingou",
- access="collaborator",
- branches="f*",
- create=True,
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- output = self.app.get("/api/0/test/contributors")
- self.assertEqual(output.status_code, 200)
- expected_rv = {
- "groups": {
- "admin": ["admin_groups"],
- "collaborators": [
- {"branches": "epel*", "user": "epel_group"},
- {"branches": "f*", "user": "fedora_group"},
- ],
- "commit": ["commit_group"],
- "ticket": ["ticket_group"],
- },
- "users": {
- "admin": ["foo", "pingou"],
- "collaborators": [
- {"branches": "f*", "user": "kevin"},
- {"branches": "epel*", "user": "ralph"},
- ],
- "commit": ["baz"],
- "ticket": ["alex"],
- },
- }
- data = json.loads(output.get_data(as_text=True))
- self.assertDictEqual(data, expected_rv)
- if __name__ == "__main__":
- unittest.main(verbosity=2)
|