2021-05-30 15:36:20 +02:00
|
|
|
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 = {
|
2021-06-04 13:19:43 +02:00
|
|
|
channelId: 'undefined', //todo uncompleted test but incompleted function too
|
2021-05-30 15:36:20 +02:00
|
|
|
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,
|
|
|
|
};
|
2021-06-04 13:19:43 +02:00
|
|
|
|
2021-05-30 15:36:20 +02:00
|
|
|
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)
|
2021-06-04 13:19:43 +02:00
|
|
|
.reply(200, expectedReplyTokenAddress);
|
|
|
|
const import_scope = nock(
|
|
|
|
`https://${paramsPeerTube.domain_name}/api/v1`
|
|
|
|
)
|
2021-05-30 15:36:20 +02:00
|
|
|
.matchHeader(
|
2021-06-04 13:19:43 +02:00
|
|
|
'authorization',
|
2021-05-30 15:36:20 +02:00
|
|
|
`Bearer ${expectedReplyTokenAddress.access_token}`
|
|
|
|
)
|
2021-06-04 13:19:43 +02:00
|
|
|
.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;
|
2021-05-30 15:36:20 +02:00
|
|
|
})
|
2021-06-04 13:19:43 +02:00
|
|
|
.times(3)
|
2021-05-30 15:36:20 +02:00
|
|
|
.reply(200);
|
|
|
|
|
|
|
|
const requester = new PeerTubeRequester(paramsPeerTube);
|
|
|
|
|
|
|
|
// when
|
|
|
|
await requester.receivedMessage(newEntriesMessage);
|
|
|
|
|
|
|
|
//expected
|
2021-06-04 13:19:43 +02:00
|
|
|
// all the scope need to be completed
|
|
|
|
expect(scope.isDone()).to.be.true;
|
|
|
|
expect(import_scope.isDone()).to.be.true;
|
2021-05-30 15:36:20 +02:00
|
|
|
});
|
|
|
|
});
|