From 8edaf4cd45c4f9e2b00435160f877ad4182e1a49 Mon Sep 17 00:00:00 2001
From: Roland Bernard <rolbernard@unibz.it>
Date: Tue, 25 May 2021 22:41:32 +0200
Subject: [PATCH] Added some project tests

---
 server/src/v1/auth.test.ts    |   2 +-
 server/src/v1/project.test.ts | 264 ++++++++++++++++++++++++++++++++++
 server/src/v1/task.ts         |   2 +-
 server/src/v1/team.test.ts    |  22 +--
 server/src/v1/user.test.ts    |   4 +-
 5 files changed, 279 insertions(+), 15 deletions(-)
 create mode 100644 server/src/v1/project.test.ts

diff --git a/server/src/v1/auth.test.ts b/server/src/v1/auth.test.ts
index cdcace9..67aaf83 100644
--- a/server/src/v1/auth.test.ts
+++ b/server/src/v1/auth.test.ts
@@ -101,7 +101,7 @@ describe('successful user registration', () => {
             .delete()
             .where({ 'users.user_name': 'user2' });
     });
-})
+});
 
 describe('password can be changed', () => {
     let response: Response;
diff --git a/server/src/v1/project.test.ts b/server/src/v1/project.test.ts
new file mode 100644
index 0000000..5c44d28
--- /dev/null
+++ b/server/src/v1/project.test.ts
@@ -0,0 +1,264 @@
+
+import supertest from 'supertest';
+
+import { api } from '../api';
+import { generateAuthToken } from './auth';
+
+const request = supertest(api);
+
+describe('GET /project', () => {
+    test('returns all projects the user can see', async () => {
+        const resp = await request
+            .get('/v1/project')
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.projects.length).toEqual(3);
+    });
+
+    test('includes projects of team 0', async () => {
+        const resp = await request
+            .get('/v1/project')
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.projects).toContainEqual({
+            id: '00000000-0000-4000-8000-000000000000',
+            name: 'Project0',
+            text: 'Project0 Text',
+            color: '#00f',
+            status: 'open',
+            deadline: '2020-10-10',
+        });
+        expect(resp.body.projects).toContainEqual({
+            id: '00000000-0000-4000-8000-000000000002',
+            name: 'Project2',
+            text: 'Project2 Text',
+            color: '#f00',
+            status: 'suspended',
+            deadline: null,
+        });
+    });
+
+    test('includes projects of team 2', async () => {
+        const resp = await request
+            .get('/v1/project')
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.projects).toContainEqual({
+            id: '00000000-0000-4000-8000-000000000001',
+            name: 'Project1',
+            text: 'Project1 Text',
+            color: '#0f0',
+            status: 'closed',
+            deadline: null,
+        });
+    });
+});
+
+describe('GET /project/:uuid', () => {
+    test('returns information for the requested project', async () => {
+        const resp = await request
+            .get('/v1/project/00000000-0000-4000-8000-000000000000')
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.project).toEqual({
+            id: '00000000-0000-4000-8000-000000000000',
+            name: 'Project0',
+            text: 'Project0 Text',
+            color: '#00f',
+            status: 'open',
+            deadline: '2020-10-10',
+            teams: [ '00000000-0000-4000-8000-000000000000' ],
+        });
+    });
+});
+
+describe('GET /project/:uuid/tasks', () => {
+    test('returns all the tasks in the project', async () => {
+        const resp = await request
+            .get('/v1/project/00000000-0000-4000-8000-000000000002/tasks')
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.tasks.length).toEqual(4);
+    });
+
+    test('contains open tasks', async () => {
+        const resp = await request
+            .get('/v1/project/00000000-0000-4000-8000-000000000002/tasks')
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.tasks).toContainEqual({
+            id: '00000000-0000-4000-8000-000000000002',
+            project: '00000000-0000-4000-8000-000000000002',
+            name: 'Task2',
+            text: 'Task2 Text',
+            icon: '2',
+            status: 'open',
+            priority: 'low',
+            created: Date.parse('2020-10-15'),
+            edited: Date.parse('2020-10-20'),
+            assigned: [],
+            dependencies: [ '00000000-0000-4000-8000-000000000005' ],
+            requirements: [],
+        });
+        expect(resp.body.tasks).toContainEqual({
+            id: '00000000-0000-4000-8000-000000000005',
+            project: '00000000-0000-4000-8000-000000000002',
+            name: 'Task5',
+            text: 'Task5 Text',
+            icon: '5',
+            status: 'open',
+            priority: 'urgent',
+            created: Date.parse('2020-10-15'),
+            edited: Date.parse('2020-11-20'),
+            assigned: [ { user: '00000000-0000-4000-8000-000000000000', time: 120, finished: false } ],
+            dependencies: [ ],
+            requirements: [
+                { role: '00000000-0000-4000-8000-000000000000', time: 60 },
+                { role: '00000000-0000-4000-8000-000000000001', time: 30 },
+            ],
+        });
+    });
+
+    test('contains closed tasks', async () => {
+        const resp = await request
+            .get('/v1/project/00000000-0000-4000-8000-000000000002/tasks')
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.tasks).toContainEqual({
+            id: '00000000-0000-4000-8000-000000000003',
+            project: '00000000-0000-4000-8000-000000000002',
+            name: 'Task3',
+            text: 'Task3 Text',
+            icon: '3',
+            status: 'closed',
+            priority: 'urgent',
+            created: Date.parse('2020-10-15'),
+            edited: Date.parse('2020-10-20'),
+            assigned: [ ],
+            dependencies: [ ],
+            requirements: [ ],
+        });
+    });
+
+    test('contains suspended tasks', async () => {
+        const resp = await request
+            .get('/v1/project/00000000-0000-4000-8000-000000000002/tasks')
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.tasks).toContainEqual({
+            id: '00000000-0000-4000-8000-000000000004',
+            project: '00000000-0000-4000-8000-000000000002',
+            name: 'Task4',
+            text: 'Task4 Text',
+            icon: '4',
+            status: 'suspended',
+            priority: 'urgent',
+            created: Date.parse('2020-10-15'),
+            edited: Date.parse('2020-10-20'),
+            assigned: [ ],
+            dependencies: [ ],
+            requirements: [ ],
+        });
+    });
+});
+
+describe('GET /project/:uuid/assigned', () => {
+    test('returns all users that are assigned to one of the projects tasks', async () => {
+        const resp = await request
+            .get('/v1/project/00000000-0000-4000-8000-000000000002/assigned')
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.assigned.length).toEqual(1);
+    });
+
+    test('contains the user information', async () => {
+        const resp = await request
+            .get('/v1/project/00000000-0000-4000-8000-000000000002/assigned')
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.assigned).toContainEqual({
+            id: '00000000-0000-4000-8000-000000000000',
+            username: 'user0',
+            email: 'test0@example.com',
+            realname: 'Testing Tester',
+            time: 120,
+        });
+    });
+});
+
+describe('GET /project/:uuid/work', () => {
+    test('returns all the work items done for the projects tasks', async () => {
+        const resp = await request
+            .get('/v1/project/00000000-0000-4000-8000-000000000002/work')
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.work.length).toEqual(3);
+    });
+
+    test('containt all finished work information', async () => {
+        const resp = await request
+            .get('/v1/project/00000000-0000-4000-8000-000000000002/work')
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.work).toContainEqual({
+            id: '00000000-0000-4000-8000-000000000000',
+            task: '00000000-0000-4000-8000-000000000005',
+            user: '00000000-0000-4000-8000-000000000000',
+            started: Date.parse('2020-10-10T12:00:00'),
+            finished: Date.parse('2020-10-10T13:00:00'),
+        });
+        expect(resp.body.work).toContainEqual({
+            id: '00000000-0000-4000-8000-000000000001',
+            task: '00000000-0000-4000-8000-000000000005',
+            user: '00000000-0000-4000-8000-000000000000',
+            started: Date.parse('2020-10-10T13:00:00'),
+            finished: Date.parse('2020-10-10T14:00:00'),
+        });
+    });
+
+    test('containt all unfinished work information', async () => {
+        const resp = await request
+            .get('/v1/project/00000000-0000-4000-8000-000000000002/work')
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.work).toContainEqual({
+            id: '00000000-0000-4000-8000-000000000002',
+            task: '00000000-0000-4000-8000-000000000005',
+            user: '00000000-0000-4000-8000-000000000000',
+            started: Date.parse('2020-10-11T12:00:00'),
+            finished: null,
+        });
+    });
+
+    test('can be limited in time with from date', async () => {
+        const resp = await request
+            .get(`/v1/project/00000000-0000-4000-8000-000000000002/work?since=${Date.parse('2020-10-10T20:00:00')}`)
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.work.length).toEqual(1);
+    });
+
+    test('can be limited in time with to date', async () => {
+        const resp = await request
+            .get(`/v1/project/00000000-0000-4000-8000-000000000002/work?to=${Date.parse('2020-10-10T20:00:00')}`)
+            .set('Authorization', `Bearer ${await generateAuthToken('00000000-0000-4000-8000-000000000000')}`);
+        expect(resp.status).toEqual(200);
+        expect(resp.body.status).toEqual('success');
+        expect(resp.body.work.length).toEqual(2);
+    });
+});
+
diff --git a/server/src/v1/task.ts b/server/src/v1/task.ts
index 30aaa0d..f452ad8 100644
--- a/server/src/v1/task.ts
+++ b/server/src/v1/task.ts
@@ -69,7 +69,7 @@ export function generateFromFlatResult(results: any[]): Task[] {
                 grouped_tasks[row.id].assigned.push({
                     user: row.assigned_user,
                     time: row.assigned_time,
-                    finished: row.assigned_finished,
+                    finished: row.assigned_finished ? true : false,
                 });
             }
         }
