routing/tests/peertubeRequeter-spec.ts

124 lines
3.5 KiB
TypeScript
Raw Normal View History

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';
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);
const import_scope = nock(
`https://${paramsPeerTube.domain_name}/api/v1`
)
.matchHeader(
'authorization',
`Bearer ${expectedReplyTokenAddress.access_token}`
)
.post(`/videos/imports`, (reqBody) => {
let links: string[] = newEntriesMessage.rawContent.items.map(
(item: any) => item.link
);
const body = new URLSearchParams(reqBody);
if (body.get('channelId') === 'undefined') {
const targUrl = body.get('targetUrl');
if (targUrl && links.includes(targUrl)) {
const index = links.findIndex(
(elmt) => elmt === targUrl
);
links.splice(index, 1);
return true;
}
}
return false;
})
.times(3)
.reply(200);
const requester = new PeerTubeRequester(paramsPeerTube);
// when
await requester.receivedMessage(newEntriesMessage);
//expected
// all the scope need to be completed
expect(scope.isDone()).to.be.true;
expect(import_scope.isDone()).to.be.true;
});
});