Skip to content

Commit 384f466

Browse files
leandreAllyAngemichel12
authored andcommitted
Merge pull request #15 from atlp-rwanda/ft-user-register-187431151
#-187431151 implementing jest test in sign up
2 parents cef80c8 + f617513 commit 384f466

File tree

16 files changed

+419
-375
lines changed

16 files changed

+419
-375
lines changed

.github/workflows/node.js.yml

+65-69
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,65 @@
1-
name: build
2-
3-
on:
4-
push:
5-
branches:
6-
- develop
7-
- "*"
8-
pull_request:
9-
branches:
10-
- develop
11-
- "*"
12-
13-
jobs:
14-
test:
15-
runs-on: ubuntu-latest
16-
17-
env:
18-
DB_TEST_URL: ${{ secrets.DB_TEST_URL }}
19-
DEV_MODE: ${{ secrets.DEV_MODE }}
20-
DB_HOSTED_MODE: ${{ secrets.DB_HOSTED_MODE }}
21-
ACCESS_TOKEN_SECRET: ${{ secrets.ACCESS_TOKEN_SECRET }}
22-
SESSION_SECRET: ${{ secrets.SESSION_SECRET }}
23-
JWT_SECRET: ${{ secrets.JWT_SECRET }}
24-
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
25-
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
26-
27-
strategy:
28-
matrix:
29-
node-version: ["20.x"]
30-
31-
steps:
32-
- uses: actions/checkout@v4
33-
- name: Use Node.js ${{ matrix.node-version }}
34-
uses: actions/setup-node@v4
35-
with:
36-
node-version: ${{ matrix.node-version }}
37-
cache: "npm"
38-
- name: Install dependencies
39-
run: npm install
40-
41-
- name: Run tests
42-
run: npm run test
43-
44-
- name: Run tests and build test coverage
45-
run: npm run test:ci
46-
47-
- name: Setup Code Climate test-reporter
48-
run: |
49-
# Download test reporter as a static binary
50-
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
51-
chmod +x ./cc-test-reporter
52-
./cc-test-reporter before-build
53-
54-
- name: Run tests
55-
run: npm run test
56-
57-
- name: Store coverage report
58-
if: always()
59-
run: mkdir -p coverage
60-
61-
62-
- name: Send coverage report to Code Climate
63-
if: always()
64-
run: ./cc-test-reporter after-build -t lcov -p coverage
65-
66-
- name: coveralls
67-
run: npx coveralls < coverage/lcov.info
68-
69-
1+
name: build
2+
3+
on:
4+
push:
5+
branches:
6+
- develop
7+
- "*"
8+
pull_request:
9+
branches:
10+
- develop
11+
- "*"
12+
13+
jobs:
14+
test:
15+
runs-on: ubuntu-latest
16+
17+
env:
18+
DB_TEST_URL: ${{ secrets.DB_TEST_URL }}
19+
DEV_MODE: ${{ secrets.DEV_MODE }}
20+
DB_HOSTED_MODE: ${{ secrets.DB_HOSTED_MODE }}
21+
ACCESS_TOKEN_SECRET: ${{ secrets.ACCESS_TOKEN_SECRET }}
22+
SESSION_SECRET: ${{ secrets.SESSION_SECRET }}
23+
JWT_SECRET: ${{ secrets.JWT_SECRET }}
24+
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
25+
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
26+
27+
strategy:
28+
matrix:
29+
node-version: ["20.x"]
30+
31+
steps:
32+
- uses: actions/checkout@v4
33+
- name: Use Node.js ${{ matrix.node-version }}
34+
uses: actions/setup-node@v4
35+
with:
36+
node-version: ${{ matrix.node-version }}
37+
cache: "npm"
38+
- name: Install dependencies
39+
run: npm install
40+
41+
- name: Run tests
42+
run: npm run test
43+
44+
- name: Run tests and build test coverage
45+
run: npm run test:ci
46+
47+
- name: Setup Code Climate test-reporter
48+
run: |
49+
# Download test reporter as a static binary
50+
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
51+
chmod +x ./cc-test-reporter
52+
./cc-test-reporter before-build
53+
- name: Run tests
54+
run: npm run test
55+
56+
- name: Store coverage report
57+
if: always()
58+
run: mkdir -p coverage
59+
60+
- name: Send coverage report to Code Climate
61+
if: always()
62+
run: ./cc-test-reporter after-build -t lcov -p coverage
63+
64+
- name: coveralls
65+
run: npx coveralls < coverage/lcov.info

package.json