diff --git a/server/src/v1/team.test.ts b/server/src/v1/team.test.ts
index c24d589..6eede49 100644
--- a/server/src/v1/team.test.ts
+++ b/server/src/v1/team.test.ts
@@ -41,7 +41,7 @@ describe('POST /team', () => {
             .delete()
             .where({ 'teams.name': 'Team20' });
     });
-})
+});
 
 describe('PUT /team/:uuid', () => {
     let response: Response;
@@ -79,7 +79,7 @@ describe('PUT /team/:uuid', () => {
                 name: 'Team0',
             });
     });
-})
+});
 
 describe('GET /team', () => {
     test('returns all teams the user is a member of', async () => {
@@ -116,7 +116,7 @@ describe('GET /team', () => {
             role: '00000000-0000-4000-8000-000000000002',
         });
     });
-})
+});
 
 describe('GET /team/:uuid', () => {
     test('returns the requested teams', async () => {
@@ -140,7 +140,7 @@ describe('GET /team/:uuid', () => {
         expect(resp.body.team.name).toEqual('Team1');
         expect(resp.body.team.role).toBeNull();
     });
-})
+});
 
 describe('GET /team/:uuid', () => {
     test('returns the requested teams', async () => {
@@ -164,7 +164,7 @@ describe('GET /team/:uuid', () => {
         expect(resp.body.team.name).toEqual('Team1');
         expect(resp.body.team.role).toBeNull();
     });
-})
+});
 
 describe('GET /team/:uuid/members', () => {
     test('returns all the members of the team', async () => {
@@ -205,7 +205,7 @@ describe('GET /team/:uuid/members', () => {
             role: { id: '00000000-0000-4000-8000-000000000003', name: 'Role3' },
         });
     });
-})
+});
 
 describe('GET /team/:uuid/roles', () => {
     test('returns all the roles of the team', async () => {
@@ -240,7 +240,7 @@ describe('GET /team/:uuid/roles', () => {
             name: 'Role3',
         });
     });
-})
+});
 
 describe('GET /team/:uuid/projects', () => {
     test('returns all the projects of the team', async () => {
@@ -275,7 +275,7 @@ describe('GET /team/:uuid/projects', () => {
             deadline: null,
         });
     });
-})
+});
 
 describe('GET /team/:uuid/work', () => {
     test('returns all the work items done by teams members', async () => {
@@ -341,7 +341,7 @@ describe('GET /team/:uuid/work', () => {
         expect(resp.body.status).toEqual('success');
         expect(resp.body.work.length).toEqual(2);
     });
-})
+});
 
 describe('GET /team/:uuid/activity', () => {
     test('returns time worked for all days', async () => {
@@ -371,7 +371,7 @@ describe('GET /team/:uuid/activity', () => {
         expect(response.body.status).toEqual('success');
         expect(response.body.activity.length).toEqual(0);
     });
-})
+});
 
 describe('GET /team/:uuid/completion', () => {
     test('returns completion for all tasks of all projects', async () => {
@@ -401,5 +401,5 @@ describe('GET /team/:uuid/completion', () => {
             overdue: 1,
         });
     });
-})
+});
 
diff --git a/server/src/v1/user.test.ts b/server/src/v1/user.test.ts
index adfcd4d..4efa7f3 100644
--- a/server/src/v1/user.test.ts
+++ b/server/src/v1/user.test.ts
@@ -26,7 +26,7 @@ describe('GET /user/name', () => {
         expect(response.body.status).toEqual('success');
         expect(response.body.user.username).toEqual('user0');
     });
-})
+});
 
 describe('GET /user/:uuid/image', () => {
     test('returns 404 without body if no image is set', async () => {
@@ -46,7 +46,7 @@ describe('GET /user/:uuid/image', () => {
         expect(response.status).toEqual(200);
         expect(response.body).toBeTruthy();
     });
-})
+});
 
 describe('GET /user', () => {
     test('returns the user that is authorized', async () => {
-- 
GitLab