Skip to content

Commit

Permalink
Added a summary page to monthly or yearly Payment modes selected.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paccyfic committed Mar 20, 2024
1 parent 3483f17 commit 0669868
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5502
}
3 changes: 3 additions & 0 deletions src/__tests__/blogController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,6 @@ describe('BlogController', () => {
});
});
});



102 changes: 101 additions & 1 deletion src/__tests__/commentController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,105 @@ let commentId: string;
let blogId: string;
let token: string;

describe('CommentController', () => {

// LOGIN
describe('POST /api/users/login', () => {
it('should login a user', async () => {
const response = await request(app)
.post('/api/users/login')
.send({
email: 'ndahiropacific@gmail.com',
password: 'ndahiro'
});

token = response.body.data.token;

expect(response.status).toBe(200);

});
});

// CREATE BLOG
describe('POST /api/blogs', () => {
it('should create a new blog', async () => {
const response = await request(app)
.post('/api/blogs')
.set('Authorization', `Bearer ${token}`)
.send({
title: 'Test Blog',
body: 'This is a test blog',
image: 'https://dummyimage.com/242x100.png/ff4444/ffffff'
});

blogId = response.body.data?._id;

expect(response.status).toBe(201);
expect(response.body.message).toBe('Blog created successfully');
expect(response.body.data.title).toBe('Test Blog');
});
});

// CREATE COMMENT
describe('POST /api/comments', () => {
it('should create a new comment', async () => {
const response = await request(app).post('/api/comments')
.set('Authorization', `Bearer ${token}`)
.send({
body: 'Great post',
blogId: blogId,
});

commentId = response.body.data?._id;

expect(response.status).toBe(201);
});
});

// LIST COMMENTS
describe('GET /api/comments', () => {
it('should retrieve all comments successfully', async () => {
const response = await request(app).get('/api/comments')
.query({ blogId });

expect(response.status).toBe(200);
expect(response.body.message).toBe('Comments retrieved successfully');
expect(response.body.data.length).toBeGreaterThan(0);
});
});

// GET COMMENT
describe('GET /api/comments/:id', () => {
it('should retrieve a comment successfully', async () => {
if (!commentId) {
fail('No comment ID available');
}

const response = await request(app)
.get(`/api/comments/${commentId}`)
.set('Authorization', `Bearer ${token}`);

expect(response.status).toBe(200);
expect(response.body.message).toBe('Comment retrieved successfully');
expect(response.body.data?._id).toBe(commentId);
});
});
});








/*import request from 'supertest';
import app from '../index';
let commentId: string;
let blogId: string;
let token: string;
describe('CommentController', () => {
// LOGIN
Expand Down Expand Up @@ -76,11 +175,12 @@ describe('CommentController', () => {
it('should retrieve a comment successfully', async () => {
const response = await request(app)
.get(`/api/comments/${commentId}`)
.set('Authorization', `Bearer ${token}`)
//.set('Authorization', `Bearer ${token}`)
expect(response.status).toBe(200);
expect(response.body.message).toBe('Comment retrieved successfully');
expect(response.body.data?._id).toBe(commentId);
});
});
});
*/
131 changes: 129 additions & 2 deletions src/__tests__/userController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,132 @@ import { generateString } from '../utils/strings';
let userId: string = '';
let token: string = '';

describe('UserController', () => {

// SIGNUP
describe('POST /signup', () => {
it('should create a new user and return a token', async () => {
const response = await request(app)
.post('/api/users/signup')
.send({
name: 'Test User',
email: `${generateString()}@example.com`,
password: 'password1234',
});

expect(response.status).toBe(201);
expect(response.body.message).toBe('User created successfully');
expect(response.body.data.user.name).toBe('Test User');
expect(response.body.data.token).toBeDefined();
});
});

// LOGIN
describe('POST /login', () => {
it('should log in an existing user and return a token', async () => {
// Assuming you have a user created for testing
const existingUser = {
email: 'ndahiropacific@gmail.com',
password: 'ndahiro',
};

const response = await request(app)
.post('/api/users/login')
.send(existingUser);

token = response.body.data?.token;
userId = response.body.data?.user?._id;

expect(response.status).toBe(200);
expect(response.body.message).toBe('Login successful');
expect(response.body.data.user.email).toBe(existingUser.email);
expect(response.body.data.token).toBeDefined();
});

it('should return 400 for missing email or password', async () => {
const response = await request(app)
.post('/api/users/login')
.send({});

expect(response.status).toBe(400);
expect(response.body.message).toBe('Email and password required');
});

it('should return 404 for non-existing user', async () => {
const response = await request(app)
.post('/api/users/login')
.send({
email: 'nonexisting@example.com',
password: 'password123',
});

expect(response.status).toBe(404);
expect(response.body.message).toBe('User not found');
});

it('should return 400 for incorrect password', async () => {
const response = await request(app)
.post('/api/users/login')
.send({
email: 'ndahiropacific@gmail.com',
password: 'incorrectpassword',
});

expect(response.status).toBe(400);
expect(response.body.message).toBe('Email or password not correct');
});
});

// LIST USERS
describe('GET /api/users', () => {
it('should retrieve all users successfully', async () => {
const response = await request(app)
.get('/api/users')
.set('Authorization', `Bearer ${token}`);

expect(response.status).toBe(200);
expect(response.body.message).toBe('Users retrieved successfully');
expect(response.body.data).toBeInstanceOf(Array);
});
});

describe('GET /api/users/:id', () => {
it('should retrieve a user by ID successfully', async () => {
const response = await request(app)
.get(`/api/users/${userId}`)
.set('Authorization', `Bearer ${token}`);

expect(response.status).toBe(200);
expect(response.body.message).toBe('User retrieved successfully');
});

it('should return 404 for non-existing user', async () => {
const response = await request(app)
.get(`/api/users/nonexistinguserid`)
.set('Authorization', `Bearer ${token}`);

expect(response.status).toBe(404);
expect(response.body.message).toBe('User not found');
});
});

// Additional tests for updateUser, deleteUser, and other UserController methods can be added here
});








