Blame tests/test_tasks.py

Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Pierre-Yves Chibon 67d1cc
Slavek Kabrda 5ab53d
from mock import patch, MagicMock, Mock
Lubomír Sedlář d6299f
from collections import namedtuple
Lubomír Sedlář d6299f
import os
Lubomír Sedlář d6299f
import unittest
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
from pagure.lib import tasks
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
Pierre-Yves Chibon 73d120
MockUser = namedtuple("MockUser", ["fullname", "default_email"])
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
class MockCommit(object):
Pierre-Yves Chibon 73d120
    def __init__(self, name, email, time="1970-01-01 00:00"):
Lubomír Sedlář d6299f
        self.author = Mock(email=email)
Lubomír Sedlář d6299f
        self.author.name = name
Lubomír Sedlář d6299f
        self.commit_time = time
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
Pierre-Yves Chibon 73d120
@patch("pagure.lib.query.create_session", new=Mock())
Lubomír Sedlář d6299f
class TestCommitsAuthorStats(unittest.TestCase):
Lubomír Sedlář d6299f
    def setUp(self):
Pierre-Yves Chibon 73d120
        self.search_user_patcher = patch("pagure.lib.query.search_user")
Lubomír Sedlář d6299f
        mock_search_user = self.search_user_patcher.start()
Lubomír Sedlář d6299f
        mock_search_user.side_effect = lambda _, email: self.authors.get(email)
Lubomír Sedlář d6299f
Pierre-Yves Chibon 73d120
        self.pygit_patcher = patch("pygit2.Repository")
Lubomír Sedlář d6299f
        mock_repo = self.pygit_patcher.start().return_value
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        def mock_walk_impl(*args, **kwargs):
Lubomír Sedlář d6299f
            for commit in self.commits:
Lubomír Sedlář d6299f
                yield commit
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        mock_repo.walk.side_effect = mock_walk_impl
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        self.repopath = Mock()
Lubomír Sedlář d6299f
        exists = os.path.exists
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        def mock_exists_impl(path):
Lubomír Sedlář d6299f
            if path == self.repopath:
Lubomír Sedlář d6299f
                return True
Lubomír Sedlář d6299f
            return exists(path)
Lubomír Sedlář d6299f
Pierre-Yves Chibon 73d120
        self.exists_patcher = patch("os.path.exists")
Lubomír Sedlář d6299f
        mock_exists = self.exists_patcher.start()
Lubomír Sedlář d6299f
        mock_exists.side_effect = mock_exists_impl
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
    def tearDown(self):
Lubomír Sedlář d6299f
        self.search_user_patcher.stop()
Lubomír Sedlář d6299f
        self.pygit_patcher.stop()
Lubomír Sedlář d6299f
        self.exists_patcher.stop()
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
    def test_no_change(self):
Lubomír Sedlář d6299f
        self.commits = [
Pierre-Yves Chibon 73d120
            MockCommit("Alice", "alice@example.com", "2018-01-01 00:00")
Lubomír Sedlář d6299f
        ]
Lubomír Sedlář d6299f
        self.authors = {
Pierre-Yves Chibon 73d120
            "alice@example.com": MockUser("Alice", "alice@example.com")
Lubomír Sedlář d6299f
        }
