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: create event mutation & events query unit tests #666

Closed
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
4 changes: 3 additions & 1 deletion lib/helper_functions/userExists.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ module.exports = async (id) => {
const user = await User.findOne({ _id: id });
if (!user) {
throw new NotFoundError(
requestContext.translate('user.notFound'),
process.env.NODE_ENV !== 'production'
? 'User not found'
: requestContext.translate('user.notFound'),
'user.notFound',
'user'
);
Expand Down
11 changes: 6 additions & 5 deletions lib/models/Event.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const User = require('./User');
const Task = require('./Task');
const UserAttende = new Schema({
userId: {
type: String,
required: true,
},
user: {
type: Schema.Types.ObjectId,
ref: 'User',
ref: User,
required: true,
},
status: {
Expand Down Expand Up @@ -89,14 +90,14 @@ const eventSchema = new Schema({
},
creator: {
type: Schema.Types.ObjectId,
ref: 'User',
ref: User,
required: true,
},
registrants: [UserAttende],
admins: [
{
type: Schema.Types.ObjectId,
ref: 'User',
ref: User,
required: true,
},
],
Expand All @@ -108,7 +109,7 @@ const eventSchema = new Schema({
tasks: [
{
type: Schema.Types.ObjectId,
ref: 'Task',
ref: Task,
},
],
status: {
Expand Down
12 changes: 9 additions & 3 deletions lib/resolvers/event_mutations/createEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const createEvent = async (parent, args, context) => {
const user = await User.findOne({ _id: context.userId });
if (!user) {
throw new NotFoundError(
requestContext.translate('user.notFound'),
process.env.NODE_ENV !== 'production'
? 'User not found'
: requestContext.translate('user.notFound'),
'user.notFound',
'user'
);
Expand All @@ -18,7 +20,9 @@ const createEvent = async (parent, args, context) => {
const org = await Organization.findOne({ _id: args.data.organizationId });
if (!org) {
throw new NotFoundError(
requestContext.translate('organization.notFound'),
process.env.NODE_ENV !== 'production'
? 'Organization not found'
: requestContext.translate('organization.notFound'),
'organization.notFound',
'organization'
);
Expand Down Expand Up @@ -80,7 +84,9 @@ const createEvent = async (parent, args, context) => {
}
// If user hasen't joined or created the org then throw an error
throw new NotFoundError(
requestContext.translate('org.notAuthorized'),
process.env.NODE_ENV !== 'production'
? 'Organization not Authorized'
: requestContext.translate('org.notAuthorized'),
'org.notAuthorized',
'org'
);
Expand Down
4 changes: 2 additions & 2 deletions lib/resolvers/user_query/myLanguage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module.exports = async (parent, args, context) => {
if (!user) {
throw new NotFoundError(
process.env.NODE_ENV !== 'production'
? 'User not found'
: requestContext.translate('user.notFound'),
? 'User not found'
: requestContext.translate('user.notFound'),
'user.notFound',
'user'
);
Expand Down
4 changes: 2 additions & 2 deletions lib/resolvers/user_query/userLanguage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module.exports = async (parent, args) => {
if (!user) {
throw new NotFoundError(
process.env.NODE_ENV !== 'production'
? 'User not found'
: requestContext.translate('user.notFound'),
? 'User not found'
: requestContext.translate('user.notFound'),
'user.notFound',
'user'
);
Expand Down
272 changes: 272 additions & 0 deletions tests/resolvers/event_mutations/createEvent.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
const createEvent = require('../../../lib/resolvers/event_mutations/createEvent');
const createOrganization = require('../../../lib/resolvers/organization_mutations/createOrganization');
const signup = require('../../../lib/resolvers/auth_mutations/signup');
const database = require('../../../db');
const shortid = require('shortid');
const mongoose = require('mongoose');

beforeAll(async () => {
require('dotenv').config(); // pull env variables from .env file
await database.connect();
});

afterAll(() => {
database.disconnect();
});

describe('Unit testing', () => {
test('Create Event Mutation without user', async () => {
// SignUp the User
let nameForNewUser = shortid.generate().toLowerCase();
let email = `${nameForNewUser}@test.com`;
let args = {
data: {
firstName: nameForNewUser,
lastName: nameForNewUser,
email: email,
password: 'password',
},
};
const signUpResponse = await signup({}, args);

const name = shortid.generate().toLowerCase();
const isPublic_boolean = Math.random() < 0.5;
const visibleInSearch_boolean = Math.random() < 0.5;

args = {
data: {
name: name,
description: name,
isPublic: isPublic_boolean,
visibleInSearch: visibleInSearch_boolean,
apiUrl: name,
},
};

let context = {
userId: signUpResponse.user._id.toString(),
};

const createOrgResponse = await createOrganization({}, args, context);

args = {
data: {
organizationId: createOrgResponse._id,
title: 'Talawa Conference Test',
description: 'National conference that happens yearly',
isPublic: true,
isRegisterable: true,
recurring: true,
recurrance: 'YEARLY',
location: 'Test',
startDate: '2/2/2020',
endDate: '2/2/2022',
allDay: true,
endTime: '2:00 PM',
startTime: '1:00 PM',
},
};

// Removed User from Context
context = {
userId: mongoose.Types.ObjectId(),
};

await expect(async () => {
await createEvent({}, args, context);
}).rejects.toEqual(Error('User not found'));
});

test('Create Event Mutation without Organization', async () => {
// SignUp the User
let nameForNewUser = shortid.generate().toLowerCase();
let email = `${nameForNewUser}@test.com`;
let args = {
data: {
firstName: nameForNewUser,
lastName: nameForNewUser,
email: email,
password: 'password',
},
};
const signUpResponse = await signup({}, args);

// Organization not created and random ID present in the args
args = {
data: {
organizationId: mongoose.Types.ObjectId(),
title: 'Talawa Conference Test',
description: 'National conference that happens yearly',
isPublic: true,
isRegisterable: true,
recurring: true,
recurrance: 'YEARLY',
location: 'Test',
startDate: '2/2/2020',
endDate: '2/2/2022',
allDay: true,
endTime: '2:00 PM',
startTime: '1:00 PM',
},
};

let context = {
userId: signUpResponse.user._id.toString(),
};

await expect(async () => {
await createEvent({}, args, context);
}).rejects.toEqual(Error('Organization not found'));
});

test('Create Event Mutation with the user not present in the Organization', async () => {
// SignUp the User
let nameForNewUser = shortid.generate().toLowerCase();
let email = `${nameForNewUser}@test.com`;
let args = {
data: {
firstName: nameForNewUser,
lastName: nameForNewUser,
email: email,
password: 'password',
},
};
let signUpResponse = await signup({}, args);

const name = shortid.generate().toLowerCase();
const isPublic_boolean = Math.random() < 0.5;
const visibleInSearch_boolean = Math.random() < 0.5;

args = {
data: {
name: name,
description: name,
isPublic: isPublic_boolean,
visibleInSearch: visibleInSearch_boolean,
apiUrl: name,
},
};

let context = {
userId: signUpResponse.user._id.toString(),
};

const createOrgResponse = await createOrganization({}, args, context);

// SignUp a new User
nameForNewUser = shortid.generate().toLowerCase();
email = `${nameForNewUser}@test.com`;
args = {
data: {
firstName: nameForNewUser,
lastName: nameForNewUser,
email: email,
password: 'password',
},
};
signUpResponse = await signup({}, args);

args = {
data: {
organizationId: createOrgResponse._id,
title: 'Talawa Conference Test',
description: 'National conference that happens yearly',
isPublic: true,
isRegisterable: true,
recurring: true,
recurrance: 'YEARLY',
location: 'Test',
startDate: '2/2/2020',
endDate: '2/2/2022',
allDay: true,
endTime: '2:00 PM',
startTime: '1:00 PM',
},
};

context = {
userId: signUpResponse.user._id.toString(),
};

await expect(async () => {
await createEvent({}, args, context);
}).rejects.toEqual(Error('Organization not Authorized'));
});

test('Create Event Mutation', async () => {
// SignUp the User
let nameForNewUser = shortid.generate().toLowerCase();
let email = `${nameForNewUser}@test.com`;
let args = {
data: {
firstName: nameForNewUser,
lastName: nameForNewUser,
email: email,
password: 'password',
},
};
const signUpResponse = await signup({}, args);

const name = shortid.generate().toLowerCase();
const isPublic_boolean = Math.random() < 0.5;
const visibleInSearch_boolean = Math.random() < 0.5;

args = {
data: {
name: name,
description: name,
isPublic: isPublic_boolean,
visibleInSearch: visibleInSearch_boolean,
apiUrl: name,
},
};

const context = {
userId: signUpResponse.user._id.toString(),
};

const createOrgResponse = await createOrganization({}, args, context);

const event_isPublic_boolean = Math.random() < 0.5;
const event_isRegisterable_boolean = Math.random() < 0.5;
const event_recurring_boolean = Math.random() < 0.5;
const event_allDay_boolean = Math.random() < 0.5;

args = {
data: {
organizationId: createOrgResponse._id,
title: 'Talawa Conference Test',
description: 'National conference that happens yearly',
isPublic: event_isPublic_boolean,
isRegisterable: event_isRegisterable_boolean,
recurring: event_recurring_boolean,
recurrance: 'YEARLY',
location: 'Test',
startDate: '2/2/2020',
endDate: '2/2/2022',
allDay: event_allDay_boolean,
endTime: '2:00 PM',
startTime: '1:00 PM',
},
};

const response = await createEvent({}, args, context);

expect(response.status).toEqual('ACTIVE');
expect(response.title).toEqual('Talawa Conference Test');
expect(response.description).toEqual(
'National conference that happens yearly'
);
expect(response.isPublic).toEqual(event_isPublic_boolean);
expect(response.isRegisterable).toEqual(event_isRegisterable_boolean);
expect(response.recurring).toEqual(event_recurring_boolean);
expect(response.recurrance).toEqual('YEARLY');
expect(response.location).toEqual('Test');
expect(response.startDate).toEqual('2/2/2020');
expect(response.allDay).toEqual(event_allDay_boolean);
expect(response.startTime).toEqual('1:00 PM');
expect(response.endTime).toEqual('2:00 PM');
expect(response.creator).toEqual(signUpResponse.user._id);
expect(response.organization).toEqual(createOrgResponse._id);
});
});
Loading