-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ft: auth (new user needs to verify one's account before login
- Loading branch information
1 parent
c709966
commit bfff83d
Showing
16 changed files
with
352 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,19 +26,22 @@ describe("testing cart", () => { | |
name: "admin123", | ||
username: "admin123", | ||
email: "[email protected]", | ||
isVerified:true, | ||
password: await bcrypt.hash("password", 10), | ||
roleId: 3, | ||
}; | ||
|
||
const testBuyer = { | ||
name: "buyer123", | ||
username: "buyer123", | ||
isVerified:true, | ||
email: "[email protected]", | ||
password: await bcrypt.hash("password", 10), | ||
}; | ||
const testSeller = { | ||
name: "seller123", | ||
username: "seller123", | ||
isVerified:true, | ||
email: "[email protected]", | ||
password: await bcrypt.hash("password", 10), | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ describe("test stripe api payment", () => { | |
const testAdmin = { | ||
name: "admin123", | ||
username: "admin123", | ||
isVerified:true, | ||
email: "[email protected]", | ||
password: await bcrypt.hash("password", 10), | ||
roleId: 3, | ||
|
@@ -30,13 +31,15 @@ describe("test stripe api payment", () => { | |
const testBuyer = { | ||
name: "buyer123", | ||
username: "buyer123", | ||
isVerified:true, | ||
email: "[email protected]", | ||
password: await bcrypt.hash("password", 10), | ||
}; | ||
|
||
const testSeller = { | ||
name: "seller123", | ||
username: "seller123", | ||
isVerified:true, | ||
email: "[email protected]", | ||
password: await bcrypt.hash("password", 10), | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,12 @@ import { placeOrder } from "../src/services/payment.service"; | |
import Cart from "../src/sequelize/models/Cart"; | ||
import CartItem from "../src/sequelize/models/CartItem"; | ||
import OrderItem from "../src/sequelize/models/orderItems"; | ||
import { generateVerificationToken } from "../src/utils/generateResetToken"; | ||
|
||
const userData: any = { | ||
name: "yvanna", | ||
username: "testuser", | ||
isVerified:true, | ||
email: "[email protected]", | ||
role:"seller", | ||
password: "test1234", | ||
|
@@ -27,6 +29,7 @@ const userData: any = { | |
const dummySeller = { | ||
name: "dummy1234", | ||
username: "username1234", | ||
isVerified:true, | ||
email: "[email protected]", | ||
password: "1234567890", | ||
}; | ||
|
@@ -35,6 +38,7 @@ const product:any = { | |
name: "pens", | ||
images: ["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg"], | ||
stockQuantity: 8, | ||
|
||
price: 5000, | ||
discount: 3.5, | ||
categoryID: 1, | ||
|
@@ -43,6 +47,7 @@ const product:any = { | |
const dummyBuyer = { | ||
name: "test user", | ||
username: "testUser", | ||
isVerified:true, | ||
email: "[email protected]", | ||
password: "soleil00", | ||
} | ||
|
@@ -60,6 +65,7 @@ describe("Testing product Routes", () => { | |
await connect(); | ||
const testAdmin = { | ||
name: "admin123", | ||
isVerified:true, | ||
username: "admin123", | ||
email: "[email protected]", | ||
password: await bcrypt.hash("password", 10), | ||
|
@@ -76,8 +82,8 @@ describe("Testing product Routes", () => { | |
]) | ||
|
||
await User.create(testAdmin); | ||
|
||
const dummy = await request(app).post("/api/v1/users/register").send(dummySeller); | ||
await User.create(dummySeller); | ||
// await request(app).post("/api/v1/users/register").send(dummySeller); | ||
await Product.destroy({}); | ||
await Category.destroy({truncate:true}); | ||
} catch (error) { | ||
|
@@ -98,12 +104,27 @@ describe("Testing product Routes", () => { | |
expect(response.status).toBe(201); | ||
}, 20000); | ||
|
||
it("It should verify user account.",async()=>{ | ||
const token = generateVerificationToken(userData.email, 60); | ||
const response = await request(app) | ||
.get(`/api/v1/users/verify-user?token=${token}`) | ||
expect(response.status).toBe(200) | ||
expect(response.body.message).toBe('User verified successfully.') | ||
},60000) | ||
|
||
test('should return 201 and register a dummy buyer user', async () => { | ||
const response = await request(app) | ||
.post("/api/v1/users/register") | ||
.send(dummyBuyer); | ||
expect(response.status).toBe(201); | ||
}) | ||
it("It should verify user account.",async()=>{ | ||
const token = generateVerificationToken(dummyBuyer.email, 60); | ||
const response = await request(app) | ||
.get(`/api/v1/users/verify-user?token=${token}`) | ||
expect(response.status).toBe(200) | ||
expect(response.body.message).toBe('User verified successfully.') | ||
},60000) | ||
let buyerToken: any; | ||
|
||
test("should login an buyer", async () =>{ | ||
|
@@ -128,6 +149,7 @@ describe("Testing product Routes", () => { | |
password: "password" | ||
}) | ||
adminToken = response.body.token; | ||
expect(response.status).toBe(200) | ||
}); | ||
|
||
test("should update dummyseller's role to seller", async () => { | ||
|
@@ -146,6 +168,7 @@ describe("Testing product Routes", () => { | |
}) | ||
.set("Authorization", "Bearer " + adminToken); | ||
expect(response.status).toBe(200); | ||
expect(response.body.message).toBe('User role updated successfully'); | ||
|
||
}); | ||
|
||
|
@@ -472,7 +495,7 @@ test('It should return status 200 for removed category',async() =>{ | |
}) | ||
it("changing product availability of product which does not exist", async ()=>{ | ||
const response = await request(app) | ||
.patch(`/api/v1/products/${91}/status`) | ||
.patch(`/api/v1/products/${4444444}/status`) | ||
.set("Authorization", "Bearer " + token); | ||
expect(response.body.message).toBe('Product not found') | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,14 +15,15 @@ import { QueryTypes } from "sequelize"; | |
// import redisClient from "../src/config/redis"; | ||
import Redis from "ioredis"; | ||
import { env } from "../src/utils/env"; | ||
import { generateResetToken } from "../src/utils/generateResetToken"; | ||
import { generateResetToken, generateVerificationToken } from "../src/utils/generateResetToken"; | ||
|
||
let redisClient:any; | ||
|
||
|
||
const userData: any = { | ||
name: "yvanna5", | ||
username: "testuser5", | ||
isVerified:true, | ||
email: "[email protected]", | ||
password: "test12345", | ||
}; | ||
|
@@ -31,11 +32,13 @@ const userData: any = { | |
const dummySeller = { | ||
name: "dummy1234", | ||
username: "username1234", | ||
isVerified:true, | ||
email: "[email protected]", | ||
password: "1234567890", | ||
}; | ||
const userTestData = { | ||
newPassword: "Test@123", | ||
isVerified:true, | ||
confirmPassword: "Test@123", | ||
wrongPassword: "Test456", | ||
}; | ||
|
@@ -107,6 +110,14 @@ describe("Testing user Routes", () => { | |
expect(response.status).toBe(201); | ||
}, 20000); | ||
|
||
it("It should verify user account.",async()=>{ | ||
const token = generateVerificationToken(userData.email, 60); | ||
const response = await request(app) | ||
.get(`/api/v1/users/verify-user?token=${token}`) | ||
expect(response.status).toBe(200) | ||
expect(response.body.message).toBe('User verified successfully.') | ||
},60000) | ||
|
||
|
||
test("should return 409 when registering with an existing email", async () => { | ||
User.create(userData); | ||
|
@@ -135,7 +146,8 @@ describe("Testing user Routes", () => { | |
email: userData.email, | ||
password: userData.password, | ||
}); | ||
expect(response.status).toBe(200); | ||
// expect(response.status).toBe(200); | ||
expect(response.body.message).toBe("Logged in"); | ||
token = response.body.token; | ||
}); | ||
|
||
|
@@ -206,7 +218,7 @@ describe("Testing user Routes", () => { | |
test("should login an Admin", async () =>{ | ||
const response = await request(app).post("/api/v1/users/login").send({ | ||
email: "[email protected]", | ||
password: "password" | ||
password: "password" | ||
}) | ||
adminToken = response.body.token; | ||
}); | ||
|
@@ -548,6 +560,28 @@ describe('Patch /api/v1/users/reset-password', () => { | |
},60000); | ||
}); | ||
|
||
describe("Verifying user account",()=>{ | ||
it("It should verify user account.",async()=>{ | ||
await User.create(userData) | ||
const token = generateVerificationToken(userData.email, 60); | ||
const response = await request(app) | ||
.get(`/api/v1/users/verify-user?token=${token}`) | ||
expect(response.status).toBe(200) | ||
expect(response.body.message).toBe('User verified successfully.') | ||
},60000) | ||
|
||
it("It should send a verification link.",async()=>{ | ||
const response = await request(app) | ||
.post('/api/v1/users/verify-user-email') | ||
.send({ | ||
email:userData.email | ||
}) | ||
expect(response.status).toBe(201) | ||
expect(response.body.message).toBe("Verification email sent successfully.") | ||
},60000) | ||
|
||
}) | ||
|
||
afterAll(async () => { | ||
try { | ||
await sequelize.query('TRUNCATE TABLE profiles, users CASCADE'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.