示例#1
0
    def setup_method(self, method):

        super(MockTest, self).setup_method(method)
        # Mocks
        self.gh = self.mox.CreateMock(GHManager)
        self.user = self.mox.CreateMock(AuthenticatedUser)
        self.org = self.mox.CreateMock(AuthenticatedUser)
        self.repo = self.mox.CreateMock(Repository)
        self.repo.organization = None

        self.user.login = "******"
        self.gh.get_repo("mock/mock").AndReturn(self.repo)
        self.mox.ReplayAll()

        self.gh_repo = GitHubRepository(self.gh, "mock", "mock")
class TestGitHubRepositoryGetPull(TestInternalRetries, InternalRetriesHelper):
    def setup_method(self, method):
        super(TestGitHubRepositoryGetPull, self).setup_method(method)
        self.gh.get_repo("mock/mock").AndReturn(self.repo)

    def generate_errors(self, error, nerrors):
        for i in range(nerrors):
            self.repo.get_pull(1).AndRaise(error)

    def mock_calls(self):
        self.repo.get_pull(1).AndReturn(self.pull)

    def run_function(self):
        self.gh_repo = GitHubRepository(self.gh_manager, "mock", "mock")
        return self.gh_repo.get_pull(1)

    def get_output(self):
        return self.pull
class TestGitHubRepositoryGetPull(TestInternalRetries,
                                  InternalRetriesHelper):

    def setUp(self):
        TestInternalRetries.setUp(self)
        self.gh.get_repo("mock/mock").AndReturn(self.repo)

    def generate_errors(self, error, nerrors):
        for i in range(nerrors):
            self.repo.get_pull(1).AndRaise(error)

    def mock_calls(self):
        self.repo.get_pull(1).AndReturn(self.pull)

    def run_function(self):
        self.gh_repo = GitHubRepository(self.gh_manager, "mock", "mock")
        return self.gh_repo.get_pull(1)

    def get_output(self):
        return self.pull
class TestGitHubRepositoryGetPulls(TestInternalRetries,
                                   InternalRetriesHelper):

    def setup_method(self, method):
        super(TestGitHubRepositoryGetPulls, self).setup_method(method)
        self.gh.get_repo("mock/mock").AndReturn(self.repo)

    def generate_errors(self, error, nerrors):
        for i in range(nerrors):
            self.repo.get_pulls().AndRaise(error)

    def mock_calls(self):
        self.repo.get_pulls().AndReturn([self.pull])

    def run_function(self):
        self.gh_repo = GitHubRepository(self.gh_manager, "mock", "mock")
        return self.gh_repo.get_pulls()

    def get_output(self):
        return [self.pull]
 def run_function(self):
     self.gh_repo = GitHubRepository(self.gh_manager, "mock", "mock")
     return self.gh_repo.get_pull(1)
示例#6
0
 def setup_repo(self):
     self.mox.ReplayAll()
     self.gh_repo = GitHubRepository(self.gh, self.user.login,
                                     self.repo.name)
