Skip to content

Commit

Permalink
Merge branch 'main' into ChatsFeature
Browse files Browse the repository at this point in the history
  • Loading branch information
FatemaKotb authored May 4, 2024
2 parents 62fe2f2 + a955bbe commit f71ae58
Show file tree
Hide file tree
Showing 89 changed files with 4,816 additions and 3,226 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ my-comments
.env
coverage
mapping
.gitignore
.gitignore
migrations
82 changes: 49 additions & 33 deletions __test__/comments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ describe("Get Comment", () => {
const request = {
body: { id: "invalid_comment_id" },
};
Comment.findById.mockResolvedValue(null);

const mockQuery = {
populate: jest.fn().mockReturnThis(),
exec: jest.fn().mockResolvedValue(null),
};
Comment.findById = jest.fn().mockReturnValue(mockQuery);

const result = await getComment(request, false);
expect(result.success).toBe(false);
Expand All @@ -57,6 +62,7 @@ describe("Get Comment", () => {
};
const verifiedUser = {
_id: "verified_user_id",
token: ["valid_token"],
generateAuthToken: jest.fn(),
};
const mockComment = {
Expand All @@ -66,7 +72,11 @@ describe("Get Comment", () => {
};
User.mockImplementation(() => mockUser);
User.findById = jest.fn().mockReturnValue(verifiedUser);
Comment.findById.mockResolvedValue(mockComment);
const mockQuery = {
populate: jest.fn().mockReturnThis(),
exec: jest.fn().mockResolvedValue(mockComment),
};
Comment.findById = jest.fn().mockReturnValue(mockQuery);
jwt.verify.mockReturnValue({ _id: verifiedUser._id });

const result = await getComment(request, true);
Expand All @@ -85,7 +95,12 @@ describe("Get Comment", () => {
text: "Test comment",
user_id: "commenter_user_id",
};
Comment.findById.mockResolvedValue(mockComment);

const mockQuery = {
populate: jest.fn().mockReturnThis(),
exec: jest.fn().mockResolvedValue(mockComment),
};
Comment.findById = jest.fn().mockReturnValue(mockQuery);

const result = await getComment(request, false);
expect(result.success).toBe(true);
Expand Down Expand Up @@ -178,36 +193,37 @@ describe("New Comment", () => {
);
});

it("should create comment successfully", async () => {
const request = { body: { description: "Test comment" } };
const mockPost = {
_id: "post_id",
locked_flag: false,
post_in_community_flag: false,
description: "test",
save: jest.fn(),
};
const mockUser = { _id: "user_id", username: "test_user" };
getPost.mockResolvedValueOnce({
success: true,
post: mockPost,
user: mockUser,
message: "Post Retrieved successfully",
});
Comment.mockReturnValueOnce({
save: jest.fn().mockResolvedValueOnce(true),
});

Post.mockReturnValueOnce({
save: jest.fn().mockResolvedValueOnce(true),
});

const result = await newComment(request);

expect(result.success).toBe(true);
expect(result.error).toEqual({});
expect(result.message).toEqual("Comment created successfully");
});
// it("should create comment successfully", async () => {
// const request = { body: { description: "Test comment" } };
// const mockPost = {
// _id: "post_id",
// locked_flag: false,
// post_in_community_flag: false,
// description: "test",
// comments_count: 0,
// save: jest.fn(),
// };
// const mockUser = { _id: "user_id", username: "test_user" };
// getPost.mockResolvedValueOnce({
// success: true,
// post: mockPost,
// user: mockUser,
// message: "Post Retrieved successfully",
// });
// Post.findById = jest.fn().mockReturnValue(mockPost);
// Comment.mockReturnValueOnce({
// save: jest.fn().mockResolvedValueOnce(true),
// });

// Post.mockReturnValueOnce({
// save: jest.fn().mockResolvedValueOnce(true),
// });
// const result = await newComment(request);

// expect(result.success).toBe(true);
// expect(result.error).toEqual({});
// expect(result.message).toEqual("Comment created successfully");
// });
});

// describe("Reply to Comment", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getCommunityContentControls, changeCommunityContentControls } from '../src/services/communitySettingsService.js';
import { Community } from '../src/db/models/Community.js';
import { CommunityContentControls } from '../src/db/models/communityContentControls.js';
import { getCommunityContentControls, changeCommunityContentControls } from '../../src/services/communitySettingsService.js';
import { Community } from '../../src/db/models/Community.js';
import { CommunityContentControls } from '../../src/db/models/communityContentControls.js';

describe('getCommunityContentControls', () => {
it('should return content controls for a valid community name', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getDetailsWidget, editDetailsWidget } from "../src/services/communityService";
import { communityNameExists } from "../src/utils/communities";
jest.mock("../src/services/communityService");
jest.mock("../src/utils/communities");
import { getDetailsWidget, editDetailsWidget } from "../../src/services/communityService";
import { communityNameExists } from "../../src/utils/communities";
jest.mock("../../src/services/communityService");
jest.mock("../../src/utils/communities");


describe("Community Details Widget", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getCommunityGeneralSettings, changeCommunityGeneralSettings } from '../src/services/communitySettingsService.js';
import { Community } from '../src/db/models/Community.js';
import { CommunityGeneralSettings } from '../src/db/models/communityGeneralSettings.js';
import { getCommunityGeneralSettings, changeCommunityGeneralSettings } from '../../src/services/communitySettingsService.js';
import { Community } from '../../src/db/models/Community.js';
import { CommunityGeneralSettings } from '../../src/db/models/communityGeneralSettings.js';

describe('getCommunityGeneralSettings', () => {
it('should return general settings for a valid community name', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getCommunityPostsAndComments, changeCommunityPostsAndComments } from '../src/services/communitySettingsService.js';
import { Community } from '../src/db/models/Community.js';
import { CommunityPostsAndComments } from '../src/db/models/communityPostsAndComments.js';
import { getCommunityPostsAndComments, changeCommunityPostsAndComments } from '../../src/services/communitySettingsService.js';
import { Community } from '../../src/db/models/Community.js';
import { CommunityPostsAndComments } from '../../src/db/models/communityPostsAndComments.js';

describe('getCommunityPostsAndComments', () => {
it('should return posts and comments for a valid community name', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { addCommunityBannerPicture, addCommunityProfilePicture, deleteCommunityBannerPicture, deleteCommunityProfilePicture } from "../src/services/communityProfileAndBannerPictures";
import { communityNameExists } from "../src/utils/communities";
jest.mock("../src/utils/communities");
jest.mock("../src/utils/communities");
import { addCommunityBannerPicture, addCommunityProfilePicture, deleteCommunityBannerPicture, deleteCommunityProfilePicture } from "../../src/services/communityProfileAndBannerPictures";
import { communityNameExists } from "../../src/utils/communities";
jest.mock("../../src/utils/communities");
jest.mock("../../src/utils/communities");

describe("Community Profile Picture", () => {
it("should add a community profile picture", async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { removeItem, spamItem, reportItem, approveItem } from '../src/services/communityQueueService.js';
import { Post } from '../src/db/models/Post.js';
import { Comment } from '../src/db/models/Comment.js';
import { Community } from '../src/db/models/Community.js';
import { removeItem, spamItem, reportItem, approveItem } from '../../src/services/communityQueueService.js';
import { Post } from '../../src/db/models/Post.js';
import { Comment } from '../../src/db/models/Comment.js';
import { Community } from '../../src/db/models/Community.js';

jest.mock('../src/db/models/Post.js');
jest.mock('../src/db/models/Comment.js');
jest.mock('../src/db/models/Community.js');
jest.mock('../../src/db/models/Post.js');
jest.mock('../../src/db/models/Comment.js');
jest.mock('../../src/db/models/Community.js');

describe('removeItem', () => {
it('should return error for invalid input parameters', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getRemovedItems, getReportedItems, getUnmoderatedItems } from '../src/services/communityQueueService.js';
import { Post } from '../src/db/models/Post.js';
import { Comment } from '../src/db/models/Comment.js';
import { getRemovedItems, getReportedItems, getUnmoderatedItems } from '../../src/services/communityQueueService.js';
import { Post } from '../../src/db/models/Post.js';
import { Comment } from '../../src/db/models/Comment.js';

const mockPosts = [];
const mockComments = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

import { getCommunityRules, addNewRuleToCommunity, editCommunityRule, deleteCommunityRule } from "../src/services/communityRulesAndRemovalReasons";
import * as communityUtils from "../src/utils/communities"; // Import the entire module
import { Rule } from "../src/db/models/Rule";
import { getCommunityRules, addNewRuleToCommunity, editCommunityRule, deleteCommunityRule } from "../../src/services/communityRulesAndRemovalReasons";
import * as communityUtils from "../../src/utils/communities"; // Import the entire module
import { Rule } from "../../src/db/models/Rule";
// Mock the entire module
jest.mock("../src/utils/communities");
jest.mock("../src/db/models/Rule");
jest.mock("../../src/utils/communities");
jest.mock("../../src/db/models/Rule");

describe('getCommunityRules', () => {
it('should return community rules for a valid community name', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { approveUser, getApprovedUsers, muteUser, getMutedUsers, banUser, getBannedUsers, getModerators, getEditableModerators, moderatorLeaveCommunity, addModerator, deleteModerator } from '../src/services/communityUserManagement';
import { User } from '../src/db/models/User';
import { communityNameExists, isUserAlreadyApproved, getApprovedUserView } from '../src/utils/communities';
import { verifyAuthToken } from '../src/controller/userAuth';
jest.mock("../src/utils/communities");
jest.mock("../src/db/models/User");
jest.mock("../src/controller/userAuth");
import { approveUser, getApprovedUsers, muteUser, getMutedUsers, banUser, getBannedUsers, getModerators, getEditableModerators, moderatorLeaveCommunity, addModerator, deleteModerator } from '../../src/services/communityUserManagement';
import { User } from '../../src/db/models/User';
import { communityNameExists, isUserAlreadyApproved, getApprovedUserView } from '../../src/utils/communities';
import { verifyAuthToken } from '../../src/controller/userAuth';
jest.mock("../../src/utils/communities");
jest.mock("../../src/db/models/User");
jest.mock("../../src/controller/userAuth");
//contents:
//1. Test the approveUser function
//2. Test the getApprovedUsers function
Expand Down Expand Up @@ -614,34 +614,35 @@ describe('getEditableModerators', () => {
});

describe('moderatorLeaveCommunity', () => {
it('should return success if the user is a moderator of the community', async () => {
jest.resetAllMocks();
const request = {
body: {
community_name: 'existingCommunityName',
},
headers: {
authorization: 'Bearer token',
},
};
const user = {
username: 'existingUsername',
profile_picture: 'profilePicture',
};
const community = {
name: 'existingCommunityName',
moderators: [
{ username: 'existingUsername', moderator_since: '2021-05-11T14:48:00.000Z', has_access: { everything: true, manage_users: true } },
],
save: jest.fn(),
};
verifyAuthToken.mockResolvedValueOnce({ success: true, user });
User.findOne.mockResolvedValueOnce(user);
communityNameExists.mockResolvedValueOnce(community);
const result = await moderatorLeaveCommunity(request);
expect(result).toEqual({ success: true });
expect(community.save).toHaveBeenCalled();
});
// it('should return success if the user is a moderator of the community', async () => {
// jest.resetAllMocks();
// const request = {
// body: {
// community_name: 'existingCommunityName',
// },
// headers: {
// authorization: 'Bearer token',
// },
// };
// const user = {
// username: 'existingUsername',
// profile_picture: 'profilePicture',
// moderated_communities: ['existingCommunityName'],
// };
// const community = {
// name: 'existingCommunityName',
// moderators: [
// { username: 'existingUsername', moderator_since: '2021-05-11T14:48:00.000Z', has_access: { everything: true, manage_users: true } },
// ],
// save: jest.fn(),
// };
// verifyAuthToken.mockResolvedValueOnce({ success: true, user });
// User.findOne.mockResolvedValueOnce(user);
// communityNameExists.mockResolvedValueOnce(community);
// const result = await moderatorLeaveCommunity(request);
// expect(result).toEqual({ success: true });
// expect(community.save).toHaveBeenCalled();
// });

it('should return error if the user is not a moderator of the community', async () => {
jest.resetAllMocks();
Expand Down Expand Up @@ -784,29 +785,29 @@ describe('addModerator', () => {
});
})
describe('deleteModerator', () => {
it('should return success if the user is a moderator of the community', async () => {
jest.resetAllMocks();
const requestBody = {
community_name: 'existingCommunityName',
username: 'existingUsername',
};
const community = {
name: 'existingCommunityName',
moderators: [
{ username: 'existingUsername', moderator_since: '2021-05-11T14:48:00.000Z', has_access: { everything: true, manage_users: true } },
],
save: jest.fn(),
};
const user = {
username: 'existingUsername',
};
communityNameExists.mockResolvedValueOnce(community);
User.findOne.mockResolvedValueOnce(user);
const result = await deleteModerator(requestBody);
expect(result).toEqual({ success: true });
expect(community.moderators).toEqual([]);
expect(community.save).toHaveBeenCalled();
});
// it('should return success if the user is a moderator of the community', async () => {
// jest.resetAllMocks();
// const requestBody = {
// community_name: 'existingCommunityName',
// username: 'existingUsername',
// };
// const community = {
// name: 'existingCommunityName',
// moderators: [
// { username: 'existingUsername', moderator_since: '2021-05-11T14:48:00.000Z', has_access: { everything: true, manage_users: true } },
// ],
// save: jest.fn(),
// };
// const user = {
// username: 'existingUsername',
// };
// communityNameExists.mockResolvedValueOnce(community);
// User.findOne.mockResolvedValueOnce(user);
// const result = await deleteModerator(requestBody);
// expect(result).toEqual({ success: true });
// expect(community.moderators).toEqual([]);
// expect(community.save).toHaveBeenCalled();
// });


it('should return error if the community is not found', async () => {
Expand Down
27 changes: 0 additions & 27 deletions __test__/db.js

This file was deleted.

Loading

0 comments on commit f71ae58

Please sign in to comment.