Skip to content

Commit

Permalink
Group tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fortuna committed Sep 10, 2018
1 parent 08621df commit b7b71bd
Showing 1 changed file with 152 additions and 127 deletions.
279 changes: 152 additions & 127 deletions src/shadowbox/server/manager_service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,160 +35,185 @@ describe('ShadowsocksManagerService', () => {
expect(responseProcessed).toEqual(true);
});

it('Return default name by default', (done) => {
const repo = new MockAccessKeyRepository();
const serverConfig = new InMemoryConfig({} as ServerConfigJson);
const service = new ShadowsocksManagerService('default name', serverConfig, repo, null);
service.getServer(
{params: {}}, {
send: (httpCode, data: ServerInfo) => {
expect(httpCode).toEqual(200);
expect(data.name).toEqual('default name');
responseProcessed = true;
}
},
done);
});

it('Rename changes the server name', (done) => {
const repo = new MockAccessKeyRepository();
const serverConfig = new InMemoryConfig({} as ServerConfigJson);
const service = new ShadowsocksManagerService('default name', serverConfig, repo, null);
service.renameServer(
{params: {name: 'new name'}}, {
send: (httpCode, _) => {
expect(httpCode).toEqual(204);
expect(serverConfig.written.name).toEqual('new name');
responseProcessed = true;
}
},
done);
});

