UNPKG

n8n-nodes-twitter-uploader-inf

Version:
125 lines 5.58 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); require('dotenv').config(); const TwitterUtils_1 = require("./TwitterUtils"); const TwitterMediaUpload_node_1 = require("./TwitterMediaUpload.node"); const axios_1 = __importDefault(require("axios")); describe('TwitterUtils', () => { describe('OAuth1Helper', () => { let node; beforeEach(() => { node = { context: { getInstance: jest.fn(), }, }; }); it('should create an OAuth1Helper instance', () => { const config = { consumerKey: 'consumerKey', consumerSecret: 'consumerSecret', accessToken: 'accessToken', accessSecret: 'accessSecret', }; const helper = new TwitterUtils_1.OAuth1Helper(config, node); expect(helper).toBeInstanceOf(TwitterUtils_1.OAuth1Helper); }); it('should throw an error if consumerKey is missing', () => { const config = { consumerKey: '', consumerSecret: 'consumerSecret', accessToken: 'accessToken', accessSecret: 'accessSecret', }; expect(() => new TwitterUtils_1.OAuth1Helper(config, node)).toThrowError('Missing required OAuth parameter: consumerKey'); }); it('should throw an error if consumerSecret is missing', () => { const config = { consumerKey: 'consumerKey', consumerSecret: '', accessToken: 'accessToken', accessSecret: 'accessSecret', }; expect(() => new TwitterUtils_1.OAuth1Helper(config, node)).toThrowError('Missing required OAuth parameter: consumerSecret'); }); it('should generate an auth header', () => { const config = { consumerKey: 'consumerKey', consumerSecret: 'consumerSecret', accessToken: 'accessToken', accessSecret: 'accessSecret', }; const helper = new TwitterUtils_1.OAuth1Helper(config, node); const header = helper.generateAuthHeader({ url: 'https://api.twitter.com/1.1/statuses/home_timeline.json', method: 'GET', }); expect(header).toContain('OAuth'); expect(header).toContain('oauth_consumer_key="consumerKey"'); expect(header).toContain('oauth_token="accessToken"'); }); it('should throw an error if accessToken or accessSecret is missing when generating auth header', () => { const config = { consumerKey: 'consumerKey', consumerSecret: 'consumerSecret', }; const helper = new TwitterUtils_1.OAuth1Helper(config, node); expect(() => helper.generateAuthHeader({ url: 'https://api.twitter.com/1.1/statuses/home_timeline.json', method: 'GET', })).toThrowError('Missing required OAuth parameters: accessToken and accessSecret'); }); }); describe('TwitterMediaUpload', () => { it('should upload a media file', async () => { const twitterMediaUpload = new TwitterMediaUpload_node_1.TwitterMediaUpload(); // const executeMock = jest.spyOn(twitterMediaUpload, 'execute' as any); // executeMock.mockImplementation(() => { // return Promise.resolve([[{ json: { success: true } }]]); // }); const node = { context: { getInstance: jest.fn(), }, }; const credentials = { consumerKey: process.env.TWITTER_CONSUMER_KEY, consumerSecret: process.env.TWITTER_CONSUMER_SECRET, accessToken: process.env.TWITTER_ACCESS_TOKEN, accessSecret: process.env.TWITTER_ACCESS_SECRET, }; const imageUrl = 'https://fastly.picsum.photos/id/544/200/300.jpg?hmac=YL3M_fg_84Kqg0EQTvbltmjeGeQetARWPFA5YLn5hS0'; const response = await axios_1.default.get(imageUrl, { responseType: 'arraybuffer' }); const buffer = Buffer.from(response.data, 'binary'); const binaryData = { data: buffer.toString('base64'), mimeType: 'image/jpeg', fileName: 'test.jpg', }; const input = [ { json: {}, binary: { data: binaryData, }, }, ]; const context = { getInputData: jest.fn().mockReturnValue(input), getNodeParameter: jest.fn().mockReturnValue('data'), getCredentials: jest.fn().mockReturnValue(credentials), helpers: { getBinaryDataBuffer: jest.fn().mockReturnValue(buffer), }, getNode: jest.fn().mockReturnValue(node), continueOnFail: jest.fn().mockReturnValue(false), }; const result = await twitterMediaUpload.execute.apply(context); expect(result[0][0].json.success).toEqual(true); }, 30000); }); }); //# sourceMappingURL=TwitterUtils.test.js.map