Example #1
0
 def mock_get_log(self):
     latest_commit = gitiles.Commit(
         sha='a1841f40264376d170269ee9473ce924b7c2c4e9',
         tree='deadbeef',
         parents=['beefdead'],
         author=None,
         committer=None,
         message=None)
     self.mock(gitiles, 'get_log', mock.Mock())
     gitiles.get_log.return_value = gitiles.Log(commits=[latest_commit], )
Example #2
0
    def test_get_log(self):
        req_path = 'project/+log/master/'
        gerrit.fetch_json.return_value = {
            'log': [
                {
                    'commit': REVISION,
                    'tree': '3cfb41e1c6c37e61c3eccfab2395752298a5743c',
                    'parents': [
                        '4087678c002d57e1148f21da5e00867df9a7d973',
                    ],
                    'author': {
                        'name': 'John Doe',
                        'email': '*****@*****.**',
                        'time': 'Tue Apr 29 00:00:00 2014',
                    },
                    'committer': {
                        'name': 'John Doe',
                        'email': '*****@*****.**',
                        'time': 'Tue Apr 29 00:00:00 2014',
                    },
                    'message': 'Subject\\n\\nBody',
                },
                {
                    'commit': '4087678c002d57e1148f21da5e00867df9a7d973',
                    'tree': '3cfb41asdc37e61c3eccfab2395752298a5743c',
                    'parents': [
                        '1237678c002d57e1148f21da5e00867df9a7d973',
                    ],
                    'author': {
                        'name': 'John Doe',
                        'email': '*****@*****.**',
                        'time': 'Tue Apr 29 00:00:00 2014',
                    },
                    'committer': {
                        'name': 'John Doe',
                        'email': '*****@*****.**',
                        'time': 'Tue Apr 29 00:00:00 2014',
                    },
                    'message': 'Subject2\\n\\nBody2',
                },
            ],
        }

        log = gitiles.get_log(HOSTNAME, 'project', 'master', limit=2)
        gerrit.fetch_json.assert_called_once_with(HOSTNAME,
                                                  req_path,
                                                  params={'n': 2})

        john = gitiles.Contribution(name='John Doe',
                                    email='*****@*****.**',
                                    time=datetime.datetime(2014, 4, 29))
        self.assertEqual(
            log,
            gitiles.Log(commits=[
                gitiles.Commit(
                    sha=REVISION,
                    tree='3cfb41e1c6c37e61c3eccfab2395752298a5743c',
                    parents=[
                        '4087678c002d57e1148f21da5e00867df9a7d973',
                    ],
                    message='Subject\\n\\nBody',
                    author=john,
                    committer=john,
                ),
                gitiles.Commit(
                    sha='4087678c002d57e1148f21da5e00867df9a7d973',
                    tree='3cfb41asdc37e61c3eccfab2395752298a5743c',
                    parents=[
                        '1237678c002d57e1148f21da5e00867df9a7d973',
                    ],
                    message='Subject2\\n\\nBody2',
                    author=john,
                    committer=john,
                ),
            ]))
Example #3
0
    def test_notify_gitiles_rejection(self):
        ctx = validation.Context()
        ctx.error('err')
        ctx.warning('warn')

        base = gitiles.Location.parse('https://example.com/x/+/infra/config')
        new_rev = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
        new_loc = base._replace(treeish=new_rev)
        old_rev = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
        old_loc = base._replace(treeish=old_rev)

        self.mock(notifications, '_send', mock.Mock())

        john = gitiles.Contribution('John', '*****@*****.**',
                                    datetime.datetime(2015, 1, 1))
        commit = gitiles.Commit(sha=new_rev,
                                tree='badcoffee',
                                parents=[],
                                author=john,
                                committer=john,
                                message='New config',
                                tree_diff=None)
        self.mock(gitiles, 'get_log_async',
                  mock.Mock(return_value=ndb.Future()))
        gitiles.get_log_async.return_value.set_result(
            gitiles.Log(commits=[commit], next_cursor=None))

        self.mock(template, 'render', mock.Mock())

        self.mock(auth, 'list_group', mock.Mock())
        auth.list_group.return_value = auth.GroupListing([
            auth.Identity('user', '*****@*****.**'),
            auth.Identity('service', 'foo'),
        ], [], [])

        # Notify.

        notifications.notify_gitiles_rejection('projects/x', new_loc,
                                               ctx.result())

        self.assertTrue(notifications._send.called)
        email = notifications._send.call_args[0][0]
        self.assertEqual(
            email.sender,
            'sample-app.appspot.com <*****@*****.**>')
        self.assertEqual(email.subject, 'Config revision aaaaaaa is rejected')
        self.assertEqual(email.to, ['John <*****@*****.**>'])
        self.assertEqual(email.cc, {'*****@*****.**'})

        template.render.assert_called_with(
            'templates/validation_notification.html', {
                'author':
                'John',
                'messages': [{
                    'severity': 'ERROR',
                    'text': 'err'
                }, {
                    'severity': 'WARNING',
                    'text': 'warn'
                }],
                'rev_link':
                new_loc,
                'rev_hash':
                'aaaaaaa',
                'rev_repo':
                'x',
                'cur_rev_hash':
                None,
                'cur_rev_link':
                None,
            })

        # Do not send second time.
        notifications._send.reset_mock()
        notifications.notify_gitiles_rejection('projects/x', new_loc,
                                               ctx.result())
        self.assertFalse(notifications._send.called)

        # Now with config set.

        ndb.Key(notifications.Notification, str(new_loc)).delete()

        storage.ConfigSet(id='projects/x',
                          latest_revision=old_rev,
                          latest_revision_url=str(old_loc),
                          location=str(base)).put()

        template.render.reset_mock()
        notifications.notify_gitiles_rejection('projects/x', new_loc,
                                               ctx.result())
        template.render.assert_called_with(
            'templates/validation_notification.html', {
                'author':
                'John',
                'messages': [{
                    'severity': 'ERROR',
                    'text': 'err'
                }, {
                    'severity': 'WARNING',
                    'text': 'warn'
                }],
                'rev_link':
                new_loc,
                'rev_hash':
                'aaaaaaa',
                'rev_repo':
                'x',
                'cur_rev_hash':
                'bbbbbbb',
                'cur_rev_link':
                old_loc,
            })
Example #4
0
 def mock_get_log(self):
     self.mock(gitiles, 'get_log', mock.Mock())
     gitiles.get_log.return_value = gitiles.Log(
         commits=[self.test_commit],
         next_cursor=None,
     )