Skip to content

Commit

Permalink
Merge branch 'UserFeature' of https://github.com/a0uda/Reddit-BE into…
Browse files Browse the repository at this point in the history
… UserFeature
  • Loading branch information
AhmeddHanyy committed Apr 8, 2024
2 parents 2c4cc2f + 22b447c commit 55d0c68
Show file tree
Hide file tree
Showing 3 changed files with 346 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"dev": "nodemon src/index.js",
"test": "jest --testEnvironment=node --runInBand ./tests",
"test": "jest --testEnvironment=node --runInBand ./tests/communityContentControls.test.js",
"docs": "jsdoc -c src/config/jsdocsconfig.json",
"seed": "node ./seeds/seed.js"
},
Expand Down
240 changes: 240 additions & 0 deletions tests/communityContentControls.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
import { getCommunityContentControls, changeCommunityContentControls } from '../src/services/communities.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 () => {
const mockContentControls = {
providing_members_with_posting_guidlines: {
flag: true,
guidline_text: 'Sample guideline text',
},
require_words_in_post_title: {
flag: false,
add_required_words: ['Sample', 'Required'],
},
ban_words_from_post_title: {
flag: true,
add_banned_words: ['Banned', 'Words'],
},
ban_words_from_post_body: {
flag: true,
add_banned_words: 'Banned words from post body',
},
require_or_ban_links_from_specific_domains: {
flag: true,
restriction_type: 'Required domains',
require_or_block_link_posts_with_these_domains: 'Required domain',
},
restrict_how_often_the_same_link_can_be_posted: {
flag: true,
number_of_days: 7,
},
};

const mockCommunity = {
name: 'SampleCommunity',
content_controls: mockContentControls,
};

const mockQuery = {
populate: jest.fn().mockReturnThis(),
exec: jest.fn().mockResolvedValue(mockCommunity),
};

Community.findOne = jest.fn().mockReturnValue(mockQuery);

const communityName = 'SampleCommunity';
const result = await getCommunityContentControls(communityName);

expect(Community.findOne).toHaveBeenCalledWith({ name: communityName });
expect(result).toEqual({ content_controls: mockContentControls });
expect(mockQuery.populate).toHaveBeenCalledWith('content_controls');
});

it('should return an error object when a database error occurs during query execution', async () => {
const mockQuery = {
populate: jest.fn().mockReturnThis(),
exec: jest.fn().mockRejectedValue(new Error('Database error')),
};

Community.findOne = jest.fn().mockReturnValue(mockQuery);

const communityName = 'SampleCommunity';

try {
const result = await getCommunityContentControls(communityName);
} catch (result) {
expect(result.err).toBeDefined();
expect(result.err.status).toBe(500);
expect(result.err.message).toBeDefined();
}

expect(Community.findOne).toHaveBeenCalledWith({ name: communityName });
expect(mockQuery.populate).toHaveBeenCalledWith('content_controls');
});

it('should return an error object when an unexpected error occurs during the population of content_controls', async () => {
// Mock the Community model's findOne method to throw an error
const mockQuery = {
populate: jest.fn().mockImplementation(() => {
throw new Error('Unexpected error');
}),
exec: jest.fn().mockReturnThis(),
};

Community.findOne = jest.fn().mockReturnValue(mockQuery);

// Call the function with a valid community name
const communityName = 'SampleCommunity';

// Use a try-catch block to catch the error returned by the function
try {
const result = await getCommunityContentControls(communityName);
} catch (result) {
// Check if the result contains an error object
expect(result.err).toBeDefined();
expect(result.err.status).toBe(500);
expect(result.err.message).toBeDefined();
}

// Check if Community.findOne was called with the correct argument
expect(Community.findOne).toHaveBeenCalledWith({ name: communityName });

// Check if the 'populate' method was called
expect(mockQuery.populate).toHaveBeenCalled();
});
});

