55from unittest .mock import Mock
66
77
8- from github_webhook .webhook import Webhook
8+ from github_webhook import Webhook
9+ from flask import Flask
10+
11+
12+ # From https://developer.github.com/v3/activity/events/types/#pushevent
13+ example_push_event = {
14+ "ref" : "refs/tags/simple-tag" ,
15+ "before" : "a10867b14bb761a232cd80139fbd4c0d33264240" ,
16+ "after" : "0000000000000000000000000000000000000000" ,
17+ "created" : False ,
18+ "deleted" : True ,
19+ "forced" : False ,
20+ "base_ref" : None ,
21+ "compare" : "https://github.com/Codertocat/Hello-World/compare/a10867b14bb7...000000000000" ,
22+ "commits" : [
23+
24+ ],
25+ "head_commit" : None ,
26+ "repository" : {
27+ "id" : 135493233 ,
28+ "node_id" : "MDEwOlJlcG9zaXRvcnkxMzU0OTMyMzM=" ,
29+ "name" : "Hello-World" ,
30+ "full_name" : "Codertocat/Hello-World" ,
31+ "owner" : {
32+ "name" : "Codertocat" ,
33+ "email" : "21031067+Codertocat@users.noreply.github.com" ,
34+ "login" : "Codertocat" ,
35+ "id" : 21031067 ,
36+ "node_id" : "MDQ6VXNlcjIxMDMxMDY3" ,
37+ "avatar_url" : "https://avatars1.githubusercontent.com/u/21031067?v=4" ,
38+ "gravatar_id" : "" ,
39+ "url" : "https://api.github.com/users/Codertocat" ,
40+ "html_url" : "https://github.com/Codertocat" ,
41+ "followers_url" : "https://api.github.com/users/Codertocat/followers" ,
42+ "following_url" : "https://api.github.com/users/Codertocat/following{/other_user}" ,
43+ "gists_url" : "https://api.github.com/users/Codertocat/gists{/gist_id}" ,
44+ "starred_url" : "https://api.github.com/users/Codertocat/starred{/owner}{/repo}" ,
45+ "subscriptions_url" : "https://api.github.com/users/Codertocat/subscriptions" ,
46+ "organizations_url" : "https://api.github.com/users/Codertocat/orgs" ,
47+ "repos_url" : "https://api.github.com/users/Codertocat/repos" ,
48+ "events_url" : "https://api.github.com/users/Codertocat/events{/privacy}" ,
49+ "received_events_url" : "https://api.github.com/users/Codertocat/received_events" ,
50+ "type" : "User" ,
51+ "site_admin" : False
52+ },
53+ "private" : False ,
54+ "html_url" : "https://github.com/Codertocat/Hello-World" ,
55+ "description" : None ,
56+ "fork" : False ,
57+ "url" : "https://github.com/Codertocat/Hello-World" ,
58+ "forks_url" : "https://api.github.com/repos/Codertocat/Hello-World/forks" ,
59+ "keys_url" : "https://api.github.com/repos/Codertocat/Hello-World/keys{/key_id}" ,
60+ "collaborators_url" : "https://api.github.com/repos/Codertocat/Hello-World/collaborators{/collaborator}" ,
61+ "teams_url" : "https://api.github.com/repos/Codertocat/Hello-World/teams" ,
62+ "hooks_url" : "https://api.github.com/repos/Codertocat/Hello-World/hooks" ,
63+ "issue_events_url" : "https://api.github.com/repos/Codertocat/Hello-World/issues/events{/number}" ,
64+ "events_url" : "https://api.github.com/repos/Codertocat/Hello-World/events" ,
65+ "assignees_url" : "https://api.github.com/repos/Codertocat/Hello-World/assignees{/user}" ,
66+ "branches_url" : "https://api.github.com/repos/Codertocat/Hello-World/branches{/branch}" ,
67+ "tags_url" : "https://api.github.com/repos/Codertocat/Hello-World/tags" ,
68+ "blobs_url" : "https://api.github.com/repos/Codertocat/Hello-World/git/blobs{/sha}" ,
69+ "git_tags_url" : "https://api.github.com/repos/Codertocat/Hello-World/git/tags{/sha}" ,
70+ "git_refs_url" : "https://api.github.com/repos/Codertocat/Hello-World/git/refs{/sha}" ,
71+ "trees_url" : "https://api.github.com/repos/Codertocat/Hello-World/git/trees{/sha}" ,
72+ "statuses_url" : "https://api.github.com/repos/Codertocat/Hello-World/statuses/{sha}" ,
73+ "languages_url" : "https://api.github.com/repos/Codertocat/Hello-World/languages" ,
74+ "stargazers_url" : "https://api.github.com/repos/Codertocat/Hello-World/stargazers" ,
75+ "contributors_url" : "https://api.github.com/repos/Codertocat/Hello-World/contributors" ,
76+ "subscribers_url" : "https://api.github.com/repos/Codertocat/Hello-World/subscribers" ,
77+ "subscription_url" : "https://api.github.com/repos/Codertocat/Hello-World/subscription" ,
78+ "commits_url" : "https://api.github.com/repos/Codertocat/Hello-World/commits{/sha}" ,
79+ "git_commits_url" : "https://api.github.com/repos/Codertocat/Hello-World/git/commits{/sha}" ,
80+ "comments_url" : "https://api.github.com/repos/Codertocat/Hello-World/comments{/number}" ,
81+ "issue_comment_url" : "https://api.github.com/repos/Codertocat/Hello-World/issues/comments{/number}" ,
82+ "contents_url" : "https://api.github.com/repos/Codertocat/Hello-World/contents/{+path}" ,
83+ "compare_url" : "https://api.github.com/repos/Codertocat/Hello-World/compare/{base}...{head}" ,
84+ "merges_url" : "https://api.github.com/repos/Codertocat/Hello-World/merges" ,
85+ "archive_url" : "https://api.github.com/repos/Codertocat/Hello-World/{archive_format}{/ref}" ,
86+ "downloads_url" : "https://api.github.com/repos/Codertocat/Hello-World/downloads" ,
87+ "issues_url" : "https://api.github.com/repos/Codertocat/Hello-World/issues{/number}" ,
88+ "pulls_url" : "https://api.github.com/repos/Codertocat/Hello-World/pulls{/number}" ,
89+ "milestones_url" : "https://api.github.com/repos/Codertocat/Hello-World/milestones{/number}" ,
90+ "notifications_url" : "https://api.github.com/repos/Codertocat/Hello-World/notifications{?since,all,participating}" ,
91+ "labels_url" : "https://api.github.com/repos/Codertocat/Hello-World/labels{/name}" ,
92+ "releases_url" : "https://api.github.com/repos/Codertocat/Hello-World/releases{/id}" ,
93+ "deployments_url" : "https://api.github.com/repos/Codertocat/Hello-World/deployments" ,
94+ "created_at" : 1527711484 ,
95+ "updated_at" : "2018-05-30T20:18:35Z" ,
96+ "pushed_at" : 1527711528 ,
97+ "git_url" : "git://github.com/Codertocat/Hello-World.git" ,
98+ "ssh_url" : "git@github.com:Codertocat/Hello-World.git" ,
99+ "clone_url" : "https://github.com/Codertocat/Hello-World.git" ,
100+ "svn_url" : "https://github.com/Codertocat/Hello-World" ,
101+ "homepage" : None ,
102+ "size" : 0 ,
103+ "stargazers_count" : 0 ,
104+ "watchers_count" : 0 ,
105+ "language" : None ,
106+ "has_issues" : True ,
107+ "has_projects" : True ,
108+ "has_downloads" : True ,
109+ "has_wiki" : True ,
110+ "has_pages" : True ,
111+ "forks_count" : 0 ,
112+ "mirror_url" : None ,
113+ "archived" : False ,
114+ "open_issues_count" : 2 ,
115+ "license" : None ,
116+ "forks" : 0 ,
117+ "open_issues" : 2 ,
118+ "watchers" : 0 ,
119+ "default_branch" : "master" ,
120+ "stargazers" : 0 ,
121+ "master_branch" : "master"
122+ },
123+ "pusher" : {
124+ "name" : "Codertocat" ,
125+ "email" : "21031067+Codertocat@users.noreply.github.com"
126+ },
127+ "sender" : {
128+ "login" : "Codertocat" ,
129+ "id" : 21031067 ,
130+ "node_id" : "MDQ6VXNlcjIxMDMxMDY3" ,
131+ "avatar_url" : "https://avatars1.githubusercontent.com/u/21031067?v=4" ,
132+ "gravatar_id" : "" ,
133+ "url" : "https://api.github.com/users/Codertocat" ,
134+ "html_url" : "https://github.com/Codertocat" ,
135+ "followers_url" : "https://api.github.com/users/Codertocat/followers" ,
136+ "following_url" : "https://api.github.com/users/Codertocat/following{/other_user}" ,
137+ "gists_url" : "https://api.github.com/users/Codertocat/gists{/gist_id}" ,
138+ "starred_url" : "https://api.github.com/users/Codertocat/starred{/owner}{/repo}" ,
139+ "subscriptions_url" : "https://api.github.com/users/Codertocat/subscriptions" ,
140+ "organizations_url" : "https://api.github.com/users/Codertocat/orgs" ,
141+ "repos_url" : "https://api.github.com/users/Codertocat/repos" ,
142+ "events_url" : "https://api.github.com/users/Codertocat/events{/privacy}" ,
143+ "received_events_url" : "https://api.github.com/users/Codertocat/received_events" ,
144+ "type" : "User" ,
145+ "site_admin" : False
146+ }
147+ }
9148
10149
11150class TestWebhook (unittest .TestCase ):
@@ -25,6 +164,27 @@ def test_constructor(self):
25164 )
26165
27166
167+ def test_push_event (self ):
168+ # GIVEN
169+ app = Flask (__name__ ) # Standard Flask app
170+ webhook = Webhook (app ) # Defines '/postreceive' endpoint
171+
172+ @webhook .hook () # Defines a handler for the 'push' event
173+ def on_push (data ):
174+ flag = data ["repository" ]["full_name" ] == "Codertocat/Hello-World"
175+ if not flag :
176+ return "Event data does not match expected data" , 400
177+
178+ # WHEN
179+ resp = None
180+ with app .test_client () as client :
181+ resp = client .post ('/postreceive' ,
182+ json = example_push_event ,
183+ headers = {"X-Github-Event" : "push" , "X-Github-Delivery" : 0 })
184+
185+ # THEN
186+ assert resp .status_code == 204
187+
28188# -----------------------------------------------------------------------------
29189# Copyright 2015 Bloomberg Finance L.P.
30190#
0 commit comments