Skip to content

Commit 1efd682

Browse files
committed
test: GitHubPush
1 parent 6d0eb69 commit 1efd682

File tree

2 files changed

+267
-1
lines changed

2 files changed

+267
-1
lines changed

src/app/app_github_test.go

Lines changed: 265 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package app
22

33
import (
4+
"github.com/erikstmartin/go-testdb"
5+
"github.com/postgres-ci/app-server/src/app/models/webhooks/common"
46
"github.com/stretchr/testify/assert"
57

68
"bytes"
9+
"database/sql/driver"
710
"encoding/json"
11+
"fmt"
812
"net/http"
913
"net/http/httptest"
14+
"strings"
1015
"testing"
1116
)
1217

@@ -33,8 +38,83 @@ func TestGitHubPing(t *testing.T) {
3338
}
3439
}
3540

41+
func TestGitHubPush(t *testing.T) {
42+
43+
server := httptest.NewServer(CreateApp())
44+
45+
req, _ := http.NewRequest("POST", server.URL+"/webhooks/github/", bytes.NewReader([]byte(PushJson)))
46+
req.Header.Set("X-GitHub-Event", "push")
47+
req.Header.Set("Content-Type", "application/json")
48+
req.Header.Set("X-Hub-Signature", PushJson)
49+
50+
testdb.SetExecWithArgsFunc(func(query string, args []driver.Value) (driver.Result, error) {
51+
52+
if strings.Contains(query, "hook.github_push") {
53+
54+
if assert.Len(t, args, 3) {
55+
56+
assert.Equal(t, "postgres-ci/http200ok", args[0].(string))
57+
58+
assert.Equal(t, "master", args[1].(string))
59+
60+
var commits []common.Commit
61+
62+
if err := json.Unmarshal(args[2].([]byte), &commits); assert.NoError(t, err) {
63+
64+
if assert.Len(t, commits, 1) {
65+
66+
assert.Equal(t, "76df967b1e53a66a2988381004c5c4d46f89e87a", commits[0].SHA)
67+
}
68+
}
69+
70+
return testdb.NewResult(0, nil, 0, nil), nil
71+
}
72+
}
73+
74+
return nil, fmt.Errorf("SQL_ERROR")
75+
})
76+
77+
testdb.SetQueryWithArgsFunc(func(query string, args []driver.Value) (result driver.Rows, err error) {
78+
79+
if strings.Contains(query, "project.get_github_secret") {
80+
81+
if assert.Len(t, args, 1) {
82+
83+
assert.Equal(t, "postgres-ci/http200ok", args[0].(string))
84+
}
85+
86+
var secret [][]driver.Value
87+
88+
secret = append(secret, []driver.Value{""})
89+
90+
return testdb.RowsFromSlice(
91+
[]string{"secret"},
92+
secret,
93+
), nil
94+
}
95+
96+
return nil, fmt.Errorf("SQL_ERROR")
97+
})
98+
99+
if response, err := (&http.Client{}).Do(req); assert.NoError(t, err) {
100+
101+
if assert.Equal(t, http.StatusOK, response.StatusCode) {
102+
103+
var resp struct {
104+
Success bool `json:"success"`
105+
}
106+
107+
if err := json.NewDecoder(response.Body).Decode(&resp); assert.NoError(t, err) {
108+
109+
assert.True(t, resp.Success)
110+
}
111+
}
112+
}
113+
}
114+
36115
const (
37-
PingSha = "27577ef3f3d983a7c34c4863cd3a36ad8a3ee128"
116+
PingSha = "sha1=27577ef3f3d983a7c34c4863cd3a36ad8a3ee128"
117+
PushSha = "sha1=5067b0b79d686d09d7222ead1ffacfdb4a6b0364"
38118
PingJson = `
39119
{
40120
"zen": "Non-blocking is better than blocking.",
@@ -173,4 +253,188 @@ const (
173253
}
174254
}
175255
`
256+
PushJson = `
257+
{
258+
"ref": "refs/heads/master",
259+
"before": "e1332340ca1c3ecf76b396b642dfae0cf65df8ce",
260+
"after": "76df967b1e53a66a2988381004c5c4d46f89e87a",
261+
"created": false,
262+
"deleted": false,
263+
"forced": false,
264+
"base_ref": null,
265+
"compare": "https://github.com/postgres-ci/http200ok/compare/e1332340ca1c...76df967b1e53",
266+
"commits": [
267+
{
268+
"id": "76df967b1e53a66a2988381004c5c4d46f89e87a",
269+
"tree_id": "e4c5dfd5d8effa0e0000df685a0079799b8c86e6",
270+
"distinct": true,
271+
"message": "add .postgres-ci.yaml",
272+
"timestamp": "2016-05-30T09:50:58+03:00",
273+
"url": "https://github.com/postgres-ci/http200ok/commit/76df967b1e53a66a2988381004c5c4d46f89e87a",
274+
"author": {
275+
"name": "kshvakov",
276+
"email": "shvakov@gmail.com",
277+
"username": "kshvakov"
278+
},
279+
"committer": {
280+
"name": "kshvakov",
281+
"email": "shvakov@gmail.com",
282+
"username": "kshvakov"
283+
},
284+
"added": [
285+
".postgres-ci.yaml",
286+
"test_setup.sh"
287+
],
288+
"removed": [
289+
290+
],
291+
"modified": [
292+
".gitignore",
293+
"vendor/manifest"
294+
]
295+
}
296+
],
297+
"head_commit": {
298+
"id": "76df967b1e53a66a2988381004c5c4d46f89e87a",
299+
"tree_id": "e4c5dfd5d8effa0e0000df685a0079799b8c86e6",
300+
"distinct": true,
301+
"message": "add .postgres-ci.yaml",
302+
"timestamp": "2016-05-30T09:50:58+03:00",
303+
"url": "https://github.com/postgres-ci/http200ok/commit/76df967b1e53a66a2988381004c5c4d46f89e87a",
304+
"author": {
305+
"name": "kshvakov",
306+
"email": "shvakov@gmail.com",
307+
"username": "kshvakov"
308+
},
309+
"committer": {
310+
"name": "kshvakov",
311+
"email": "shvakov@gmail.com",
312+
"username": "kshvakov"
313+
},
314+
"added": [
315+
".postgres-ci.yaml",
316+
"test_setup.sh"
317+
],
318+
"removed": [
319+
320+
],
321+
"modified": [
322+
".gitignore",
323+
"vendor/manifest"
324+
]
325+
},
326+
"repository": {
327+
"id": 56714132,
328+
"name": "http200ok",
329+
"full_name": "postgres-ci/http200ok",
330+
"owner": {
331+
"name": "postgres-ci",
332+
"email": ""
333+
},
334+
"private": false,
335+
"html_url": "https://github.com/postgres-ci/http200ok",
336+
"description": "",
337+
"fork": false,
338+
"url": "https://github.com/postgres-ci/http200ok",
339+
"forks_url": "https://api.github.com/repos/postgres-ci/http200ok/forks",
340+
"keys_url": "https://api.github.com/repos/postgres-ci/http200ok/keys{/key_id}",
341+
"collaborators_url": "https://api.github.com/repos/postgres-ci/http200ok/collaborators{/collaborator}",
342+
"teams_url": "https://api.github.com/repos/postgres-ci/http200ok/teams",
343+
"hooks_url": "https://api.github.com/repos/postgres-ci/http200ok/hooks",
344+
"issue_events_url": "https://api.github.com/repos/postgres-ci/http200ok/issues/events{/number}",
345+
"events_url": "https://api.github.com/repos/postgres-ci/http200ok/events",
346+
"assignees_url": "https://api.github.com/repos/postgres-ci/http200ok/assignees{/user}",
347+
"branches_url": "https://api.github.com/repos/postgres-ci/http200ok/branches{/branch}",
348+
"tags_url": "https://api.github.com/repos/postgres-ci/http200ok/tags",
349+
"blobs_url": "https://api.github.com/repos/postgres-ci/http200ok/git/blobs{/sha}",
350+
"git_tags_url": "https://api.github.com/repos/postgres-ci/http200ok/git/tags{/sha}",
351+
"git_refs_url": "https://api.github.com/repos/postgres-ci/http200ok/git/refs{/sha}",
352+
"trees_url": "https://api.github.com/repos/postgres-ci/http200ok/git/trees{/sha}",
353+
"statuses_url": "https://api.github.com/repos/postgres-ci/http200ok/statuses/{sha}",
354+
"languages_url": "https://api.github.com/repos/postgres-ci/http200ok/languages",
355+
"stargazers_url": "https://api.github.com/repos/postgres-ci/http200ok/stargazers",
356+
"contributors_url": "https://api.github.com/repos/postgres-ci/http200ok/contributors",
357+
"subscribers_url": "https://api.github.com/repos/postgres-ci/http200ok/subscribers",
358+
"subscription_url": "https://api.github.com/repos/postgres-ci/http200ok/subscription",
359+
"commits_url": "https://api.github.com/repos/postgres-ci/http200ok/commits{/sha}",
360+
"git_commits_url": "https://api.github.com/repos/postgres-ci/http200ok/git/commits{/sha}",
361+
"comments_url": "https://api.github.com/repos/postgres-ci/http200ok/comments{/number}",
362+
"issue_comment_url": "https://api.github.com/repos/postgres-ci/http200ok/issues/comments{/number}",
363+
"contents_url": "https://api.github.com/repos/postgres-ci/http200ok/contents/{+path}",
364+
"compare_url": "https://api.github.com/repos/postgres-ci/http200ok/compare/{base}...{head}",
365+
"merges_url": "https://api.github.com/repos/postgres-ci/http200ok/merges",
366+
"archive_url": "https://api.github.com/repos/postgres-ci/http200ok/{archive_format}{/ref}",
367+
"downloads_url": "https://api.github.com/repos/postgres-ci/http200ok/downloads",
368+
"issues_url": "https://api.github.com/repos/postgres-ci/http200ok/issues{/number}",
369+
"pulls_url": "https://api.github.com/repos/postgres-ci/http200ok/pulls{/number}",
370+
"milestones_url": "https://api.github.com/repos/postgres-ci/http200ok/milestones{/number}",
371+
"notifications_url": "https://api.github.com/repos/postgres-ci/http200ok/notifications{?since,all,participating}",
372+
"labels_url": "https://api.github.com/repos/postgres-ci/http200ok/labels{/name}",
373+
"releases_url": "https://api.github.com/repos/postgres-ci/http200ok/releases{/id}",
374+
"deployments_url": "https://api.github.com/repos/postgres-ci/http200ok/deployments",
375+
"created_at": 1461179409,
376+
"updated_at": "2016-05-17T21:32:50Z",
377+
"pushed_at": 1464591866,
378+
"git_url": "git://github.com/postgres-ci/http200ok.git",
379+
"ssh_url": "git@github.com:postgres-ci/http200ok.git",
380+
"clone_url": "https://github.com/postgres-ci/http200ok.git",
381+
"svn_url": "https://github.com/postgres-ci/http200ok",
382+
"homepage": null,
383+
"size": 131,
384+
"stargazers_count": 0,
385+
"watchers_count": 0,
386+
"language": "Go",
387+
"has_issues": false,
388+
"has_downloads": true,
389+
"has_wiki": false,
390+
"has_pages": false,
391+
"forks_count": 0,
392+
"mirror_url": null,
393+
"open_issues_count": 0,
394+
"forks": 0,
395+
"open_issues": 0,
396+
"watchers": 0,
397+
"default_branch": "master",
398+
"stargazers": 0,
399+
"master_branch": "master",
400+
"organization": "postgres-ci"
401+
},
402+
"pusher": {
403+
"name": "kshvakov",
404+
"email": "shvakov@gmail.com"
405+
},
406+
"organization": {
407+
"login": "postgres-ci",
408+
"id": 16963162,
409+
"url": "https://api.github.com/orgs/postgres-ci",
410+
"repos_url": "https://api.github.com/orgs/postgres-ci/repos",
411+
"events_url": "https://api.github.com/orgs/postgres-ci/events",
412+
"hooks_url": "https://api.github.com/orgs/postgres-ci/hooks",
413+
"issues_url": "https://api.github.com/orgs/postgres-ci/issues",
414+
"members_url": "https://api.github.com/orgs/postgres-ci/members{/member}",
415+
"public_members_url": "https://api.github.com/orgs/postgres-ci/public_members{/member}",
416+
"avatar_url": "https://avatars.githubusercontent.com/u/16963162?v=3",
417+
"description": ""
418+
},
419+
"sender": {
420+
"login": "kshvakov",
421+
"id": 978534,
422+
"avatar_url": "https://avatars.githubusercontent.com/u/978534?v=3",
423+
"gravatar_id": "",
424+
"url": "https://api.github.com/users/kshvakov",
425+
"html_url": "https://github.com/kshvakov",
426+
"followers_url": "https://api.github.com/users/kshvakov/followers",
427+
"following_url": "https://api.github.com/users/kshvakov/following{/other_user}",
428+
"gists_url": "https://api.github.com/users/kshvakov/gists{/gist_id}",
429+
"starred_url": "https://api.github.com/users/kshvakov/starred{/owner}{/repo}",
430+
"subscriptions_url": "https://api.github.com/users/kshvakov/subscriptions",
431+
"organizations_url": "https://api.github.com/users/kshvakov/orgs",
432+
"repos_url": "https://api.github.com/users/kshvakov/repos",
433+
"events_url": "https://api.github.com/users/kshvakov/events{/privacy}",
434+
"received_events_url": "https://api.github.com/users/kshvakov/received_events",
435+
"type": "User",
436+
"site_admin": false
437+
}
438+
}
439+
`
176440
)

src/app/controllers/webhooks/github.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ func githubHandler(c *http200ok.Context) {
5050
if err != nil {
5151

5252
if errors.IsNotFound(err) {
53+
5354
render.JSONError(c, http.StatusNotFound, "Project not nound")
5455
} else {
56+
5557
render.JSONError(c, http.StatusInternalServerError, err.Error())
5658
}
5759

0 commit comments

Comments
 (0)