Blame tests/test_pagure_lib_watch_list.py

Pierre-Yves Chibon fc049c
# -*- coding: utf-8 -*-
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
"""
Pierre-Yves Chibon fc049c
 (c) 2016 - Copyright Red Hat Inc
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
 Authors:
Pierre-Yves Chibon fc049c
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
"""
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon fc049c
import unittest
Pierre-Yves Chibon fc049c
import shutil
Pierre-Yves Chibon fc049c
import sys
Pierre-Yves Chibon fc049c
import os
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
import mock
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
import pagure.lib.model
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon fc049c
import tests
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
@mock.patch("pagure.lib.git.update_git", mock.MagicMock(return_value=True))
Pierre-Yves Chibon 73d120
@mock.patch("pagure.lib.notify.send_email", mock.MagicMock(return_value=True))
Pierre-Yves Chibon fc049c
class PagureLibGetWatchListtests(tests.Modeltests):
Pierre-Yves Chibon 930073
    """ Tests for pagure.lib.query.get_watch_list """
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
    def test_get_watch_list_invalid_object(self):
Pierre-Yves Chibon fc049c
        """ Test get_watch_list when given an invalid object """
Pierre-Yves Chibon fc049c
        # Create a project ns/test
Pierre-Yves Chibon fc049c
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fc049c
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            namespace="ns",
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccdd",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon 73d120
        item.close_status = ["Invalid", "Insufficient data", "Fixed"]
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        self.assertRaises(
Pierre-Yves Chibon fc049c
            pagure.exceptions.InvalidObjectException,
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list,
Pierre-Yves Chibon fc049c
            self.session,
Pierre-Yves Chibon 73d120
            item,
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
    def test_get_watch_list_simple(self):
Pierre-Yves Chibon fc049c
        """ Test get_watch_list when the creator of the ticket is the
Pierre-Yves Chibon fc049c
        creator of the project """
Pierre-Yves Chibon fc049c
        # Create a project ns/test
Pierre-Yves Chibon fc049c
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fc049c
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            namespace="ns",
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccdd",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon 73d120
        item.close_status = ["Invalid", "Insufficient data", "Fixed"]
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Create the ticket
Pierre-Yves Chibon 930073
        iss = pagure.lib.query.new_issue(
Pierre-Yves Chibon fc049c
            issue_id=4,
Pierre-Yves Chibon fc049c
            session=self.session,
Pierre-Yves Chibon fc049c
            repo=item,
Pierre-Yves Chibon 73d120
            title="test issue",
Pierre-Yves Chibon 73d120
            content="content test issue",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
        self.assertEqual(iss.id, 4)
Pierre-Yves Chibon 73d120
        self.assertEqual(iss.title, "test issue")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.lib.query.get_watch_list(self.session, iss), set(["pingou"])
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
    def test_get_watch_list_different_creator(self):
Pierre-Yves Chibon fc049c
        """ Test get_watch_list when the creator of the ticket is not the
Pierre-Yves Chibon fc049c
        creator of the project """
Pierre-Yves Chibon fc049c
        # Create a project ns/test
Pierre-Yves Chibon fc049c
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fc049c
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            namespace="ns",
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccdd",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon 73d120
        item.close_status = ["Invalid", "Insufficient data", "Fixed"]
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Create the ticket
Pierre-Yves Chibon 930073
        iss = pagure.lib.query.new_issue(
Pierre-Yves Chibon fc049c
            issue_id=4,
Pierre-Yves Chibon fc049c
            session=self.session,
Pierre-Yves Chibon fc049c
            repo=item,
Pierre-Yves Chibon 73d120
            title="test issue",
Pierre-Yves Chibon 73d120
            content="content test issue",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
        self.assertEqual(iss.id, 4)
Pierre-Yves Chibon 73d120
        self.assertEqual(iss.title, "test issue")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, iss),
Pierre-Yves Chibon 73d120
            set(["pingou", "foo"]),
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
    def test_get_watch_list_project_w_contributor(self):
Pierre-Yves Chibon fc049c
        """ Test get_watch_list when the project has more than one
Pierre-Yves Chibon fc049c
        contributor """
Pierre-Yves Chibon fc049c
        # Create a project ns/test3
Pierre-Yves Chibon fc049c
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fc049c
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            namespace="ns",
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccdd",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon 73d120
        item.close_status = ["Invalid", "Insufficient data", "Fixed"]
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Add a contributor to the project
Pierre-Yves Chibon fc049c
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="bar",
Pierre-Yves Chibon 73d120
            fullname="bar foo",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@bar.com",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon 930073
        project = pagure.lib.query._get_project(
Pierre-Yves Chibon 73d120
            self.session, "test3", namespace="ns"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon fc049c
            session=self.session,
Pierre-Yves Chibon fc049c
            project=project,
Pierre-Yves Chibon 73d120
            new_user="bar",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User added")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Create the ticket
Pierre-Yves Chibon 930073
        iss = pagure.lib.query.new_issue(
Pierre-Yves Chibon fc049c
            issue_id=4,
Pierre-Yves Chibon fc049c
            session=self.session,
Pierre-Yves Chibon fc049c
            repo=project,
Pierre-Yves Chibon 73d120
            title="test issue",
Pierre-Yves Chibon 73d120
            content="content test issue",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
        self.assertEqual(iss.id, 4)
Pierre-Yves Chibon 73d120
        self.assertEqual(iss.title, "test issue")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, iss),
Pierre-Yves Chibon 73d120
            set(["pingou", "foo", "bar"]),
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
    def test_get_watch_list_user_in_group(self):
Pierre-Yves Chibon fc049c
        """ Test get_watch_list when the project has groups of contributors
Pierre-Yves Chibon fc049c
        """
Pierre-Yves Chibon fc049c
        # Create a project ns/test3
Pierre-Yves Chibon fc049c
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fc049c
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            namespace="ns",
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccdd",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon 73d120
        item.close_status = ["Invalid", "Insufficient data", "Fixed"]
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Create a third user
Pierre-Yves Chibon fc049c
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="bar",
Pierre-Yves Chibon 73d120
            fullname="bar foo",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@bar.com",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Create a group
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group(
Pierre-Yves Chibon fc049c
            self.session,
Pierre-Yves Chibon 73d120
            group_name="foo",
Pierre-Yves Chibon 73d120
            display_name="foo group",
Pierre-Yves Chibon fc049c
            description=None,
Pierre-Yves Chibon 73d120
            group_type="bar",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon fc049c
            is_admin=False,
Pierre-Yves Chibon fc049c
            blacklist=[],
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User `pingou` added to the group `foo`.")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Add user to group
Pierre-Yves Chibon 73d120
        group = pagure.lib.query.search_groups(self.session, group_name="foo")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_group(
Pierre-Yves Chibon fc049c
            self.session,
Pierre-Yves Chibon 73d120
            username="bar",
Pierre-Yves Chibon fc049c
            group=group,
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon fc049c
            is_admin=False,
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User `bar` added to the group `foo`.")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon 930073
        project = pagure.lib.query._get_project(
Pierre-Yves Chibon 73d120
            self.session, "test3", namespace="ns"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Add group to project
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon fc049c
            session=self.session,
Pierre-Yves Chibon fc049c
            project=project,
Pierre-Yves Chibon 73d120
            new_group="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "Group added")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Create the ticket
Pierre-Yves Chibon 930073
        iss = pagure.lib.query.new_issue(
Pierre-Yves Chibon fc049c
            issue_id=4,
Pierre-Yves Chibon fc049c
            session=self.session,
Pierre-Yves Chibon fc049c
            repo=project,
Pierre-Yves Chibon 73d120
            title="test issue",
Pierre-Yves Chibon 73d120
            content="content test issue",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
        self.assertEqual(iss.id, 4)
Pierre-Yves Chibon 73d120
        self.assertEqual(iss.title, "test issue")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, iss),
Pierre-Yves Chibon 73d120
            set(["pingou", "foo", "bar"]),
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
    def test_get_watch_list_project_w_contributor_out(self):
Pierre-Yves Chibon fc049c
        """ Test get_watch_list when the project has one contributor not
Pierre-Yves Chibon fc049c
        watching the project """
Pierre-Yves Chibon fc049c
        # Create a project ns/test3
Pierre-Yves Chibon fc049c
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fc049c
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            namespace="ns",
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccdd",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon 73d120
        item.close_status = ["Invalid", "Insufficient data", "Fixed"]
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Add a contributor to the project
Pierre-Yves Chibon fc049c
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="bar",
Pierre-Yves Chibon 73d120
            fullname="bar foo",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@bar.com",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon 930073
        project = pagure.lib.query._get_project(
Pierre-Yves Chibon 73d120
            self.session, "test3", namespace="ns"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon fc049c
            session=self.session,
Pierre-Yves Chibon fc049c
            project=project,
Pierre-Yves Chibon 73d120
            new_user="bar",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User added")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Set the user `pingou` to not watch the project
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.update_watch_status(
Pierre-Yves Chibon 73d120
            session=self.session, project=project, user="pingou", watch="0"
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "You are no longer watching this project")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Create the ticket
Pierre-Yves Chibon 930073
        iss = pagure.lib.query.new_issue(
Pierre-Yves Chibon fc049c
            issue_id=4,
Pierre-Yves Chibon fc049c
            session=self.session,
Pierre-Yves Chibon fc049c
            repo=project,
Pierre-Yves Chibon 73d120
            title="test issue",
Pierre-Yves Chibon 73d120
            content="content test issue",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
        self.assertEqual(iss.id, 4)
Pierre-Yves Chibon 73d120
        self.assertEqual(iss.title, "test issue")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, iss),
Pierre-Yves Chibon 73d120
            set(["foo", "bar"]),
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
    def test_get_watch_list_project_w_contributor_out_pr(self):
Pierre-Yves Chibon fc049c
        """ Test get_watch_list when the project has one contributor not
Pierre-Yves Chibon fc049c
        watching the pull-request """
Pierre-Yves Chibon fc049c
        # Create a project ns/test3
Pierre-Yves Chibon fc049c
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fc049c
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            namespace="ns",
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccdd",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon 73d120
        item.close_status = ["Invalid", "Insufficient data", "Fixed"]
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Add a contributor to the project
Pierre-Yves Chibon fc049c
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="bar",
Pierre-Yves Chibon 73d120
            fullname="bar foo",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@bar.com",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon 930073
        project = pagure.lib.query._get_project(
Pierre-Yves Chibon 73d120
            self.session, "test3", namespace="ns"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon fc049c
            session=self.session,
Pierre-Yves Chibon fc049c
            project=project,
Pierre-Yves Chibon 73d120
            new_user="bar",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User added")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Create the pull-request
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon fc049c
            session=self.session,
Pierre-Yves Chibon fc049c
            repo_from=project,
Pierre-Yves Chibon 73d120
            branch_from="dev",
Pierre-Yves Chibon fc049c
            repo_to=project,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Set the user `pingou` to not watch the pull-request
Pierre-Yves Chibon 73d120
        out = pagure.lib.query.set_watch_obj(
Pierre-Yves Chibon 73d120
            self.session, "pingou", req, False
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(out, "You are no longer watching this pull-request")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, req),
Pierre-Yves Chibon 73d120
            set(["foo", "bar"]),
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
    def test_get_watch_list_project_w_contributor_watching_project(self):
Pierre-Yves Chibon fc049c
        """ Test get_watch_list when the project has one contributor watching
Pierre-Yves Chibon fc049c
        the project """
Pierre-Yves Chibon fc049c
        # Create a project ns/test3
Pierre-Yves Chibon fc049c
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fc049c
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            namespace="ns",
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccdd",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon 73d120
        item.close_status = ["Invalid", "Insufficient data", "Fixed"]
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Add a new user
Pierre-Yves Chibon fc049c
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="bar",
Pierre-Yves Chibon 73d120
            fullname="bar foo",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@bar.com",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
Pierre-Yves Chibon fc049c
        self.session.add(item)
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Set the user `bar` to watch the project
Pierre-Yves Chibon 930073
        project = pagure.lib.query._get_project(
Pierre-Yves Chibon 73d120
            self.session, "test3", namespace="ns"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.update_watch_status(
Pierre-Yves Chibon 73d120
            session=self.session, project=project, user="bar", watch="1"
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Matt Prahl b49f93
        self.assertEqual(
Pierre-Yves Chibon 73d120
            msg, "You are now watching issues and PRs on this project"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        # Create the pull-request
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon fc049c
            session=self.session,
Pierre-Yves Chibon fc049c
            repo_from=project,
Pierre-Yves Chibon 73d120
            branch_from="dev",
Pierre-Yves Chibon fc049c
            repo_to=project,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
        self.session.commit()
Pierre-Yves Chibon fc049c
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon fc049c
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, req),
Pierre-Yves Chibon 73d120
            set(["foo", "bar", "pingou"]),
Pierre-Yves Chibon fc049c
        )
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon 73d120
    @mock.patch.dict("pagure.config.config", {"PAGURE_ADMIN_USERS": "foo"})
Pierre-Yves Chibon 53887d
    def test_get_watch_list_project_w_private_issue(self):
Pierre-Yves Chibon 53887d
        """ Test get_watch_list when the project has one contributor watching
Pierre-Yves Chibon 53887d
        the project and the issue is private """
Pierre-Yves Chibon 53887d
        # Create a project ns/test3
Pierre-Yves Chibon 53887d
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 53887d
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test3",
Pierre-Yves Chibon 73d120
            namespace="ns",
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccdd",
Pierre-Yves Chibon 53887d
        )
Pierre-Yves Chibon 73d120
        item.close_status = ["Invalid", "Insufficient data", "Fixed"]
Pierre-Yves Chibon 53887d
        self.session.add(item)
Pierre-Yves Chibon 53887d
        self.session.commit()
Pierre-Yves Chibon 53887d
Pierre-Yves Chibon 53887d
        # Add a new user
Pierre-Yves Chibon 53887d
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="bar",
Pierre-Yves Chibon 73d120
            fullname="bar foo",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@bar.com",
Pierre-Yves Chibon 53887d
        )
Pierre-Yves Chibon 53887d
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
Pierre-Yves Chibon 53887d
        self.session.add(item)
Pierre-Yves Chibon 53887d
Pierre-Yves Chibon 53887d
        # Set the user `bar` to watch the project
Pierre-Yves Chibon 930073
        project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test3", namespace="ns"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.update_watch_status(
Pierre-Yves Chibon 73d120
            session=self.session, project=project, user="bar", watch="1"
Pierre-Yves Chibon 53887d
        )
Pierre-Yves Chibon 53887d
        self.session.commit()
Matt Prahl b49f93
        self.assertEqual(
Pierre-Yves Chibon 73d120
            msg, "You are now watching issues and PRs on this project"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 53887d
Pierre-Yves Chibon 53887d
        # Create the ticket
Pierre-Yves Chibon 930073
        iss = pagure.lib.query.new_issue(
Pierre-Yves Chibon 53887d
            issue_id=4,
Pierre-Yves Chibon 53887d
            session=self.session,
Pierre-Yves Chibon 53887d
            repo=project,
Pierre-Yves Chibon 73d120
            title="test issue",
Pierre-Yves Chibon 73d120
            content="content test issue",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 53887d
            private=True,
Pierre-Yves Chibon 53887d
        )
Pierre-Yves Chibon 53887d
        self.session.commit()
Pierre-Yves Chibon 53887d
        self.assertEqual(iss.id, 4)
Pierre-Yves Chibon 73d120
        self.assertEqual(iss.title, "test issue")
Pierre-Yves Chibon 53887d
Pierre-Yves Chibon 53887d
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.lib.query.get_watch_list(self.session, iss), set(["pingou"])
Pierre-Yves Chibon 53887d
        )
Pierre-Yves Chibon 73d120
        out = pagure.lib.query.set_watch_obj(self.session, "foo", iss, True)
Pierre-Yves Chibon 73d120
        self.assertEqual(out, "You are now watching this issue")
Karsten Hopp 19b326
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, iss),
Pierre-Yves Chibon 73d120
            set(["pingou", "foo"]),
Karsten Hopp 19b326
        )
Pierre-Yves Chibon 73d120
        out = pagure.lib.query.set_watch_obj(self.session, "foo", iss, False)
Pierre-Yves Chibon 73d120
        self.assertEqual(out, "You are no longer watching this issue")
Karsten Hopp 19b326
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.lib.query.get_watch_list(self.session, iss), set(["pingou"])
Karsten Hopp 19b326
        )
Pierre-Yves Chibon 53887d
Pierre-Yves Chibon fc049c
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)