UNPKG

node-onesky-utils

Version:

Node.js utils for working with OneSky translation service. Original package from @brainly/onesky-utils

299 lines (244 loc) 10.1 kB
'use strict'; const postFile = rootRequire('lib/postFile.js'); const privateFunctions = rootRequire('lib/privateFunctions.js'); const FormData = require('form-data'); describe('postFile', function() { let sandbox; beforeEach(function() { sandbox = sinon.createSandbox(); }); afterEach(function() { sandbox.restore(); }); describe('postFile function', function() { it('should call getDevHash with the secret', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; const getDevHashStub = sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('success'); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key', format: 'JSON', content: '{"key": "value"}', keepStrings: true, language: 'en', fileName: 'test.json' }; await postFile(options); expect(getDevHashStub).to.have.been.calledOnceWith('test-secret'); }); it('should call makeRequest with correct parameters for string content', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('success'); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key', format: 'JSON', content: '{"key": "value"}', keepStrings: true, language: 'en', fileName: 'test.json' }; await postFile(options); expect(makeRequestStub).to.have.been.calledOnce; const requestCall = makeRequestStub.getCall(0); const requestOptions = requestCall.args[0]; const errorMessage = requestCall.args[1]; expect(requestOptions.method).to.equal('POST'); expect(requestOptions.url).to.include('/1/projects/123/files?'); expect(requestOptions.url).to.include('api_key=test-api-key'); expect(requestOptions.url).to.include('timestamp=1234567890'); expect(requestOptions.url).to.include('dev_hash=test-hash'); expect(requestOptions.data).to.be.instanceOf(FormData); expect(requestOptions.headers).to.be.an('object'); expect(errorMessage).to.equal('Unable to upload document'); }); it('should handle Buffer content', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('success'); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key', format: 'JSON', content: Buffer.from('{"key": "value"}'), keepStrings: false, language: 'en', fileName: 'test.json' }; await postFile(options); expect(makeRequestStub).to.have.been.calledOnce; const requestOptions = makeRequestStub.getCall(0).args[0]; expect(requestOptions.data).to.be.instanceOf(FormData); }); it('should include all required form fields', async function() { const mockHash = { devHash: 'abc123', timestamp: 9876543210 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('success'); const options = { projectId: 456, secret: 'my-secret', apiKey: 'my-api-key', format: 'GETTEXT_PO', content: 'file content', keepStrings: false, language: 'fr', fileName: 'messages.po', allowSameAsOriginal: true }; await postFile(options); const requestCall = makeRequestStub.getCall(0); const requestOptions = requestCall.args[0]; // Verify the FormData instance was created and passed expect(requestOptions.data).to.be.instanceOf(FormData); expect(requestOptions.method).to.equal('POST'); expect(requestOptions.url).to.include('/1/projects/456/files?'); }); it('should set allowSameAsOriginal to false by default', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('success'); const options = { projectId: 123, secret: 'secret', apiKey: 'api-key', format: 'JSON', content: '{}', keepStrings: true, language: 'en', fileName: 'test.json' // allowSameAsOriginal not specified }; await postFile(options); const requestCall = makeRequestStub.getCall(0); const requestOptions = requestCall.args[0]; expect(requestOptions.data).to.be.instanceOf(FormData); }); it('should return the result from makeRequest', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; const expectedResult = { success: true, message: 'File uploaded' }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); sandbox.stub(privateFunctions, 'makeRequest').resolves(expectedResult); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key', format: 'JSON', content: '{"key": "value"}', keepStrings: true, language: 'en', fileName: 'test.json' }; const result = await postFile(options); expect(result).to.deep.equal(expectedResult); }); it('should handle file path content', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('success'); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key', format: 'JSON', content: { path: '/path/to/file.json' }, keepStrings: true, language: 'en', fileName: 'test.json' }; await postFile(options); expect(makeRequestStub).to.have.been.calledOnce; const requestOptions = makeRequestStub.getCall(0).args[0]; expect(requestOptions.data).to.be.instanceOf(FormData); }); it('should propagate errors from makeRequest', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; const expectedError = new Error('Upload failed'); sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); sandbox.stub(privateFunctions, 'makeRequest').rejects(expectedError); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key', format: 'JSON', content: '{"key": "value"}', keepStrings: true, language: 'en', fileName: 'test.json' }; try { await postFile(options); expect.fail('Should have thrown an error'); } catch (error) { expect(error).to.equal(expectedError); } }); it('should modify the options object by adding hash', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); sandbox.stub(privateFunctions, 'makeRequest').resolves('success'); const options = { projectId: 123, secret: 'test-secret', apiKey: 'test-api-key', format: 'JSON', content: '{"key": "value"}', keepStrings: true, language: 'en', fileName: 'test.json' }; await postFile(options); expect(options.hash).to.deep.equal(mockHash); }); it('should construct the correct upload URL', async function() { const mockHash = { devHash: 'hash123', timestamp: 1111111111 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('success'); const options = { projectId: 789, secret: 'upload-secret', apiKey: 'upload-api-key', format: 'XLIFF', content: '<xliff></xliff>', keepStrings: false, language: 'de', fileName: 'translations.xlf' }; await postFile(options); const requestUrl = makeRequestStub.getCall(0).args[0].url; expect(requestUrl).to.include('https://platform.api.onesky.io/1/projects/789/files?'); expect(requestUrl).to.include('api_key=upload-api-key'); expect(requestUrl).to.include('timestamp=1111111111'); expect(requestUrl).to.include('dev_hash=hash123'); }); it('should handle boolean keepStrings correctly', async function() { const mockHash = { devHash: 'test-hash', timestamp: 1234567890 }; sandbox.stub(privateFunctions, 'getDevHash').returns(mockHash); const makeRequestStub = sandbox.stub(privateFunctions, 'makeRequest').resolves('success'); // Test with true let options = { projectId: 123, secret: 'secret', apiKey: 'api-key', format: 'JSON', content: '{}', keepStrings: true, language: 'en', fileName: 'test.json' }; await postFile(options); let requestOptions = makeRequestStub.getCall(0).args[0]; expect(requestOptions.data).to.be.instanceOf(FormData); // Reset and test with false makeRequestStub.resetHistory(); options.keepStrings = false; await postFile(options); requestOptions = makeRequestStub.getCall(0).args[0]; expect(requestOptions.data).to.be.instanceOf(FormData); }); }); });