Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/users/users-api-client/users-api-client.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ describe('UsersApiClient', () => {

describe('removeUserFromDatabase', () => {
it('should return 200 and message', async () => {
const { uId } = await await usersClient.addUserToDatabase(config.database, testEmail).then(response => response.json());
const response = await usersClient.removeUserFromDatabase(config.database, uId);
await usersClient.addUserToDatabase(config.database, testEmail);
const response = await usersClient.removeUserFromDatabase(config.database, testEmail);
const body = await response.json();
expect(response.status).toEqual(200);
expect(body.status).toEqual(UserApiResponseStatus.success);
Expand Down
306 changes: 168 additions & 138 deletions src/users/users-api-client/users-api-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,178 +5,208 @@ import { UsersApiClient } from './users-api-client';
import { UsersApiRow } from './users-api-row';

describe('UsersApiClient', () => {
let sut: UsersApiClient;

let apiClient;
let apiClientResponse;
let companyId;
let database;
let fakeFormData;
let rows;

beforeEach(() => {
companyId = 123;
database = 'zzz';
rows = [new UsersApiRow(1, 'x', 1, 'y', 'z', 1, 1, 'a', 'b')];
fakeFormData = createFakeFormData();
apiClientResponse = createFakeResponseBody(200, rows);
apiClient = createFakeBugSplatApiClient(fakeFormData, apiClientResponse);

sut = new UsersApiClient(apiClient);
});

describe('getUsers', () => {
let result;
let request;

beforeEach(async () => {
request = { database };
result = await sut.getUsers(request);
});

it('should throw if both database and companyId are specified', async () => {
request = { database, companyId: 1 };
await expectAsync(sut.getUsers(request)).toBeRejectedWithError('Cannot specify both database and companyId');
});
let sut: UsersApiClient;

let apiClient;
let apiClientResponse;
let companyId;
let database;
let fakeFormData;
let rows;

beforeEach(() => {
companyId = 123;
database = 'zzz';
rows = [new UsersApiRow(1, 'x', 1, 'y', 'z', 1, 1, 'a', 'b')];
fakeFormData = createFakeFormData();
apiClientResponse = createFakeResponseBody(200, rows);
apiClient = createFakeBugSplatApiClient(fakeFormData, apiClientResponse);

sut = new UsersApiClient(apiClient);
});

describe('getUsers', () => {
let result;
let request;

beforeEach(async () => {
request = { database };
result = await sut.getUsers(request);
});

it('should throw if neither database nor companyId are specified', async () => {
request = {};
await expectAsync(sut.getUsers(request)).toBeRejectedWithError('Must specify either database or companyId');
});
it('should throw if both database and companyId are specified', async () => {
request = { database, companyId: 1 };
await expectAsync(sut.getUsers(request)).toBeRejectedWithError(
'Cannot specify both database and companyId'
);
});

it('should call fetch with url containing database param', () => {
expect(apiClient.fetch).toHaveBeenCalledWith(`/api/user/users.php?database=${database}`);
});
it('should throw if neither database nor companyId are specified', async () => {
request = {};
await expectAsync(sut.getUsers(request)).toBeRejectedWithError(
'Must specify either database or companyId'
);
});

it('should call fetch with url containing companyId param', async () => {
request = { companyId: 1 };
await sut.getUsers(request);
expect(apiClient.fetch).toHaveBeenCalledWith(`/api/user/users.php?companyId=${request.companyId}`);
});
it('should call fetch with url containing database param', () => {
expect(apiClient.fetch).toHaveBeenCalledWith(
`/api/user/users.php?database=${database}`
);
});

it('should return rows from response', () => {
expect(result.rows).toEqual(rows);
});
it('should call fetch with url containing companyId param', async () => {
request = { companyId: 1 };
await sut.getUsers(request);
expect(apiClient.fetch).toHaveBeenCalledWith(
`/api/user/users.php?companyId=${request.companyId}`
);
});

describe('addUserToDatabase', () => {
let result;
let email;
it('should return rows from response', () => {
expect(result.rows).toEqual(rows);
});
});

beforeEach(async () => {
email = '☕️';
result = await sut.addUserToDatabase(database, email);
});
describe('addUserToDatabase', () => {
let result;
let email;

it('should call createFormData', () => {
expect(apiClient.createFormData).toHaveBeenCalled();
});
beforeEach(async () => {
email = '☕️';
result = await sut.addUserToDatabase(database, email);
});

it('should call append with database and email', () => {
expect(fakeFormData.append).toHaveBeenCalledWith('database', database);
expect(fakeFormData.append).toHaveBeenCalledWith('username', email);
});
it('should call createFormData', () => {
expect(apiClient.createFormData).toHaveBeenCalled();
});

it('should call fetch with url and request containing formData', () => {
expect(apiClient.fetch).toHaveBeenCalledWith('/api/user/users.php', jasmine.objectContaining({ method: 'POST', body: fakeFormData }));
});
it('should call append with database and email', () => {
expect(fakeFormData.append).toHaveBeenCalledWith('database', database);
expect(fakeFormData.append).toHaveBeenCalledWith('username', email);
});

it('should return response', () => {
expect(result).toEqual(apiClientResponse);
});
it('should call fetch with url and request containing formData', () => {
expect(apiClient.fetch).toHaveBeenCalledWith(
'/api/user/users.php',
jasmine.objectContaining({ method: 'POST', body: fakeFormData })
);
});

describe('removeUserFromDatabase', () => {
let result;
let uId;
it('should return response', () => {
expect(result).toEqual(apiClientResponse);
});
});

beforeEach(async () => {
uId = 1;
result = await sut.removeUserFromDatabase(database, uId);
});
describe('removeUserFromDatabase', () => {
let result;
let email;

it('should call fetch with url', () => {
expect(apiClient.fetch).toHaveBeenCalledWith(`/api/user/users.php?database=${database}&uId=${uId}`, jasmine.objectContaining({ method: 'DELETE' }));
});
beforeEach(async () => {
email = 'test@bugsplat.com';
result = await sut.removeUserFromDatabase(database, email);
});

it('should return response', () => {
expect(result).toEqual(apiClientResponse);
});
it('should call fetch with url', () => {
expect(apiClient.fetch).toHaveBeenCalledWith(
`/api/user/users.php?database=${database}&username=${encodeURIComponent(
email
)}`,
jasmine.objectContaining({ method: 'DELETE' })
);
});

describe('updateUserForDatabase', () => {
let result;
let email;
let isRestricted;
it('should return response', () => {
expect(result).toEqual(apiClientResponse);
});
});

beforeEach(async () => {
email = 'fred@bugsplat.com';
isRestricted = true;
result = await sut.updateUserForDatabase(database, email, isRestricted);
});
describe('updateUserForDatabase', () => {
let result;
let email;
let isRestricted;

it('should call createFormData', () => {
expect(apiClient.createFormData).toHaveBeenCalled();
});
beforeEach(async () => {
email = 'fred@bugsplat.com';
isRestricted = true;
result = await sut.updateUserForDatabase(database, email, isRestricted);
});

it('should call append with database and email', () => {
expect(fakeFormData.append).toHaveBeenCalledWith('database', database);
expect(fakeFormData.append).toHaveBeenCalledWith('username', email);
expect(fakeFormData.append).toHaveBeenCalledWith('rights', isRestricted ? '0' : '1');
});
it('should call createFormData', () => {
expect(apiClient.createFormData).toHaveBeenCalled();
});

it('should call fetch with url and request containing formData', () => {
expect(apiClient.fetch).toHaveBeenCalledWith('/api/user/users.php', jasmine.objectContaining({ method: 'POST', body: fakeFormData }));
});
it('should call append with database and email', () => {
expect(fakeFormData.append).toHaveBeenCalledWith('database', database);
expect(fakeFormData.append).toHaveBeenCalledWith('username', email);
expect(fakeFormData.append).toHaveBeenCalledWith(
'rights',
isRestricted ? '0' : '1'
);
});

it('should return response', () => {
expect(result).toEqual(apiClientResponse);
});
it('should call fetch with url and request containing formData', () => {
expect(apiClient.fetch).toHaveBeenCalledWith(
'/api/user/users.php',
jasmine.objectContaining({ method: 'POST', body: fakeFormData })
);
});

describe('addUserToCompany', () => {
let result;
let email;
it('should return response', () => {
expect(result).toEqual(apiClientResponse);
});
});

beforeEach(async () => {
email = '☕️';
result = await sut.addUserToCompany(companyId, email);
});
describe('addUserToCompany', () => {
let result;
let email;

beforeEach(async () => {
email = '☕️';
result = await sut.addUserToCompany(companyId, email);
});

it('should call createFormData', () => {
expect(apiClient.createFormData).toHaveBeenCalled();
});
it('should call createFormData', () => {
expect(apiClient.createFormData).toHaveBeenCalled();
});

it('should call append with companyId and email', () => {
expect(fakeFormData.append).toHaveBeenCalledWith('companyId', `${companyId}`);
expect(fakeFormData.append).toHaveBeenCalledWith('username', email);
});
it('should call append with companyId and email', () => {
expect(fakeFormData.append).toHaveBeenCalledWith(
'companyId',
`${companyId}`
);
expect(fakeFormData.append).toHaveBeenCalledWith('username', email);
});

it('should call fetch with url and request containing formData', () => {
expect(apiClient.fetch).toHaveBeenCalledWith('/api/user/users.php', jasmine.objectContaining({ method: 'POST', body: fakeFormData }));
});
it('should call fetch with url and request containing formData', () => {
expect(apiClient.fetch).toHaveBeenCalledWith(
'/api/user/users.php',
jasmine.objectContaining({ method: 'POST', body: fakeFormData })
);
});

it('should return response', () => {
expect(result).toEqual(apiClientResponse);
});
it('should return response', () => {
expect(result).toEqual(apiClientResponse);
});
});

describe('removeUserFromCompany', () => {
let result;
let uId;
describe('removeUserFromCompany', () => {
let result;
let uId;

beforeEach(async () => {
uId = 1;
result = await sut.removeUserFromCompany(companyId, uId);
});
beforeEach(async () => {
uId = 1;
result = await sut.removeUserFromCompany(companyId, uId);
});

it('should call fetch with url', () => {
expect(apiClient.fetch).toHaveBeenCalledWith(`/api/user/users.php?companyId=${companyId}&uId=${uId}`, jasmine.objectContaining({ method: 'DELETE' }));
});
it('should call fetch with url', () => {
expect(apiClient.fetch).toHaveBeenCalledWith(
`/api/user/users.php?companyId=${companyId}&uId=${uId}`,
jasmine.objectContaining({ method: 'DELETE' })
);
});

it('should return response', () => {
expect(result).toEqual(apiClientResponse);
});
it('should return response', () => {
expect(result).toEqual(apiClientResponse);
});
});
});
});
Loading