12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223 |
- // Copyright (c) Jupyter Development Team.
- import 'jest';
- import { Contents, ContentsManager, Drive, ServerConnection } from '../../src';
- import { expectFailure, JupyterServer } from '@jupyterlab/testutils';
- import { DEFAULT_FILE, makeSettings, handleRequest } from '../utils';
- const DEFAULT_DIR: Contents.IModel = {
- name: 'bar',
- path: 'foo/bar',
- type: 'directory',
- created: 'yesterday',
- last_modified: 'today',
- writable: false,
- mimetype: '',
- content: [
- { name: 'buzz.txt', path: 'foo/bar/buzz.txt' },
- { name: 'bazz.py', path: 'foo/bar/bazz.py' }
- ],
- format: 'json'
- };
- const DEFAULT_CP: Contents.ICheckpointModel = {
- id: '1234',
- last_modified: 'yesterday'
- };
- const server = new JupyterServer();
- beforeAll(async () => {
- await server.start();
- });
- afterAll(async () => {
- await server.shutdown();
- });
- describe('contents', () => {
- let contents: ContentsManager;
- let serverSettings: ServerConnection.ISettings;
- beforeEach(() => {
- serverSettings = makeSettings();
- contents = new ContentsManager({ serverSettings });
- });
- afterEach(() => {
- contents.dispose();
- });
- describe('#constructor()', () => {
- it('should accept no options', () => {
- const contents = new ContentsManager();
- expect(contents).toBeInstanceOf(ContentsManager);
- });
- it('should accept options', () => {
- const contents = new ContentsManager({
- defaultDrive: new Drive()
- });
- expect(contents).toBeInstanceOf(ContentsManager);
- });
- });
- describe('#fileChanged', () => {
- it('should be emitted when a file changes', async () => {
- handleRequest(contents, 201, DEFAULT_FILE);
- let called = false;
- contents.fileChanged.connect((sender, args) => {
- expect(sender).toBe(contents);
- expect(args.type).toBe('new');
- expect(args.oldValue).toBeNull();
- expect(args.newValue!.path).toBe(DEFAULT_FILE.path);
- called = true;
- });
- await contents.newUntitled();
- expect(called).toBe(true);
- });
- it('should include the full path for additional drives', async () => {
- const drive = new Drive({ name: 'other', serverSettings });
- contents.addDrive(drive);
- handleRequest(drive, 201, DEFAULT_FILE);
- let called = false;
- contents.fileChanged.connect((sender, args) => {
- expect(args.newValue!.path).toBe('other:' + DEFAULT_FILE.path);
- called = true;
- });
- await contents.newUntitled({ path: 'other:' });
- expect(called).toBe(true);
- });
- });
- describe('#isDisposed', () => {
- it('should test whether the manager is disposed', () => {
- expect(contents.isDisposed).toBe(false);
- contents.dispose();
- expect(contents.isDisposed).toBe(true);
- });
- });
- describe('#dispose()', () => {
- it('should dispose of the resources used by the manager', () => {
- expect(contents.isDisposed).toBe(false);
- contents.dispose();
- expect(contents.isDisposed).toBe(true);
- contents.dispose();
- expect(contents.isDisposed).toBe(true);
- });
- });
- describe('#addDrive()', () => {
- it('should add a new drive to the manager', () => {
- contents.addDrive(new Drive({ name: 'other' }));
- handleRequest(contents, 200, DEFAULT_FILE);
- return contents.get('other:');
- });
- });
- describe('#localPath()', () => {
- it('should parse the local part of a path', () => {
- contents.addDrive(new Drive({ name: 'other' }));
- contents.addDrive(new Drive({ name: 'alternative' }));
- expect(contents.localPath('other:foo/bar/example.txt')).toBe(
- 'foo/bar/example.txt'
- );
- expect(contents.localPath('alternative:/foo/bar/example.txt')).toBe(
- 'foo/bar/example.txt'
- );
- });
- it('should allow the ":" character in other parts of the path', () => {
- contents.addDrive(new Drive({ name: 'other' }));
- expect(
- contents.localPath('other:foo/odd:directory/example:file.txt')
- ).toBe('foo/odd:directory/example:file.txt');
- });
- it('should leave alone names with ":" that are not drive names', () => {
- contents.addDrive(new Drive({ name: 'other' }));
- expect(
- contents.localPath('which:foo/odd:directory/example:file.txt')
- ).toBe('which:foo/odd:directory/example:file.txt');
- });
- });
- describe('.driveName()', () => {
- it('should parse the drive name a path', () => {
- contents.addDrive(new Drive({ name: 'other' }));
- contents.addDrive(new Drive({ name: 'alternative' }));
- expect(contents.driveName('other:foo/bar/example.txt')).toBe('other');
- expect(contents.driveName('alternative:/foo/bar/example.txt')).toBe(
- 'alternative'
- );
- });
- it('should allow the ":" character in other parts of the path', () => {
- contents.addDrive(new Drive({ name: 'other' }));
- expect(
- contents.driveName('other:foo/odd:directory/example:file.txt')
- ).toBe('other');
- });
- it('should leave alone names with ":" that are not drive names', () => {
- contents.addDrive(new Drive({ name: 'other' }));
- expect(
- contents.driveName('which:foo/odd:directory/example:file.txt')
- ).toBe('');
- });
- });
- describe('#get()', () => {
- it('should get a file', async () => {
- handleRequest(contents, 200, DEFAULT_FILE);
- const options: Contents.IFetchOptions = { type: 'file' };
- const model = await contents.get('/foo', options);
- expect(model.path).toBe('foo');
- });
- it('should get a directory', async () => {
- handleRequest(contents, 200, DEFAULT_DIR);
- const options: Contents.IFetchOptions = { type: 'directory' };
- const model = await contents.get('/foo', options);
- expect(model.content[0].path).toBe(DEFAULT_DIR.content[0].path);
- });
- it('should get a file from an additional drive', async () => {
- const drive = new Drive({ name: 'other', serverSettings });
- contents.addDrive(drive);
- handleRequest(drive, 200, DEFAULT_FILE);
- const options: Contents.IFetchOptions = { type: 'file' };
- const model = await contents.get('other:/foo', options);
- expect(model.path).toBe('other:foo');
- });
- it('should get a directory from an additional drive', async () => {
- const drive = new Drive({ name: 'other', serverSettings });
- contents.addDrive(drive);
- handleRequest(drive, 200, DEFAULT_DIR);
- const options: Contents.IFetchOptions = { type: 'directory' };
- const model = await contents.get('other:/foo', options);
- expect(model.content[0].path).toBe('other:foo/bar/buzz.txt');
- });
- it('should fail for an incorrect response', async () => {
- handleRequest(contents, 201, DEFAULT_DIR);
- const get = contents.get('/foo');
- await expectFailure(get, 'Invalid response: 201 Created');
- });
- });
- describe('#getDownloadUrl()', () => {
- const settings = ServerConnection.makeSettings({
- baseUrl: 'http://foo'
- });
- it('should get the url of a file', async () => {
- const drive = new Drive({ serverSettings: settings });
- const contents = new ContentsManager({ defaultDrive: drive });
- const test1 = contents.getDownloadUrl('bar.txt');
- const test2 = contents.getDownloadUrl('fizz/buzz/bar.txt');
- const test3 = contents.getDownloadUrl('/bar.txt');
- const urls = await Promise.all([test1, test2, test3]);
- expect(urls[0]).toBe('http://foo/files/bar.txt');
- expect(urls[1]).toBe('http://foo/files/fizz/buzz/bar.txt');
- expect(urls[2]).toBe('http://foo/files/bar.txt');
- });
- it('should encode characters', async () => {
- const drive = new Drive({ serverSettings: settings });
- const contents = new ContentsManager({ defaultDrive: drive });
- const url = await contents.getDownloadUrl('b ar?3.txt');
- expect(url).toBe('http://foo/files/b%20ar%3F3.txt');
- });
- it('should not handle relative paths', async () => {
- const drive = new Drive({ serverSettings: settings });
- const contents = new ContentsManager({ defaultDrive: drive });
- const url = await contents.getDownloadUrl('fizz/../bar.txt');
- expect(url).toBe('http://foo/files/fizz/../bar.txt');
- });
- it('should get the url of a file from an additional drive', async () => {
- const contents = new ContentsManager();
- const other = new Drive({ name: 'other', serverSettings: settings });
- contents.addDrive(other);
- const test1 = contents.getDownloadUrl('other:bar.txt');
- const test2 = contents.getDownloadUrl('other:fizz/buzz/bar.txt');
- const test3 = contents.getDownloadUrl('other:/bar.txt');
- const urls = await Promise.all([test1, test2, test3]);
- expect(urls[0]).toBe('http://foo/files/bar.txt');
- expect(urls[1]).toBe('http://foo/files/fizz/buzz/bar.txt');
- expect(urls[2]).toBe('http://foo/files/bar.txt');
- });
- });
- describe('#newUntitled()', () => {
- it('should create a file', async () => {
- handleRequest(contents, 201, DEFAULT_FILE);
- const model = await contents.newUntitled({ path: '/foo' });
- expect(model.path).toBe('foo/test');
- });
- it('should create a directory', async () => {
- handleRequest(contents, 201, DEFAULT_DIR);
- const options: Contents.ICreateOptions = {
- path: '/foo',
- type: 'directory'
- };
- const model = await contents.newUntitled(options);
- expect(model.content[0].path).toBe(DEFAULT_DIR.content[0].path);
- });
- it('should create a file on an additional drive', async () => {
- const other = new Drive({ name: 'other', serverSettings });
- contents.addDrive(other);
- handleRequest(other, 201, DEFAULT_FILE);
- const model = await contents.newUntitled({ path: 'other:/foo' });
- expect(model.path).toBe('other:foo/test');
- });
- it('should create a directory on an additional drive', async () => {
- const other = new Drive({ name: 'other', serverSettings });
- contents.addDrive(other);
- handleRequest(other, 201, DEFAULT_DIR);
- const options: Contents.ICreateOptions = {
- path: 'other:/foo',
- type: 'directory'
- };
- const model = await contents.newUntitled(options);
- expect(model.path).toBe('other:' + DEFAULT_DIR.path);
- });
- it('should emit the fileChanged signal', async () => {
- handleRequest(contents, 201, DEFAULT_FILE);
- let called = false;
- contents.fileChanged.connect((sender, args) => {
- expect(args.type).toBe('new');
- expect(args.oldValue).toBeNull();
- expect(args.newValue!.path).toBe(DEFAULT_FILE.path);
- called = true;
- });
- await contents.newUntitled({ type: 'file', ext: 'test' });
- expect(called).toBe(true);
- });
- it('should fail for an incorrect model', async () => {
- const dir = JSON.parse(JSON.stringify(DEFAULT_DIR));
- dir.name = 1;
- handleRequest(contents, 201, dir);
- const options: Contents.ICreateOptions = {
- path: '/foo',
- type: 'file',
- ext: 'py'
- };
- const newFile = contents.newUntitled(options);
- await expectFailure(newFile);
- });
- it('should fail for an incorrect response', async () => {
- handleRequest(contents, 200, DEFAULT_DIR);
- const newDir = contents.newUntitled();
- await expectFailure(newDir, 'Invalid response: 200 OK');
- });
- });
- describe('#delete()', () => {
- it('should delete a file', () => {
- handleRequest(contents, 204, {});
- return contents.delete('/foo/bar.txt');
- });
- it('should delete a file on an additional drive', () => {
- const other = new Drive({ name: 'other', serverSettings });
- contents.addDrive(other);
- handleRequest(other, 204, {});
- return contents.delete('other:/foo/bar.txt');
- });
- it('should emit the fileChanged signal', async () => {
- const path = '/foo/bar.txt';
- handleRequest(contents, 204, { path });
- let called = false;
- contents.fileChanged.connect((sender, args) => {
- expect(args.type).toBe('delete');
- expect(args.oldValue!.path).toBe('foo/bar.txt');
- called = true;
- });
- await contents.delete(path);
- expect(called).toBe(true);
- });
- it('should fail for an incorrect response', async () => {
- handleRequest(contents, 200, {});
- const del = contents.delete('/foo/bar.txt');
- await expectFailure(del, 'Invalid response: 200 OK');
- });
- it('should throw a specific error', async () => {
- handleRequest(contents, 400, {});
- const del = contents.delete('/foo/');
- await expectFailure(del, '');
- });
- it('should throw a general error', async () => {
- handleRequest(contents, 500, {});
- const del = contents.delete('/foo/');
- await expectFailure(del, '');
- });
- });
- describe('#rename()', () => {
- it('should rename a file', async () => {
- handleRequest(contents, 200, DEFAULT_FILE);
- const rename = contents.rename('/foo/bar.txt', '/foo/baz.txt');
- const model = await rename;
- expect(model.created).toBe(DEFAULT_FILE.created);
- });
- it('should rename a file on an additional drive', async () => {
- const other = new Drive({ name: 'other', serverSettings });
- contents.addDrive(other);
- handleRequest(other, 200, DEFAULT_FILE);
- const rename = contents.rename(
- 'other:/foo/bar.txt',
- 'other:/foo/baz.txt'
- );
- const model = await rename;
- expect(model.created).toBe(DEFAULT_FILE.created);
- });
- it('should emit the fileChanged signal', async () => {
- handleRequest(contents, 200, DEFAULT_FILE);
- let called = false;
- contents.fileChanged.connect((sender, args) => {
- expect(args.type).toBe('rename');
- expect(args.oldValue!.path).toBe('foo/bar.txt');
- expect(args.newValue!.path).toBe('foo/test');
- called = true;
- });
- await contents.rename('/foo/bar.txt', '/foo/baz.txt');
- expect(called).toBe(true);
- });
- it('should fail for an incorrect model', async () => {
- const dir = JSON.parse(JSON.stringify(DEFAULT_FILE));
- delete dir.path;
- handleRequest(contents, 200, dir);
- const rename = contents.rename('/foo/bar.txt', '/foo/baz.txt');
- await expectFailure(rename);
- });
- it('should fail for an incorrect response', async () => {
- handleRequest(contents, 201, DEFAULT_FILE);
- const rename = contents.rename('/foo/bar.txt', '/foo/baz.txt');
- await expectFailure(rename, 'Invalid response: 201 Created');
- });
- });
- describe('#save()', () => {
- it('should save a file', async () => {
- handleRequest(contents, 200, DEFAULT_FILE);
- const save = contents.save('/foo', { type: 'file', name: 'test' });
- const model = await save;
- expect(model.created).toBe(DEFAULT_FILE.created);
- });
- it('should save a file on an additional drive', async () => {
- const other = new Drive({ name: 'other', serverSettings });
- contents.addDrive(other);
- handleRequest(contents, 200, DEFAULT_FILE);
- const save = contents.save('other:/foo', { type: 'file', name: 'test' });
- const model = await save;
- expect(model.path).toBe('other:foo');
- });
- it('should create a new file', async () => {
- handleRequest(contents, 201, DEFAULT_FILE);
- const save = contents.save('/foo', { type: 'file', name: 'test' });
- const model = await save;
- expect(model.created).toBe(DEFAULT_FILE.created);
- });
- it('should emit the fileChanged signal', async () => {
- handleRequest(contents, 201, DEFAULT_FILE);
- let called = false;
- contents.fileChanged.connect((sender, args) => {
- expect(args.type).toBe('save');
- expect(args.oldValue).toBeNull();
- expect(args.newValue!.path).toBe(DEFAULT_FILE.path);
- called = true;
- });
- await contents.save('/foo', { type: 'file', name: 'test' });
- expect(called).toBe(true);
- });
- it('should fail for an incorrect model', async () => {
- const file = JSON.parse(JSON.stringify(DEFAULT_FILE));
- delete file.format;
- handleRequest(contents, 200, file);
- const save = contents.save('/foo', { type: 'file', name: 'test' });
- await expectFailure(save);
- });
- it('should fail for an incorrect response', async () => {
- handleRequest(contents, 204, DEFAULT_FILE);
- const save = contents.save('/foo', { type: 'file', name: 'test' });
- await expectFailure(save, 'Invalid response: 204 No Content');
- });
- });
- describe('#copy()', () => {
- it('should copy a file', async () => {
- handleRequest(contents, 201, DEFAULT_FILE);
- const model = await contents.copy('/foo/bar.txt', '/baz');
- expect(model.created).toBe(DEFAULT_FILE.created);
- });
- it('should copy a file on an additional drive', async () => {
- const other = new Drive({ serverSettings, name: 'other' });
- contents.addDrive(other);
- handleRequest(other, 201, DEFAULT_FILE);
- const model = await contents.copy('other:/foo/test', 'other:/baz');
- expect(model.path).toBe('other:foo/test');
- });
- it('should emit the fileChanged signal', async () => {
- handleRequest(contents, 201, DEFAULT_FILE);
- let called = false;
- contents.fileChanged.connect((sender, args) => {
- expect(args.type).toBe('new');
- expect(args.oldValue).toBeNull();
- expect(args.newValue!.path).toBe(DEFAULT_FILE.path);
- called = true;
- });
- await contents.copy('/foo/bar.txt', '/baz');
- expect(called).toBe(true);
- });
- it('should fail for an incorrect model', async () => {
- const file = JSON.parse(JSON.stringify(DEFAULT_FILE));
- delete file.type;
- handleRequest(contents, 201, file);
- const copy = contents.copy('/foo/bar.txt', '/baz');
- await expectFailure(copy);
- });
- it('should fail for an incorrect response', async () => {
- handleRequest(contents, 200, DEFAULT_FILE);
- const copy = contents.copy('/foo/bar.txt', '/baz');
- await expectFailure(copy, 'Invalid response: 200 OK');
- });
- });
- describe('#createCheckpoint()', () => {
- it('should create a checkpoint', async () => {
- handleRequest(contents, 201, DEFAULT_CP);
- const checkpoint = contents.createCheckpoint('/foo/bar.txt');
- const model = await checkpoint;
- expect(model.last_modified).toBe(DEFAULT_CP.last_modified);
- });
- it('should create a checkpoint on an additional drive', async () => {
- const other = new Drive({ name: 'other', serverSettings });
- contents.addDrive(other);
- handleRequest(other, 201, DEFAULT_CP);
- const checkpoint = contents.createCheckpoint('other:/foo/bar.txt');
- const model = await checkpoint;
- expect(model.last_modified).toBe(DEFAULT_CP.last_modified);
- });
- it('should fail for an incorrect model', async () => {
- const cp = JSON.parse(JSON.stringify(DEFAULT_CP));
- delete cp.last_modified;
- handleRequest(contents, 201, cp);
- const checkpoint = contents.createCheckpoint('/foo/bar.txt');
- await expectFailure(checkpoint);
- });
- it('should fail for an incorrect response', async () => {
- handleRequest(contents, 200, DEFAULT_CP);
- const checkpoint = contents.createCheckpoint('/foo/bar.txt');
- await expectFailure(checkpoint, 'Invalid response: 200 OK');
- });
- });
- describe('#listCheckpoints()', () => {
- it('should list the checkpoints', async () => {
- handleRequest(contents, 200, [DEFAULT_CP, DEFAULT_CP]);
- const checkpoints = contents.listCheckpoints('/foo/bar.txt');
- const models = await checkpoints;
- expect(models[0].last_modified).toBe(DEFAULT_CP.last_modified);
- });
- it('should list the checkpoints on an additional drive', async () => {
- const other = new Drive({ name: 'other', serverSettings });
- contents.addDrive(other);
- handleRequest(other, 200, [DEFAULT_CP, DEFAULT_CP]);
- const checkpoints = contents.listCheckpoints('other:/foo/bar.txt');
- const models = await checkpoints;
- expect(models[0].last_modified).toBe(DEFAULT_CP.last_modified);
- });
- it('should fail for an incorrect model', async () => {
- const cp = JSON.parse(JSON.stringify(DEFAULT_CP));
- delete cp.id;
- handleRequest(contents, 200, [cp, DEFAULT_CP]);
- const checkpoints = contents.listCheckpoints('/foo/bar.txt');
- await expectFailure(checkpoints);
- handleRequest(contents, 200, DEFAULT_CP);
- const newCheckpoints = contents.listCheckpoints('/foo/bar.txt');
- await expectFailure(newCheckpoints, 'Invalid Checkpoint list');
- });
- it('should fail for an incorrect response', async () => {
- handleRequest(contents, 201, {});
- const checkpoints = contents.listCheckpoints('/foo/bar.txt');
- await expectFailure(checkpoints, 'Invalid response: 201 Created');
- });
- });
- describe('#restoreCheckpoint()', () => {
- it('should restore a checkpoint', () => {
- handleRequest(contents, 204, {});
- const checkpoint = contents.restoreCheckpoint(
- '/foo/bar.txt',
- DEFAULT_CP.id
- );
- return checkpoint;
- });
- it('should restore a checkpoint on an additional drive', () => {
- const other = new Drive({ name: 'other', serverSettings });
- contents.addDrive(other);
- handleRequest(other, 204, {});
- const checkpoint = contents.restoreCheckpoint(
- 'other:/foo/bar.txt',
- DEFAULT_CP.id
- );
- return checkpoint;
- });
- it('should fail for an incorrect response', async () => {
- handleRequest(contents, 200, {});
- const checkpoint = contents.restoreCheckpoint(
- '/foo/bar.txt',
- DEFAULT_CP.id
- );
- await expectFailure(checkpoint, 'Invalid response: 200 OK');
- });
- });
- describe('#deleteCheckpoint()', () => {
- it('should delete a checkpoint', () => {
- handleRequest(contents, 204, {});
- return contents.deleteCheckpoint('/foo/bar.txt', DEFAULT_CP.id);
- });
- it('should delete a checkpoint on an additional drive', () => {
- const other = new Drive({ name: 'other', serverSettings });
- contents.addDrive(other);
- handleRequest(other, 204, {});
- return contents.deleteCheckpoint('other:/foo/bar.txt', DEFAULT_CP.id);
- });
- it('should fail for an incorrect response', async () => {
- handleRequest(contents, 200, {});
- const checkpoint = contents.deleteCheckpoint(
- '/foo/bar.txt',
- DEFAULT_CP.id
- );
- await expectFailure(checkpoint, 'Invalid response: 200 OK');
- });
- });
- });
- describe('drive', () => {
- let serverSettings: ServerConnection.ISettings;
- let contents: ContentsManager;
- beforeEach(() => {
- serverSettings = makeSettings();
- contents = new ContentsManager({ serverSettings });
- });
- afterEach(() => {
- contents.dispose();
- });
- describe('#constructor()', () => {
- it('should accept no options', () => {
- const drive = new Drive();
- expect(drive).toBeInstanceOf(Drive);
- });
- it('should accept options', () => {
- const drive = new Drive({
- name: 'name'
- });
- expect(drive).toBeInstanceOf(Drive);
- });
- });
- describe('#name', () => {
- it('should return the name of the drive', () => {
- const drive = new Drive({
- name: 'name'
- });
- expect(drive.name).toBe('name');
- });
- });
- describe('#fileChanged', () => {
- it('should be emitted when a file changes', async () => {
- const drive = new Drive();
- handleRequest(drive, 201, DEFAULT_FILE);
- let called = false;
- drive.fileChanged.connect((sender, args) => {
- expect(sender).toBe(drive);
- expect(args.type).toBe('new');
- expect(args.oldValue).toBeNull();
- expect(args.newValue!.path).toBe(DEFAULT_FILE.path);
- called = true;
- });
- await drive.newUntitled();
- expect(called).toBe(true);
- });
- });
- describe('#isDisposed', () => {
- it('should test whether the drive is disposed', () => {
- const drive = new Drive();
- expect(drive.isDisposed).toBe(false);
- drive.dispose();
- expect(drive.isDisposed).toBe(true);
- });
- });
- describe('#dispose()', () => {
- it('should dispose of the resources used by the drive', () => {
- const drive = new Drive();
- expect(drive.isDisposed).toBe(false);
- drive.dispose();
- expect(drive.isDisposed).toBe(true);
- drive.dispose();
- expect(drive.isDisposed).toBe(true);
- });
- });
- describe('#get()', () => {
- it('should get a file', async () => {
- const drive = new Drive();
- handleRequest(drive, 200, DEFAULT_FILE);
- const options: Contents.IFetchOptions = { type: 'file' };
- const get = drive.get('/foo', options);
- const model = await get;
- expect(model.path).toBe(DEFAULT_FILE.path);
- });
- it('should get a directory', async () => {
- const drive = new Drive();
- handleRequest(drive, 200, DEFAULT_DIR);
- const options: Contents.IFetchOptions = { type: 'directory' };
- const get = drive.get('/foo', options);
- const model = await get;
- expect(model.content[0].path).toBe(DEFAULT_DIR.content[0].path);
- });
- it('should accept server settings', async () => {
- const drive = new Drive({ serverSettings });
- handleRequest(drive, 200, DEFAULT_DIR);
- const options: Contents.IFetchOptions = { type: 'directory' };
- const get = drive.get('/foo', options);
- const model = await get;
- expect(model.content[0].path).toBe(DEFAULT_DIR.content[0].path);
- });
- it('should fail for an incorrect response', async () => {
- const drive = new Drive();
- handleRequest(drive, 201, DEFAULT_DIR);
- const get = drive.get('/foo');
- await expectFailure(get, 'Invalid response: 201 Created');
- });
- });
- describe('#getDownloadUrl()', () => {
- const settings = ServerConnection.makeSettings({
- baseUrl: 'http://foo'
- });
- it('should get the url of a file', async () => {
- const drive = new Drive({ serverSettings: settings });
- const test1 = drive.getDownloadUrl('bar.txt');
- const test2 = drive.getDownloadUrl('fizz/buzz/bar.txt');
- const test3 = drive.getDownloadUrl('/bar.txt');
- const urls = await Promise.all([test1, test2, test3]);
- expect(urls[0]).toBe('http://foo/files/bar.txt');
- expect(urls[1]).toBe('http://foo/files/fizz/buzz/bar.txt');
- expect(urls[2]).toBe('http://foo/files/bar.txt');
- });
- it('should encode characters', async () => {
- const drive = new Drive({ serverSettings: settings });
- const url = await drive.getDownloadUrl('b ar?3.txt');
- expect(url).toBe('http://foo/files/b%20ar%3F3.txt');
- });
- it('should not handle relative paths', async () => {
- const drive = new Drive({ serverSettings: settings });
- const url = await drive.getDownloadUrl('fizz/../bar.txt');
- expect(url).toBe('http://foo/files/fizz/../bar.txt');
- });
- });
- describe('#newUntitled()', () => {
- it('should create a file', async () => {
- const drive = new Drive();
- handleRequest(drive, 201, DEFAULT_FILE);
- const model = await drive.newUntitled({ path: '/foo' });
- expect(model.path).toBe(DEFAULT_FILE.path);
- });
- it('should create a directory', async () => {
- const drive = new Drive();
- handleRequest(drive, 201, DEFAULT_DIR);
- const options: Contents.ICreateOptions = {
- path: '/foo',
- type: 'directory'
- };
- const newDir = drive.newUntitled(options);
- const model = await newDir;
- expect(model.content[0].path).toBe(DEFAULT_DIR.content[0].path);
- });
- it('should emit the fileChanged signal', async () => {
- const drive = new Drive();
- handleRequest(drive, 201, DEFAULT_FILE);
- let called = false;
- drive.fileChanged.connect((sender, args) => {
- expect(args.type).toBe('new');
- expect(args.oldValue).toBeNull();
- expect(args.newValue!.path).toBe(DEFAULT_FILE.path);
- called = true;
- });
- await drive.newUntitled({ type: 'file', ext: 'test' });
- expect(called).toBe(true);
- });
- it('should accept server settings', async () => {
- const drive = new Drive({ serverSettings });
- handleRequest(drive, 201, DEFAULT_DIR);
- const options: Contents.ICreateOptions = {
- path: '/foo',
- type: 'file',
- ext: 'txt'
- };
- const model = await drive.newUntitled(options);
- expect(model.content[0].path).toBe(DEFAULT_DIR.content[0].path);
- });
- it('should fail for an incorrect model', async () => {
- const drive = new Drive();
- const dir = JSON.parse(JSON.stringify(DEFAULT_DIR));
- dir.name = 1;
- handleRequest(drive, 201, dir);
- const options: Contents.ICreateOptions = {
- path: '/foo',
- type: 'file',
- ext: 'py'
- };
- const newFile = drive.newUntitled(options);
- await expectFailure(newFile);
- });
- it('should fail for an incorrect response', async () => {
- const drive = new Drive();
- handleRequest(drive, 200, DEFAULT_DIR);
- const newDir = drive.newUntitled();
- await expectFailure(newDir, 'Invalid response: 200 OK');
- });
- });
- describe('#delete()', () => {
- it('should delete a file', async () => {
- const drive = new Drive();
- handleRequest(drive, 204, {});
- await drive.delete('/foo/bar.txt');
- });
- it('should emit the fileChanged signal', async () => {
- const drive = new Drive();
- const path = '/foo/bar.txt';
- handleRequest(drive, 204, { path });
- let called = false;
- drive.fileChanged.connect((sender, args) => {
- expect(args.type).toBe('delete');
- expect(args.oldValue!.path).toBe('/foo/bar.txt');
- called = true;
- });
- await drive.delete(path);
- expect(called).toBe(true);
- });
- it('should accept server settings', async () => {
- const drive = new Drive({ serverSettings });
- handleRequest(drive, 204, {});
- await drive.delete('/foo/bar.txt');
- });
- it('should fail for an incorrect response', async () => {
- const drive = new Drive();
- handleRequest(drive, 200, {});
- const del = drive.delete('/foo/bar.txt');
- await expectFailure(del, 'Invalid response: 200 OK');
- });
- it('should throw a specific error', async () => {
- const drive = new Drive();
- handleRequest(drive, 400, {});
- const del = drive.delete('/foo/');
- await expectFailure(del, '');
- });
- it('should throw a general error', async () => {
- const drive = new Drive();
- handleRequest(drive, 500, {});
- const del = drive.delete('/foo/');
- await expectFailure(del, '');
- });
- });
- describe('#rename()', () => {
- it('should rename a file', async () => {
- const drive = new Drive();
- handleRequest(drive, 200, DEFAULT_FILE);
- const rename = drive.rename('/foo/bar.txt', '/foo/baz.txt');
- const model = await rename;
- expect(model.created).toBe(DEFAULT_FILE.created);
- });
- it('should emit the fileChanged signal', async () => {
- const drive = new Drive();
- handleRequest(drive, 200, DEFAULT_FILE);
- let called = false;
- drive.fileChanged.connect((sender, args) => {
- expect(args.type).toBe('rename');
- expect(args.oldValue!.path).toBe('/foo/bar.txt');
- expect(args.newValue!.path).toBe('foo/test');
- called = true;
- });
- await drive.rename('/foo/bar.txt', '/foo/baz.txt');
- expect(called).toBe(true);
- });
- it('should accept server settings', async () => {
- const drive = new Drive({ serverSettings });
- handleRequest(drive, 200, DEFAULT_FILE);
- const rename = drive.rename('/foo/bar.txt', '/foo/baz.txt');
- const model = await rename;
- expect(model.created).toBe(DEFAULT_FILE.created);
- });
- it('should fail for an incorrect model', async () => {
- const drive = new Drive();
- const dir = JSON.parse(JSON.stringify(DEFAULT_FILE));
- delete dir.path;
- handleRequest(drive, 200, dir);
- const rename = drive.rename('/foo/bar.txt', '/foo/baz.txt');
- await expectFailure(rename);
- });
- it('should fail for an incorrect response', async () => {
- const drive = new Drive();
- handleRequest(drive, 201, DEFAULT_FILE);
- const rename = drive.rename('/foo/bar.txt', '/foo/baz.txt');
- await expectFailure(rename, 'Invalid response: 201 Created');
- });
- });
- describe('#save()', () => {
- it('should save a file', async () => {
- const drive = new Drive();
- handleRequest(drive, 200, DEFAULT_FILE);
- const save = drive.save('/foo', { type: 'file', name: 'test' });
- const model = await save;
- expect(model.created).toBe(DEFAULT_FILE.created);
- });
- it('should create a new file', async () => {
- const drive = new Drive();
- handleRequest(drive, 201, DEFAULT_FILE);
- const save = drive.save('/foo', { type: 'file', name: 'test' });
- const model = await save;
- expect(model.created).toBe(DEFAULT_FILE.created);
- });
- it('should emit the fileChanged signal', async () => {
- const drive = new Drive();
- handleRequest(drive, 201, DEFAULT_FILE);
- let called = false;
- drive.fileChanged.connect((sender, args) => {
- expect(args.type).toBe('save');
- expect(args.oldValue).toBeNull();
- expect(args.newValue!.path).toBe(DEFAULT_FILE.path);
- called = true;
- });
- await drive.save('/foo', { type: 'file', name: 'test' });
- expect(called).toBe(true);
- });
- it('should accept server settings', async () => {
- const drive = new Drive({ serverSettings });
- handleRequest(drive, 200, DEFAULT_FILE);
- const save = drive.save('/foo', { type: 'file', name: 'test' });
- const model = await save;
- expect(model.created).toBe(DEFAULT_FILE.created);
- });
- it('should fail for an incorrect model', async () => {
- const drive = new Drive();
- const file = JSON.parse(JSON.stringify(DEFAULT_FILE));
- delete file.format;
- handleRequest(drive, 200, file);
- const save = drive.save('/foo', { type: 'file', name: 'test' });
- await expectFailure(save);
- });
- it('should fail for an incorrect response', async () => {
- const drive = new Drive();
- handleRequest(drive, 204, DEFAULT_FILE);
- const save = drive.save('/foo', { type: 'file', name: 'test' });
- await expectFailure(save, 'Invalid response: 204 No Content');
- });
- });
- describe('#copy()', () => {
- it('should copy a file', async () => {
- const drive = new Drive();
- handleRequest(drive, 201, DEFAULT_FILE);
- const model = await drive.copy('/foo/bar.txt', '/baz');
- expect(model.created).toBe(DEFAULT_FILE.created);
- });
- it('should emit the fileChanged signal', async () => {
- const drive = new Drive();
- handleRequest(drive, 201, DEFAULT_FILE);
- let called = false;
- drive.fileChanged.connect((sender, args) => {
- expect(args.type).toBe('new');
- expect(args.oldValue).toBeNull();
- expect(args.newValue!.path).toBe(DEFAULT_FILE.path);
- called = true;
- });
- await drive.copy('/foo/bar.txt', '/baz');
- expect(called).toBe(true);
- });
- it('should accept server settings', async () => {
- const drive = new Drive({ serverSettings });
- handleRequest(drive, 201, DEFAULT_FILE);
- const model = await drive.copy('/foo/bar.txt', '/baz');
- expect(model.created).toBe(DEFAULT_FILE.created);
- });
- it('should fail for an incorrect model', async () => {
- const drive = new Drive();
- const file = JSON.parse(JSON.stringify(DEFAULT_FILE));
- delete file.type;
- handleRequest(drive, 201, file);
- const copy = drive.copy('/foo/bar.txt', '/baz');
- await expectFailure(copy);
- });
- it('should fail for an incorrect response', async () => {
- const drive = new Drive();
- handleRequest(drive, 200, DEFAULT_FILE);
- const copy = drive.copy('/foo/bar.txt', '/baz');
- await expectFailure(copy, 'Invalid response: 200 OK');
- });
- });
- describe('#createCheckpoint()', () => {
- it('should create a checkpoint', async () => {
- const drive = new Drive();
- handleRequest(drive, 201, DEFAULT_CP);
- const checkpoint = drive.createCheckpoint('/foo/bar.txt');
- const model = await checkpoint;
- expect(model.last_modified).toBe(DEFAULT_CP.last_modified);
- });
- it('should accept server settings', async () => {
- const drive = new Drive({ serverSettings });
- handleRequest(drive, 201, DEFAULT_CP);
- const checkpoint = drive.createCheckpoint('/foo/bar.txt');
- const model = await checkpoint;
- expect(model.last_modified).toBe(DEFAULT_CP.last_modified);
- });
- it('should fail for an incorrect model', async () => {
- const drive = new Drive();
- const cp = JSON.parse(JSON.stringify(DEFAULT_CP));
- delete cp.last_modified;
- handleRequest(drive, 201, cp);
- const checkpoint = drive.createCheckpoint('/foo/bar.txt');
- await expectFailure(checkpoint);
- });
- it('should fail for an incorrect response', async () => {
- const drive = new Drive();
- handleRequest(drive, 200, DEFAULT_CP);
- const checkpoint = drive.createCheckpoint('/foo/bar.txt');
- await expectFailure(checkpoint, 'Invalid response: 200 OK');
- });
- });
- describe('#listCheckpoints()', () => {
- it('should list the checkpoints', async () => {
- const drive = new Drive();
- handleRequest(drive, 200, [DEFAULT_CP, DEFAULT_CP]);
- const checkpoints = drive.listCheckpoints('/foo/bar.txt');
- const models = await checkpoints;
- expect(models[0].last_modified).toBe(DEFAULT_CP.last_modified);
- });
- it('should accept server settings', async () => {
- const drive = new Drive({ serverSettings });
- handleRequest(drive, 200, [DEFAULT_CP, DEFAULT_CP]);
- const checkpoints = drive.listCheckpoints('/foo/bar.txt');
- const models = await checkpoints;
- expect(models[0].last_modified).toBe(DEFAULT_CP.last_modified);
- });
- it('should fail for an incorrect model', async () => {
- const drive = new Drive();
- const cp = JSON.parse(JSON.stringify(DEFAULT_CP));
- delete cp.id;
- handleRequest(drive, 200, [cp, DEFAULT_CP]);
- const checkpoints = drive.listCheckpoints('/foo/bar.txt');
- await expectFailure(checkpoints);
- handleRequest(drive, 200, DEFAULT_CP);
- const newCheckpoints = drive.listCheckpoints('/foo/bar.txt');
- await expectFailure(newCheckpoints, 'Invalid Checkpoint list');
- });
- it('should fail for an incorrect response', async () => {
- const drive = new Drive();
- handleRequest(drive, 201, {});
- const checkpoints = drive.listCheckpoints('/foo/bar.txt');
- await expectFailure(checkpoints, 'Invalid response: 201 Created');
- });
- });
- describe('#restoreCheckpoint()', () => {
- it('should restore a checkpoint', () => {
- const drive = new Drive();
- handleRequest(drive, 204, {});
- const checkpoint = drive.restoreCheckpoint('/foo/bar.txt', DEFAULT_CP.id);
- return checkpoint;
- });
- it('should accept server settings', () => {
- const drive = new Drive({ serverSettings });
- handleRequest(drive, 204, {});
- const checkpoint = drive.restoreCheckpoint('/foo/bar.txt', DEFAULT_CP.id);
- return checkpoint;
- });
- it('should fail for an incorrect response', async () => {
- const drive = new Drive();
- handleRequest(drive, 200, {});
- const checkpoint = drive.restoreCheckpoint('/foo/bar.txt', DEFAULT_CP.id);
- await expectFailure(checkpoint, 'Invalid response: 200 OK');
- });
- });
- describe('#deleteCheckpoint()', () => {
- it('should delete a checkpoint', () => {
- const drive = new Drive();
- handleRequest(drive, 204, {});
- return drive.deleteCheckpoint('/foo/bar.txt', DEFAULT_CP.id);
- });
- it('should accept server settings', () => {
- const drive = new Drive({ serverSettings });
- handleRequest(drive, 204, {});
- return drive.deleteCheckpoint('/foo/bar.txt', DEFAULT_CP.id);
- });
- it('should fail for an incorrect response', async () => {
- const drive = new Drive();
- handleRequest(drive, 200, {});
- const checkpoint = drive.deleteCheckpoint('/foo/bar.txt', DEFAULT_CP.id);
- await expectFailure(checkpoint, 'Invalid response: 200 OK');
- });
- });
- describe('integration tests', () => {
- it('should list a directory and get the file contents', async () => {
- let content: Contents.IModel[];
- let path = '';
- const listing = await contents.get('src');
- content = listing.content as Contents.IModel[];
- let called = false;
- for (let i = 0; i < content.length; i++) {
- if (content[i].type === 'file') {
- path = content[i].path;
- const msg = await contents.get(path, { type: 'file' });
- expect(msg.path).toBe(path);
- called = true;
- }
- }
- expect(called).toBe(true);
- });
- it('should create a new file, rename it, and delete it', async () => {
- const options: Contents.ICreateOptions = { type: 'file', ext: '.ipynb' };
- const model0 = await contents.newUntitled(options);
- const model1 = await contents.rename(model0.path, 'foo.ipynb');
- expect(model1.path).toBe('foo.ipynb');
- return contents.delete('foo.ipynb');
- });
- it('should create a file by name and delete it', async () => {
- const options: Partial<Contents.IModel> = {
- type: 'file',
- content: '',
- format: 'text'
- };
- await contents.save('baz.txt', options);
- await contents.delete('baz.txt');
- });
- it('should exercise the checkpoint API', async () => {
- const options: Partial<Contents.IModel> = {
- type: 'file',
- format: 'text',
- content: 'foo'
- };
- let checkpoint: Contents.ICheckpointModel;
- const model0 = await contents.save('baz.txt', options);
- expect(model0.name).toBe('baz.txt');
- const value = await contents.createCheckpoint('baz.txt');
- checkpoint = value;
- const checkpoints = await contents.listCheckpoints('baz.txt');
- expect(checkpoints[0]).toEqual(checkpoint);
- await contents.restoreCheckpoint('baz.txt', checkpoint.id);
- await contents.deleteCheckpoint('baz.txt', checkpoint.id);
- await contents.delete('baz.txt');
- });
- });
- });
|