Lubomír Sedlář d6299f
Pierre-Yves Chibon 73d120
        num_commits, authors, num_authors, last_time = tasks.commits_author_stats(
Pierre-Yves Chibon 73d120
            self.repopath
Pierre-Yves Chibon 73d120
        )
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        self.assertEqual(num_commits, 1)
Lubomír Sedlář d6299f
        self.assertEqual(num_authors, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(last_time, "2018-01-01 00:00")
Pierre-Yves Chibon 409376
        self.assertIn(
Pierre-Yves Chibon 409376
            authors,
Pierre-Yves Chibon 409376
            [
Pierre-Yves Chibon 73d120
                [
Pierre-Yves Chibon 73d120
                    (
Pierre-Yves Chibon 73d120
                        1,
Pierre-Yves Chibon 73d120
                        [
Pierre-Yves Chibon 73d120
                            (
Pierre-Yves Chibon 73d120
                                "Alice",
Pierre-Yves Chibon 73d120
                                "alice@example.com",
Pierre-Yves Chibon 73d120
                                "https://seccdn.libravatar.org/avatar/"
Pierre-Yves Chibon 73d120
                                "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
Pierre-Yves Chibon 73d120
                                "?s=32&d=retro",
Pierre-Yves Chibon 73d120
                            )
Pierre-Yves Chibon 73d120
                        ],
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                ],
Pierre-Yves Chibon 73d120
                [
Pierre-Yves Chibon 73d120
                    (
Pierre-Yves Chibon 73d120
                        1,
Pierre-Yves Chibon 73d120
                        [
Pierre-Yves Chibon 73d120
                            (
Pierre-Yves Chibon 73d120
                                "Alice",
Pierre-Yves Chibon 73d120
                                "alice@example.com",
Pierre-Yves Chibon 73d120
                                "https://seccdn.libravatar.org/avatar/"
Pierre-Yves Chibon 73d120
                                "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
Pierre-Yves Chibon 73d120
                                "?d=retro&s=32",
Pierre-Yves Chibon 73d120
                            )
Pierre-Yves Chibon 73d120
                        ],
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                ],
Pierre-Yves Chibon 73d120
            ],
Pierre-Yves Chibon 409376
        )
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
    def test_rename_user_and_merge(self):
Lubomír Sedlář d6299f
        self.commits = [
Pierre-Yves Chibon 73d120
            MockCommit("Alice", "alice@example.com"),
Pierre-Yves Chibon 73d120
            MockCommit("Bad name", "alice@example.com", "2018-01-01 00:00"),
Lubomír Sedlář d6299f
        ]
Lubomír Sedlář d6299f
        self.authors = {
Pierre-Yves Chibon 73d120
            "alice@example.com": MockUser("Alice", "alice@example.com")
Lubomír Sedlář d6299f
        }
Lubomír Sedlář d6299f
Pierre-Yves Chibon 73d120
        num_commits, authors, num_authors, last_time = tasks.commits_author_stats(
Pierre-Yves Chibon 73d120
            self.repopath
Pierre-Yves Chibon 73d120
        )
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        self.assertEqual(num_commits, 2)
Lubomír Sedlář d6299f
        self.assertEqual(num_authors, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(last_time, "2018-01-01 00:00")
Pierre-Yves Chibon 409376
        self.assertIn(
Pierre-Yves Chibon 409376
            authors,
Pierre-Yves Chibon 409376
            [
Pierre-Yves Chibon 73d120
                [
Pierre-Yves Chibon 73d120
                    (
Pierre-Yves Chibon 73d120
                        2,
Pierre-Yves Chibon 73d120
                        [
Pierre-Yves Chibon 73d120
                            (
Pierre-Yves Chibon 73d120
                                "Alice",
Pierre-Yves Chibon 73d120
                                "alice@example.com",
Pierre-Yves Chibon 73d120
                                "https://seccdn.libravatar.org/avatar/"
Pierre-Yves Chibon 73d120
                                "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
Pierre-Yves Chibon 73d120
                                "?s=32&d=retro",
Pierre-Yves Chibon 73d120
                            )
Pierre-Yves Chibon 73d120
                        ],
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                ],
Pierre-Yves Chibon 73d120
                [
Pierre-Yves Chibon 73d120
                    (
Pierre-Yves Chibon 73d120
                        2,
Pierre-Yves Chibon 73d120
                        [
Pierre-Yves Chibon 73d120
                            (
Pierre-Yves Chibon 73d120
                                "Alice",
Pierre-Yves Chibon 73d120
                                "alice@example.com",
Pierre-Yves Chibon 73d120
                                "https://seccdn.libravatar.org/avatar/"
Pierre-Yves Chibon 73d120
                                "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
Pierre-Yves Chibon 73d120
                                "?d=retro&s=32",
Pierre-Yves Chibon 73d120
                            )
Pierre-Yves Chibon 73d120
                        ],
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                ],
Pierre-Yves Chibon 73d120
            ],
Pierre-Yves Chibon 409376
        )
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
    def test_preserve_unknown_author(self):
Lubomír Sedlář d6299f
        self.commits = [
Pierre-Yves Chibon 73d120
            MockCommit("Alice", "alice@example.com", "2018-01-01 00:00")
Lubomír Sedlář d6299f
        ]
Lubomír Sedlář d6299f
        self.authors = {}
Lubomír Sedlář d6299f
Pierre-Yves Chibon 73d120
        num_commits, authors, num_authors, last_time = tasks.commits_author_stats(
Pierre-Yves Chibon 73d120
            self.repopath
Pierre-Yves Chibon 73d120
        )
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        self.assertEqual(num_commits, 1)
Lubomír Sedlář d6299f
        self.assertEqual(num_authors, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(last_time, "2018-01-01 00:00")
Pierre-Yves Chibon 409376
        self.assertIn(
Pierre-Yves Chibon 409376
            authors,
Pierre-Yves Chibon 409376
            [
Pierre-Yves Chibon 73d120
                [
Pierre-Yves Chibon 73d120
                    (
Pierre-Yves Chibon 73d120
                        1,
Pierre-Yves Chibon 73d120
                        [
Pierre-Yves Chibon 73d120
                            (
Pierre-Yves Chibon 73d120
                                "Alice",
Pierre-Yves Chibon 73d120
                                "alice@example.com",
Pierre-Yves Chibon 73d120
                                "https://seccdn.libravatar.org/avatar/"
Pierre-Yves Chibon 73d120
                                "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
Pierre-Yves Chibon 73d120
                                "?s=32&d=retro",
Pierre-Yves Chibon 73d120
                            )
Pierre-Yves Chibon 73d120
                        ],
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                ],
Pierre-Yves Chibon 73d120
                [
Pierre-Yves Chibon 73d120
                    (
Pierre-Yves Chibon 73d120
                        1,
Pierre-Yves Chibon 73d120
                        [
Pierre-Yves Chibon 73d120
                            (
Pierre-Yves Chibon 73d120
                                "Alice",
Pierre-Yves Chibon 73d120
                                "alice@example.com",
Pierre-Yves Chibon 73d120
                                "https://seccdn.libravatar.org/avatar/"
Pierre-Yves Chibon 73d120
                                "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
Pierre-Yves Chibon 73d120
                                "?d=retro&s=32",
Pierre-Yves Chibon 73d120
                            )
Pierre-Yves Chibon 73d120
                        ],
Pierre-Yves Chibon 73d120
                    )
Pierre-Yves Chibon 73d120
                ],
Pierre-Yves Chibon 73d120
            ],
Pierre-Yves Chibon 409376
        )
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
    def test_handle_empty_email(self):
Lubomír Sedlář d6299f
        self.commits = [
Lubomír Sedlář d6299f
            # Two commits for Alice to ensure order of the result.
Pierre-Yves Chibon 73d120
            MockCommit("Alice", None),
Pierre-Yves Chibon 73d120
            MockCommit("Alice", None),
Pierre-Yves Chibon 73d120
            MockCommit("Bob", "", "2018-01-01 00:00"),
Lubomír Sedlář d6299f
        ]
Lubomír Sedlář d6299f
        self.authors = {}
Lubomír Sedlář d6299f
Pierre-Yves Chibon 73d120
        num_commits, authors, num_authors, last_time = tasks.commits_author_stats(
Pierre-Yves Chibon 73d120
            self.repopath
Pierre-Yves Chibon 73d120
        )
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        self.assertEqual(num_commits, 3)
Lubomír Sedlář d6299f
        self.assertEqual(num_authors, 2)
Pierre-Yves Chibon 73d120
        self.assertEqual(last_time, "2018-01-01 00:00")
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            authors, [(2, [("Alice", None, None)]), (1, [("Bob", "", None)])]
Pierre-Yves Chibon 73d120
        )
Slavek Kabrda 5ab53d
Slavek Kabrda 5ab53d
Slavek Kabrda 5ab53d
class TestGitolitePostCompileOnly(object):
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git_auth.get_git_auth_helper")
Slavek Kabrda 5ab53d
    def test_backend_has_post_compile_only(self, get_helper):
Slavek Kabrda 5ab53d
        helper = MagicMock()
Slavek Kabrda 5ab53d
        get_helper.return_value = helper
Slavek Kabrda 5ab53d
        helper.post_compile_only = MagicMock()
Slavek Kabrda 5ab53d
        tasks.gitolite_post_compile_only()
Slavek Kabrda 5ab53d
        helper.post_compile_only.assert_called_once()
Slavek Kabrda 5ab53d
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git_auth.get_git_auth_helper")
Slavek Kabrda 5ab53d
    def test_backend_doesnt_have_post_compile_only(self, get_helper):
Slavek Kabrda 5ab53d
        helper = MagicMock()
Slavek Kabrda 5ab53d
        get_helper.return_value = helper
Slavek Kabrda 5ab53d
        helper.generate_acls = MagicMock()
Slavek Kabrda 5ab53d
        del helper.post_compile_only
Slavek Kabrda 5ab53d
        tasks.gitolite_post_compile_only()
Slavek Kabrda 5ab53d
        helper.generate_acls.assert_called_once_with(project=None)