Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test project routes #107

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
84 changes: 84 additions & 0 deletions __tests__/projects.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import request from "supertest";
import dotenv from 'dotenv';
import app from "../src/app";
import buildDatabase from "../src/database/config/build";
import connection from "../src/database/config";

dotenv.config();

beforeAll(() => {
buildDatabase();
});

// describe('Test Project Routes', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Job Amal, but delete comments Please!!

// test('Should add a new project', (done) => {
// request(app)
// .post('/project')
// .set('cookie', `token=${process.env.TOKEN}`)
// .send({
// title: 'Team-5',
// description: 'This is team 5 project',
// })
// .expect(201)
// .end((err, res) => {
// if (err) return done(err);
// expect(res.body.message).toBe('New Project added Successfully')
// expect(res.body.data.length).toBe(1);
// done();
// })
// });

// test('Should return validation error for missing project title', (done) => {
// request(app)
// .post('/project')
// .set('cookie', `token=${process.env.TOKEN}`)
// .send({
// description: 'This is the description for the project',
// })
// .expect(406)
// .expect({'message': '\"title\" is required'})
// .end((err, res) => {
// if (err) return done(err);
// done();
// });
// });

// test('Should return validation error for missing project description', (done) => {
// request(app)
// .post('/project')
// .set('cookie', `token=${process.env.TOKEN}`)
// .send({
// title:'Team 5',
// })
// .expect(406)
// .end((err, res) => {
// if (err) return done(err);
// expect(res.body.message).toBe('\"description\" is required');
// done();
// });
// });

// });


describe('Get Project Route', () => {
test('Should display projects', (done) => {
request(app)
.get('/projects')
.set('cookie', `token=${process.env.TOKEN}`)
.expect(200)
.end((err, res) => {
if (err) return done(err);
expect(res.body.message).toBe('Show Projects Successfully');
// expect(res.body.data.length).toBe(2);
console.log(res.body.data.length);

done();
});
});
});

afterAll((done) => {
connection.end();
done();
})
10 changes: 8 additions & 2 deletions src/controllers/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
getProjectByProjectIDQuery,
deleteProjectByIdQuery,
} from '../database/query/projects';
import { TokenRequest, ProjectData } from '../interfaces';
import { TokenRequest, ProjectData, joiInterface } from '../interfaces';
import { CustomError } from '../helpers';
import { projectSchema } from '../helpers/validation';

Expand All @@ -28,7 +28,13 @@ const addProjectController = (req: TokenRequest, res: Response, next: NextFuncti
data: [project],
})
})
.catch(() => next(new CustomError(500, 'server Error')));
.catch((err: CustomError | joiInterface) => {
if ('isJoi' in err) {
next(new CustomError(406, err.details[0].message));
} else {
next(err);
}
});
};

const getProjectsController = (req: TokenRequest, res: Response, next: NextFunction) => {
Expand Down
122 changes: 70 additions & 52 deletions src/database/config/build.sql
Original file line number Diff line number Diff line change
@@ -1,64 +1,82 @@
-- SQLBook: Code

BEGIN;

DROP TABLE IF EXISTS users, projects, tasks, project_users, roles, user_tasks , attachments, priorities, sections CASCADE;
DROP TABLE
IF EXISTS users,
projects,
tasks,
project_users,
roles,
user_tasks,
attachments,
priorities,
sections CASCADE;

CREATE TABLE users(
id SERIAL PRIMARY KEY ,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
phone VARCHAR(60) NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT now()
);
CREATE TABLE
users(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
phone VARCHAR(60) NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT now()
);

CREATE TABLE projects(
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT now()
);
CREATE TABLE
projects(
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT now()
);

CREATE TABLE roles(
id SERIAL PRIMARY KEY,
role VARCHAR(55) NOT NULL
);
CREATE TABLE
roles(
id SERIAL PRIMARY KEY,
role VARCHAR(55) NOT NULL
);

CREATE TABLE project_users(
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE,
project_id INTEGER REFERENCES projects(id) ON UPDATE CASCADE ON DELETE CASCADE,
role_id INTEGER REFERENCES roles(id) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE
project_users(
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE,
project_id INTEGER REFERENCES projects(id) ON UPDATE CASCADE ON DELETE CASCADE,
role_id INTEGER REFERENCES roles(id) ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE priorities(
id SERIAL PRIMARY KEY,
priority VARCHAR(55) NOT NULL,
color VARCHAR(55) NOT NULL
);
CREATE TABLE
priorities(
id SERIAL PRIMARY KEY,
priority VARCHAR(55) NOT NULL,
color VARCHAR(55) NOT NULL
);

CREATE TABLE sections(
id SERIAL PRIMARY KEY,
section VARCHAR(55) NOT NULL
);
CREATE TABLE
sections(
id SERIAL PRIMARY KEY,
section VARCHAR(55) NOT NULL
);

CREATE TABLE tasks(
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT ,
created_at TIMESTAMP DEFAULT now(),
due_date DATE,
user_id INTEGER REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE,
project_id INTEGER REFERENCES projects(id) ON UPDATE CASCADE ON DELETE CASCADE,
priority_id INTEGER REFERENCES priorities(id) ON UPDATE CASCADE ON DELETE CASCADE,
section_id INTEGER REFERENCES sections(id) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE
tasks(
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT now(),
due_date DATE,
user_id INTEGER REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE,
project_id INTEGER REFERENCES projects(id) ON UPDATE CASCADE ON DELETE CASCADE,
priority_id INTEGER REFERENCES priorities(id) ON UPDATE CASCADE ON DELETE CASCADE,
section_id INTEGER REFERENCES sections(id) ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE attachments(
id SERIAL PRIMARY KEY ,
attach_s3 TEXT NOT NULL,
user_id INTEGER REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE,
task_id INTEGER REFERENCES tasks(id) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE
attachments(
id SERIAL PRIMARY KEY,
attach_s3 TEXT NOT NULL,
user_id INTEGER REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE,
task_id INTEGER REFERENCES tasks(id) ON UPDATE CASCADE ON DELETE CASCADE
);

COMMIT;
COMMIT;
2 changes: 1 addition & 1 deletion src/database/config/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const buildDatabase = () => {
const buildFile = readFileSync(join(__dirname, 'build.sql')).toString();
const fakeDataFile = readFileSync(join(__dirname, 'fakeData.sql')).toString();
connection.query(buildFile + fakeDataFile)
.catch(() => { throw Error('DB connection error') })
.catch((error) => { throw (error) })
}

export default buildDatabase;
3 changes: 3 additions & 0 deletions src/database/config/fakeData.sql
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ INSERT INTO project_users (user_id, project_id, role_id) VALUES (
INSERT INTO project_users (user_id, project_id, role_id) VALUES (
7, 2, 2
);
INSERT INTO project_users (user_id, project_id, role_id) VALUES (
3, 2, 1
);
INSERT INTO sections (section) VALUES ('To-Do'), ('Doing'), ('Reviewing'), ('Done');
INSERT INTO tasks (title,description,created_at,due_date,user_id,project_id,priority_id,section_id) VALUES
('add query tasks', 'add tasks', '2023-07-2', '2023-07-2',3,2,1,1);
Expand Down