import chai from 'chai'; import sinon from 'ts-sinon'; import sinonChai from 'sinon-chai'; chai.use(sinonChai); const expect = chai.expect; import nock, { disableNetConnect, RequestBodyMatcher } from 'nock'; import { ImplementableApi } from '../src'; import { PeerTubeRequester } from '../src/peertubeRequester'; import { Request } from 'node-fetch'; const paramsPeerTube: PeerTubeRequester.Config = { name: 'testedRequester', domain_name: 'myApiAddress.yolo', password: 'mySuperSecretPassphrase', username: 'myUsername', }; const newEntriesMessage: ImplementableApi.Message = { type: 'newEntriesNotify', rawContent: { items: [ { author: 'channel1', link: 'link1', title: 'title1', }, { author: 'channel2', link: 'link2', title: 'title2', }, { author: 'channel3', link: 'link3', title: 'title3', }, ], }, }; const UploadInstruction = { channelID: 'undefined', //todo uncompleted test but incompleted function too targetUrl: 'myTargerUrl', }; // nock data const expectedReplyOauthClient = { client_id: 'expectedClientID', client_secret: 'expectedClientSecret', }; const expectedReplyTokenAddress = { access_token: 'expectedAccessToken', }; const bodyTokenRequest: RequestBodyMatcher = { client_id: expectedReplyOauthClient.client_id, client_secret: expectedReplyOauthClient.client_secret, grant_type: 'password', response_type: 'code', username: paramsPeerTube.username, password: paramsPeerTube.password, }; describe('PeerTube Requester Test', function () { before(function () { disableNetConnect(); }); after(function () { nock.cleanAll(); }); it('it will test the upload function', async function () { // given const scope = nock(`https://${paramsPeerTube.domain_name}/api/v1`) .get(`/oauth-clients/local`) .times(3) .reply(200, expectedReplyOauthClient) .post(`/users/token`, bodyTokenRequest) .times(3) .reply(200, expectedReplyTokenAddress) .post(`/videos/imports`, { channelID: 'undefined', targeUrl: newEntriesMessage.rawContent.items[0].link, }) .matchHeader( 'Authorization', `Bearer ${expectedReplyTokenAddress.access_token}` ) .reply(200) .post(`/videos/imports`, { channelID: 'undefined', targeUrl: newEntriesMessage.rawContent.items[1].link, }) .matchHeader( 'Authorization', `Bearer ${expectedReplyTokenAddress.access_token}` ) .reply(200) .post(`/videos/imports`, { channelID: 'undefined', targeUrl: newEntriesMessage.rawContent.items[2].link, }) .matchHeader( 'Authorization', `Bearer ${expectedReplyTokenAddress.access_token}` ) .reply(200); if (scope.isDone()) console.log(scope.activeMocks()); const requester = new PeerTubeRequester(paramsPeerTube); // when await requester.receivedMessage(newEntriesMessage); //expected if (scope.isDone()) console.log(scope.activeMocks()); }); });