Best JavaScript code snippet using mountebank
delivery.test.js
Source:delivery.test.js
1import request from 'supertest';2import path from 'path';3import app from '../../src/app';4import factory from '../factories';5import truncate from '../util/truncate';6import getToken from '../getToken';7describe('Delivery', () => {8 beforeEach(async () => {9 await truncate();10 });11 it('should be able to list deliveries', async () => {12 const { body } = await getToken();13 const { status } = await request(app)14 .get('/deliveries')15 .set('Authorization', `Bearer ${body.token}`);16 expect(status).toBe(200);17 });18 it('should be able to register a new delivery', async () => {19 const delivery = await factory.attrs('Delivery');20 const delivery_man = await factory.attrs('DeliveryMan');21 const recipient = await factory.attrs('Recipient');22 const { body } = await getToken();23 const responseFile = await request(app)24 .post('/files')25 .attach('file', path.join(__dirname, 'img/avatar.jpg'))26 .set('Authorization', `Bearer ${body.token}`);27 const responseDeliveryMan = await request(app)28 .post('/deliverymen')29 .send({30 name: delivery_man.name,31 email: delivery_man.email,32 avatar_id: responseFile.body.id,33 })34 .set('Authorization', `Bearer ${body.token}`);35 const responseRecipient = await request(app)36 .post('/recipients')37 .send(recipient)38 .set('Authorization', `Bearer ${body.token}`);39 const response = await request(app)40 .post('/deliveries')41 .send({42 recipient_id: responseRecipient.body.id,43 deliveryman_id: responseDeliveryMan.body.id,44 signature_id: responseFile.body.id,45 product: delivery.product,46 })47 .set('Authorization', `Bearer ${body.token}`);48 expect(response.body).toHaveProperty('id');49 });50 it('should not be able to register a new delivery when recipient is not found', async () => {51 const delivery = await factory.attrs('Delivery');52 const delivery_man = await factory.attrs('DeliveryMan');53 const { body } = await getToken();54 const responseFile = await request(app)55 .post('/files')56 .attach('file', path.join(__dirname, 'img/avatar.jpg'))57 .set('Authorization', `Bearer ${body.token}`);58 const responseDeliveryMan = await request(app)59 .post('/deliverymen')60 .send({61 name: delivery_man.name,62 email: delivery_man.email,63 avatar_id: responseFile.body.id,64 })65 .set('Authorization', `Bearer ${body.token}`);66 const { status } = await request(app)67 .post('/deliveries')68 .send({69 recipient_id: 0,70 deliveryman_id: responseDeliveryMan.body.id,71 signature_id: responseFile.body.id,72 product: delivery.product,73 })74 .set('Authorization', `Bearer ${body.token}`);75 expect(status).toBe(400);76 });77 it('should not be able to register a new delivery when deliveryman is not found', async () => {78 const delivery = await factory.attrs('Delivery');79 const recipient = await factory.attrs('Recipient');80 const { body } = await getToken();81 const responseFile = await request(app)82 .post('/files')83 .attach('file', path.join(__dirname, 'img/avatar.jpg'))84 .set('Authorization', `Bearer ${body.token}`);85 const responseRecipient = await request(app)86 .post('/recipients')87 .send(recipient)88 .set('Authorization', `Bearer ${body.token}`);89 const { status } = await request(app)90 .post('/deliveries')91 .send({92 recipient_id: responseRecipient.body.id,93 deliveryman_id: 0,94 signature_id: responseFile.body.id,95 product: delivery.product,96 })97 .set('Authorization', `Bearer ${body.token}`);98 expect(status).toBe(400);99 });100 it('should not be able to register a new delivery when signature is not found', async () => {101 const delivery = await factory.attrs('Delivery');102 const delivery_man = await factory.attrs('DeliveryMan');103 const recipient = await factory.attrs('Recipient');104 const { body } = await getToken();105 const responseFile = await request(app)106 .post('/files')107 .attach('file', path.join(__dirname, 'img/avatar.jpg'))108 .set('Authorization', `Bearer ${body.token}`);109 const responseDeliveryMan = await request(app)110 .post('/deliverymen')111 .send({112 name: delivery_man.name,113 email: delivery_man.email,114 avatar_id: responseFile.body.id,115 })116 .set('Authorization', `Bearer ${body.token}`);117 const responseRecipient = await request(app)118 .post('/recipients')119 .send(recipient)120 .set('Authorization', `Bearer ${body.token}`);121 const { status } = await request(app)122 .post('/deliveries')123 .send({124 recipient_id: responseRecipient.body.id,125 deliveryman_id: responseDeliveryMan.body.id,126 signature_id: 0,127 product: delivery.product,128 })129 .set('Authorization', `Bearer ${body.token}`);130 expect(status).toBe(400);131 });132 it('should not be able to register a new delivery when some field is missing', async () => {133 const delivery_man = await factory.attrs('DeliveryMan');134 const recipient = await factory.attrs('Recipient');135 const { body } = await getToken();136 const responseFile = await request(app)137 .post('/files')138 .attach('file', path.join(__dirname, 'img/avatar.jpg'))139 .set('Authorization', `Bearer ${body.token}`);140 const responseDeliveryMan = await request(app)141 .post('/deliverymen')142 .send({143 name: delivery_man.name,144 email: delivery_man.email,145 avatar_id: responseFile.body.id,146 })147 .set('Authorization', `Bearer ${body.token}`);148 const responseRecipient = await request(app)149 .post('/recipients')150 .send(recipient)151 .set('Authorization', `Bearer ${body.token}`);152 const { status } = await request(app)153 .post('/deliveries')154 .send({155 recipient_id: responseRecipient.body.id,156 deliveryman_id: responseDeliveryMan.body.id,157 signature_id: responseFile.body.id,158 })159 .set('Authorization', `Bearer ${body.token}`);160 expect(status).toBe(400);161 });162 it('should not be able to register delivery with duplicated fields', async () => {163 const delivery = await factory.attrs('Delivery');164 const delivery_man = await factory.attrs('DeliveryMan');165 const recipient = await factory.attrs('Recipient');166 const { body } = await getToken();167 const responseFile = await request(app)168 .post('/files')169 .attach('file', path.join(__dirname, 'img/avatar.jpg'))170 .set('Authorization', `Bearer ${body.token}`);171 const responseDeliveryMan = await request(app)172 .post('/deliverymen')173 .send({174 name: delivery_man.name,175 email: delivery_man.email,176 avatar_id: responseFile.body.id,177 })178 .set('Authorization', `Bearer ${body.token}`);179 const responseRecipient = await request(app)180 .post('/recipients')181 .send(recipient)182 .set('Authorization', `Bearer ${body.token}`);183 await request(app)184 .post('/deliveries')185 .send({186 recipient_id: responseRecipient.body.id,187 deliveryman_id: responseDeliveryMan.body.id,188 signature_id: responseFile.body.id,189 product: delivery.product,190 })191 .set('Authorization', `Bearer ${body.token}`);192 const { status } = await request(app)193 .post('/deliveries')194 .send({195 recipient_id: responseRecipient.body.id,196 deliveryman_id: responseDeliveryMan.body.id,197 signature_id: responseFile.body.id,198 product: delivery.product,199 })200 .set('Authorization', `Bearer ${body.token}`);201 expect(status).toBe(401);202 });203 it('should be able to update delivery', async () => {204 const delivery = await factory.attrs('Delivery');205 const delivery_man = await factory.attrs('DeliveryMan');206 const recipient = await factory.attrs('Recipient');207 const { body } = await getToken();208 const responseFile = await request(app)209 .post('/files')210 .attach('file', path.join(__dirname, 'img/avatar.jpg'))211 .set('Authorization', `Bearer ${body.token}`);212 const responseDeliveryMan = await request(app)213 .post('/deliverymen')214 .send({215 name: delivery_man.name,216 email: delivery_man.email,217 avatar_id: responseFile.body.id,218 })219 .set('Authorization', `Bearer ${body.token}`);220 const responseRecipient = await request(app)221 .post('/recipients')222 .send(recipient)223 .set('Authorization', `Bearer ${body.token}`);224 const response = await request(app)225 .post('/deliveries')226 .send({227 recipient_id: responseRecipient.body.id,228 deliveryman_id: responseDeliveryMan.body.id,229 signature_id: responseFile.body.id,230 product: delivery.product,231 })232 .set('Authorization', `Bearer ${body.token}`);233 const updatedDelivery = await request(app)234 .put(`/deliveries/${response.body.id}`)235 .send({236 product: 'Updated Name',237 })238 .set('Authorization', `Bearer ${body.token}`);239 expect(updatedDelivery.body.product).toBe('Updated Name');240 });241 it('should not be able to update delivery with the same product as another delivery', async () => {242 const delivery_man = await factory.attrs('DeliveryMan');243 const recipient = await factory.attrs('Recipient');244 const { body } = await getToken();245 const responseFile = await request(app)246 .post('/files')247 .attach('file', path.join(__dirname, 'img/avatar.jpg'))248 .set('Authorization', `Bearer ${body.token}`);249 const responseDeliveryMan = await request(app)250 .post('/deliverymen')251 .send({252 name: delivery_man.name,253 email: delivery_man.email,254 avatar_id: responseFile.body.id,255 })256 .set('Authorization', `Bearer ${body.token}`);257 const responseRecipient = await request(app)258 .post('/recipients')259 .send(recipient)260 .set('Authorization', `Bearer ${body.token}`);261 await request(app)262 .post('/deliveries')263 .send({264 recipient_id: responseRecipient.body.id,265 deliveryman_id: responseDeliveryMan.body.id,266 signature_id: responseFile.body.id,267 product: 'product_1',268 })269 .set('Authorization', `Bearer ${body.token}`);270 const response = await request(app)271 .post('/deliveries')272 .send({273 recipient_id: responseRecipient.body.id,274 deliveryman_id: responseDeliveryMan.body.id,275 signature_id: responseFile.body.id,276 product: 'product_2',277 })278 .set('Authorization', `Bearer ${body.token}`);279 const updatedDelivery = await request(app)280 .put(`/deliveries/${response.body.id}`)281 .send({282 product: 'product_1',283 })284 .set('Authorization', `Bearer ${body.token}`);285 expect(updatedDelivery.status).toBe(401);286 });287 it('should not be able to update delivery if validation fails', async () => {288 const delivery = await factory.attrs('Delivery');289 const delivery_man = await factory.attrs('DeliveryMan');290 const recipient = await factory.attrs('Recipient');291 const { body } = await getToken();292 const responseFile = await request(app)293 .post('/files')294 .attach('file', path.join(__dirname, 'img/avatar.jpg'))295 .set('Authorization', `Bearer ${body.token}`);296 const responseDeliveryMan = await request(app)297 .post('/deliverymen')298 .send({299 name: delivery_man.name,300 email: delivery_man.email,301 avatar_id: responseFile.body.id,302 })303 .set('Authorization', `Bearer ${body.token}`);304 const responseRecipient = await request(app)305 .post('/recipients')306 .send(recipient)307 .set('Authorization', `Bearer ${body.token}`);308 const response = await request(app)309 .post('/deliveries')310 .send({311 recipient_id: responseRecipient.body.id,312 deliveryman_id: responseDeliveryMan.body.id,313 signature_id: responseFile.body.id,314 product: delivery.product,315 })316 .set('Authorization', `Bearer ${body.token}`);317 const updatedDelivery = await request(app)318 .put(`/deliveries/${response.body.id}`)319 .send({})320 .set('Authorization', `Bearer ${body.token}`);321 expect(updatedDelivery.status).toBe(400);322 });323 it('should not be able to update delivery if does not exist', async () => {324 const delivery = await factory.attrs('Delivery');325 const { body } = await getToken();326 const updatedDelivery = await request(app)327 .put('/deliveries/999')328 .send({329 product: delivery.product,330 })331 .set('Authorization', `Bearer ${body.token}`);332 expect(updatedDelivery.status).toBe(400);333 });334 it('should be able to delete a delivery', async () => {335 const delivery = await factory.attrs('Delivery');336 const delivery_man = await factory.attrs('DeliveryMan');337 const recipient = await factory.attrs('Recipient');338 const { body } = await getToken();339 const responseFile = await request(app)340 .post('/files')341 .attach('file', path.join(__dirname, 'img/avatar.jpg'))342 .set('Authorization', `Bearer ${body.token}`);343 const responseDeliveryMan = await request(app)344 .post('/deliverymen')345 .send({346 name: delivery_man.name,347 email: delivery_man.email,348 avatar_id: responseFile.body.id,349 })350 .set('Authorization', `Bearer ${body.token}`);351 const responseRecipient = await request(app)352 .post('/recipients')353 .send(recipient)354 .set('Authorization', `Bearer ${body.token}`);355 const response = await request(app)356 .post('/deliveries')357 .send({358 recipient_id: responseRecipient.body.id,359 deliveryman_id: responseDeliveryMan.body.id,360 signature_id: responseFile.body.id,361 product: delivery.product,362 })363 .set('Authorization', `Bearer ${body.token}`);364 const { status } = await request(app)365 .delete(`/deliveries/${response.body.id}`)366 .set('Authorization', `Bearer ${body.token}`);367 expect(status).toBe(204);368 });369 it('should not be able to delete a delivery if does not exist', async () => {370 const { body } = await getToken();371 const { status } = await request(app)372 .delete('/deliveries/0')373 .set('Authorization', `Bearer ${body.token}`);374 expect(status).toBe(400);375 });...
delivery_checkin.test.js
Source:delivery_checkin.test.js
1import request from 'supertest';2import path from 'path';3import app from '../../src/app';4import factory from '../factories';5import truncate from '../util/truncate';6import getToken from '../getToken';7describe('DeliveryCheckIn', () => {8 beforeEach(async () => {9 await truncate();10 });11 it('should be able to update the start date of delivery', async () => {12 const delivery = await factory.attrs('Delivery');13 const delivery_man = await factory.attrs('DeliveryMan');14 const recipient = await factory.attrs('Recipient');15 const { body } = await getToken();16 const responseFile = await request(app)17 .post('/files')18 .attach('file', path.join(__dirname, 'img/avatar.jpg'))19 .set('Authorization', `Bearer ${body.token}`);20 const responseDeliveryMan = await request(app)21 .post('/deliverymen')22 .send({23 name: delivery_man.name,24 email: delivery_man.email,25 avatar_id: responseFile.body.id,26 })27 .set('Authorization', `Bearer ${body.token}`);28 const responseRecipient = await request(app)29 .post('/recipients')30 .send(recipient)31 .set('Authorization', `Bearer ${body.token}`);32 const responseDelivery = await request(app)33 .post('/deliveries')34 .send({35 recipient_id: responseRecipient.body.id,36 deliveryman_id: responseDeliveryMan.body.id,37 signature_id: responseFile.body.id,38 product: delivery.product,39 })40 .set('Authorization', `Bearer ${body.token}`);41 const updatedDeliveryStatus = await request(app)42 .put(`/deliveries/checkin/${responseDelivery.body.id}`)43 .send({44 deliveryman_id: responseDeliveryMan.body.id,45 });46 expect(updatedDeliveryStatus.body).toHaveProperty('id');47 });48 it('should not be able to update delivery if validation fails', async () => {49 const delivery = await factory.attrs('Delivery');50 const delivery_man = await factory.attrs('DeliveryMan');51 const recipient = await factory.attrs('Recipient');52 const { body } = await getToken();53 const responseFile = await request(app)54 .post('/files')55 .attach('file', path.join(__dirname, 'img/avatar.jpg'))56 .set('Authorization', `Bearer ${body.token}`);57 const responseDeliveryMan = await request(app)58 .post('/deliverymen')59 .send({60 name: delivery_man.name,61 email: delivery_man.email,62 avatar_id: responseFile.body.id,63 })64 .set('Authorization', `Bearer ${body.token}`);65 const responseRecipient = await request(app)66 .post('/recipients')67 .send(recipient)68 .set('Authorization', `Bearer ${body.token}`);69 const responseDelivery = await request(app)70 .post('/deliveries')71 .send({72 recipient_id: responseRecipient.body.id,73 deliveryman_id: responseDeliveryMan.body.id,74 signature_id: responseFile.body.id,75 product: delivery.product,76 })77 .set('Authorization', `Bearer ${body.token}`);78 const updatedDeliveryStatus = await request(app)79 .put(`/deliveries/checkin/${responseDelivery.body.id}`)80 .send({});81 expect(updatedDeliveryStatus.status).toBe(400);82 });83 it('should not be able to update if delivery man not found', async () => {84 const delivery = await factory.attrs('Delivery');85 const delivery_man = await factory.attrs('DeliveryMan');86 const recipient = await factory.attrs('Recipient');87 const { body } = await getToken();88 const responseFile = await request(app)89 .post('/files')90 .attach('file', path.join(__dirname, 'img/avatar.jpg'))91 .set('Authorization', `Bearer ${body.token}`);92 const responseDeliveryMan = await request(app)93 .post('/deliverymen')94 .send({95 name: delivery_man.name,96 email: delivery_man.email,97 avatar_id: responseFile.body.id,98 })99 .set('Authorization', `Bearer ${body.token}`);100 const responseRecipient = await request(app)101 .post('/recipients')102 .send(recipient)103 .set('Authorization', `Bearer ${body.token}`);104 const responseDelivery = await request(app)105 .post('/deliveries')106 .send({107 recipient_id: responseRecipient.body.id,108 deliveryman_id: responseDeliveryMan.body.id,109 signature_id: responseFile.body.id,110 product: delivery.product,111 })112 .set('Authorization', `Bearer ${body.token}`);113 const updatedDeliveryStatus = await request(app)114 .put(`/deliveries/checkin/${responseDelivery.body.id}`)115 .send({116 deliveryman_id: 0,117 });118 expect(updatedDeliveryStatus.status).toBe(400);119 });120 it('should not be able to update if delivery not found', async () => {121 const delivery_man = await factory.attrs('DeliveryMan');122 const { body } = await getToken();123 const responseFile = await request(app)124 .post('/files')125 .attach('file', path.join(__dirname, 'img/avatar.jpg'))126 .set('Authorization', `Bearer ${body.token}`);127 const responseDeliveryMan = await request(app)128 .post('/deliverymen')129 .send({130 name: delivery_man.name,131 email: delivery_man.email,132 avatar_id: responseFile.body.id,133 })134 .set('Authorization', `Bearer ${body.token}`);135 const updatedDeliveryStatus = await request(app)136 .put('/deliveries/checkin/0')137 .send({138 deliveryman_id: responseDeliveryMan.body.id,139 });140 expect(updatedDeliveryStatus.status).toBe(400);141 });142 it('should not be able to update if delivery not registered to delivery man', async () => {143 const delivery = await factory.attrs('Delivery');144 const deliveryman_1 = await factory.attrs('DeliveryMan');145 const deliveryman_2 = await factory.attrs('DeliveryMan');146 const recipient = await factory.attrs('Recipient');147 const { body } = await getToken();148 const responseFile = await request(app)149 .post('/files')150 .attach('file', path.join(__dirname, 'img/avatar.jpg'))151 .set('Authorization', `Bearer ${body.token}`);152 const responseDeliveryMan_1 = await request(app)153 .post('/deliverymen')154 .send({155 name: deliveryman_1.name,156 email: deliveryman_1.email,157 avatar_id: responseFile.body.id,158 })159 .set('Authorization', `Bearer ${body.token}`);160 const responseDeliveryMan_2 = await request(app)161 .post('/deliverymen')162 .send({163 name: deliveryman_2.name,164 email: 'deliveryman_2@email.com',165 avatar_id: responseFile.body.id,166 })167 .set('Authorization', `Bearer ${body.token}`);168 const responseRecipient = await request(app)169 .post('/recipients')170 .send(recipient)171 .set('Authorization', `Bearer ${body.token}`);172 const responseDelivery = await request(app)173 .post('/deliveries')174 .send({175 recipient_id: responseRecipient.body.id,176 deliveryman_id: responseDeliveryMan_1.body.id,177 signature_id: responseFile.body.id,178 product: delivery.product,179 })180 .set('Authorization', `Bearer ${body.token}`);181 const updatedDeliveryStatus = await request(app)182 .put(`/deliveries/checkin/${responseDelivery.body.id}`)183 .send({184 deliveryman_id: responseDeliveryMan_2.body.id,185 });186 expect(updatedDeliveryStatus.status).toBe(400);187 });188 it('should not be able to update if delivery man has reached 5 pickups in one day', async () => {189 const delivery = await factory.attrs('Delivery');190 const delivery_man = await factory.attrs('DeliveryMan');191 const recipient = await factory.attrs('Recipient');192 const { body } = await getToken();193 const responseFile = await request(app)194 .post('/files')195 .attach('file', path.join(__dirname, 'img/avatar.jpg'))196 .set('Authorization', `Bearer ${body.token}`);197 const responseDeliveryMan = await request(app)198 .post('/deliverymen')199 .send({200 name: delivery_man.name,201 email: delivery_man.email,202 avatar_id: responseFile.body.id,203 })204 .set('Authorization', `Bearer ${body.token}`);205 const responseRecipient = await request(app)206 .post('/recipients')207 .send(recipient)208 .set('Authorization', `Bearer ${body.token}`);209 const responseDelivery = await request(app)210 .post('/deliveries')211 .send({212 recipient_id: responseRecipient.body.id,213 deliveryman_id: responseDeliveryMan.body.id,214 signature_id: responseFile.body.id,215 product: delivery.product,216 })217 .set('Authorization', `Bearer ${body.token}`);218 const todayDate = new Date();219 await factory.createMany('Delivery', 5, {220 deliveryman_id: responseDeliveryMan.body.id,221 start_date: todayDate,222 });223 const updatedDeliveryStatus = await request(app)224 .put(`/deliveries/checkin/${responseDelivery.body.id}`)225 .send({226 deliveryman_id: responseDeliveryMan.body.id,227 });228 expect(updatedDeliveryStatus.status).toBe(401);229 });...
auth.js
Source:auth.js
1// let User = require('../../models/user');2const auth = require('../../lib/auth');3const bcrypt = require("bcryptjs");4module.exports.auth = function (responseFile, collection) {5 const {User} = collection;6 return {7 signUp: (request, response) => {8 let password = request.body.password;9 let hash = bcrypt.hashSync(password);10 request.body.password = hash;11 User.insertMany(request.body).then((result) => {12 if (result) {13 let output = {14 error: false,15 code: responseFile[4000]['code'],16 msg: responseFile[4000]['msg']17 }18 response.json(output);19 }20 }).catch((error) => {21 let output = {22 error: true,23 msg: responseFile[1000]['msg'],24 code: responseFile[1000]['code'],25 }26 response.status(500).send(output);27 })28 },29 logIn: (request, response) => {30 let email = request.body.email;31 let password = request.body.password;32 User.findOne({ email: email }).then((userDetails) => {33 if (!userDetails) {34 let output = {35 error: false,36 msg: responseFile[4002]['msg'],37 code: responseFile[4002]['code']38 }39 response.status(200).send(output);40 } else {41 bcrypt.compare(password, userDetails.password, async (error, result) => {42 if (error) {43 let output = {44 error: true,45 msg: responseFile[4005]['msg'],46 code: responseFile[4005]['code']47 }48 response.status(500).send(output);49 } else if (!result) {50 let output = {51 error: false,52 msg: responseFile[4007]['msg'],53 code: responseFile[4007]['code']54 }55 response.status(200).send(output);56 } else {57 let payload = {58 id: userDetails._id,59 email: userDetails.email,60 name: userDetails.name,61 mobile: userDetails.mobile62 }63 let token = await auth.generateAuthToken(payload);64 let output = {65 error: false,66 msg: responseFile[4001]['msg'],67 code: responseFile[4001]['code'],68 token: token,69 data: payload70 }71 response.status(200).send(output);72 }73 });74 }75 }).catch((error) => {76 let output = {77 error: true,78 msg: responseFile[1000]['msg'],79 code: responseFile[1000]['code'],80 }81 response.status(500).send(output);82 })83 },84 logOut: (request, response) => {85 console.log("Authentication Done");86 },87 sendPhoneCode: (request, response) => {88 User.findById(request.params.id).then(async (userDetails) => {89 let { mobile } = userDetails;90 if (userDetails.is_phone_verified) {91 console.log("phone already verified");92 }93 let OTP = util.generateOTP("phone");94 let paramForMsg = util.prepareOTPParam("phone", OTP);95 let otpDateTime = new Date();96 await util.putOTPIntoCollection(mobile, OTP, otpDateTime, "phone");97 smsService.sendMsg(paramForMsg, "+919897821299", function (err, done) {98 if (err) {99 let output = {100 error: true,101 msg: responseFile[4010]['msg'],102 code: responseFile[4010]['code']103 }104 response.status(200).send(output);105 } else {106 let output = {107 error: false,108 msg: responseFile[4009]['msg'],109 code: responseFile[4009]['code']110 }111 response.status(200).send(output);112 }113 })114 }).catch((error) => {115 let output = {116 error: true,117 msg: responseFile[1000]['msg'],118 code: responseFile[1000]['code'],119 }120 response.status(500).send(output);121 })122 },123 sendEmailCode: (request, response) => {124 User.findById(request.params.id).then(async (userDetails) => {125 let { email } = userDetails;126 if (userDetails.is_email_verified) {127 let output = {128 error: true,129 msg: responseFile[4011]['msg'],130 code: responseFile[4011]['code'],131 }132 response.status(200).send(output);133 }134 let OTP = util.generateOTP("email");135 let paramForMsg = util.prepareOTPParam("email", OTP);136 let otpDateTime = new Date();137 await util.putOTPIntoCollection(email, OTP, otpDateTime, "email");138 emailService.sendEmail(email, "Verification", paramForMsg, function (output) {139 if (!output.error) {140 response.status(200).send(output);141 } else {142 response.status(400).send(output);143 }144 })145 }).catch((error) => {146 let output = {147 error: true,148 msg: responseFile[1000]['msg'],149 code: responseFile[1000]['code'],150 }151 response.status(500).send(output);152 })153 },154 verifyEmailCode: async function (request, response) {155 let id = request.body.email;156 let code = request.body.code;157 try {158 let otpData = await util.getUserOTP(id, "email");159 let OTP = otpData[0].email_otp;160 if (OTP == code) {161 let output = {162 error: false,163 msg: responseFile[4016]['msg'],164 code: responseFile[4016]['code'],165 }166 response.status(200).send(output);167 } else {168 let output = {169 error: false,170 msg: responseFile[4018]['msg'],171 code: responseFile[4018]['code'],172 }173 response.status(200).send(output);174 }175 } catch (error) {176 utils.sendResponse(response, true, 500, 1000, "dunzen ");177 }178 }179 }...
Using AI Code Generation
1var mb = require('mountebank');2var imposter = {3 {4 {5 is: {6 headers: {7 },8 }9 }10 }11};12mb.create(imposter, function (error, imposter) {13 if (error) {14 console.error(error);15 } else {16 console.log('Imposter %d created', imposter.port);17 }18});19var mb = require('mountebank');20var imposter = {21 {22 {23 is: {24 headers: {25 },26 },27 _behaviors: {28 }29 }30 }31};32mb.create(imposter, function (error, imposter) {33 if (error) {34 console.error(error);35 } else {36 console.log('Imposter %d created', imposter.port);37 }38});39var mb = require('mountebank');40var imposter = {41 {42 {43 is: {44 headers: {45 },46 },47 _behaviors: {48 }49 },50 {51 is: {52 headers: {53 },54 }55 }56 }57};58mb.create(imposter, function (error, imposter) {59 if (error) {60 console.error(error);
Using AI Code Generation
1var mb = require('mountebank');2var fs = require('fs');3var imposter = {4 {5 {6 is: {7 }8 }9 }10};11mb.create(imposter).then(function (server) {12 console.log('Server running on port ' + server.port);13 fs.writeFileSync('imposter.json', JSON.stringify(server.toJSON()));14});15var mb = require('mountebank');16var fs = require('fs');17var imposter = {18 {19 {20 is: {21 }22 }23 }24};25mb.create(imposter).then(function (server) {26 console.log('Server running on port ' + server.port);27 fs.writeFileSync('imposter.json', JSON.stringify(server.toJSON()));28});29var mb = require('mountebank');30var fs = require('fs');31var imposter = {32 {33 {34 is: {35 }36 }37 }38};39mb.create(imposter).then(function (server) {40 console.log('Server running on port ' + server.port);41 fs.writeFileSync('imposter.json', JSON.stringify(server.toJSON()));42});43var mb = require('mountebank');44var fs = require('fs');45var imposter = {46 {47 {48 is: {49 }50 }51 }52};53mb.create(imposter).then(function (server) {54 console.log('Server running on port ' + server.port);55 fs.writeFileSync('imposter.json', JSON.stringify(server.toJSON()));56});
Using AI Code Generation
1var mb = require('mountebank');2var imposter = { protocol: 'http', port: 4545, stubs: [{ responses: [{ is: { body: 'hello world' } }] }] };3mb.create(imposter).then(function (server) {4 return server.get('/').then(function (response) {5 console.log(response.body);6 });7});
Using AI Code Generation
1var mb = require('mountebank');2var fs = require('fs');3var path = require('path');4var imposter = {5 {6 {7 is: {8 headers: {9 },10 body: fs.readFileSync(path.resolve(__dirname, 'test.html')).toString()11 }12 }13 }14};15mb.create({ port: 2525 }, imposter).then(function (imposter) {16 console.log('Imposter created');17});
Using AI Code Generation
1var mb = require('mountebank');2var response = {3 headers: {4 },5 body: JSON.stringify({6 })7};8mb.create({9}, function (error) {10 if (error) {11 console.error(error);12 } else {13 console.log('running on port 2525');14 mb.post('/test', {15 stubs: [{16 predicates: [{17 equals: {18 }19 }],20 responses: [{21 is: {22 }23 }]24 }]25 });26 }27});28var mb = require('mountebank');29var assert = require('assert');30var request = require('request');31describe('test', function () {32 it('should return 200', function (done) {33 mb.create({34 }, function (error) {35 if (error) {36 console.error(error);37 } else {38 console.log('running on port 2525');39 mb.post('/test', {40 stubs: [{41 predicates: [{42 equals: {43 }44 }],45 responses: [{46 is: {47 }48 }]49 }]50 });51 assert.ok(!error);52 assert.equal(response.statusCode, 200);53 assert.equal(body, 'Hello, world!');54 done();55 });56 }57 });58 });59});
Using AI Code Generation
1var responseFile = require('mountebank').responseFile;2var mb = require('mountebank')({3});4var mbHelper = require('mountebank-helper')(mb);5var chai = require('chai');6var expect = chai.expect;7var supertest = require('supertest');8var request = require('request');9var fs = require('fs');10var path = require('path');11var _ = require('lodash');12var async = require('async');13var rimraf = require('rimraf');14var mkdirp = require('mkdirp');15var mkdirp = require('mkdirp');
Using AI Code Generation
1var mb = require('mountebank');2var fs = require('fs');3var responseFile = fs.readFileSync("./response.json");4var server = mb.create({ port: 2525, allowInjection: true, debug: true });5server.then(function () {6 server.post('/imposters', {7 {8 {9 equals: {10 }11 }12 {13 is: {14 headers: { 'Content-Type': 'application/json' },15 }16 }17 }18 });19});20{21}22var mb = require('mountebank');23var fs = require('fs');24var responseFile = fs.readFileSync("./response.json");25var server = mb.create({ port: 2525, allowInjection: true, debug: true });26server.then(function () {27 server.post('/imposters', {28 {29 {30 equals: {31 }32 }33 {34 is: {35 headers: { 'Content-Type': 'application/json' },36 }37 }38 }39 });40});41{42}43var mb = require('mountebank');44var fs = require('fs');45var responseFile = fs.readFileSync("./response.json");46var server = mb.create({ port: 2525, allowInjection: true, debug: true });47server.then(function () {48 server.post('/imposters', {49 {50 {51 equals: {52 }53 }54 {
Using AI Code Generation
1var response = require('mountebank').responseFile;2var port = 2525;3var protocol = 'http';4var host = 'localhost';5var path = '/test';6var method = 'GET';7 {8 {9 equals: {10 }11 }12 {13 is: {14 body: response('test.json')15 }16 }17 }18];19var imposter = {20};21var mb = require('mountebank');22mb.create(imposter, function (error, result) {23 if (error) {24 console.log('Error creating imposter: ' + error.message);25 } else {26 }27});28{29}30{31}32{33}34var response = require('mountebank').response;35var delay = require('mountebank').delay;36var port = 2525;37var protocol = 'http';38var host = 'localhost';39var path = '/test';40var method = 'GET';41 {42 {43 equals: {
Using AI Code Generation
1var imposter = require('mountebank').create();2imposter.post('/test',function(req,res){3 res.responseFile('test.json');4});5imposter.listen(3000);6{7}8var imposter = require('mountebank').create();9imposter.post('/test',function(req,res){10 res.responseFile('test2.json');11});12imposter.listen(3000);13{14}15var imposter = require('mountebank').create();16imposter.post('/test',function(req,res){17 res.responseFile('test3.json');18});19imposter.listen(3000);20{21}22var imposter = require('mountebank').create();23imposter.post('/test',function(req,res){24 res.responseFile('test4.json');25});26imposter.listen(3000);27{28}29var imposter = require('mountebank').create();30imposter.post('/test',function(req,res){31 res.responseFile('test5.json');32});33imposter.listen(3000);34{35}36var imposter = require('mountebank').create();37imposter.post('/test',function(req,res){38 res.responseFile('test6.json');39});40imposter.listen(3000);41{42}43var imposter = require('mountebank').create();44imposter.post('/test',function(req,res){45 res.responseFile('test7.json');46});47imposter.listen(3000);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!