class TestApprovals(object):
    def setup_method(self, _method):
        self.api = Mock(Api)
        self.api.version = Mock(return_value=Version.parse('9.2.3-ee'))
        self.approvals = Approvals(api=self.api, info=INFO)

    def test_fetch_from_merge_request(self):
        api = self.api
        api.call = Mock(return_value=INFO)

        merge_request = MergeRequest(api, {
            'id': 74,
            'iid': 6,
            'project_id': 1234
        })
        approvals = merge_request.fetch_approvals()

        api.call.assert_called_once_with(
            GET('/projects/1234/merge_requests/6/approvals'))
        assert approvals.info == INFO

    def test_properties(self):
        assert self.approvals.project_id == 1
        assert self.approvals.approvals_left == 1
        assert self.approvals.approver_usernames == ['root', 'ebert']
        assert not self.approvals.sufficient

    def test_sufficiency(self):
        good_approvals = Approvals(api=self.api,
                                   info=dict(INFO,
                                             approvals_required=1,
                                             approvals_left=0))
        assert good_approvals.sufficient

    def test_refetch_info(self):
        self.approvals.reapprove()
        self.api.call.has_calls([
            call(POST(endpoint='/projects/1/merge_requests/6/approve',
                      args={},
                      extract=None),
                 sudo=1),
            call(POST(endpoint='/projects/1/merge_requests/6/approve',
                      args={},
                      extract=None),
                 sudo=2)
        ])

    @patch('marge.user.User.fetch_by_id')
    def test_get_reviewer_names_and_emails(self, user_fetch_by_id):
        user_fetch_by_id.side_effect = lambda id, _: marge.user.User(
            self.api, USERS[id])
        assert _get_reviewer_names_and_emails(
            approvals=self.approvals, api=self.api) == [
                'Administrator <root@localhost>',
                'Roger Ebert <*****@*****.**>'
            ]
 def test_approvals_fails_when_same_author(self, user_fetch_by_id):
     info = dict(INFO, approved_by=list(INFO['approved_by']))
     del info['approved_by'][1]
     approvals = Approvals(self.api, info)
     user_fetch_by_id.side_effect = lambda id, _: marge.user.User(
         self.api, USERS[id])
     commits = [{'author_email': 'root@localhost'}]
     with pytest.raises(CannotMerge):
         _get_reviewer_names_and_emails(commits=commits,
                                        approvals=approvals,
                                        api=self.api)
 def setup_method(self, _method):
     self.api = Mock(Api)
     self.api.version = Mock(return_value=Version.parse('9.2.3-ee'))
     self.approvals = Approvals(api=self.api, info=INFO)
class TestApprovals(object):
    def setup_method(self, _method):
        self.api = Mock(Api)
        self.api.version = Mock(return_value=Version.parse('9.2.3-ee'))
        self.approvals = Approvals(api=self.api, info=INFO)

    def test_fetch_from_merge_request(self):
        api = self.api
        api.call = Mock(return_value=INFO)

        merge_request = MergeRequest(api, {
            'id': 74,
            'iid': 6,
            'project_id': 1234
        })
        approvals = merge_request.fetch_approvals()

        api.call.assert_called_once_with(
            GET('/projects/1234/merge_requests/6/approvals'))
        assert approvals.info == INFO

    def test_fetch_from_merge_request_ce_compat(self):
        api = self.api
        api.version = Mock(return_value=Version.parse('9.2.3'))
        api.call = Mock()

        merge_request = MergeRequest(api, {
            'id': 74,
            'iid': 6,
            'project_id': 1234
        })
        approvals = merge_request.fetch_approvals()

        api.call.assert_not_called()
        assert approvals.info == {
            'id': 74,
            'iid': 6,
            'project_id': 1234,
            'approvals_left': 0,
            'approved_by': [],
        }

    def test_properties(self):
        assert self.approvals.project_id == 1
        assert self.approvals.approvals_left == 1
        assert self.approvals.approver_usernames == ['root', 'ebert']
        assert not self.approvals.sufficient

    def test_sufficiency(self):
        good_approvals = Approvals(api=self.api,
                                   info=dict(INFO,
                                             approvals_required=1,
                                             approvals_left=0))
        assert good_approvals.sufficient

    def test_reapprove(self):
        self.approvals.reapprove()
        self.api.call.has_calls([
            call(POST(endpoint='/projects/1/merge_requests/6/approve',
                      args={},
                      extract=None),
                 sudo=1),
            call(POST(endpoint='/projects/1/merge_requests/6/approve',
                      args={},
                      extract=None),
                 sudo=2)
        ])

    @patch('marge.user.User.fetch_by_id')
    def test_get_reviewer_names_and_emails(self, user_fetch_by_id):
        user_fetch_by_id.side_effect = lambda id, _: marge.user.User(
            self.api, USERS[id])
        assert _get_reviewer_names_and_emails(
            commits=[], approvals=self.approvals, api=self.api) == [
                'Administrator <root@localhost>',
                'Roger Ebert <*****@*****.**>'
            ]

    @patch('marge.user.User.fetch_by_id')
    def test_approvals_fails_when_same_author(self, user_fetch_by_id):
        info = dict(INFO, approved_by=list(INFO['approved_by']))
        del info['approved_by'][1]
        approvals = Approvals(self.api, info)
        user_fetch_by_id.side_effect = lambda id, _: marge.user.User(
            self.api, USERS[id])
        commits = [{'author_email': 'root@localhost'}]
        with pytest.raises(CannotMerge):
            _get_reviewer_names_and_emails(commits=commits,
                                           approvals=approvals,
                                           api=self.api)

    @patch('marge.user.User.fetch_by_id')
    def test_approvals_succeeds_with_independent_author(
            self, user_fetch_by_id):
        user_fetch_by_id.side_effect = lambda id, _: marge.user.User(
            self.api, USERS[id])
        print(INFO['approved_by'])
        commits = [{'author_email': 'root@localhost'}]
        assert _get_reviewer_names_and_emails(
            commits=commits, approvals=self.approvals, api=self.api) == [
                'Administrator <root@localhost>',
                'Roger Ebert <*****@*****.**>',
            ]
 def test_sufficiency(self):
     good_approvals = Approvals(api=self.api,
                                info=dict(INFO,
                                          approvals_required=1,
                                          approvals_left=0))
     assert good_approvals.sufficient