describe('changeCommunityContentControls', () => {
it('should update and return the content controls for a valid community name', async () => {
const mockContentControls = {
providing_members_with_posting_guidlines: {
flag: true,
guidline_text: 'Sample guideline text',
},
require_words_in_post_title: {
flag: false,
add_required_words: ['Sample', 'Required'],
},
ban_words_from_post_title: {
flag: true,
add_banned_words: ['Banned', 'Words'],
},
ban_words_from_post_body: {
flag: true,
add_banned_words: 'Banned words from post body',
},
require_or_ban_links_from_specific_domains: {
flag: true,
restriction_type: 'Required domains',
require_or_block_link_posts_with_these_domains: 'Required domain',
},
restrict_how_often_the_same_link_can_be_posted: {
flag: true,
number_of_days: 7,
},
};

const updatedContentControls = {
providing_members_with_posting_guidlines: {
flag: false,
guidline_text: 'Updated guideline text',
},
require_words_in_post_title: {
flag: true,
add_required_words: ['Updated', 'Required'],
},
ban_words_from_post_title: {
flag: false,
add_banned_words: ['UpdatedBanned', 'Words'],
},
ban_words_from_post_body: {
flag: false,
add_banned_words: 'Updated banned words from post body',
},
require_or_ban_links_from_specific_domains: {
flag: false,
restriction_type: 'Blocked domains',
require_or_block_link_posts_with_these_domains: 'Updated blocked domain',
},
restrict_how_often_the_same_link_can_be_posted: {
flag: false,
number_of_days: 14,
},
};

const mockCommunity = {
name: 'SampleCommunity',
content_controls: mockContentControls._id,
};

const mockContentControlsInstance = {
...mockContentControls,
save: jest.fn().mockResolvedValue({
...mockContentControls,
...updatedContentControls,
}),
};

Community.findOne = jest.fn().mockResolvedValue(mockCommunity);
CommunityContentControls.findById = jest.fn().mockResolvedValue(mockContentControlsInstance);

const communityName = 'SampleCommunity';
const result = await changeCommunityContentControls(communityName, updatedContentControls);

const { save, ...resultData } = result.updated_content_controls;

expect(Community.findOne).toHaveBeenCalledWith({ name: communityName });
expect(CommunityContentControls.findById).toHaveBeenCalledWith(mockCommunity.content_controls);
expect(resultData).toEqual({ ...mockContentControls, ...updatedContentControls });
expect(mockContentControlsInstance.save).toHaveBeenCalled();
});

it('should return an error object when a database error occurs during query execution', async () => {
Community.findOne = jest.fn().mockRejectedValue(new Error('Database error'));

const communityName = 'SampleCommunity';
const contentControls = {};

try {
const result = await changeCommunityContentControls(communityName, contentControls);
} catch (result) {
const { err, ...resultData } = result;
expect(err).toBeDefined();
expect(err.status).toBe(500);
expect(err.message).toBeDefined();
expect(resultData).toEqual({});
}

expect(Community.findOne).toHaveBeenCalledWith({ name: communityName });
});

it('should return an error object when an unexpected error occurs during the update of content_controls', async () => {
const mockCommunity = {
name: 'SampleCommunity',
content_controls: '123',
};

Community.findOne = jest.fn().mockResolvedValue(mockCommunity);
CommunityContentControls.findById = jest.fn().mockImplementation(() => {
throw new Error('Unexpected error');
});

const communityName = 'SampleCommunity';
const contentControls = {};

try {
const result = await changeCommunityContentControls(communityName, contentControls);
} catch (result) {
const { err, ...resultData } = result;
expect(err).toBeDefined();
expect(err.status).toBe(500);
expect(err.message).toBeDefined();
expect(resultData).toEqual({});
}

expect(Community.findOne).toHaveBeenCalledWith({ name: communityName });
expect(CommunityContentControls.findById).toHaveBeenCalledWith(mockCommunity.content_controls);
});
});
106 changes: 105 additions & 1 deletion tests/communityGeneralSettings.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getCommunityGenerlSettings } from '../src/services/communities.js';
import { getCommunityGenerlSettings, changeCommunityGeneralSettings } from '../src/services/communities.js';
import { Community } from '../src/db/models/Community.js';
import { CommunityGeneralSettings } from '../src/db/models/communityGeneralSettings.js';

