Skip to content

Commit 0fcd223

Browse files
committed
feat(user-Test): implementing sign up using jest
1 parent fe1b880 commit 0fcd223

File tree

8 files changed

+43
-152
lines changed

8 files changed

+43
-152
lines changed

.github/workflows/node.js.yml

+30-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
# This workflow will install node dependencies, run tests and report coverage
22
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
3-
43
name: build
54

65
on:
76
push:
87
branches:
9-
- feat-test-CI-implementation-187419042
10-
- "*"
118
- develop
12-
9+
- "*"
1310
pull_request:
1411
branches:
1512
- develop
13+
- "*"
1614

1715
jobs:
1816
test:
@@ -22,6 +20,11 @@ jobs:
2220
DB_TEST_URL: ${{ secrets.DB_TEST_URL }}
2321
DEV_MODE: ${{ secrets.DEV_MODE }}
2422
DB_HOSTED_MODE: ${{ secrets.DB_HOSTED_MODE }}
23+
ACCESS_TOKEN_SECRET: ${{ secrets.ACCESS_TOKEN_SECRET }}
24+
SESSION_SECRET: ${{ secrets.SESSION_SECRET }}
25+
JWT_SECRET: ${{ secrets.JWT_SECRET }}
26+
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
27+
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
2528

2629
strategy:
2730
matrix:
@@ -37,15 +40,29 @@ jobs:
3740
- name: Install dependencies
3841
run: npm install
3942

43+
- name: Run tests
44+
run: npm run test
45+
4046
- name: Run tests and build test coverage
4147
run: npm run test:ci
4248

43-
- name: Test & publish code climate coverage
44-
uses: paambaati/[email protected]
45-
env:
46-
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
47-
with:
48-
coverageCommand: npm run test:ci
49-
debug: true
50-
coverageLocations: |
51-
${{github.workspace}}/*.lcov:lcov
49+
- name: Setup Code Climate test-reporter
50+
run: |
51+
# Download test reporter as a static binary
52+
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
53+
chmod +x ./cc-test-reporter
54+
./cc-test-reporter before-build
55+
- name: Run tests
56+
run: npm run test
57+
58+
- name: Store coverage report
59+
if: always()
60+
run: mkdir -p coverage
61+
62+
63+
- name: Send coverage report to Code Climate
64+
if: always()
65+
run: ./cc-test-reporter after-build -t lcov -p coverage
66+
67+
- name: coveralls
68+
run: npx coveralls < coverage/lcov.info

src/__test__/login.test.ts

-23
This file was deleted.

src/__test__/product.test.ts

-31
This file was deleted.

src/__test__/users.test.ts

-29
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,13 @@
11
import app from "../app";
22
import request from "supertest";
3-
// import { connectionToDatabase } from "../database/config/db.config";
4-
53
jest.setTimeout(30000);
64

7-
describe("USER API TEST", () => {
8-
// beforeAll(async () => {
9-
// await connectionToDatabase();
10-
// });
11-
12-
it("Should return 200 and list of all users", async () => {
13-
// Your test implementation goes here
14-
});
15-
16-
it("should create a new user", async () => {
17-
// Your test implementation goes here
18-
});
19-
20-
it("should return validation errors when required fields are missing", async () => {
21-
// Your test implementation goes here
22-
});
23-
24-
it("should return an error if the email is already in use", async () => {
25-
// Your test implementation goes here
26-
});
27-
});
28-
295
import { connectionToDatabase } from "../database/config/db.config";
306
import { deleteTableData } from "../utils/database.utils";
317
import { User } from "../database/models/User";
328
import {
339
NewUser,
3410
user_bad_request,
35-
User_without_email,
3611
exist_user,
3712
} from "../mock/static";
3813
import { createUser } from "../services/user.services";
@@ -62,7 +37,6 @@ describe("USER API TEST", () => {
6237
/**
6338
* ----------------------------register new user --------------------------------------------
6439
*/
65-
describe("FAILED SIGN UP", () => {
6640
it("it should return a user not found and status 400", async () => {
6741
const { body } = await Jest_request.post("/api/v1/users/register")
6842
.send(user_bad_request)
@@ -76,15 +50,12 @@ describe("USER API TEST", () => {
7650
expect(body.status).toStrictEqual("CONFLICT");
7751
expect(body.message).toStrictEqual("User already exist!");
7852
});
79-
});
80-
describe("SUCCCESSFULL SIGN UP WITH TOKEN CREATED", () => {
8153
it("it should register a user and return 201", async () => {
8254
const { body } = await Jest_request.post("/api/v1/users/register")
8355
.send(NewUser)
8456
.expect(201);
8557
expect(body.status).toStrictEqual("SUCCESS");
8658
expect(body.message).toStrictEqual("Account Created successfully!");
8759
expect(body.token).toBeDefined();
88-
});
8960
});
9061
});