+14-15
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,20 @@
5555
"/src/database/config/db.config.ts"
5656
]
5757
},
58-
59-
"devDependencies": {
60-
"@types/bcrypt": "^5.0.2",
61-
"@eslint/js": "^9.0.0",
62-
"@types/eslint": "^8.56.9",
63-
"@types/express": "^4.17.21",
64-
"@types/express-session": "^1.18.0",
65-
"@types/jest": "^29.5.12",
66-
"@types/jsonwebtoken": "^9.0.6",
67-
"@types/node": "^20.12.7",
68-
"@types/passport": "^1.0.16",
69-
"@types/passport-local": "^1.0.38",
70-
"@types/pg": "^8.11.5",
71-
"passport-stub": "^1.1.1",
72-
"@typescript-eslint/eslint-plugin": "^7.7.0",
58+
"devDependencies": {
59+
"@types/bcrypt": "^5.0.2",
60+
"@eslint/js": "^9.0.0",
61+
"@types/eslint": "^8.56.9",
62+
"@types/express": "^4.17.21",
63+
"@types/express-session": "^1.18.0",
64+
"@types/jest": "^29.5.12",
65+
"@types/jsonwebtoken": "^9.0.6",
66+
"@types/node": "^20.12.7",
67+
"@types/passport": "^1.0.16",
68+
"@types/passport-local": "^1.0.38",
69+
"@types/pg": "^8.11.5",
70+
"passport-stub": "^1.1.1",
71+
"@typescript-eslint/eslint-plugin": "^7.7.0",
7372
"@typescript-eslint/parser": "^7.7.0",
7473
"eslint": "^8.57.0",
7574
"eslint-config-airbnb-base": "^15.0.0",

src/__test__/product.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import request from "supertest";
55
jest.setTimeout(30000);
66

77
describe("PRODUCT API TEST", () => {
8-
// beforeAll(async () => {
9-
// await connectionToDatabase();
10-
// });
8+
// beforeAll(async () => {
9+
// await connectionToDatabase();
10+
// });
1111

12-
it("Seller should create a product", async () => {
13-
// Your test implementation goes here
14-
});
12+
it("Seller should create a product", async () => {
13+
// Your test implementation goes here
14+
});
1515
});

src/__test__/users.test.ts

+96-81
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,96 @@
1-
import app from "../app";
2-
import request from "supertest";
3-
import { connectionToDatabase } from "../database/config/db.config";
4-
import { deleteTableData } from "../utils/database.utils";
5-
import { User } from "../database/models/User";
6-
import {
7-
login_user,
8-
login_user_invalid_email,
9-
login_user_wrong_credentials,
10-
register_user,
11-
} from "../mock/static";
12-
13-
jest.setTimeout(30000);
14-
15-
function logErrors(
16-
err: { stack: any },
17-
_req: any,
18-
_res: any,
19-
next: (arg0: any) => void
20-
) {
21-
console.log(err.stack);
22-
next(err);
23-
}
24-
25-
const Jest_request = request(app.use(logErrors));
26-
27-
describe("USER API TEST", () => {
28-
beforeAll(async () => {
29-
await connectionToDatabase();
30-
});
31-
32-
afterAll(async () => {
33-
await deleteTableData(User, "users");
34-
});
35-
36-
it("should create a new user", async () => {
37-
// Your test implementation goes here
38-
const { body } = await Jest_request.post("/api/v1/users/register")
39-
.send(register_user)
40-
.expect(201);
41-
});
42-
43-
/**
44-
* ---------------------------- LOGIN --------------------------------------------
45-
*/
46-
47-
it("should successfully login a user and return 200", async () => {
48-
const { body } = await Jest_request.post("/api/v1/users/login")
49-
.send(login_user)
50-
.expect(200);
51-
expect(body.status).toStrictEqual("SUCCESS");
52-
expect(body.message).toStrictEqual(
53-
"Logged in to your account successfully!"
54-
);
55-
expect(body.token).toBeDefined();
56-
});
57-
58-
it("should return 404 when a user login with wrong credentials", async () => {
59-
const { body } = await Jest_request.post("/api/v1/users/login")
60-
.send(login_user_wrong_credentials)
61-
.expect(404);
62-
expect(body.status).toStrictEqual("NOT FOUND");
63-
expect(body.message).toStrictEqual("Wrong credentials!");
64-
});
65-
66-
it("should return 400 when a user user enter invalid email (login validation purposes)", async () => {
67-
const { body } = await Jest_request.post("/api/v1/users/login")
68-
.send(login_user_invalid_email)
69-
.expect(400);
70-
expect(body.status).toStrictEqual("BAD REQUEST");
71-
expect(body.message).toStrictEqual("Invalid email!");
72-
});
73-
74-
it("should return 400 when request body is invalid", async () => {
75-
const { body } = await Jest_request.post("/api/v1/users/login")
76-
.send({})
77-
.expect(400);
78-
expect(body.status).toStrictEqual("BAD REQUEST");
79-
expect(body.message).toBeDefined();
80-
})
81-
});
1+
import app from "../app";
2+
import request from "supertest";
3+
import { connectionToDatabase } from "../database/config/db.config";
4+
import { deleteTableData } from "../utils/database.utils";
5+
import { User } from "../database/models/User";
6+
import {
7+
login_user,
8+
login_user_invalid_email,
9+
login_user_wrong_credentials,
10+
NewUser,
11+
user_bad_request,
12+
} from "../mock/static";
13+
14+
jest.setTimeout(30000);
15+
16+
function logErrors(
17+
err: { stack: any },
18+
_req: any,
19+
_res: any,
20+
next: (arg0: any) => void,
21+
) {
22+
console.log(err.stack);
23+
next(err);
24+
}
25+
26+
const Jest_request = request(app.use(logErrors));
27+
28+
describe("USER API TEST", () => {
29+
beforeAll(async () => {
30+
await connectionToDatabase();
31+
});
32+
33+
afterAll(async () => {
34+
await deleteTableData(User, "users");
35+
});
36+
it("it should register a user and return 201", async () => {
37+
const { body } = await Jest_request.post("/api/v1/users/register")
38+
.send(NewUser)
39+
.expect(201);
40+
expect(body.status).toStrictEqual("SUCCESS");
41+
expect(body.message).toStrictEqual("Account Created successfully!");
42+
expect(body.token).toBeDefined();
43+
});
44+
it("it should return a user not found and status 400", async () => {
45+
const { body } = await Jest_request.post("/api/v1/users/register")
46+
.send(user_bad_request)
47+
.expect(400);
48+
});
49+
50+
it("it should return a user exist and status 409 when Email is already used in database", async () => {
51+
const { body } = await Jest_request.post("/api/v1/users/register")
52+
.send(NewUser)
53+
.expect(409);
54+
expect(body.status).toStrictEqual("CONFLICT");
55+
expect(body.message).toStrictEqual("User already exist!");
56+
});
57+
58+
/**
59+
* ---------------------------- LOGIN --------------------------------------------
60+
*/
61+
62+
it("should successfully login a user and return 200", async () => {
63+
const { body } = await Jest_request.post("/api/v1/users/login")
64+
.send(login_user)
65+
.expect(200);
66+
expect(body.status).toStrictEqual("SUCCESS");
67+
expect(body.message).toStrictEqual(
68+
"Logged in to your account successfully!",
69+
);
70+
expect(body.token).toBeDefined();
71+
});
72+
73+
it("should return 404 when a user login with wrong credentials", async () => {
74+
const { body } = await Jest_request.post("/api/v1/users/login")
75+
.send(login_user_wrong_credentials)
76+
.expect(404);
77+
expect(body.status).toStrictEqual("NOT FOUND");
78+
expect(body.message).toStrictEqual("Wrong credentials!");
79+
});
80+
81+
it("should return 400 when a user user enter invalid email (login validation purposes)", async () => {
82+
const { body } = await Jest_request.post("/api/v1/users/login")
83+
.send(login_user_invalid_email)
84+
.expect(400);
85+
expect(body.status).toStrictEqual("BAD REQUEST");
86+
expect(body.message).toStrictEqual("Invalid email!");
87+
});
88+
89+
it("should return 400 when request body is invalid", async () => {
90+
const { body } = await Jest_request.post("/api/v1/users/login")
91+
.send({})
92+
.expect(400);
93+
expect(body.status).toStrictEqual("BAD REQUEST");
94+
expect(body.message).toBeDefined();
95+
});
96+
});

src/app.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ const swaggerSpec = swaggerJsdoc(options);
3131
app.use("/api/v1/docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
3232
app.use("/api/v1", router);
3333
app.get("/api/v1", (_req, res) => {
34-
res.status(200).json({
35-
message: "Welcome to Hacker's e-commerce backend!",
36-
});
34+
res.status(200).json({
35+
message: "Welcome to Hacker's e-commerce backend!",
36+
});
3737
});
3838
app.get("/", (_req, res) => {
39-
res.send(root_home_page);
39+
res.send(root_home_page);
4040
});
4141

4242
export default app;

0 commit comments

Comments
 (0)