describe('getCommunityGenerlSettings', () => {
it('should return general settings for a valid community name', async () => {
Expand Down Expand Up @@ -108,3 +109,106 @@ describe('getCommunityGenerlSettings', () => {
expect(mockQuery.populate).toHaveBeenCalled();
});
});

describe('changeCommunityGeneralSettings', () => {
it('should update and return the general settings for a valid community name', async () => {
const mockGeneralSettings = {
_id: '123',
description: 'Sample description',
welcome_message: {
send_welcome_message_flag: true,
message: 'Welcome to our community!',
},
language: 'English',
region: 'US',
visibility: 'Public',
nsfw_flag: false,
};

const updatedGeneralSettings = {
description: 'Updated description',
welcome_message: {
send_welcome_message_flag: false,
message: 'Welcome!',
},
language: 'Spanish',
region: 'ES',
visibility: 'Private',
nsfw_flag: true,
};

const mockCommunity = {
name: 'SampleCommunity',
general_settings: mockGeneralSettings._id,
};

const mockGeneralSettingsInstance = {
...mockGeneralSettings,
save: jest.fn().mockResolvedValue({
...mockGeneralSettings,
...updatedGeneralSettings,
}),
};

Community.findOne = jest.fn().mockResolvedValue(mockCommunity);
CommunityGeneralSettings.findById = jest.fn().mockResolvedValue(mockGeneralSettingsInstance);

const communityName = 'SampleCommunity';
const result = await changeCommunityGeneralSettings(communityName, updatedGeneralSettings);

// Exclude the save function from the result object
const { save, ...resultData } = result.updated_general_settings;

expect(Community.findOne).toHaveBeenCalledWith({ name: communityName });
expect(CommunityGeneralSettings.findById).toHaveBeenCalledWith(mockCommunity.general_settings);
expect(resultData).toEqual({ ...mockGeneralSettings, ...updatedGeneralSettings });
expect(mockGeneralSettingsInstance.save).toHaveBeenCalled();
});

it('should return an error object when a database error occurs during query execution', async () => {
Community.findOne = jest.fn().mockRejectedValue(new Error('Database error'));

const communityName = 'SampleCommunity';
const generalSettings = {};

try {
const result = await changeCommunityGeneralSettings(communityName, generalSettings);
} catch (result) {
const { err, ...resultData } = result;
expect(err).toBeDefined();
expect(err.status).toBe(500);
expect(err.message).toBeDefined();
expect(resultData).toEqual({});
}

expect(Community.findOne).toHaveBeenCalledWith({ name: communityName });
});

it('should return an error object when an unexpected error occurs during the update of general_settings', async () => {
const mockCommunity = {
name: 'SampleCommunity',
general_settings: '123',
};

Community.findOne = jest.fn().mockResolvedValue(mockCommunity);
CommunityGeneralSettings.findById = jest.fn().mockImplementation(() => {
throw new Error('Unexpected error');
});

const communityName = 'SampleCommunity';
const generalSettings = {};

try {
const result = await changeCommunityGeneralSettings(communityName, generalSettings);
} catch (result) {
const { err, ...resultData } = result;
expect(err).toBeDefined();
expect(err.status).toBe(500);
expect(err.message).toBeDefined();
expect(resultData).toEqual({});
}

expect(Community.findOne).toHaveBeenCalledWith({ name: communityName });
expect(CommunityGeneralSettings.findById).toHaveBeenCalledWith(mockCommunity.general_settings);
});
});

0 comments on commit 55d0c68

Please sign in to comment.