src/controllers/userController.ts

+2-39
Original file line numberDiff line numberDiff line change
@@ -36,47 +36,10 @@ const registerUser = async (
3636
} catch (error) {
3737
res
3838
.status(500)
39-
.json(new HttpException("SERVER FAIL", "Something went wrong!"));
39+
.json(new HttpException("SERVER FAILS", "Something went wrong!"));
4040
}
4141
};
4242

43-
const login = async (req: Request, res: Response, next: NextFunction) => {
44-
passport.authenticate(
45-
"login",
46-
(error: Error, user: UserModelAttributes, info: InfoAttribute) => {
47-
if (error) {
48-
return res
49-
.status(400)
50-
.json(new HttpException("BAD REQUEST", "Bad Request!"));
51-
}
52-
53-
if (info) {
54-
return res
55-
.status(409)
56-
.json(new HttpException("CONFLICT", info.message));
57-
}
58-
59-
(req as any).login(user, (err: Error) => {
60-
if (err) {
61-
return res
62-
.status(400)
63-
.json(new HttpException("BAD REQUEST", "Bad Request!"));
64-
}
65-
66-
const { id, role } = user;
67-
68-
const token = generateAccessToken({ id, role });
69-
const response = new HttpException(
70-
"SUCCESS",
71-
"Logged in to you account successfully!",
72-
).response();
73-
return res.status(200).json({ ...response, token });
74-
});
75-
},
76-
)(req, res, next);
77-
};
78-
7943
export default {
80-
login,
81-
registerUser,
44+
registerUser
8245
};

src/database/config/db.config.ts

+11-12
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,21 @@ DB_HOST_MODE === "local"
3434
export const sequelizeConnection: Sequelize = new Sequelize(db_uri, {
3535
dialect: "postgres",
3636
dialectOptions: dialect_option,
37-
logging: true,
37+
logging: false,
3838
pool: {
3939
max: 10,
4040
min: 0,
4141
acquire: 30000,
4242
idle: 10000,
4343
},
4444
});
45-
46-
export const connectionToDatabase = () =>
47-
sequelizeConnection
48-
.authenticate()
49-
.then(() => {
50-
console.log("Database connected successfully.", db_uri);
51-
})
52-
.catch((error) => {
53-
console.error("Unable to connect to the database:", error);
54-
process.exit(1);
55-
});
45+
export const connectionToDatabase = async () => {
46+
try {
47+
await sequelizeConnection.authenticate();
48+
await sequelizeConnection.sync();
49+
console.log("Database connected successfully.", db_uri);
50+
} catch (error) {
51+
console.log("Unable to connect to the database:", error);
52+
process.exit(1);
53+
}
54+
}

src/routes/userRoutes.ts

-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ userRoutes.post(
88
userMiddleware.userValid,
99
userController.registerUser
1010
);
11-
userRoutes.post("/login", userMiddleware.logInValidated, userController.login);
1211

1312
export default userRoutes;

src/utils/database.utils.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import {
2-
connectionToDatabase,
3-
sequelizeConnection,
4-
} from "../database/config/db.config";
51

62
export const deleteTableData = async (Model: any, tableName: string) => {
73
try {

0 commit comments

Comments
 (0)