Skip to content

Commit

Permalink
new tests and MemberRole changes
Browse files Browse the repository at this point in the history
- new tests added
- enum with no strings
  • Loading branch information
e11sy committed Jan 10, 2024
1 parent 3195898 commit cb01adb
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/domain/entities/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import type User from './user.js';

export enum MemberRole {
/**
* Team member can read and write notes
* Team member can only read notes
*/
read = 'read',
read = 0,

/**
* Team member can only read notes
* Team member can read and write notes
*/
write = 'write',
write = 1,
}

/**
Expand Down
54 changes: 52 additions & 2 deletions src/presentation/http/router/noteSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,41 @@ describe('NoteSettings API', () => {
test.todo('Return 403 when user authorized, but not member of the team');
});

describe('PATCH /note-teams/:NotePublicId,userId', () => {
test('returns something', async () => {
describe('PATCH /note-teams/new-role/:newRole', () => {
test('if we want write role, in database it will be stored as 1', async () => {
await global.api?.fakeRequest({
method: 'PATCH',
headers: {
authorization: `Bearer ${global.auth(1)}`,
},
url: '/note-settings/new-role/write',
body: {
'userId': 3,
'noteId': 2,
},
});

const team = await global.api?.fakeRequest({
method: 'GET',
headers: {
authorization: `Bearer ${global.auth(1)}`,
},
url: '/note-settings/Pq1T9vc23Q/team',
});

if (team?.json() !== undefined) {
expect(team?.json()).toStrictEqual([
{
'id': 1,
'noteId': 2,
'userId': 3,
'role': 1,
},
]);
}
});

test('returns status code 200 and new role if it was patched', async () => {
const response = await global.api?.fakeRequest({
method: 'PATCH',
headers: {
Expand All @@ -299,5 +332,22 @@ describe('NoteSettings API', () => {
expect(response?.statusCode).toBe(200);
expect(response?.body).toBe('write');
});

test('returns 404 and User in team not found message if no such a note exists', async () => {
const response = await global.api?.fakeRequest({
method: 'PATCH',
headers: {
authorization: `Bearer ${global.auth(1)}`,
},
url: '/note-settings/new-role/write',
body: {
'userId': 15,
'noteId': 0,
},
});

expect(response?.statusCode).toBe(404);
expect(response?.json().message).toBe('User in team not found');
});
});
});
2 changes: 1 addition & 1 deletion src/presentation/http/router/noteSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
const newRole = await noteSettingsService.patchMemberRoleByUserId(request.body.userId, request.body.noteId, request.params.newRole);

if (newRole === null) {
return reply.notFound('user in team not found');
return reply.notFound('User in team not found');
}

return reply.send(newRole);
Expand Down
6 changes: 3 additions & 3 deletions src/repository/storage/postgres/orm/sequelize/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default class TeamsSequelizeStorage {
},
},
role: {
type: DataTypes.STRING,
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: MemberRole.read,
},
Expand Down Expand Up @@ -205,9 +205,9 @@ export default class TeamsSequelizeStorage {
* @param noteId - note internal id
* @param role - new team member role
*/
public async patchMemberRoleById(userId: TeamMember['id'], noteId: NoteInternalId, role : MemberRole): Promise<TeamMember['role'] | null> {
public async patchMemberRoleById(userId: TeamMember['id'], noteId: NoteInternalId, role: MemberRole): Promise<TeamMember['role'] | null> {
const affectedRows = await this.model.update({
role: role,
role: MemberRole[role],
}, {
where: {
userId,
Expand Down
2 changes: 1 addition & 1 deletion src/tests/test-data/note-teams.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"id": 2,
"note_id": 2,
"user_id": 3,
"role": "read"
"role": 0
}
]

0 comments on commit cb01adb

Please sign in to comment.