it('lists access keys in order', (done) => {
const repo = new MockAccessKeyRepository();
const service = new ShadowsocksManagerService('default name', null, repo, null);

// Create 2 access keys with names.
Promise
.all([
createNewAccessKeyWithName(repo, 'keyName1'), createNewAccessKeyWithName(repo, 'keyName2')
])
.then((keys) => {
// Verify that response returns keys in correct order with correct names.
const res = {
send: (httpCode, data) => {
describe('getServer', () => {
it('Return default name if name is absent', (done) => {
const repo = new MockAccessKeyRepository();
const serverConfig = new InMemoryConfig({} as ServerConfigJson);
const service = new ShadowsocksManagerService('default name', serverConfig, repo, null);
service.getServer(
{params: {}}, {
send: (httpCode, data: ServerInfo) => {
expect(httpCode).toEqual(200);
expect(data.accessKeys.length).toEqual(2);
expect(data.accessKeys[0].name).toEqual(keys[0].name);
expect(data.accessKeys[0].id).toEqual(keys[0].id);
expect(data.accessKeys[1].name).toEqual(keys[1].name);
expect(data.accessKeys[1].id).toEqual(keys[1].id);
responseProcessed = true; // required for afterEach to pass.
expect(data.name).toEqual('default name');
responseProcessed = true;
}
};
service.listAccessKeys({params: {}}, res, done);
});
});

it('creates keys', (done) => {
const repo = new MockAccessKeyRepository();
const service = new ShadowsocksManagerService('default name', null, repo, null);

// Verify that response returns a key with the expected properties.
const res = {
send: (httpCode, data) => {
expect(httpCode).toEqual(201);
const expectedProperties = ['id', 'name', 'password', 'port', 'method', 'accessUrl'];
expect(Object.keys(data).sort()).toEqual(expectedProperties.sort());
responseProcessed = true; // required for afterEach to pass.
}
};
service.createNewAccessKey({params: {}}, res, done);
},
done);
});
it('Return saved name', (done) => {
const repo = new MockAccessKeyRepository();
const serverConfig = new InMemoryConfig({name: 'Server'} as ServerConfigJson);
const service = new ShadowsocksManagerService('default name', serverConfig, repo, null);
service.getServer(
{params: {}}, {
send: (httpCode, data: ServerInfo) => {
expect(httpCode).toEqual(200);
expect(data.name).toEqual('Server');
responseProcessed = true;
}
},
done);
});
});

it('removes keys', (done) => {
const repo = new MockAccessKeyRepository();
const service = new ShadowsocksManagerService('default name', null, repo, null);

// Create 2 access keys with names.
Promise
.all([
createNewAccessKeyWithName(repo, 'keyName1'), createNewAccessKeyWithName(repo, 'keyName2')
])
.then((keys) => {
const res = {
send: (httpCode, data) => {
describe('renameServer', () => {
it('Rename changes the server name', (done) => {
const repo = new MockAccessKeyRepository();
const serverConfig = new InMemoryConfig({} as ServerConfigJson);
const service = new ShadowsocksManagerService('default name', serverConfig, repo, null);
service.renameServer(
{params: {name: 'new name'}}, {
send: (httpCode, _) => {
expect(httpCode).toEqual(204);
// expect that the only remaining key is the 2nd key we created.
expect(getFirstAccessKey(repo).id === keys[1].id);
responseProcessed = true; // required for afterEach to pass.
expect(serverConfig.written.name).toEqual('new name');
responseProcessed = true;
}
};
// remove the 1st key.
service.removeAccessKey({params: {id: keys[0].id}}, res, done);
});
},
done);
});
});

it('renames keys', (done) => {
const repo = new MockAccessKeyRepository();
const service = new ShadowsocksManagerService('default name', null, repo, null);
const OLD_NAME = 'oldName';
const NEW_NAME = 'newName';
describe('listAccessKeys', () => {
it('lists access keys in order', (done) => {
const repo = new MockAccessKeyRepository();
const service = new ShadowsocksManagerService('default name', null, repo, null);

// Create 2 access keys with names.
Promise
.all([
createNewAccessKeyWithName(repo, 'keyName1'),
createNewAccessKeyWithName(repo, 'keyName2')
])
.then((keys) => {
// Verify that response returns keys in correct order with correct names.
const res = {
send: (httpCode, data) => {
expect(httpCode).toEqual(200);
expect(data.accessKeys.length).toEqual(2);
expect(data.accessKeys[0].name).toEqual(keys[0].name);
expect(data.accessKeys[0].id).toEqual(keys[0].id);
expect(data.accessKeys[1].name).toEqual(keys[1].name);
expect(data.accessKeys[1].id).toEqual(keys[1].id);
responseProcessed = true; // required for afterEach to pass.
}
};
service.listAccessKeys({params: {}}, res, done);
});
});
});

createNewAccessKeyWithName(repo, OLD_NAME).then((key) => {
expect(getFirstAccessKey(repo).name === OLD_NAME);
describe('createNewAccessKey', () => {
it('creates keys', (done) => {
const repo = new MockAccessKeyRepository();
const service = new ShadowsocksManagerService('default name', null, repo, null);

// Verify that response returns a key with the expected properties.
const res = {
send: (httpCode, data) => {
expect(httpCode).toEqual(204);
expect(getFirstAccessKey(repo).name === NEW_NAME);
expect(httpCode).toEqual(201);
const expectedProperties = ['id', 'name', 'password', 'port', 'method', 'accessUrl'];
expect(Object.keys(data).sort()).toEqual(expectedProperties.sort());
responseProcessed = true; // required for afterEach to pass.
}
};
service.renameAccessKey({params: {id: key.id, name: NEW_NAME}}, res, done);
service.createNewAccessKey({params: {}}, res, done);
});
});

it('Rename returns a 500 when the repository throws an exception', (done) => {
const repo = new MockAccessKeyRepository();
spyOn(repo, 'renameAccessKey').and.throwError('cannot write to disk');
const service = new ShadowsocksManagerService('default name', null, repo, null);
it('Create returns a 500 when the repository throws an exception', (done) => {
const repo = new MockAccessKeyRepository();
spyOn(repo, 'createNewAccessKey').and.throwError('cannot write to disk');
const service = new ShadowsocksManagerService('default name', null, repo, null);

createNewAccessKeyWithName(repo, 'oldName').then((key) => {
const res = {send: (httpCode, data) => {}};
service.renameAccessKey({params: {id: key.id, name: 'newName'}}, res, (error) => {
service.createNewAccessKey({params: {}}, res, (error) => {
expect(error.statusCode).toEqual(500);
responseProcessed = true; // required for afterEach to pass.
done();
});
});
});

it('Create returns a 500 when the repository throws an exception', (done) => {
const repo = new MockAccessKeyRepository();
spyOn(repo, 'createNewAccessKey').and.throwError('cannot write to disk');
const service = new ShadowsocksManagerService('default name', null, repo, null);

const res = {send: (httpCode, data) => {}};
service.createNewAccessKey({params: {}}, res, (error) => {
expect(error.statusCode).toEqual(500);
responseProcessed = true; // required for afterEach to pass.
done();
describe('removeAccessKey', () => {
it('removes keys', (done) => {
const repo = new MockAccessKeyRepository();
const service = new ShadowsocksManagerService('default name', null, repo, null);

// Create 2 access keys with names.
Promise
.all([
createNewAccessKeyWithName(repo, 'keyName1'),
createNewAccessKeyWithName(repo, 'keyName2')
])
.then((keys) => {
const res = {
send: (httpCode, data) => {
expect(httpCode).toEqual(204);
// expect that the only remaining key is the 2nd key we created.
expect(getFirstAccessKey(repo).id === keys[1].id);
responseProcessed = true; // required for afterEach to pass.
}
};
// remove the 1st key.
service.removeAccessKey({params: {id: keys[0].id}}, res, done);
});
});
it('Remove returns a 500 when the repository throws an exception', (done) => {
const repo = new MockAccessKeyRepository();
spyOn(repo, 'removeAccessKey').and.throwError('cannot write to disk');
const service = new ShadowsocksManagerService('default name', null, repo, null);

// Create 2 access keys with names.
createNewAccessKeyWithName(repo, 'keyName1').then((key) => {
const res = {send: (httpCode, data) => {}};
service.removeAccessKey({params: {id: key.id}}, res, (error) => {
expect(error.statusCode).toEqual(500);
responseProcessed = true; // required for afterEach to pass.
done();
});
});
});
});

it('Remove returns a 500 when the repository throws an exception', (done) => {
const repo = new MockAccessKeyRepository();
spyOn(repo, 'removeAccessKey').and.throwError('cannot write to disk');
const service = new ShadowsocksManagerService('default name', null, repo, null);

// Create 2 access keys with names.
createNewAccessKeyWithName(repo, 'keyName1').then((key) => {
const res = {send: (httpCode, data) => {}};
service.removeAccessKey({params: {id: key.id}}, res, (error) => {
expect(error.statusCode).toEqual(500);
responseProcessed = true; // required for afterEach to pass.
done();
describe('renameAccessKey', () => {
it('renames keys', (done) => {
const repo = new MockAccessKeyRepository();
const service = new ShadowsocksManagerService('default name', null, repo, null);
const OLD_NAME = 'oldName';
const NEW_NAME = 'newName';

createNewAccessKeyWithName(repo, OLD_NAME).then((key) => {
expect(getFirstAccessKey(repo).name === OLD_NAME);
const res = {
send: (httpCode, data) => {
expect(httpCode).toEqual(204);
expect(getFirstAccessKey(repo).name === NEW_NAME);
responseProcessed = true; // required for afterEach to pass.
}
};
service.renameAccessKey({params: {id: key.id, name: NEW_NAME}}, res, done);
});
});
it('Rename returns a 500 when the repository throws an exception', (done) => {
const repo = new MockAccessKeyRepository();
spyOn(repo, 'renameAccessKey').and.throwError('cannot write to disk');
const service = new ShadowsocksManagerService('default name', null, repo, null);

createNewAccessKeyWithName(repo, 'oldName').then((key) => {
const res = {send: (httpCode, data) => {}};
service.renameAccessKey({params: {id: key.id, name: 'newName'}}, res, (error) => {
expect(error.statusCode).toEqual(500);
responseProcessed = true; // required for afterEach to pass.
done();
});
});
});
});
Expand Down

0 comments on commit b7b71bd

Please sign in to comment.