Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for send_membership_request.js #763

Merged
merged 8 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const MEMBERSHIP_REQUEST_NOT_FOUND_MESSAGE = 'membershipRequest.notFound';
const MEMBERSHIP_REQUEST_NOT_FOUND_CODE = 'membershipRequest.notFound';
const MEMBERSHIP_REQUEST_NOT_FOUND_PARAM = 'membershipRequest';

const MEMBERSHIP_REQUEST_ALREADY_EXIST = 'Membership Request already Exists';
const MEMBERSHIP_REQUEST_ALREADY_EXIST_MESSAGE =
'membershipRequest.alreadyExists';
const MEMBERSHIP_REQUEST_ALREADY_EXIST_CODE = 'membershipRequest.alreadyExists';
const MEMBERSHIP_REQUEST_ALREADY_EXIST_PARAM = 'membershipRequest';

const USER_NOT_FOUND = 'User not found';
const USER_NOT_FOUND_MESSAGE = 'user.notFound';
const USER_NOT_FOUND_CODE = 'user.notFound';
Expand Down Expand Up @@ -132,6 +138,11 @@ module.exports = {
MEMBERSHIP_REQUEST_NOT_FOUND_MESSAGE,
MEMBERSHIP_REQUEST_NOT_FOUND_PARAM,

MEMBERSHIP_REQUEST_ALREADY_EXIST,
MEMBERSHIP_REQUEST_ALREADY_EXIST_CODE,
MEMBERSHIP_REQUEST_ALREADY_EXIST_MESSAGE,
MEMBERSHIP_REQUEST_ALREADY_EXIST_PARAM,

REGISTRANT_ALREADY_EXIST,
REGISTRANT_ALREADY_EXIST_CODE,
REGISTRANT_ALREADY_EXIST_MESSAGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,59 @@ const MembershipRequest = require('../../models/MembershipRequest');
const { NotFoundError, ConflictError } = require('errors');
const requestContext = require('talawa-request-context');

const {
ORGANIZATION_NOT_FOUND,
ORGANIZATION_NOT_FOUND_PARAM,
ORGANIZATION_NOT_FOUND_MESSAGE,
ORGANIZATION_NOT_FOUND_CODE,
USER_NOT_FOUND,
USER_NOT_FOUND_MESSAGE,
USER_NOT_FOUND_CODE,
USER_NOT_FOUND_PARAM,
MEMBERSHIP_REQUEST_ALREADY_EXIST,
MEMBERSHIP_REQUEST_ALREADY_EXIST_CODE,
MEMBERSHIP_REQUEST_ALREADY_EXIST_MESSAGE,
MEMBERSHIP_REQUEST_ALREADY_EXIST_PARAM,
IN_PRODUCTION,
} = require('../../../constants');

module.exports = async (parent, args, context) => {
// ensure user exists
const user = await User.findOne({ _id: context.userId });
if (!user) {
throw new NotFoundError(
requestContext.translate('user.notFound'),
'user.notFound',
'user'
!IN_PRODUCTION
? USER_NOT_FOUND
: requestContext.translate(USER_NOT_FOUND_MESSAGE),
USER_NOT_FOUND_CODE,
USER_NOT_FOUND_PARAM
);
}

// ensure organization exists
const org = await Organization.findOne({ _id: args.organizationId });
if (!org) {
throw new NotFoundError(
requestContext.translate('organization.notFound'),
'organization.notFound',
'organization'
!IN_PRODUCTION
? ORGANIZATION_NOT_FOUND
: requestContext.translate(ORGANIZATION_NOT_FOUND_MESSAGE),
ORGANIZATION_NOT_FOUND_CODE,
ORGANIZATION_NOT_FOUND_PARAM
);
}

// create membership request
const exists = await MembershipRequest.find({
user: user.id,
organization: org.id,
});
console.log(exists);
console.log({ exists });
if (exists.length > 0) {
throw new ConflictError(
requestContext.translate('membershipRequest.alreadyExists'),
'membershipRequest.alreadyExists',
'membershipRequest'
!IN_PRODUCTION
? MEMBERSHIP_REQUEST_ALREADY_EXIST
: requestContext.translate(MEMBERSHIP_REQUEST_ALREADY_EXIST_MESSAGE),
MEMBERSHIP_REQUEST_ALREADY_EXIST_CODE,
MEMBERSHIP_REQUEST_ALREADY_EXIST_PARAM
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
const shortid = require('shortid');
const sendMembershipRequest = require('../../../lib/resolvers/membership_request_mutations/send_membership_request');
const {
Types: { ObjectId },
} = require('mongoose');

const database = require('../../../db');
const getUserIdFromSignUp = require('../../functions/getUserIdFromSignup');
const Organization = require('../../../lib/models/Organization');
const User = require('../../../lib/models/User');
const MembershipRequest = require('../../../lib/models/MembershipRequest');
const {
ORGANIZATION_NOT_FOUND,
USER_NOT_FOUND,
MEMBERSHIP_REQUEST_ALREADY_EXIST,
} = require('../../../constants');

let adminId;
let memberId;
let membershipRequesterId;

let organizationId;
let validMembershipRequestId;
let invalidOrgMembershipRequestId;
let invalidUserMembershipRequestId;

beforeAll(async () => {
// we need 1 org, 1 admin, 1 requester, 1 member
require('dotenv').config();
await database.connect();

const adminEmail = `${shortid.generate().toLowerCase()}@test.com`;
adminId = await getUserIdFromSignUp(adminEmail);

const memberEmail = `${shortid.generate().toLowerCase()}@test.com`;
memberId = await getUserIdFromSignUp(memberEmail);

const membershipRequesterEmail = `${shortid
.generate()
.toLowerCase()}@test.com`;
membershipRequesterId = await getUserIdFromSignUp(membershipRequesterEmail);

const organization = new Organization({
name: 'an organization',
description:
'an organization for testing the send_membership_request resolver',
isPublic: true,
visibileInSearch: true,
status: 'ACTIVE',
members: [adminId, memberId],
admins: [adminId],
posts: [],
membershipRequests: [],
blockedUsers: [],
groupChats: [],
image: '',
creator: adminId,
});

const savedOrg = await organization.save();
organizationId = savedOrg._id;

const membershipRequest = new MembershipRequest({
organization: organizationId,
user: membershipRequesterId,
});
const invalidOrgMembershipRequest = new MembershipRequest({
organization: new ObjectId(),
user: membershipRequesterId,
});
const invalidUserMembershipRequest = new MembershipRequest({
organization: organizationId,
user: new ObjectId(),
});

const savedMembershipRequest = await membershipRequest.save();
validMembershipRequestId = savedMembershipRequest._id;

const savedInvalidOrgMembershipRequest =
await invalidOrgMembershipRequest.save();
invalidOrgMembershipRequestId = savedInvalidOrgMembershipRequest._id;

const savedInvalidUserMembershipRequest =
await invalidUserMembershipRequest.save();
invalidUserMembershipRequestId = savedInvalidUserMembershipRequest._id;

const admin = await User.findById(adminId);
admin.overwrite({
...admin._doc,
joinedOrganizations: [organizationId],
createdOrganizations: [organizationId],
adminFor: [organizationId],
});
await admin.save();

const member = await User.findById(memberId);
member.overwrite({
...member._doc,
joinedOrganizations: [organizationId],
});
await member.save();

const membershipRequester = await User.findById(membershipRequesterId);
membershipRequester.overwrite({
...membershipRequester._doc,
membershipRequests: [validMembershipRequestId],
});
await membershipRequester.save();

savedOrg.overwrite({
...savedOrg._doc,
membershipRequests: [validMembershipRequestId],
});
await savedOrg.save();
});

afterAll(async () => {
await User.findByIdAndDelete(adminId);
await User.findByIdAndDelete(membershipRequesterId);
await User.findByIdAndDelete(memberId);

await Organization.findByIdAndDelete(organizationId);
await MembershipRequest.findByIdAndDelete(validMembershipRequestId);
await MembershipRequest.findByIdAndDelete(invalidOrgMembershipRequestId);
await MembershipRequest.findByIdAndDelete(invalidUserMembershipRequestId);

await database.disconnect();
});

describe('send membership request', () => {
test("user doesn't exist", async () => {
await expect(async () => {
await sendMembershipRequest(
{},
{ membershipRequestId: invalidUserMembershipRequestId },
{ userId: new ObjectId() }
);
}).rejects.toThrow(USER_NOT_FOUND);
});

test("organization doesn't exist", async () => {
await expect(async () => {
await sendMembershipRequest(
{},
{ membershipRequestId: invalidOrgMembershipRequestId },
{ userId: adminId }
);
}).rejects.toThrow(ORGANIZATION_NOT_FOUND);
});

test('membership already exist', async () => {
await expect(async () => {
await sendMembershipRequest(
{},
{ organizationId },
{ userId: membershipRequesterId }
);
}).rejects.toThrow(MEMBERSHIP_REQUEST_ALREADY_EXIST);
});

test('a valid accept member', async () => {
const membershipRequest = await sendMembershipRequest(
{},
{
membershipRequestId: validMembershipRequestId,
organizationId,
},
{ userId: adminId }
);
const org = await Organization.findById(organizationId);
const user = await User.findById(membershipRequesterId);

expect(membershipRequest).toBeTruthy();
expect(org.membershipRequests.length).toBeTruthy();
expect(user.membershipRequests.length).toBeTruthy();
});
});