Renamed module from gitea to builbot_gitea, added unit tests for pull requests and secret phrase

This commit is contained in:
Marvin Pohl 2018-09-05 14:14:31 +02:00
parent cd603f73a5
commit 293acd8a2b
5 changed files with 158 additions and 4 deletions

View File

@ -5,10 +5,12 @@ from buildbot.test.fake.web import fakeMasterForHooks
import mock import mock
import calendar
from twisted.internet import defer from twisted.internet import defer
from twisted.trial import unittest from twisted.trial import unittest
from gitea.webhook import _HEADER_EVENT_TYPE from buildbot_gitea.webhook import _HEADER_EVENT_TYPE
giteaJsonPushPayload = rb""" giteaJsonPushPayload = rb"""
{ {
@ -94,6 +96,90 @@ giteaJsonPushPayload = rb"""
} }
""" """
giteaInvalidSecretPush = rb"""
{
"secret": "invalidSecret",
"ref": "refs/heads/feature-branch",
"before": "0000000000000000000000000000000000000000",
"after": "9d7157cc4a137b3e1dfe92750ccfb1bbad239f99",
"compare_url": "https://git.example.com/",
"commits": [
{
"id": "9d7157cc4a137b3e1dfe92750ccfb1bbad239f99",
"message": "TestBranch\n",
"url": "https://git.example.com/max/webhook_test/commit/9d7157cc4a137b3e1dfe92750ccfb1bbad239f99",
"author": {
"name": "Max Mustermann",
"email": "max@example.com",
"username": "max"
},
"committer": {
"name": "Max Mustermann",
"email": "max@example.com",
"username": "max"
},
"verification": null,
"timestamp": "2018-09-04T12:10:14Z"
}
],
"repository": {
"id": 20,
"owner": {
"id": 1,
"login": "max",
"full_name": "Max Mustermann",
"email": "max@example.com",
"avatar_url": "https://secure.gravatar.com/avatar/c9a5fca94b7fd6f8d4dbab8d1575e4fc?d=identicon",
"language": "en-US",
"username": "max"
},
"name": "webhook_test",
"full_name": "max/webhook_test",
"description": "",
"empty": false,
"private": true,
"fork": false,
"parent": null,
"mirror": false,
"size": 48,
"html_url": "https://git.example.com/max/webhook_test",
"ssh_url": "ssh://git@git.example.com/max/webhook_test.git",
"clone_url": "https://git.example.com/max/webhook_test.git",
"website": "",
"stars_count": 0,
"forks_count": 0,
"watchers_count": 1,
"open_issues_count": 0,
"default_branch": "master",
"created_at": "2018-09-04T10:45:23Z",
"updated_at": "2018-09-04T12:10:14Z",
"permissions": {
"admin": false,
"push": false,
"pull": false
}
},
"pusher": {
"id": 1,
"login": "max",
"full_name": "Max Mustermann",
"email": "max@example.com",
"avatar_url": "https://secure.gravatar.com/avatar/c9a5fca94b7fd6f8d4dbab8d1575e4fc?d=identicon",
"language": "en-US",
"username": "max"
},
"sender": {
"id": 1,
"login": "max",
"full_name": "Max Mustermann",
"email": "max@example.com",
"avatar_url": "https://secure.gravatar.com/avatar/c9a5fca94b7fd6f8d4dbab8d1575e4fc?d=identicon",
"language": "en-US",
"username": "max"
}
}
"""
giteaJsonPullRequestPayload = rb""" giteaJsonPullRequestPayload = rb"""
{ {
"secret": "test", "secret": "test",
@ -285,12 +371,46 @@ class TestChangeHookGiteaPush(unittest.TestCase):
change["author"], "Max Mustermann <max@example.com>") change["author"], "Max Mustermann <max@example.com>")
self.assertEqual( self.assertEqual(
change["revision"], '9d7157cc4a137b3e1dfe92750ccfb1bbad239f99') change["revision"], '9d7157cc4a137b3e1dfe92750ccfb1bbad239f99')
self.assertEqual(
calendar.timegm(change["when_timestamp"].utctimetuple()),
1536063014)
self.assertEqual( self.assertEqual(
change["comments"], "TestBranch\n") change["comments"], "TestBranch\n")
self.assertEqual(change["branch"], "feature-branch") self.assertEqual(change["branch"], "feature-branch")
self.assertEqual(change[ self.assertEqual(change[
"revlink"], "https://git.example.com/max/webhook_test/commit/9d7157cc4a137b3e1dfe92750ccfb1bbad239f99") "revlink"], "https://git.example.com/max/webhook_test/commit/9d7157cc4a137b3e1dfe92750ccfb1bbad239f99")
def checkChangesFromPullRequest(self, codebase=None):
self.assertEqual(len(self.changeHook.master.addedChanges), 1)
change = self.changeHook.master.addedChanges[0]
self.assertEqual(change['repository'], 'ssh://git@git.example.com/max/webhook_test.git')
self.assertEqual(
change["author"], "Max Mustermann <max@example.com>")
self.assertEqual(
change["revision"], '9d7157cc4a137b3e1dfe92750ccfb1bbad239f99')
self.assertEqual(
calendar.timegm(change["when_timestamp"].utctimetuple()),
1536063289)
self.assertEqual(
change["comments"], "PR#1: TestPR\n\n")
self.assertEqual(change["branch"], "feature-branch")
self.assertEqual(change[
"revlink"], "https://git.example.com/max/webhook_test/pulls/1")
properties = change["properties"]
self.assertEqual(properties["base_branch"], "master")
self.assertEqual(properties["base_sha"], "7c5de0796c409e7802abe759113d7fc37e0d6578")
self.assertEqual(properties["base_repository"], "https://git.example.com/max/webhook_test.git")
self.assertEqual(properties["base_git_ssh_url"], "ssh://git@git.example.com/max/webhook_test.git")
self.assertEqual(properties["head_branch"], "feature-branch")
self.assertEqual(properties["head_sha"], "9d7157cc4a137b3e1dfe92750ccfb1bbad239f99")
self.assertEqual(properties["head_repository"], "https://git.example.com/max/webhook_test.git")
self.assertEqual(properties["head_git_ssh_url"], "ssh://git@git.example.com/max/webhook_test.git")
self.assertEqual(properties["pr_id"], 8)
self.assertEqual(properties["pr_number"], 1)
@defer.inlineCallbacks @defer.inlineCallbacks
def testPushEvent(self): def testPushEvent(self):
self.request = FakeRequest(content=giteaJsonPushPayload) self.request = FakeRequest(content=giteaJsonPushPayload)
@ -299,3 +419,37 @@ class TestChangeHookGiteaPush(unittest.TestCase):
self.request.received_headers[_HEADER_EVENT_TYPE] = b"push" self.request.received_headers[_HEADER_EVENT_TYPE] = b"push"
res = yield self.request.test_render(self.changeHook) res = yield self.request.test_render(self.changeHook)
self.checkChangesFromPush(res) self.checkChangesFromPush(res)
@defer.inlineCallbacks
def testPullRequestEvent(self):
self.request = FakeRequest(content=giteaJsonPullRequestPayload)
self.request.uri = b'/change_hook/gitea'
self.request.method = b'POST'
self.request.received_headers[_HEADER_EVENT_TYPE] = b"pull_request"
res = yield self.request.test_render(self.changeHook)
self.checkChangesFromPullRequest(res)
class TestChangeHookGiteaSecretPhrase(unittest.TestCase):
def setUp(self):
self.changeHook = change_hook.ChangeHookResource(
dialects={'gitea': {"secret": "test"}},
master=fakeMasterForHooks())
@defer.inlineCallbacks
def testValidSecret(self):
self.request = FakeRequest(content=giteaJsonPushPayload)
self.request.uri = b'/change_hook/gitea'
self.request.method = b'POST'
self.request.received_headers[_HEADER_EVENT_TYPE] = b"push"
yield self.request.test_render(self.changeHook)
self.assertEqual(len(self.changeHook.master.addedChanges), 1)
@defer.inlineCallbacks
def testInvalidSecret(self):
self.request = FakeRequest(content=giteaInvalidSecretPush)
self.request.uri = b'/change_hook/gitea'
self.request.method = b'POST'
self.request.received_headers[_HEADER_EVENT_TYPE] = b"push"
yield self.request.test_render(self.changeHook)
self.assertEqual(len(self.changeHook.master.addedChanges), 0)

View File

@ -70,7 +70,7 @@ class GiteaHandler(BaseHookHandler):
pull_request['number'], pull_request['number'],
pull_request['title'], pull_request['title'],
pull_request['body']), pull_request['body']),
'revision': pull_request['merge_base'], 'revision': head['sha'],
'when_timestamp': timestamp, 'when_timestamp': timestamp,
'branch': head['ref'], 'branch': head['ref'],
'revlink': pull_request['html_url'], 'revlink': pull_request['html_url'],

View File

@ -8,13 +8,13 @@ setup(name='buildbot-gitea',
author='Marvin Pohl', author='Marvin Pohl',
author_email='marvin@lab132.com', author_email='marvin@lab132.com',
url='https://lab132.com', url='https://lab132.com',
packages=['gitea'], packages=['buildbot_gitea'],
install_requires=[ install_requires=[
"buildbot" "buildbot"
], ],
entry_points={ entry_points={
"buildbot.webhooks": [ "buildbot.webhooks": [
"gitea = gitea.webhook:gitea" "gitea = buildbot_gitea.webhook:gitea"
] ]
}, },
) )