2018-09-01 13:58:06 -04:00
|
|
|
const loadAdapter = require('../lib/Adapters/AdapterLoader').loadAdapter;
|
|
|
|
|
const FilesAdapter = require('@parse/fs-files-adapter').default;
|
|
|
|
|
const S3Adapter = require('@parse/s3-files-adapter').default;
|
|
|
|
|
const ParsePushAdapter = require('@parse/push-adapter').default;
|
2018-07-02 23:30:14 -04:00
|
|
|
const Config = require('../lib/Config');
|
2016-02-21 23:47:07 -05:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
describe('AdapterLoader', () => {
|
|
|
|
|
it('should instantiate an adapter from string in object', done => {
|
|
|
|
|
const adapterPath = require('path').resolve('./spec/MockAdapter');
|
2016-02-21 23:47:07 -05:00
|
|
|
|
2018-02-17 09:55:30 -05:00
|
|
|
const adapter = loadAdapter({
|
2016-02-21 23:47:07 -05:00
|
|
|
adapter: adapterPath,
|
2016-02-23 21:05:27 -05:00
|
|
|
options: {
|
2018-09-01 13:58:06 -04:00
|
|
|
key: 'value',
|
|
|
|
|
foo: 'bar',
|
|
|
|
|
},
|
2016-02-21 23:47:07 -05:00
|
|
|
});
|
2016-03-07 00:47:08 -08:00
|
|
|
|
2016-02-21 23:47:07 -05:00
|
|
|
expect(adapter instanceof Object).toBe(true);
|
2018-09-01 13:58:06 -04:00
|
|
|
expect(adapter.options.key).toBe('value');
|
|
|
|
|
expect(adapter.options.foo).toBe('bar');
|
2016-02-21 23:47:07 -05:00
|
|
|
done();
|
|
|
|
|
});
|
2016-03-07 00:47:08 -08:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
it('should instantiate an adapter from string', done => {
|
|
|
|
|
const adapterPath = require('path').resolve('./spec/MockAdapter');
|
2018-02-17 09:55:30 -05:00
|
|
|
const adapter = loadAdapter(adapterPath);
|
2016-03-07 00:47:08 -08:00
|
|
|
|
2016-02-21 23:47:07 -05:00
|
|
|
expect(adapter instanceof Object).toBe(true);
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-03-07 00:47:08 -08:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
it('should instantiate an adapter from string that is module', done => {
|
|
|
|
|
const adapterPath = require('path').resolve(
|
|
|
|
|
'./lib/Adapters/Files/FilesAdapter'
|
|
|
|
|
);
|
2018-02-17 09:55:30 -05:00
|
|
|
const adapter = loadAdapter({
|
2018-09-01 13:58:06 -04:00
|
|
|
adapter: adapterPath,
|
2016-02-21 23:47:07 -05:00
|
|
|
});
|
2016-03-07 00:47:08 -08:00
|
|
|
|
2016-03-24 00:57:49 -04:00
|
|
|
expect(typeof adapter).toBe('object');
|
|
|
|
|
expect(typeof adapter.createFile).toBe('function');
|
|
|
|
|
expect(typeof adapter.deleteFile).toBe('function');
|
|
|
|
|
expect(typeof adapter.getFileData).toBe('function');
|
|
|
|
|
expect(typeof adapter.getFileLocation).toBe('function');
|
2016-08-12 13:25:24 -04:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
it('should instantiate an adapter from npm module', done => {
|
2018-02-17 09:55:30 -05:00
|
|
|
const adapter = loadAdapter({
|
2018-09-01 13:58:06 -04:00
|
|
|
module: '@parse/fs-files-adapter',
|
2016-08-12 13:25:24 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(typeof adapter).toBe('object');
|
|
|
|
|
expect(typeof adapter.createFile).toBe('function');
|
|
|
|
|
expect(typeof adapter.deleteFile).toBe('function');
|
|
|
|
|
expect(typeof adapter.getFileData).toBe('function');
|
|
|
|
|
expect(typeof adapter.getFileLocation).toBe('function');
|
2016-02-21 23:47:07 -05:00
|
|
|
done();
|
|
|
|
|
});
|
2016-03-07 00:47:08 -08:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
it('should instantiate an adapter from function/Class', done => {
|
2018-02-17 09:55:30 -05:00
|
|
|
const adapter = loadAdapter({
|
2018-09-01 13:58:06 -04:00
|
|
|
adapter: FilesAdapter,
|
2016-02-21 23:47:07 -05:00
|
|
|
});
|
|
|
|
|
expect(adapter instanceof FilesAdapter).toBe(true);
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-03-07 00:47:08 -08:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
it('should instantiate the default adapter from Class', done => {
|
2018-02-17 09:55:30 -05:00
|
|
|
const adapter = loadAdapter(null, FilesAdapter);
|
2016-02-21 23:47:07 -05:00
|
|
|
expect(adapter instanceof FilesAdapter).toBe(true);
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-03-07 00:47:08 -08:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
it('should use the default adapter', done => {
|
2018-02-17 09:55:30 -05:00
|
|
|
const defaultAdapter = new FilesAdapter();
|
|
|
|
|
const adapter = loadAdapter(null, defaultAdapter);
|
2016-02-21 23:47:07 -05:00
|
|
|
expect(adapter instanceof FilesAdapter).toBe(true);
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-03-07 00:47:08 -08:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
it('should use the provided adapter', done => {
|
2018-02-17 09:55:30 -05:00
|
|
|
const originalAdapter = new FilesAdapter();
|
|
|
|
|
const adapter = loadAdapter(originalAdapter);
|
2016-02-21 23:47:07 -05:00
|
|
|
expect(adapter).toBe(originalAdapter);
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-03-07 00:47:08 -08:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
it('should fail loading an improperly configured adapter', done => {
|
2018-02-17 09:55:30 -05:00
|
|
|
const Adapter = function(options) {
|
2016-02-23 21:05:27 -05:00
|
|
|
if (!options.foo) {
|
2018-09-01 13:58:06 -04:00
|
|
|
throw 'foo is required for that adapter';
|
2016-02-23 21:05:27 -05:00
|
|
|
}
|
2018-09-01 13:58:06 -04:00
|
|
|
};
|
2018-02-17 09:55:30 -05:00
|
|
|
const adapterOptions = {
|
2018-09-01 13:58:06 -04:00
|
|
|
param: 'key',
|
|
|
|
|
doSomething: function() {},
|
2016-02-23 21:05:27 -05:00
|
|
|
};
|
2016-03-07 00:47:08 -08:00
|
|
|
|
2016-02-23 21:05:27 -05:00
|
|
|
expect(() => {
|
2018-02-17 09:55:30 -05:00
|
|
|
const adapter = loadAdapter(adapterOptions, Adapter);
|
2016-02-23 21:05:27 -05:00
|
|
|
expect(adapter).toEqual(adapterOptions);
|
2018-09-01 13:58:06 -04:00
|
|
|
}).not.toThrow('foo is required for that adapter');
|
2016-02-23 21:05:27 -05:00
|
|
|
done();
|
|
|
|
|
});
|
2016-03-07 00:47:08 -08:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
it('should load push adapter from options', done => {
|
2018-02-17 09:55:30 -05:00
|
|
|
const options = {
|
2017-07-01 23:34:47 -04:00
|
|
|
android: {
|
|
|
|
|
senderId: 'yolo',
|
2018-09-01 13:58:06 -04:00
|
|
|
apiKey: 'yolo',
|
|
|
|
|
},
|
|
|
|
|
};
|
2016-03-04 12:04:41 -05:00
|
|
|
expect(() => {
|
2018-02-17 09:55:30 -05:00
|
|
|
const adapter = loadAdapter(undefined, ParsePushAdapter, options);
|
2016-03-04 12:04:41 -05:00
|
|
|
expect(adapter.constructor).toBe(ParsePushAdapter);
|
|
|
|
|
expect(adapter).not.toBe(undefined);
|
|
|
|
|
}).not.toThrow();
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-03-07 00:47:08 -08:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
it('should load custom push adapter from string (#3544)', done => {
|
|
|
|
|
const adapterPath = require('path').resolve('./spec/MockPushAdapter');
|
2018-02-17 09:55:30 -05:00
|
|
|
const options = {
|
2017-02-21 15:06:45 -05:00
|
|
|
ios: {
|
2018-09-01 13:58:06 -04:00
|
|
|
bundleId: 'bundle.id',
|
|
|
|
|
},
|
|
|
|
|
};
|
2017-02-21 15:06:45 -05:00
|
|
|
const pushAdapterOptions = {
|
|
|
|
|
adapter: adapterPath,
|
2018-09-01 13:58:06 -04:00
|
|
|
options,
|
2017-02-21 15:06:45 -05:00
|
|
|
};
|
|
|
|
|
expect(() => {
|
|
|
|
|
reconfigureServer({
|
|
|
|
|
push: pushAdapterOptions,
|
|
|
|
|
}).then(() => {
|
2017-10-23 08:43:05 -04:00
|
|
|
const config = Config.get(Parse.applicationId);
|
2017-02-21 15:06:45 -05:00
|
|
|
const pushAdapter = config.pushWorker.adapter;
|
|
|
|
|
expect(pushAdapter.getValidPushTypes()).toEqual(['ios']);
|
|
|
|
|
expect(pushAdapter.options).toEqual(pushAdapterOptions);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
}).not.toThrow();
|
|
|
|
|
});
|
|
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
it('should load S3Adapter from direct passing', done => {
|
2019-12-03 18:21:12 -06:00
|
|
|
spyOn(console, 'warn').and.callFake(() => {});
|
2018-09-01 13:58:06 -04:00
|
|
|
const s3Adapter = new S3Adapter('key', 'secret', 'bucket');
|
2016-03-04 12:04:41 -05:00
|
|
|
expect(() => {
|
2018-02-17 09:55:30 -05:00
|
|
|
const adapter = loadAdapter(s3Adapter, FilesAdapter);
|
2016-03-04 12:04:41 -05:00
|
|
|
expect(adapter).toBe(s3Adapter);
|
|
|
|
|
}).not.toThrow();
|
|
|
|
|
done();
|
2018-09-01 13:58:06 -04:00
|
|
|
});
|
2016-02-22 18:31:10 -05:00
|
|
|
});
|