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