From 3958ad66810dd25cc6ac175570c37bc05bb1e7a3 Mon Sep 17 00:00:00 2001
From: Martin Burgos <miburgos@stud.ntnu.no>
Date: Wed, 17 Feb 2021 13:37:11 +0100
Subject: [PATCH] Issue: Adding/Fixing tests (#7)

---
 server/src/app.ts                             |  1 -
 .../controllers/categoryController/index.ts   |  2 +-
 .../tests/categoryController.test.ts          |  8 ++++--
 .../src/controllers/postcontroller/index.ts   |  4 +--
 .../tests/postController.test.ts              | 28 +++++++++++++++++++
 .../tests/userController.test.ts              | 27 ++++++++++++++++++
 server/src/routes/routes.ts                   |  2 +-
 server/src/services/db.ts                     |  2 +-
 8 files changed, 65 insertions(+), 9 deletions(-)
 create mode 100644 server/src/controllers/postcontroller/tests/postController.test.ts
 create mode 100644 server/src/controllers/userController/tests/userController.test.ts

diff --git a/server/src/app.ts b/server/src/app.ts
index 366a655..1c62ec1 100644
--- a/server/src/app.ts
+++ b/server/src/app.ts
@@ -2,7 +2,6 @@ import express, { Application } from 'express';
 import routes from "./routes/routes";
 import bodyParser from 'body-parser';
 import cors from 'cors';
-import mysql from 'mysql2';
 // Boot express
 const app: Application = express();
 
diff --git a/server/src/controllers/categoryController/index.ts b/server/src/controllers/categoryController/index.ts
index 6d9d91a..04d2f97 100644
--- a/server/src/controllers/categoryController/index.ts
+++ b/server/src/controllers/categoryController/index.ts
@@ -20,7 +20,7 @@ const category = new Category();
 router.route('/').post(async (request: Request, response: Response) => {
 	const {category} = request.body;
 	try {
-		const input = (` INSERT INTO category(navn) VALUES (?);`)
+		const input = (` INSERT INTO category(name) VALUES (?);`)
 		return response.status(200).json(
             await query(input,[category])
         );
diff --git a/server/src/controllers/categoryController/tests/categoryController.test.ts b/server/src/controllers/categoryController/tests/categoryController.test.ts
index a0283e7..22c7fc8 100644
--- a/server/src/controllers/categoryController/tests/categoryController.test.ts
+++ b/server/src/controllers/categoryController/tests/categoryController.test.ts
@@ -11,7 +11,9 @@ describe('Test categoryController', () => {
     });
 
     it('Request /api/category should return request of 200!', async () => {
-        const result = await request(app).get('/api/category').send();
+        const result = await request(app)
+        .get('/api/category')
+        .send()
 
         expect(result.status).toBe(200);
     });
@@ -20,6 +22,6 @@ describe('Test categoryController', () => {
         const result = await request(app).get('/api/category/1').send();
 
         expect(result.status).toBe(200);
-        expect(result.body.data[0]?.navn).toBe('Antikviteter og Kunst');
+        expect(result.body.data[0]?.name).toBe('Antikviteter og Kunst');
     });
-});
\ No newline at end of file
+});
diff --git a/server/src/controllers/postcontroller/index.ts b/server/src/controllers/postcontroller/index.ts
index c437f2c..a296498 100644
--- a/server/src/controllers/postcontroller/index.ts
+++ b/server/src/controllers/postcontroller/index.ts
@@ -36,7 +36,7 @@ router.route('/').post(async (request: Request, response: Response) => {
 router.route('/').get(async (_: Request, response: Response) => {
 	try {
 		//response.status(200).json(await query("SELECT * FROM post;",""));
-		const input = `SELECT p.id, p.title, p.description, p.timestamp, p.owner, category.navn, p.imageUrl 
+		const input = `SELECT p.id, p.title, p.description, p.timestamp, p.owner, category.name, p.imageUrl 
 		FROM post as p
 		INNER JOIN category ON category.categoryid = p.categoryid;`
 		response.status(200).json(await query(input,""));
@@ -50,7 +50,7 @@ router.route('/:id').get(async (request: Request, response: Response) => {
 	const postId = request.params.id;
 	try {
 		//response.status(200).json(await query("SELECT * FROM post WHERE id=?;",[postId]));
-		const input = `SELECT p.id, p.title, p.description, p.timestamp, p.owner, category.navn, p.imageUrl 
+		const input = `SELECT p.id, p.title, p.description, p.timestamp, p.owner, category.name, p.imageUrl 
 		FROM post as p
 		INNER JOIN category ON category.categoryid = p.categoryid WHERE p.id=?;`
 		response.status(200).json(await query(input,[postId]));
diff --git a/server/src/controllers/postcontroller/tests/postController.test.ts b/server/src/controllers/postcontroller/tests/postController.test.ts
new file mode 100644
index 0000000..536b27c
--- /dev/null
+++ b/server/src/controllers/postcontroller/tests/postController.test.ts
@@ -0,0 +1,28 @@
+import request from 'supertest';
+import app from '../../../app';
+
+describe('Test postController', () => {
+    beforeAll(async () => { // kjører før testing
+        console.log("Post controller test starting...");
+    });
+
+    afterAll(async () => { // kjører når all testing er gått gjennom
+        console.log("...Test ending");
+    });
+
+    it('Request /api/post should return request of 200!', async () => {
+        const result = await request(app)
+        .get('/api/post')
+        .send()
+
+        expect(result.status).toBe(200);
+    });
+
+    it('Request /api/post/1 should return data with name "test"!', async () => {
+        const result = await request(app).get('/api/post/1').send();
+
+        expect(result.status).toBe(200);
+        expect(result.body.data[0]?.title).toBe('test');
+    });
+
+});
\ No newline at end of file
diff --git a/server/src/controllers/userController/tests/userController.test.ts b/server/src/controllers/userController/tests/userController.test.ts
new file mode 100644
index 0000000..000014a
--- /dev/null
+++ b/server/src/controllers/userController/tests/userController.test.ts
@@ -0,0 +1,27 @@
+import request from 'supertest';
+import app from '../../../app';
+
+describe('Test userController', () => {
+    beforeAll(async () => { // kjører før testing
+        console.log("User controller test starting...");
+    });
+
+    afterAll(async () => { // kjører når all testing er gått gjennom
+        console.log("...Test ending");
+    });
+
+    it('Request /api/category should return request of 200!', async () => {
+        const result = await request(app)
+        .get('/api/user')
+        .send()
+
+        expect(result.status).toBe(200);
+    });
+
+    it('Request /api/category/1 should return data with name "zorg"!', async () => {
+        const result = await request(app).get('/api/user/1').send();
+
+        expect(result.status).toBe(200);
+        expect(result.body.data[0]?.username).toBe('zorg');
+    });
+});
\ No newline at end of file
diff --git a/server/src/routes/routes.ts b/server/src/routes/routes.ts
index a47eab9..fa6b3f5 100644
--- a/server/src/routes/routes.ts
+++ b/server/src/routes/routes.ts
@@ -1,4 +1,4 @@
-import postController from '../controllers/postcontroller';
+import postController from '../controllers/postController';
 import categoryController from '../controllers/categoryController';
 import userController from '../controllers/userController';
 import express from 'express';
diff --git a/server/src/services/db.ts b/server/src/services/db.ts
index 9aef9c6..22a62cf 100644
--- a/server/src/services/db.ts
+++ b/server/src/services/db.ts
@@ -9,5 +9,5 @@ async function query(sql:any, params:any) {
 }
 
 export default {
-	query
+	query,
 }
-- 
GitLab