|
| 1 | +/** |
| 2 | + * Copyright 2020 The AMP HTML Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS-IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import {createTokenAuth} from '@octokit/auth'; |
| 18 | +import nock from 'nock'; |
| 19 | +import {Octokit} from '@octokit/rest'; |
| 20 | + |
| 21 | +import {GitHub} from '../src/github'; |
| 22 | + |
| 23 | +describe('GitHub interface', () => { |
| 24 | + const githubClient: Octokit = new Octokit({ |
| 25 | + authStrategy: createTokenAuth, |
| 26 | + auth: '_TOKEN_', |
| 27 | + }); |
| 28 | + let github: GitHub; |
| 29 | + |
| 30 | + beforeAll(() => { |
| 31 | + nock.disableNetConnect(); |
| 32 | + }); |
| 33 | + |
| 34 | + afterAll(() => { |
| 35 | + nock.enableNetConnect(); |
| 36 | + }); |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + nock.cleanAll(); |
| 40 | + github = new GitHub(githubClient, 'test_org', 'test_repo'); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + // Fail the test if there were unused nocks. |
| 45 | + if (!nock.isDone()) { |
| 46 | + throw new Error('Not all nock interceptors were used!'); |
| 47 | + } |
| 48 | + nock.cleanAll(); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('addComment', () => { |
| 52 | + it('POSTs /repos/:owner/:repo/issues/:issue_number/comments', async done => { |
| 53 | + nock('https://api.github.com') |
| 54 | + .post('/repos/test_org/test_repo/issues/1337/comments', body => { |
| 55 | + expect(body).toEqual({body: 'Test comment'}); |
| 56 | + return true; |
| 57 | + }) |
| 58 | + .reply(200); |
| 59 | + |
| 60 | + await github.addComment(1337, 'Test comment'); |
| 61 | + done(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('updatePullBody', () => { |
| 66 | + it('PATCHes /repos/:owner/:repo/pulls/:pull_number', async done => { |
| 67 | + nock('https://api.github.com') |
| 68 | + .patch('/repos/test_org/test_repo/pulls/1337', ({body}) => { |
| 69 | + expect(body).toEqual('Test description'); |
| 70 | + return true; |
| 71 | + }) |
| 72 | + .reply(200); |
| 73 | + |
| 74 | + await github.updatePullBody(1337, 'Test description'); |
| 75 | + done(); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('findNewDirectory', () => { |
| 80 | + describe('without regex match', () => { |
| 81 | + it('GETs /repos/:owner/:repo/pulls/:pull/files', async done => { |
| 82 | + nock('https://api.github.com') |
| 83 | + .get('/repos/test_org/test_repo/pulls/1337/files') |
| 84 | + .reply(200, [{filename: 'foo/bar'}, {filename: 'tacos/no/1'}]); |
| 85 | + |
| 86 | + expect(await github.findNewDirectory(1337, /no-match/)).toBeFalsy(); |
| 87 | + |
| 88 | + done(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('GETs (once) /repos/:owner/:repo/pulls/:pull/files', async done => { |
| 92 | + nock('https://api.github.com') |
| 93 | + .get('/repos/test_org/test_repo/pulls/1337/files') |
| 94 | + .once() |
| 95 | + .reply(200, [{filename: 'foo'}]); |
| 96 | + |
| 97 | + expect(await github.findNewDirectory(1337, /no-match/)).toBeFalsy(); |
| 98 | + expect(await github.findNewDirectory(1337, /no-match/)).toBeFalsy(); |
| 99 | + expect(await github.findNewDirectory(1337, /no-match/)).toBeFalsy(); |
| 100 | + |
| 101 | + done(); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('with regex match', () => { |
| 106 | + const pathA = new RegExp('^a/([^/]+)/c'); |
| 107 | + const pathXY = new RegExp('^x/y/([^/]+)/'); |
| 108 | + const finds = ['x/y/added-1/file', 'x/y', 'added-1']; |
| 109 | + |
| 110 | + describe.each([ |
| 111 | + {finds, contents: [404]}, |
| 112 | + {finds, contents: [200, []]}, |
| 113 | + {finds, contents: [200, [{name: 'foo'}, {name: 'bar'}]]}, |
| 114 | + {contents: [200, [{name: 'added-1'}]]}, |
| 115 | + ])( |
| 116 | + 'GETs /repos/:owner/:repo/contents/:path', |
| 117 | + ({ |
| 118 | + contents, |
| 119 | + finds, |
| 120 | + }: { |
| 121 | + finds: string[] | undefined; |
| 122 | + contents: [number, {name: string}[] | undefined]; |
| 123 | + }) => { |
| 124 | + it(`does${!finds ? ' not' : ''} find when replying ${JSON.stringify( |
| 125 | + contents |
| 126 | + )}`, async done => { |
| 127 | + nock('https://api.github.com') |
| 128 | + .get('/repos/test_org/test_repo/pulls/1337/files') |
| 129 | + .reply(200, [ |
| 130 | + {filename: 'a/b/c'}, |
| 131 | + {filename: 'x/y/added-1/file'}, |
| 132 | + ]); |
| 133 | + |
| 134 | + nock('https://api.github.com') |
| 135 | + .get('/repos/test_org/test_repo/contents/x/y') |
| 136 | + .reply(...contents); |
| 137 | + |
| 138 | + const result = await github.findNewDirectory(1337, pathXY); |
| 139 | + expect(result).toEqual(finds); |
| 140 | + |
| 141 | + done(); |
| 142 | + }); |
| 143 | + } |
| 144 | + ); |
| 145 | + |
| 146 | + it('GETs (once) /repos/:owner/:repo/contents/:path', async done => { |
| 147 | + nock('https://api.github.com') |
| 148 | + .get('/repos/test_org/test_repo/pulls/1337/files') |
| 149 | + .reply(200, [{filename: 'a/b/c'}, {filename: 'x/y/added-1/file'}]); |
| 150 | + |
| 151 | + nock('https://api.github.com') |
| 152 | + .get('/repos/test_org/test_repo/contents/x/y') |
| 153 | + .once() |
| 154 | + .reply(200, []); |
| 155 | + |
| 156 | + expect(await github.findNewDirectory(1337, pathXY)).toEqual(finds); |
| 157 | + expect(await github.findNewDirectory(1337, pathXY)).toEqual(finds); |
| 158 | + expect(await github.findNewDirectory(1337, pathXY)).toEqual(finds); |
| 159 | + |
| 160 | + nock('https://api.github.com') |
| 161 | + .get('/repos/test_org/test_repo/contents/a') |
| 162 | + .once() |
| 163 | + .reply(200, [{name: 'b'}]); |
| 164 | + |
| 165 | + expect(await github.findNewDirectory(1337, pathA)).toEqual(undefined); |
| 166 | + expect(await github.findNewDirectory(1337, pathA)).toEqual(undefined); |
| 167 | + expect(await github.findNewDirectory(1337, pathA)).toEqual(undefined); |
| 168 | + |
| 169 | + done(); |
| 170 | + }); |
| 171 | + }); |
| 172 | + }); |
| 173 | +}); |
0 commit comments