示例#7
0
class TestGithubRepository(MoxTestBase):
    def setup_method(self, method):
        super(TestGithubRepository, self).setup_method(method)
        # Mocks
        self.gh = self.mox.CreateMock(GHManager)
        self.user = self.mox.CreateMock(AuthenticatedUser)
        self.user.login = "******"
        self.org = self.mox.CreateMock(Organization)
        self.org.login = "******"
        self.repo = self.mox.CreateMock(Repository)
        self.repo.name = "mock_repo"
        self.repo.owner = self.user
        self.repo.organization = None

        self.pulls = []
        self.milestones = []
        self.gh.get_repo(
            "%s/%s" % (self.user.login, self.repo.name)).AndReturn(self.repo)

    def create_pulls(self, baserefs=["master"]):

        for baseref in baserefs:
            base = self.mox.CreateMock(PullRequestPart)
            base.ref = baseref
            pullrequest = self.mox.CreateMock(PullRequest)
            pullrequest.base = base
            self.pulls.append(pullrequest)

    def create_milestones(self, titles):

        for title in titles:
            milestone = self.mox.CreateMock(Milestone)
            milestone.title = title
            self.milestones.append(milestone)

    def iter_pulls(self):
        for x in self.pulls:
            yield x

    def setup_org(self):
        self.repo.organization = self.org
        self.gh.get_organization(self.org.login).AndReturn(self.org)

    def setup_repo(self):
        self.mox.ReplayAll()
        self.gh_repo = GitHubRepository(self.gh, self.user.login,
                                        self.repo.name)

    @pytest.mark.parametrize('with_org', [True, False])
    def test_init(self, with_org):
        if with_org:
            self.setup_org()
        self.setup_repo()
        assert self.gh_repo.gh == self.gh
        assert self.gh_repo.repo == self.repo
        assert self.gh_repo.user_name == self.user.login
        assert self.gh_repo.repo_name == self.repo.name
        if with_org:
            assert self.gh_repo.org is self.org
        else:
            assert self.gh_repo.org is None

    def test_repr(self):
        self.setup_repo()
        repo_str = "Repository: %s/%s" % (self.user.login, self.repo.name)
        assert str(self.gh_repo) == repo_str

    def test_get_issue(self):
        issue = self.mox.CreateMock(Issue)
        self.repo.get_issue(1).AndReturn(issue)
        self.setup_repo()
        assert self.gh_repo.get_issue(1) == issue

    def test_get_pull(self):
        pullrequest = self.mox.CreateMock(PullRequest)
        self.repo.get_pull(1).AndReturn(pullrequest)
        self.setup_repo()
        assert self.gh_repo.get_pull(1) == pullrequest

    def test_get_pulls(self):
        pulls = self.mox.CreateMock(PaginatedList)
        self.repo.get_pulls().AndReturn(pulls)
        self.setup_repo()
        assert self.gh_repo.get_pulls() == pulls

    def test_get_pulls_by_base(self):
        self.create_pulls(["master", "master", "develop"])
        pulls_list = self.mox.CreateMock(PaginatedList)
        pulls_list.__iter__().AndReturn(self.iter_pulls())
        self.repo.get_pulls().AndReturn(pulls_list)
        self.setup_repo()
        assert self.gh_repo.get_pulls_by_base("master") == \
            self.pulls[:-1]

    def testGetMilestoneOpen(self):
        self.create_milestones(["open-1", "open-2"])
        self.repo.get_milestones(state="open").AndReturn(self.milestones)
        self.setup_repo()
        assert self.gh_repo.get_milestone("open-2") == self.milestones[1]

    def testGetMilestoneClosed(self):
        self.create_milestones(["closed-1", "closed-2"])
        self.repo.get_milestones(state="open").AndReturn([])
        self.repo.get_milestones(state="closed").AndReturn(self.milestones)
        self.setup_repo()
        assert self.gh_repo.get_milestone("closed-2") == self.milestones[1]

    def testGetMilestoneFails(self):
        self.repo.get_milestones(state="open").AndReturn([])
        self.repo.get_milestones(state="closed").AndReturn([])
        self.setup_repo()
        assert self.gh_repo.get_milestone("closed-2") is None

    def test_get_owner(self):
        self.setup_repo()
        assert self.gh_repo.get_owner() == self.user.login

    def test_create_open_pr(self):
        pullrequest = self.mox.CreateMock(PullRequest)
        title = "mock-title"
        description = "mock-description"
        base = "mock-base"
        head = "mock-head"
        self.repo.create_pull(title, description, base,
                              head).AndReturn(pullrequest)
        self.setup_repo()
        assert self.gh_repo.create_pull(title, description, base, head) == \
            pullrequest

    @pytest.mark.parametrize('whitelist',
                             [["#all"], ["test"], ["test", "test2"]])
    @pytest.mark.parametrize('with_org', [True, False])
    def test_whitelisted(self, whitelist, with_org):
        user = self.mox.CreateMock(NamedUser)
        user.login = '******'
        if with_org:
            self.setup_org()
        if whitelist and with_org and "#org" in whitelist:
            self.org.has_in_public_members(user).AndReturn(True)
        self.setup_repo()

        assert self.gh_repo.is_whitelisted(user, whitelist)

    @pytest.mark.parametrize('whitelist',
                             [["#all", "#org"], ["#org"], ["#org", "test2"]])
    def test_org_whitelist(self, whitelist):
        user = self.mox.CreateMock(NamedUser)
        user.login = '******'
        self.setup_org()
        if "#org" in whitelist and "#all" not in whitelist:
            self.org.has_in_public_members(user).AndReturn(True)
        self.setup_repo()

        assert self.gh_repo.is_whitelisted(user, whitelist)

    @pytest.mark.parametrize('whitelist',
                             [None, ["test2"], ["#org"], ["#org", "test2"]])
    @pytest.mark.parametrize('with_org', [True, False])
    def test_blacklisted(self, whitelist, with_org):
        user = self.mox.CreateMock(NamedUser)
        user.login = '******'
        if with_org:
            self.setup_org()
        if whitelist and with_org and "#org" in whitelist:
            self.org.has_in_public_members(user).AndReturn(False)
        self.setup_repo()

        assert not self.gh_repo.is_whitelisted(user, whitelist)
 def setup_repo(self):
     self.mox.ReplayAll()
     self.gh_repo = GitHubRepository(
         self.gh, self.user.login, self.repo.name)
