diff --git a/src/remote/github.ts b/src/remote/github.ts index 0a41993..fb29eed 100644 --- a/src/remote/github.ts +++ b/src/remote/github.ts @@ -16,14 +16,13 @@ export class GithubRemote extends BaseRemote implements } public async connect(): Promise { - 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 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; diff --git a/test/remote/TestGithubRemote.ts b/test/remote/TestGithubRemote.ts index f79426a..0cd6416 100644 --- a/test/remote/TestGithubRemote.ts +++ b/test/remote/TestGithubRemote.ts @@ -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).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).toFactory(client); + + const remote = await container.create(GithubRemote, { + data: { + type: 'test', + }, + logger, + type: '', + }); + + return expect(remote.connect()).to.eventually.be.rejectedWith(InvalidArgumentError); + }); });