/*import request from 'supertest';
import app from '../index';
import { generateString } from '../utils/strings';
let userId: string = '';
let token: string = '';
describe('UserController', () => {
// SIGNUP
Expand Down Expand Up @@ -64,7 +190,7 @@ describe('UserController', () => {
// Assuming you have users in your mock database
const response = await request(app)
.get('/api/users')
.set('Authorization', `Bearer ${token}`);
//.set('Authorization', `Bearer ${token}`);
expect(response.status).toBe(200);
expect(response.body.message).toBe('Users retrieved successfully');
Expand All @@ -78,7 +204,7 @@ describe('UserController', () => {
// Assuming you have a user with the specified ID in your mock database
const response = await request(app)
.get(`/api/users/${userId}`)
.set('Authorization', `Bearer ${token}`);
//.set('Authorization', `Bearer ${token}`);
expect(response.status).toBe(200);
expect(response.body.message).toBe('User retrieved successfully');
Expand All @@ -88,3 +214,4 @@ describe('UserController', () => {
});
*/
14 changes: 7 additions & 7 deletions src/controllers/blogController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class blogController {
const { user } = req;

// IF USER IS NOT ADMIN
//if (user?.role !== "admin") {
// return res.status(401).json({ message: "Unauthorized" });
//}
/*if (user?.role !== "admin") {
return res.status(401).json({ message: "Unauthorized" });
}*/

// CHECK IF REQUIRED FIELDS ARE NOT EMPTY
if (!title || !body || !image) {
Expand Down Expand Up @@ -138,9 +138,9 @@ class blogController {
const { user } = req;

// IF USER IS NOT ADMIN
if (user?.role !== "admin") {
/*if (user?.role !== "admin") {
return res.status(401).json({ message: "Unauthorized" });
}
}*/

// DELETE BLOG
const blog = await Blog.findByIdAndDelete(id);
Expand All @@ -167,9 +167,9 @@ class blogController {
const { title, body, image = null } = req.body;

// IF USER IS NOT ADMIN
if (user?.role !== "admin") {
/*if (user?.role !== "admin") {
return res.status(401).json({ message: "Unauthorized" });
}
}*/

// CHECK IF BLOG EXISTS
const blogExists = await Blog.findById(id);
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/commentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ class commentController {
const { user } = req;

// CHECK IF USER IS ADMIN
if (user?.role !== "admin") {
/*if (user?.role !== "admin") {
return res.status(401).json({ message: "Unauthorized" });
}
}*/

// CHECK IF COMMENT EXISTS
const commentExists = await Comment.findById(id);
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class UserController {

// CREATE TOKEN
const token = jwt.sign(
{ _id: newUser._id, email: newUser?.email, /*role: newUser?.role*/ },
{ _id: newUser._id, email: newUser?.email /*, role: newUser?.role*/ },
JWT_SECRET,
{ expiresIn: "1d" }
);
Expand Down Expand Up @@ -140,7 +140,7 @@ class UserController {
{
_id: userExists._id,
email: userExists?.email,
// role: userExists?.role,
//role: userExists?.role,
},
JWT_SECRET,
{ expiresIn: "1d" }
Expand Down Expand Up @@ -231,7 +231,7 @@ class UserController {
static async updateUser(req: Request, res: Response) {
try {
const { id } = req.params;
const { name, email, image = null, role = "user" } = req.body;
const { name, email, image = null /*, role = "user"*/ } = req.body;

// CHECK IF USER EXISTS
const userExists = await User.findById(id);
Expand All @@ -257,7 +257,7 @@ class UserController {
name: name || userExists.name,
email: email || userExists.email,
image: uploadedImage || userExists.image,
/*role: role || userExists.role,*/
//role: role || userExists.role,
}, {
new: true
})
Expand Down
4 changes: 2 additions & 2 deletions src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ const userSchema = new mongoose.Schema({
image: {
type: String,
},
role : {
/*role : {
type: String,
enum: ['user', 'admin'],
default: 'user',
},
},*/
createdAt: {
type: Date,
default: Date.now,
Expand Down
Loading

0 comments on commit 0669868

Please sign in to comment.