class TestGithubRepository(MoxTestBase):

    def setup_method(self, method):
        super(TestGithubRepository, self).setup_method(method)
        # Mocks
        self.gh = self.mox.CreateMock(GHManager)
        self.user = self.mox.CreateMock(AuthenticatedUser)
        self.user.login = "******"
        self.org = self.mox.CreateMock(Organization)
        self.org.login = "******"
        self.repo = self.mox.CreateMock(Repository)
        self.repo.name = "mock_repo"
        self.repo.owner = self.user
        self.repo.organization = None

        self.pulls = []
        self.milestones = []
        self.gh.get_repo(
            "%s/%s" % (self.user.login, self.repo.name)).AndReturn(self.repo)

    def create_pulls(self, baserefs=["master"]):

        for baseref in baserefs:
            base = self.mox.CreateMock(PullRequestPart)
            base.ref = baseref
            pullrequest = self.mox.CreateMock(PullRequest)
            pullrequest.base = base
            self.pulls.append(pullrequest)

    def create_milestones(self, titles):

        for title in titles:
            milestone = self.mox.CreateMock(Milestone)
            milestone.title = title
            self.milestones.append(milestone)

    def iter_pulls(self):
        for x in self.pulls:
            yield x

    def setup_org(self):
        self.repo.organization = self.org
        self.gh.get_organization(self.org.login).AndReturn(self.org)

    def setup_repo(self):
        self.mox.ReplayAll()
        self.gh_repo = GitHubRepository(
            self.gh, self.user.login, self.repo.name)

    @pytest.mark.parametrize('with_org', [True, False])
    def test_init(self, with_org):
        if with_org:
            self.setup_org()
        self.setup_repo()
        assert self.gh_repo.gh == self.gh
        assert self.gh_repo.repo == self.repo
        assert self.gh_repo.user_name == self.user.login
        assert self.gh_repo.repo_name == self.repo.name
        if with_org:
            assert self.gh_repo.org is self.org
        else:
            assert self.gh_repo.org is None

    def test_repr(self):
        self.setup_repo()
        repo_str = "Repository: %s/%s" % (self.user.login, self.repo.name)
        assert str(self.gh_repo) == repo_str

    def test_get_issue(self):
        issue = self.mox.CreateMock(Issue)
        self.repo.get_issue(1).AndReturn(issue)
        self.setup_repo()
        assert self.gh_repo.get_issue(1) == issue

    def test_get_pull(self):
        pullrequest = self.mox.CreateMock(PullRequest)
        self.repo.get_pull(1).AndReturn(pullrequest)
        self.setup_repo()
        assert self.gh_repo.get_pull(1) == pullrequest

    def test_get_pulls(self):
        pulls = self.mox.CreateMock(PaginatedList)
        self.repo.get_pulls().AndReturn(pulls)
        self.setup_repo()
        assert self.gh_repo.get_pulls() == pulls

    def test_get_pulls_by_base(self):
        self.create_pulls(["master", "master", "develop"])
        pulls_list = self.mox.CreateMock(PaginatedList)
        pulls_list.__iter__().AndReturn(self.iter_pulls())
        self.repo.get_pulls().AndReturn(pulls_list)
        self.setup_repo()
        assert self.gh_repo.get_pulls_by_base("master") == \
            self.pulls[:-1]

    def testGetMilestoneOpen(self):
        self.create_milestones(["open-1", "open-2"])
        self.repo.get_milestones(state="open").AndReturn(self.milestones)
        self.setup_repo()
        assert self.gh_repo.get_milestone("open-2") == self.milestones[1]

    def testGetMilestoneClosed(self):
        self.create_milestones(["closed-1", "closed-2"])
        self.repo.get_milestones(state="open").AndReturn([])
        self.repo.get_milestones(state="closed").AndReturn(self.milestones)
        self.setup_repo()
        assert self.gh_repo.get_milestone("closed-2") == self.milestones[1]

    def testGetMilestoneFails(self):
        self.repo.get_milestones(state="open").AndReturn([])
        self.repo.get_milestones(state="closed").AndReturn([])
        self.setup_repo()
        assert self.gh_repo.get_milestone("closed-2") is None

    def test_get_owner(self):
        self.setup_repo()
        assert self.gh_repo.get_owner() == self.user.login

    def test_create_open_pr(self):
        pullrequest = self.mox.CreateMock(PullRequest)
        title = "mock-title"
        description = "mock-description"
        base = "mock-base"
        head = "mock-head"
        self.repo.create_pull(
            title, description, base, head).AndReturn(pullrequest)
        self.setup_repo()
        assert self.gh_repo.create_pull(title, description, base, head) == \
            pullrequest

    @pytest.mark.parametrize(
        'whitelist', [["#all"], ["test"], ["test", "test2"]])
    @pytest.mark.parametrize('with_org', [True, False])
    def test_whitelisted(self, whitelist, with_org):
        user = self.mox.CreateMock(NamedUser)
        user.login = '******'
        if with_org:
            self.setup_org()
        if whitelist and with_org and "#org" in whitelist:
            self.org.has_in_public_members(user).AndReturn(True)
        self.setup_repo()

        assert self.gh_repo.is_whitelisted(user, whitelist)

    @pytest.mark.parametrize(
        'whitelist', [["#all", "#org"], ["#org"], ["#org", "test2"]])
    def test_org_whitelist(self, whitelist):
        user = self.mox.CreateMock(NamedUser)
        user.login = '******'
        self.setup_org()
        if "#org" in whitelist and "#all" not in whitelist:
            self.org.has_in_public_members(user).AndReturn(True)
        self.setup_repo()

        assert self.gh_repo.is_whitelisted(user, whitelist)

    @pytest.mark.parametrize(
        'whitelist', [None, ["test2"], ["#org"], ["#org", "test2"]])
    @pytest.mark.parametrize('with_org', [True, False])
    def test_blacklisted(self, whitelist, with_org):
        user = self.mox.CreateMock(NamedUser)
        user.login = '******'
        if with_org:
            self.setup_org()
        if whitelist and with_org and "#org" in whitelist:
            self.org.has_in_public_members(user).AndReturn(False)
        self.setup_repo()

        assert not self.gh_repo.is_whitelisted(user, whitelist)
 def run_function(self):
     self.gh_repo = GitHubRepository(self.gh_manager, "mock", "mock")
     return self.gh_repo.get_pull(1)
 def run_function(self):
     gh_repo = GitHubRepository(self.gh_manager, "mock", "mock")
     return gh_repo.repo