Skip to content

Commit

Permalink
Added Users Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
R1c4rdCo5t4 committed Jun 21, 2024
1 parent bbe7ec3 commit a01b696
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestDatabases } from '../../src/databases/TestDatabases';
import { Services } from '../../src/services/Services';
import { ResourceType } from '@notespace/shared/src/workspace/types/resource';
import { excludeRoot } from './utils';
import { excludeRoot } from '../workspaces/utils';

let services: Services;

Expand Down
8 changes: 8 additions & 0 deletions code/server/test/users/authentication.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// implement integration tests for user authentication using firebase auth sdk

describe('User authentication', () => {
test('should register a user', async () => {
// TODO
expect(true).toBe(true);
});
});
65 changes: 65 additions & 0 deletions code/server/test/users/users.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { TestDatabases } from '../../src/databases/TestDatabases';
import { Services } from '../../src/services/Services';
import { getRandomUserId } from './utils';
import { User } from '@notespace/shared/src/users/types';

let services: Services;

beforeEach(() => {
services = new Services(new TestDatabases());
});

describe('User operations', () => {
test('should create a user', async () => {
const id = getRandomUserId();
const userData = {
name: 'test',
email: '[email protected]',
};
await services.users.createUser(id, userData);
const user = await services.users.getUser(id);
expect(user.name).toEqual('test');
expect(user.email).toEqual('[email protected]');
});

test('should update a user', async () => {
const id = getRandomUserId();
const userData = {
name: 'test',
email: '[email protected]',
};
await services.users.createUser(id, userData);
await services.users.updateUser(id, { name: 'test2' });
const user = await services.users.getUser(id);
expect(user.name).toEqual('test2');
});

test('should delete a user', async () => {
const id = getRandomUserId();
const userData = {
name: 'test',
email: '[email protected]',
};
await services.users.createUser(id, userData);
await services.users.deleteUser(id);
await expect(services.users.getUser(id)).rejects.toThrow('User not found');
});

test('should get all users', async () => {
const id1 = getRandomUserId();
const user1Data = {
name: 'test1',
email: '[email protected]',
};
const id2 = getRandomUserId();
const user2Data = {
name: 'test2',
email: '[email protected]',
};
await services.users.createUser(id1, user1Data);
await services.users.createUser(id2, user2Data);
const users: User[] = await services.users.getUsers();
expect(users.some(u => u.id === id1)).toBe(true);
expect(users.some(u => u.id === id2)).toBe(true);
});
});
3 changes: 3 additions & 0 deletions code/server/test/users/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getRandomUserId() {
return Math.random().toString(36).substring(2, 30);
}
44 changes: 44 additions & 0 deletions code/server/test/workspaces/members.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { TestDatabases } from '../../src/databases/TestDatabases';
import { Services } from '../../src/services/Services';
import { getRandomUserId } from '../users/utils';

let services: Services;

beforeEach(() => {
services = new Services(new TestDatabases());
});

describe('Workspace members operations', () => {
test('should add a member to a workspace', async () => {
const wid = await services.workspaces.createWorkspace('test', false);
const userId = getRandomUserId();
const userData = {
name: 'test',
email: '[email protected]',
};
await services.users.createUser(userId, userData);
await services.workspaces.addWorkspaceMember(wid, userData.email);
const workspace = await services.workspaces.getWorkspace(wid);
expect(workspace.members).toContain(userData.email);
});

test('should remove a member from a workspace', async () => {
const wid = await services.workspaces.createWorkspace('test', false);
const userId = getRandomUserId();
const userData = {
name: 'test',
email: '[email protected]',
};

// add user to workspace
await services.users.createUser(userId, userData);
await services.workspaces.addWorkspaceMember(wid, userData.email);
const workspace = await services.workspaces.getWorkspace(wid);
expect(workspace.members).toContain(userData.email);

// remove user from workspace
await services.workspaces.removeWorkspaceMember(wid, userData.email);
const workspaceAfter = await services.workspaces.getWorkspace(wid);
expect(workspaceAfter.members).not.toContain(userData.email);
});
});

0 comments on commit a01b696

Please sign in to comment.