11const index = require ( '../GitForWindowsHelper/index' )
2+ const crypto = require ( 'crypto' )
23
34process . env [ 'GITHUB_WEBHOOK_SECRET' ] = 'for-testing'
45
@@ -41,4 +42,89 @@ test('reject requests other than webhook payloads', async () => {
4142 context . req . headers [ 'x-hub-signature-256' ] = 'sha256=incorrect'
4243 context . req . rawBody = '# empty'
4344 await expectInvalidWebhook ( 'Incorrect X-Hub-Signature' )
44- } )
45+ } )
46+
47+ let mockGetInstallationAccessToken = jest . fn ( ( ) => 'installation-access-token' )
48+ jest . mock ( '../GitForWindowsHelper/get-installation-access-token' , ( ) => {
49+ return mockGetInstallationAccessToken
50+ } )
51+
52+ let mockGitHubApiRequestAsApp = jest . fn ( )
53+ jest . mock ( '../GitForWindowsHelper/github-api-request-as-app' , ( ) => {
54+ return mockGitHubApiRequestAsApp
55+ } )
56+
57+ let mockGitHubApiRequest = jest . fn ( ( _context , _token , method , requestPath , payload ) => {
58+ if ( method === 'POST' && requestPath . endsWith ( '/comments' ) ) return {
59+ id : - 124 ,
60+ html_url : `new-comment-url-${ payload . body } `
61+ }
62+ } )
63+ jest . mock ( '../GitForWindowsHelper/github-api-request' , ( ) => {
64+ return mockGitHubApiRequest
65+ } )
66+
67+ const makeContext = ( body , headers ) => {
68+ const rawBody = JSON . stringify ( body )
69+ const sha256 = crypto . createHmac ( 'sha256' , process . env [ 'GITHUB_WEBHOOK_SECRET' ] ) . update ( rawBody ) . digest ( 'hex' )
70+ return {
71+ log : jest . fn ( ) ,
72+ req : {
73+ body,
74+ headers : {
75+ 'content-type' : 'application/json' ,
76+ 'x-hub-signature-256' : `sha256=${ sha256 } ` ,
77+ ...headers || { }
78+ } ,
79+ method : 'POST' ,
80+ rawBody
81+ }
82+ }
83+ }
84+
85+ const mockIssueComment = ( comment ) => {
86+ return makeContext ( {
87+ action : 'created' ,
88+ comment : {
89+ body : comment ,
90+ html_url : 'https://github.com/git-for-windows/git/issues/0' ,
91+ id : 0 ,
92+ user : {
93+ login : 'statler and waldorf'
94+ }
95+ } ,
96+ installation : {
97+ id : 123
98+ } ,
99+ issue : {
100+ number : 0
101+ } ,
102+ repository : {
103+ name : 'git' ,
104+ owner : {
105+ login : 'git-for-windows'
106+ }
107+ }
108+ } , {
109+ 'x-github-event' : 'issue_comment'
110+ } )
111+ }
112+
113+ test ( 'test /hi' , async ( ) => {
114+ const context = mockIssueComment ( '/hi' )
115+ expect ( await index ( context , context . req ) ) . toBeUndefined ( )
116+ expect ( context . res ) . toEqual ( {
117+ body : 'I said hi! new-comment-url-Hi @statler and waldorf!' ,
118+ headers : undefined ,
119+ status : undefined
120+ } )
121+ expect ( mockGetInstallationAccessToken ) . toHaveBeenCalledTimes ( 1 )
122+ expect ( mockGitHubApiRequestAsApp ) . not . toHaveBeenCalled ( )
123+ expect ( mockGitHubApiRequest ) . toHaveBeenCalledTimes ( 1 )
124+ expect ( mockGitHubApiRequest . mock . calls [ 0 ] . slice ( 1 ) ) . toEqual ( [
125+ "installation-access-token" ,
126+ "POST" ,
127+ "/repos/git-for-windows/git/issues/0/comments" ,
128+ { "body" : "Hi @statler and waldorf!" }
129+ ] )
130+ } )
0 commit comments