@@ -34,12 +34,15 @@ describe('index', () => {
3434 githubMock = {
3535 authenticate : sinon . stub ( ) ,
3636 repos : {
37+ createHook : sinon . stub ( ) ,
3738 createStatus : sinon . stub ( ) ,
39+ editHook : sinon . stub ( ) ,
3840 get : sinon . stub ( ) ,
3941 getBranch : sinon . stub ( ) ,
4042 getById : sinon . stub ( ) ,
4143 getCommit : sinon . stub ( ) ,
42- getContent : sinon . stub ( )
44+ getContent : sinon . stub ( ) ,
45+ getHooks : sinon . stub ( )
4346 } ,
4447 users : {
4548 getForUser : sinon . stub ( )
@@ -1179,4 +1182,150 @@ jobs:
11791182 } ) ;
11801183 } ) ;
11811184 } ) ;
1185+
1186+ describe ( '_addWebhook' , ( ) => {
1187+ const webhookConfig = {
1188+ scmUri : 'github.com:1263:branchName' ,
1189+ token : 'fakeToken' ,
1190+ webhookUrl : 'https://somewhere.in/the/interwebs'
1191+ } ;
1192+
1193+ beforeEach ( ( ) => {
1194+ githubMock . repos . getById . yieldsAsync ( null , {
1195+ full_name : 'dolores/violentdelights'
1196+ } ) ;
1197+ githubMock . repos . getHooks . yieldsAsync ( null , [ {
1198+ config : { url : 'https://somewhere.in/the/interwebs' } ,
1199+ id : 783150
1200+ } ] ) ;
1201+ } ) ;
1202+
1203+ it ( 'add a hook' , ( ) => {
1204+ githubMock . repos . getHooks . yieldsAsync ( null , [ ] ) ;
1205+ githubMock . repos . createHook . yieldsAsync ( null ) ;
1206+
1207+ /* eslint-disable no-underscore-dangle */
1208+ return scm . _addWebhook ( webhookConfig ) . then ( ( ) => {
1209+ /* eslint-enable no-underscore-dangle */
1210+ assert . calledWith ( githubMock . authenticate , sinon . match ( {
1211+ token : 'fakeToken'
1212+ } ) ) ;
1213+ assert . calledWith ( githubMock . repos . getById , {
1214+ id : '1263'
1215+ } ) ;
1216+ assert . calledWith ( githubMock . repos . createHook , {
1217+ active : true ,
1218+ config : {
1219+ content_type : 'json' ,
1220+ secret : 'somesecret' ,
1221+ url : 'https://somewhere.in/the/interwebs'
1222+ } ,
1223+ events : [ 'push' , 'pull_request' ] ,
1224+ name : 'web' ,
1225+ owner : 'dolores' ,
1226+ repo : 'violentdelights'
1227+ } ) ;
1228+ } ) ;
1229+ } ) ;
1230+
1231+ it ( 'updates a pre-existing hook' , ( ) => {
1232+ githubMock . repos . editHook . yieldsAsync ( null ) ;
1233+
1234+ /* eslint-disable no-underscore-dangle */
1235+ return scm . _addWebhook ( webhookConfig ) . then ( ( ) => {
1236+ /* eslint-enable no-underscore-dangle */
1237+ assert . calledWith ( githubMock . repos . getHooks , {
1238+ owner : 'dolores' ,
1239+ repo : 'violentdelights' ,
1240+ page : 1 ,
1241+ per_page : 30
1242+ } ) ;
1243+ assert . calledWith ( githubMock . repos . editHook , {
1244+ active : true ,
1245+ config : {
1246+ content_type : 'json' ,
1247+ secret : 'somesecret' ,
1248+ url : 'https://somewhere.in/the/interwebs'
1249+ } ,
1250+ events : [ 'push' , 'pull_request' ] ,
1251+ id : 783150 ,
1252+ name : 'web' ,
1253+ owner : 'dolores' ,
1254+ repo : 'violentdelights'
1255+ } ) ;
1256+ } ) ;
1257+ } ) ;
1258+
1259+ it ( 'updates hook on a repo with a lot of other hooks' , ( ) => {
1260+ const invalidHooks = [ ] ;
1261+
1262+ for ( let i = 0 ; i < 30 ; i += 1 ) {
1263+ invalidHooks . push ( { } ) ;
1264+ }
1265+
1266+ githubMock . repos . getHooks . onCall ( 0 ) . yieldsAsync ( null , invalidHooks ) ;
1267+ githubMock . repos . editHook . yieldsAsync ( null ) ;
1268+
1269+ /* eslint-disable no-underscore-dangle */
1270+ return scm . _addWebhook ( webhookConfig ) . then ( ( ) => {
1271+ /* eslint-enable no-underscore-dangle */
1272+ assert . calledWith ( githubMock . repos . getHooks , {
1273+ owner : 'dolores' ,
1274+ repo : 'violentdelights' ,
1275+ page : 2 ,
1276+ per_page : 30
1277+ } ) ;
1278+ assert . calledWith ( githubMock . repos . editHook , {
1279+ active : true ,
1280+ config : {
1281+ content_type : 'json' ,
1282+ secret : 'somesecret' ,
1283+ url : 'https://somewhere.in/the/interwebs'
1284+ } ,
1285+ events : [ 'push' , 'pull_request' ] ,
1286+ id : 783150 ,
1287+ name : 'web' ,
1288+ owner : 'dolores' ,
1289+ repo : 'violentdelights'
1290+ } ) ;
1291+ } ) ;
1292+ } ) ;
1293+
1294+ it ( 'throws an error when failing to getHooks' , ( ) => {
1295+ const testError = new Error ( 'getHooksError' ) ;
1296+
1297+ githubMock . repos . getHooks . yieldsAsync ( testError ) ;
1298+
1299+ /* eslint-disable no-underscore-dangle */
1300+ return scm . _addWebhook ( webhookConfig ) . then ( assert . fail , ( err ) => {
1301+ /* eslint-enable no-underscore-dangle */
1302+ assert . equal ( err , testError ) ;
1303+ } ) ;
1304+ } ) ;
1305+
1306+ it ( 'throws an error when failing to createHook' , ( ) => {
1307+ const testError = new Error ( 'createHookError' ) ;
1308+
1309+ githubMock . repos . getHooks . yieldsAsync ( null , [ ] ) ;
1310+ githubMock . repos . createHook . yieldsAsync ( testError ) ;
1311+
1312+ /* eslint-disable no-underscore-dangle */
1313+ return scm . _addWebhook ( webhookConfig ) . then ( assert . fail , ( err ) => {
1314+ /* eslint-enable no-underscore-dangle */
1315+ assert . equal ( err , testError ) ;
1316+ } ) ;
1317+ } ) ;
1318+
1319+ it ( 'throws an error when failing to editHook' , ( ) => {
1320+ const testError = new Error ( 'editHookError' ) ;
1321+
1322+ githubMock . repos . editHook . yieldsAsync ( testError ) ;
1323+
1324+ /* eslint-disable no-underscore-dangle */
1325+ return scm . _addWebhook ( webhookConfig ) . then ( assert . fail , ( err ) => {
1326+ /* eslint-enable no-underscore-dangle */
1327+ assert . equal ( err , testError ) ;
1328+ } ) ;
1329+ } ) ;
1330+ } ) ;
11821331} ) ;
0 commit comments