1
0
Fork 0

fix(test): begin covering github auth

This commit is contained in:
ssube 2020-08-28 11:02:19 -05:00
parent 9364266c86
commit c16c10e140
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
2 changed files with 54 additions and 5 deletions

View File

@ -16,14 +16,13 @@ export class GithubRemote extends BaseRemote<Octokit, RemoteOptions> implements
}
public async connect(): Promise<boolean> {
this.options.logger.info('connecting to github');
const type = mustExist(this.options.data.type);
this.options.logger.info({ type }, 'connecting to github');
switch (type) {
case 'app':
this.options.logger.info('using app auth');
this.client = new Octokit({
this.client = await this.options.container.create(Octokit, {
auth: {
id: parseInt(mustExist(this.options.data.id), 10),
installationId: parseInt(mustExist(this.options.data.installationId), 10),
@ -34,7 +33,7 @@ export class GithubRemote extends BaseRemote<Octokit, RemoteOptions> implements
break;
case 'token':
this.options.logger.info('using token auth');
this.client = new Octokit({
this.client = await this.options.container.create(Octokit, {
auth: mustExist(this.options.data.token),
});
break;

View File

@ -1,5 +1,55 @@
import { InvalidArgumentError } from '@apextoaster/js-utils';
import { Octokit } from '@octokit/rest';
import { expect } from 'chai';
import { BaseOptions, Container, NullLogger } from 'noicejs';
import { stub } from 'sinon';
import { RemoteModule } from '../../src/module/RemoteModule';
import { GithubRemote } from '../../src/remote/github';
describe('github remote', () => {
it('should authenticate to Github API');
it('should create an octokit client with token auth', async () => {
const logger = NullLogger.global;
const module = new RemoteModule();
const container = Container.from(module);
await container.configure();
const client = stub();
module.bind<Octokit, Octokit, BaseOptions>(Octokit).toFactory(client);
const remote = await container.create(GithubRemote, {
data: {
token: 'foo',
type: 'token',
},
logger,
type: '',
});
const status = await remote.connect();
expect(status).to.equal(true);
expect(client).to.have.been.calledWithMatch({
auth: 'foo',
});
});
it('should throw on invalid auth methods', async () => {
const logger = NullLogger.global;
const module = new RemoteModule();
const container = Container.from(module);
await container.configure();
const client = stub();
module.bind<Octokit, Octokit, BaseOptions>(Octokit).toFactory(client);
const remote = await container.create(GithubRemote, {
data: {
type: 'test',
},
logger,
type: '',
});
return expect(remote.connect()).to.eventually.be.rejectedWith(InvalidArgumentError);
});
});