Skip to content

Commit 01688b3

Browse files
committed
Use the example in our README.md as a test
1 parent 95c7381 commit 01688b3

File tree

2 files changed

+163
-4
lines changed

2 files changed

+163
-4
lines changed

github_webhook/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
:license: Apache License, Version 2.0
99
"""
1010

11-
<<<<<<< HEAD
1211
from textwrap import dedent
1312

1413
import sys
@@ -19,9 +18,6 @@
1918
version=sys.version_info[0])))
2019

2120
from github_webhook.webhook import Webhook
22-
=======
23-
from github_webhook.webhook import Webhook # noqa
24-
>>>>>>> origin/master
2521

2622
# -----------------------------------------------------------------------------
2723
# Copyright 2015 Bloomberg Finance L.P.

tests/test_webhook.py

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
import werkzeug
7+
from flask import Flask
78

89
try:
910
from unittest import mock
@@ -128,6 +129,168 @@ def test_request_had_headers(webhook, handler, mock_request):
128129
webhook._postreceive()
129130

130131

132+
# From https://developer.github.com/v3/activity/events/types/#pushevent
133+
example_push_event = {
134+
"ref": "refs/tags/simple-tag",
135+
"before": "a10867b14bb761a232cd80139fbd4c0d33264240",
136+
"after": "0000000000000000000000000000000000000000",
137+
"created": False,
138+
"deleted": True,
139+
"forced": False,
140+
"base_ref": None,
141+
"compare": "https://github.com/Codertocat/Hello-World/compare/a10867b14bb7...000000000000",
142+
"commits": [
143+
144+
],
145+
"head_commit": None,
146+
"repository": {
147+
"id": 135493233,
148+
"node_id": "MDEwOlJlcG9zaXRvcnkxMzU0OTMyMzM=",
149+
"name": "Hello-World",
150+
"full_name": "Codertocat/Hello-World",
151+
"owner": {
152+
"name": "Codertocat",
153+
"email": "21031067+Codertocat@users.noreply.github.com",
154+
"login": "Codertocat",
155+
"id": 21031067,
156+
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
157+
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
158+
"gravatar_id": "",
159+
"url": "https://api.github.com/users/Codertocat",
160+
"html_url": "https://github.com/Codertocat",
161+
"followers_url": "https://api.github.com/users/Codertocat/followers",
162+
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
163+
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
164+
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
165+
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
166+
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
167+
"repos_url": "https://api.github.com/users/Codertocat/repos",
168+
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
169+
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
170+
"type": "User",
171+
"site_admin": False
172+
},
173+
"private": False,
174+
"html_url": "https://github.com/Codertocat/Hello-World",
175+
"description": None,
176+
"fork": False,
177+
"url": "https://github.com/Codertocat/Hello-World",
178+
"forks_url": "https://api.github.com/repos/Codertocat/Hello-World/forks",
179+
"keys_url": "https://api.github.com/repos/Codertocat/Hello-World/keys{/key_id}",
180+
"collaborators_url": "https://api.github.com/repos/Codertocat/Hello-World/collaborators{/collaborator}",
181+
"teams_url": "https://api.github.com/repos/Codertocat/Hello-World/teams",
182+
"hooks_url": "https://api.github.com/repos/Codertocat/Hello-World/hooks",
183+
"issue_events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/events{/number}",
184+
"events_url": "https://api.github.com/repos/Codertocat/Hello-World/events",
185+
"assignees_url": "https://api.github.com/repos/Codertocat/Hello-World/assignees{/user}",
186+
"branches_url": "https://api.github.com/repos/Codertocat/Hello-World/branches{/branch}",
187+
"tags_url": "https://api.github.com/repos/Codertocat/Hello-World/tags",
188+
"blobs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/blobs{/sha}",
189+
"git_tags_url": "https://api.github.com/repos/Codertocat/Hello-World/git/tags{/sha}",
190+
"git_refs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/refs{/sha}",
191+
"trees_url": "https://api.github.com/repos/Codertocat/Hello-World/git/trees{/sha}",
192+
"statuses_url": "https://api.github.com/repos/Codertocat/Hello-World/statuses/{sha}",
193+
"languages_url": "https://api.github.com/repos/Codertocat/Hello-World/languages",
194+
"stargazers_url": "https://api.github.com/repos/Codertocat/Hello-World/stargazers",
195+
"contributors_url": "https://api.github.com/repos/Codertocat/Hello-World/contributors",
196+
"subscribers_url": "https://api.github.com/repos/Codertocat/Hello-World/subscribers",
197+
"subscription_url": "https://api.github.com/repos/Codertocat/Hello-World/subscription",
198+
"commits_url": "https://api.github.com/repos/Codertocat/Hello-World/commits{/sha}",
199+
"git_commits_url": "https://api.github.com/repos/Codertocat/Hello-World/git/commits{/sha}",
200+
"comments_url": "https://api.github.com/repos/Codertocat/Hello-World/comments{/number}",
201+
"issue_comment_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments{/number}",
202+
"contents_url": "https://api.github.com/repos/Codertocat/Hello-World/contents/{+path}",
203+
"compare_url": "https://api.github.com/repos/Codertocat/Hello-World/compare/{base}...{head}",
204+
"merges_url": "https://api.github.com/repos/Codertocat/Hello-World/merges",
205+
"archive_url": "https://api.github.com/repos/Codertocat/Hello-World/{archive_format}{/ref}",
206+
"downloads_url": "https://api.github.com/repos/Codertocat/Hello-World/downloads",
207+
"issues_url": "https://api.github.com/repos/Codertocat/Hello-World/issues{/number}",
208+
"pulls_url": "https://api.github.com/repos/Codertocat/Hello-World/pulls{/number}",
209+
"milestones_url": "https://api.github.com/repos/Codertocat/Hello-World/milestones{/number}",
210+
"notifications_url": "https://api.github.com/repos/Codertocat/Hello-World/notifications{?since,all,participating}",
211+
"labels_url": "https://api.github.com/repos/Codertocat/Hello-World/labels{/name}",
212+
"releases_url": "https://api.github.com/repos/Codertocat/Hello-World/releases{/id}",
213+
"deployments_url": "https://api.github.com/repos/Codertocat/Hello-World/deployments",
214+
"created_at": 1527711484,
215+
"updated_at": "2018-05-30T20:18:35Z",
216+
"pushed_at": 1527711528,
217+
"git_url": "git://github.com/Codertocat/Hello-World.git",
218+
"ssh_url": "git@github.com:Codertocat/Hello-World.git",
219+
"clone_url": "https://github.com/Codertocat/Hello-World.git",
220+
"svn_url": "https://github.com/Codertocat/Hello-World",
221+
"homepage": None,
222+
"size": 0,
223+
"stargazers_count": 0,
224+
"watchers_count": 0,
225+
"language": None,
226+
"has_issues": True,
227+
"has_projects": True,
228+
"has_downloads": True,
229+
"has_wiki": True,
230+
"has_pages": True,
231+
"forks_count": 0,
232+
"mirror_url": None,
233+
"archived": False,
234+
"open_issues_count": 2,
235+
"license": None,
236+
"forks": 0,
237+
"open_issues": 2,
238+
"watchers": 0,
239+
"default_branch": "master",
240+
"stargazers": 0,
241+
"master_branch": "master"
242+
},
243+
"pusher": {
244+
"name": "Codertocat",
245+
"email": "21031067+Codertocat@users.noreply.github.com"
246+
},
247+
"sender": {
248+
"login": "Codertocat",
249+
"id": 21031067,
250+
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
251+
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
252+
"gravatar_id": "",
253+
"url": "https://api.github.com/users/Codertocat",
254+
"html_url": "https://github.com/Codertocat",
255+
"followers_url": "https://api.github.com/users/Codertocat/followers",
256+
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
257+
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
258+
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
259+
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
260+
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
261+
"repos_url": "https://api.github.com/users/Codertocat/repos",
262+
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
263+
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
264+
"type": "User",
265+
"site_admin": False
266+
}
267+
}
268+
269+
def test_push_request():
270+
""" Uses the example event defined in the GitHub documentation to ensure
271+
that our webhook app can receive the event.
272+
"""
273+
274+
# GIVEN
275+
app = Flask(__name__) # Standard Flask app
276+
webhook = Webhook(app) # Defines '/postreceive' endpoint
277+
278+
@webhook.hook() # Defines a handler for the 'push' event
279+
def on_push(data):
280+
flag = data["repository"]["full_name"] == "Codertocat/Hello-World"
281+
if not flag:
282+
return "Event data does not match expected data", 400
283+
284+
# WHEN
285+
resp = None
286+
with app.test_client() as client:
287+
resp = client.post('/postreceive',
288+
json=example_push_event,
289+
headers={"X-Github-Event": "push", "X-Github-Delivery": 0})
290+
291+
# THEN
292+
assert resp.status_code == 204
293+
131294
# -----------------------------------------------------------------------------
132295
# Copyright 2015 Bloomberg Finance L.P.
133296
#

0 commit comments

Comments
 (0)