fix(test): begin covering github auth
This commit is contained in:
parent
9364266c86
commit
c16c10e140
|
@ -16,14 +16,13 @@ export class GithubRemote extends BaseRemote<Octokit, RemoteOptions> implements
|
||||||
}
|
}
|
||||||
|
|
||||||
public async connect(): Promise<boolean> {
|
public async connect(): Promise<boolean> {
|
||||||
this.options.logger.info('connecting to github');
|
|
||||||
|
|
||||||
const type = mustExist(this.options.data.type);
|
const type = mustExist(this.options.data.type);
|
||||||
|
this.options.logger.info({ type }, 'connecting to github');
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'app':
|
case 'app':
|
||||||
this.options.logger.info('using app auth');
|
this.options.logger.info('using app auth');
|
||||||
this.client = new Octokit({
|
this.client = await this.options.container.create(Octokit, {
|
||||||
auth: {
|
auth: {
|
||||||
id: parseInt(mustExist(this.options.data.id), 10),
|
id: parseInt(mustExist(this.options.data.id), 10),
|
||||||
installationId: parseInt(mustExist(this.options.data.installationId), 10),
|
installationId: parseInt(mustExist(this.options.data.installationId), 10),
|
||||||
|
@ -34,7 +33,7 @@ export class GithubRemote extends BaseRemote<Octokit, RemoteOptions> implements
|
||||||
break;
|
break;
|
||||||
case 'token':
|
case 'token':
|
||||||
this.options.logger.info('using token auth');
|
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),
|
auth: mustExist(this.options.data.token),
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -1,5 +1,55 @@
|
||||||
|
import { InvalidArgumentError } from '@apextoaster/js-utils';
|
||||||
|
import { Octokit } from '@octokit/rest';
|
||||||
import { expect } from 'chai';
|
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', () => {
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue