2017-02-28 14:02:56 -05:00
|
|
|
// Imports
|
2018-05-10 08:56:07 -04:00
|
|
|
import {GithubApi} from '../../lib/common/github-api';
|
2017-06-18 18:15:07 -04:00
|
|
|
import {GithubPullRequests, PullRequest} from '../../lib/common/github-pull-requests';
|
2017-02-28 14:02:56 -05:00
|
|
|
import {GithubTeams} from '../../lib/common/github-teams';
|
2018-08-15 08:47:45 -04:00
|
|
|
import {BuildVerifier} from '../../lib/preview-server/build-verifier';
|
2017-02-28 14:02:56 -05:00
|
|
|
|
|
|
|
// Tests
|
|
|
|
describe('BuildVerifier', () => {
|
|
|
|
const defaultConfig = {
|
|
|
|
allowedTeamSlugs: ['team1', 'team2'],
|
2018-05-10 08:56:07 -04:00
|
|
|
githubOrg: 'organization',
|
|
|
|
githubRepo: 'repo',
|
2017-02-28 14:02:56 -05:00
|
|
|
githubToken: 'githubToken',
|
|
|
|
secret: 'secret',
|
2017-06-18 18:15:07 -04:00
|
|
|
trustedPrLabel: 'trusted: pr-label',
|
2017-02-28 14:02:56 -05:00
|
|
|
};
|
2018-05-10 08:56:07 -04:00
|
|
|
let prs: GithubPullRequests;
|
2017-02-28 14:02:56 -05:00
|
|
|
let bv: BuildVerifier;
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
const createBuildVerifier = (partialConfig: Partial<typeof defaultConfig> = {}) => {
|
2017-06-17 14:03:10 -04:00
|
|
|
const cfg = {...defaultConfig, ...partialConfig} as typeof defaultConfig;
|
2018-05-10 08:56:07 -04:00
|
|
|
const api = new GithubApi(cfg.githubToken);
|
|
|
|
prs = new GithubPullRequests(api, cfg.githubOrg, cfg.githubRepo);
|
|
|
|
const teams = new GithubTeams(api, cfg.githubOrg);
|
|
|
|
return new BuildVerifier(prs, teams, cfg.allowedTeamSlugs, cfg.trustedPrLabel);
|
2017-02-28 14:02:56 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => bv = createBuildVerifier());
|
|
|
|
|
|
|
|
|
|
|
|
describe('constructor()', () => {
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
['githubToken', 'githubRepo', 'githubOrg', 'allowedTeamSlugs', 'trustedPrLabel'].
|
2017-06-18 18:15:07 -04:00
|
|
|
forEach(param => {
|
|
|
|
it(`should throw if '${param}' is missing or empty`, () => {
|
|
|
|
expect(() => createBuildVerifier({[param]: ''})).
|
|
|
|
toThrowError(`Missing or empty required parameter '${param}'!`);
|
|
|
|
});
|
2017-02-28 14:02:56 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should throw if \'allowedTeamSlugs\' is an empty array', () => {
|
|
|
|
expect(() => createBuildVerifier({allowedTeamSlugs: []})).
|
|
|
|
toThrowError('Missing or empty required parameter \'allowedTeamSlugs\'!');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
describe('getSignificantFilesChanged', () => {
|
|
|
|
it('should return false if none of the fetched files match the given pattern', async () => {
|
|
|
|
const fetchFilesSpy = spyOn(prs, 'fetchFiles');
|
2020-05-02 09:14:05 -04:00
|
|
|
fetchFilesSpy.and.callFake(() => Promise.resolve([
|
|
|
|
{filename: 'a/b/c', sha: 'a1'},
|
|
|
|
{filename: 'd/e/f', sha: 'b2'},
|
|
|
|
]));
|
2018-05-10 08:56:07 -04:00
|
|
|
expect(await bv.getSignificantFilesChanged(777, /^x/)).toEqual(false);
|
|
|
|
expect(fetchFilesSpy).toHaveBeenCalledWith(777);
|
|
|
|
|
|
|
|
fetchFilesSpy.calls.reset();
|
|
|
|
expect(await bv.getSignificantFilesChanged(777, /^a/)).toEqual(true);
|
|
|
|
expect(fetchFilesSpy).toHaveBeenCalledWith(777);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-06-18 18:15:07 -04:00
|
|
|
describe('getPrIsTrusted()', () => {
|
|
|
|
const pr = 9;
|
|
|
|
let mockPrInfo: PullRequest;
|
|
|
|
let prsFetchSpy: jasmine.Spy;
|
|
|
|
let teamsIsMemberBySlugSpy: jasmine.Spy;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockPrInfo = {
|
|
|
|
labels: [
|
|
|
|
{name: 'foo'},
|
|
|
|
{name: 'bar'},
|
|
|
|
],
|
|
|
|
number: 9,
|
|
|
|
user: {login: 'username'},
|
|
|
|
};
|
|
|
|
|
|
|
|
prsFetchSpy = spyOn(GithubPullRequests.prototype, 'fetch').
|
2018-05-10 08:56:07 -04:00
|
|
|
and.callFake(() => Promise.resolve(mockPrInfo));
|
2017-06-18 18:15:07 -04:00
|
|
|
|
|
|
|
teamsIsMemberBySlugSpy = spyOn(GithubTeams.prototype, 'isMemberBySlug').
|
2018-05-10 08:56:07 -04:00
|
|
|
and.callFake(() => Promise.resolve(true));
|
2017-06-18 18:15:07 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should return a promise', done => {
|
|
|
|
const promise = bv.getPrIsTrusted(pr);
|
|
|
|
promise.then(done); // Do not complete the test (and release the spies) synchronously
|
|
|
|
// to avoid running the actual `GithubTeams#isMemberBySlug()`.
|
|
|
|
|
|
|
|
expect(promise).toEqual(jasmine.any(Promise));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should fetch the corresponding PR', done => {
|
|
|
|
bv.getPrIsTrusted(pr).then(() => {
|
|
|
|
expect(prsFetchSpy).toHaveBeenCalledWith(pr);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should fail if fetching the PR errors', done => {
|
|
|
|
prsFetchSpy.and.callFake(() => Promise.reject('Test'));
|
|
|
|
bv.getPrIsTrusted(pr).catch(err => {
|
|
|
|
expect(err).toBe('Test');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('when the PR has the "trusted PR" label', () => {
|
|
|
|
|
|
|
|
beforeEach(() => mockPrInfo.labels.push({name: 'trusted: pr-label'}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should resolve to true', done => {
|
|
|
|
bv.getPrIsTrusted(pr).then(isTrusted => {
|
|
|
|
expect(isTrusted).toBe(true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should not try to verify the author\'s membership status', done => {
|
|
|
|
bv.getPrIsTrusted(pr).then(() => {
|
|
|
|
expect(teamsIsMemberBySlugSpy).not.toHaveBeenCalled();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('when the PR does not have the "trusted PR" label', () => {
|
|
|
|
|
|
|
|
it('should verify the PR author\'s membership in the specified teams', done => {
|
|
|
|
bv.getPrIsTrusted(pr).then(() => {
|
|
|
|
expect(teamsIsMemberBySlugSpy).toHaveBeenCalledWith('username', ['team1', 'team2']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should fail if verifying membership errors', done => {
|
|
|
|
teamsIsMemberBySlugSpy.and.callFake(() => Promise.reject('Test'));
|
|
|
|
bv.getPrIsTrusted(pr).catch(err => {
|
|
|
|
expect(err).toBe('Test');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should resolve to true if the PR\'s author is a member', done => {
|
2018-05-10 08:56:07 -04:00
|
|
|
teamsIsMemberBySlugSpy.and.callFake(() => Promise.resolve(true));
|
2017-06-18 18:15:07 -04:00
|
|
|
|
|
|
|
bv.getPrIsTrusted(pr).then(isTrusted => {
|
|
|
|
expect(isTrusted).toBe(true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should resolve to false if the PR\'s author is not a member', done => {
|
2018-05-10 08:56:07 -04:00
|
|
|
teamsIsMemberBySlugSpy.and.callFake(() => Promise.resolve(false));
|
2017-06-18 18:15:07 -04:00
|
|
|
|
|
|
|
bv.getPrIsTrusted(pr).then(isTrusted => {
|
|
|
|
expect(isTrusted).toBe(false);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-02-28 14:02:56 -05:00
|
|
|
});
|