How to use jsonResponse method in mountebank

Best JavaScript code snippet using mountebank

directory_kafka.js

Source:directory_kafka.js Github

copy

Full Screen

1/**2 * Created by ManaliJain on 10/31/17.3 * This file handles all teh operations an a file or folder.. ie get all files/folder, create directory, delete4 * file/directory, share file/directory etc5 */6var mongo = require("./mongoConnector");7var mongodb = require('mongodb');8var mongoLogin = "mongodb://localhost:27017/Dropboxuser";9var bcrypt = require('bcryptjs');10var jwt = require('jsonwebtoken');11var uuid = require('uuid/v4');12var moment = require('moment');13function handleDirRequest(req, api, callback) {14 console.log('api is', api);15 switch (api) {16 case "getFiles" :17 getFiles(req, function (err, jsonResponse) {18 callback(null, jsonResponse);19 return;20 });21 return;22 case "createDirectory" :23 createDirectory(req, function (err, jsonResponse) {24 callback(null, jsonResponse);25 });26 return;27 case "deleteFile" :28 deleteFile(req, function (err, jsonResponse) {29 callback(null, jsonResponse);30 });31 return;32 case "deleteDirectory" :33 deleteDirectory(req, function (err, jsonResponse) {34 callback(null, jsonResponse);35 });36 return;37 case "shareFile" :38 shareFile(req, function (err, jsonResponse) {39 callback(null, jsonResponse);40 });41 return;42 case "shareDirectory" :43 shareDirectory(req, function (err, jsonResponse) {44 callback(null, jsonResponse);45 });46 return;47 case "shareLink" :48 shareLink(req, function (err, jsonResponse) {49 callback(null, jsonResponse);50 });51 return;52 case "deleteFileInDir" :53 deleteFileInDir(req, function (err, jsonResponse) {54 callback(null, jsonResponse);55 });56 return;57 case "starDirFile" :58 starDirFile(req, function (err, jsonResponse) {59 callback(null, jsonResponse);60 });61 return;62 }63}64exports.handleDirRequest = handleDirRequest;65function getFiles(req,callback) {66 let jsonResponse = {};67 let files = [];68 mongo.connect(mongoLogin, function (mongoConn) {69 console.log('Connected to mongo at: ' + mongoLogin);70 let collection = mongoConn.collection('files');71 collection.find({'user_uuid': req.user_uuid}).toArray(function(err, result) {72 console.log("file result is", result);73 if (err) {74 var msg = "Error Occured";75 jsonResponse = {76 "statusCode": 500,77 "result": "Error",78 "message": msg79 };80 // res.send(jsonResponse);81 callback(null, jsonResponse);82 } else if (result !== null) {83 var msg = "";84 jsonResponse = {85 "statusCode": 201,86 "result": "success",87 "files": result,88 "message": msg89 };90 // res.send(jsonResponse);91 callback(null, jsonResponse);92 } else {93 var msg = "";94 jsonResponse = {95 "statusCode": 201,96 "result": "success",97 "files": files,98 "message": msg99 };100 // res.send(jsonResponse);101 callback(null, jsonResponse);102 }103 });104 mongo.releaseConnection(mongoConn);105 });106}107function createDirectory(req,callback) {108 //assigning unique id to directory109 let uuidv4 = uuid();110 console.log(uuidv4);111 let dir_created_timestamp = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');112 let jsonResponse = {};113 console.log("user id in create directory is ", req.user_uuid);114 mongo.connect(mongoLogin, function (mongoConn) {115 console.log('Connected to mongo at: ' + mongoLogin);116 let collection = mongoConn.collection('files');117 let payload = {118 "user_uuid" : [req.user_uuid],119 "dir_name" : req.dir_name,120 "dir_uuid" : uuidv4,121 "dir_created": dir_created_timestamp,122 "star_id" : '0',123 "owner_uuid" : req.user_uuid,124 "filesArray" : []125 }126 collection.insert(payload, function (err, result) {127 // console.log("file result is", result);128 if (err) {129 var msg = "Error Occured. Create once again";130 jsonResponse = {131 "statusCode": 500,132 "result": "Error",133 "message": msg134 };135 // res.send(jsonResponse);136 callback(null, jsonResponse);137 } else {138 if (result!==null) {139 var msg = "Directory created";140 jsonResponse = {141 "statusCode": 201,142 "result": "Success",143 "data" : result[0],144 "message": msg145 };146 // res.send(jsonResponse);147 callback(null, jsonResponse);148 } else {149 var msg = "Error Occured";150 jsonResponse = {151 "statusCode": 400,152 "result": "Error",153 "message": msg154 };155 // res.send(jsonResponse);156 callback(null, jsonResponse);157 }158 }159 });160 mongo.releaseConnection(mongoConn);161 });162}163function deleteFile(req,callback){164 let jsonResponse = {};165 mongo.connect(mongoLogin, function (mongoConn) {166 console.log('Connected to mongo at: ' + mongoLogin);167 let collection = mongoConn.collection('files');168 let id = "ObjectId('"+req._id+"')";169 console.log("id is",id);170 collection.remove({ "_id" : new mongodb.ObjectID(req._id)}, function (err, result) {171 // console.log("file result is", result);172 if (err) {173 let msg = "Error Occured. delete once again";174 jsonResponse = {175 "statusCode": 500,176 "result": "Error",177 "message": msg178 };179 // res.send(jsonResponse);180 callback(null, jsonResponse);181 } else {182 if (result!==null) {183 let msg = "file deleted";184 jsonResponse = {185 "statusCode": 201,186 "result": "Success",187 "data" : result,188 "message": msg189 };190 console.log(jsonResponse)191 // res.send(jsonResponse);192 callback(null, jsonResponse);193 } else {194 let msg = "Error Occured";195 jsonResponse = {196 "statusCode": 400,197 "result": "Error",198 "message": msg199 };200 // res.send(jsonResponse);201 callback(null, jsonResponse);202 }203 }204 });205 mongo.releaseConnection(mongoConn);206 });207}208function deleteDirectory(req,callback){209 let jsonResponse = {};210 mongo.connect(mongoLogin, function (mongoConn) {211 console.log('Connected to mongo at: ' + mongoLogin);212 let collection = mongoConn.collection('files');213 let id = "ObjectId('"+req._id+"')";214 console.log("id is",id);215 collection.remove({ "_id" : new mongodb.ObjectID(req._id)}, function (err, result) {216 // console.log("file result is", result);217 if (err) {218 let msg = "Error Occured. delete once again";219 jsonResponse = {220 "statusCode": 500,221 "result": "Error",222 "message": msg223 };224 // res.send(jsonResponse);225 callback(null, jsonResponse);226 } else {227 if (result!==null) {228 let msg = "directory deleted";229 jsonResponse = {230 "statusCode": 201,231 "result": "Success",232 "data" : result,233 "message": msg234 };235 console.log(jsonResponse)236 // res.send(jsonResponse);237 callback(null, jsonResponse);238 } else {239 let msg = "Error Occured";240 jsonResponse = {241 "statusCode": 400,242 "result": "Error",243 "message": msg244 };245 // res.send(jsonResponse);246 callback(null, jsonResponse);247 }248 }249 });250 mongo.releaseConnection(mongoConn);251 });252}253function shareFile(req,callback){254 let jsonResponse = {};255 let file = req.file;256 mongo.connect(mongoLogin, function(mongoConn){257 console.log('Connected to mongo at: ' + mongoLogin);258 let collection = mongoConn.collection('user');259 let collectionFile = mongoConn.collection('files');260 collection.findOne({email: req.shareToEmail }, function(err, result){261 if(err){262 var msg = "Error Occured";263 jsonResponse = {264 "statusCode": 500,265 "result": "Error",266 "message": msg267 };268 // res.send(jsonResponse);269 callback(null, jsonResponse);270 }271 else if (result === null) {272 var msg = "User is not available in dropbox";273 jsonResponse = {274 "statusCode": 300,275 "result": "Error",276 "message": msg277 };278 // res.send(jsonResponse);279 callback(null, jsonResponse);280 } else if (result) {281 collectionFile.update({ "_id" : new mongodb.ObjectID(req._id)},282 {$push:{"user_uuid": result.user_uuid }}, function (err, result1) {283 console.log("result is", result1);284 if (err) {285 var msg = "Share file failed";286 jsonResponse = {287 "statusCode": 500,288 "result": "Error",289 "message": msg290 };291 // res.send(jsonResponse);292 callback(null, jsonResponse);293 } else {294 if(result1.result.nModified > 0){295 var msg = "Share file success";296 jsonResponse = {297 "statusCode": 201,298 "result": "Success",299 "message": msg300 };301 // res.send(jsonResponse);302 callback(null, jsonResponse);303 } else {304 var msg = "Error Occured";305 jsonResponse = {306 "statusCode": 500,307 "result": "Error",308 "message": msg309 };310 // res.send(jsonResponse);311 callback(null, jsonResponse);312 }313 }314 });315 }316 });317 mongo.releaseConnection(mongoConn);318 });319}320function shareDirectory(req,callback){321 let jsonResponse = {};322 let file = req.file;323 mongo.connect(mongoLogin, function(mongoConn){324 console.log('Connected to mongo at: ' + mongoLogin);325 let collection = mongoConn.collection('user');326 let collectionFile = mongoConn.collection('files');327 collection.findOne({email: req.shareToEmail }, function(err, result){328 if(err){329 var msg = "Error Occured";330 jsonResponse = {331 "statusCode": 500,332 "result": "Error",333 "message": msg334 };335 // res.send(jsonResponse);336 callback(null, jsonResponse);337 }338 else if (result === null) {339 var msg = "User is not available in dropbox";340 jsonResponse = {341 "statusCode": 300,342 "result": "Error",343 "message": msg344 };345 res.send(jsonResponse);346 } else if (result) {347 collectionFile.update({ "_id" : new mongodb.ObjectID(req._id)},348 {$push:{"user_uuid": result.user_uuid }}, function (err, result1) {349 console.log("result is", result1);350 if (err) {351 var msg = "Share folder failed";352 jsonResponse = {353 "statusCode": 500,354 "result": "Error",355 "message": msg356 };357 // res.send(jsonResponse);358 callback(null, jsonResponse);359 } else {360 if(result1.result.nModified > 0){361 var msg = "Share folder success";362 jsonResponse = {363 "statusCode": 201,364 "result": "Success",365 "message": msg366 };367 // res.send(jsonResponse);368 callback(null, jsonResponse);369 } else {370 var msg = "Error Occured";371 jsonResponse = {372 "statusCode": 500,373 "result": "Error",374 "message": msg375 };376 // res.send(jsonResponse);377 callback(null, jsonResponse);378 }379 }380 });381 }382 });383 mongo.releaseConnection(mongoConn);384 });385}386function shareLink(req,callback){387 let jsonResponse = {};388 let file = req.file;389 mongo.connect(mongoLogin, function(mongoConn){390 console.log('Connected to mongo at: ' + mongoLogin);391 let collection = mongoConn.collection('user');392 let collectionLink = mongoConn.collection('link');393 collection.findOne({email: req.shareToEmail }, function(err, result){394 console.log("share to email is",req.shareToEmail);395 console.log("result is",result);396 if(err){397 var msg = "Error Occured";398 jsonResponse = {399 "statusCode": 500,400 "result": "Error",401 "message": msg402 };403 // res.send(jsonResponse);404 callback(null, jsonResponse);405 }406 else if (result === null) {407 var msg = "User is not available in dropbox";408 jsonResponse = {409 "statusCode": 300,410 "result": "Error",411 "message": msg412 };413 // res.send(jsonResponse);414 callback(null, jsonResponse);415 } else if(result){416 let data = {417 "user_uuid":result.user_uuid,418 "link":req.link419 }420 collectionLink.insert(data , function(err,result1){421 if (err) {422 var msg = "Share link failed";423 jsonResponse = {424 "statusCode": 500,425 "result": "Error",426 "message": msg427 };428 // res.send(jsonResponse);429 callback(null, jsonResponse);430 } else {431 var msg = "Share file success";432 jsonResponse = {433 "statusCode": 201,434 "result": "Error",435 "message": msg436 };437 // res.send(jsonResponse);438 callback(null, jsonResponse);439 }440 });441 }442 });443 mongo.releaseConnection(mongoConn);444 });445}446function deleteFileInDir(req,callback){447 let jsonResponse = {};448 mongo.connect(mongoLogin, function (mongoConn) {449 console.log('Connected to mongo at: ' + mongoLogin);450 let collection = mongoConn.collection('files');451 collection.update({"_id" : new mongodb.ObjectID(req._id)},452 { $pull: { filesArray: {file_uuid : req.file_uuid }}}, function (err, result) {453 // console.log("file result is", result);454 if (err) {455 let msg = "Error Occured. delete once again";456 jsonResponse = {457 "statusCode": 500,458 "result": "Error",459 "message": msg460 };461 // res.send(jsonResponse);462 callback(null, jsonResponse);463 } else {464 if (result!==null) {465 let msg = "directory deleted";466 jsonResponse = {467 "statusCode": 201,468 "result": "Success",469 "data" : result,470 "message": msg471 };472 console.log(jsonResponse)473 // res.send(jsonResponse);474 callback(null, jsonResponse);475 } else {476 let msg = "Error Occured";477 jsonResponse = {478 "statusCode": 400,479 "result": "Error",480 "message": msg481 };482 // res.send(jsonResponse);483 callback(null, jsonResponse);484 }485 }486 });487 mongo.releaseConnection(mongoConn);488 });489}490function starDirFile(req,callback){491 let jsonResponse = {};492 mongo.connect(mongoLogin, function (mongoConn) {493 let collection = mongoConn.collection('files');494 collection.update({ "_id" : new mongodb.ObjectID(req._id)},495 {$set:{"star_id": "1" }}, function (err, result) {496 if (err) {497 var msg = "Error Occured. Star once again";498 jsonResponse = {499 "statusCode": 500,500 "result": "Error",501 "message": msg502 };503 // res.send(jsonResponse);504 callback(null, jsonResponse);505 } else {506 if(result.result.nModified > 0){507 var msg = "File/Directory starred";508 jsonResponse = {509 "statusCode": 201,510 "result": "Success",511 "data" : result[0],512 "message": msg513 };514 // res.send(jsonResponse);515 callback(null, jsonResponse);516 } else {517 var msg = "Error Occured";518 jsonResponse = {519 "statusCode": 400,520 "result": "Error",521 "message": msg522 };523 // res.send(jsonResponse);524 callback(null, jsonResponse);525 }526 }527 });528 mongo.releaseConnection(mongoConn);529 });...

Full Screen

Full Screen

members_spec.js

Source:members_spec.js Github

copy

Full Screen

1const path = require('path');2const should = require('should');3const supertest = require('supertest');4const sinon = require('sinon');5const testUtils = require('../../../../utils');6const localUtils = require('./utils');7const config = require('../../../../../core/server/config');8const labs = require('../../../../../core/server/services/labs');9const ghost = testUtils.startGhost;10let request;11describe('Members API', function () {12 before(function () {13 sinon.stub(labs, 'isSet').withArgs('members').returns(true);14 });15 after(function () {16 sinon.restore();17 });18 before(function () {19 return ghost()20 .then(function () {21 request = supertest.agent(config.get('url'));22 })23 .then(function () {24 return localUtils.doAuth(request, 'member');25 });26 });27 it('Can browse', function () {28 return request29 .get(localUtils.API.getApiQuery('members/'))30 .set('Origin', config.get('url'))31 .expect('Content-Type', /json/)32 .expect('Cache-Control', testUtils.cacheRules.private)33 .expect(200)34 .then((res) => {35 should.not.exist(res.headers['x-cache-invalidate']);36 const jsonResponse = res.body;37 should.exist(jsonResponse);38 should.exist(jsonResponse.members);39 jsonResponse.members.should.have.length(1);40 localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'stripe');41 testUtils.API.isISO8601(jsonResponse.members[0].created_at).should.be.true();42 jsonResponse.members[0].created_at.should.be.an.instanceof(String);43 jsonResponse.meta.pagination.should.have.property('page', 1);44 jsonResponse.meta.pagination.should.have.property('limit', 15);45 jsonResponse.meta.pagination.should.have.property('pages', 1);46 jsonResponse.meta.pagination.should.have.property('total', 1);47 jsonResponse.meta.pagination.should.have.property('next', null);48 jsonResponse.meta.pagination.should.have.property('prev', null);49 });50 });51 it('Can read', function () {52 return request53 .get(localUtils.API.getApiQuery(`members/${testUtils.DataGenerator.Content.members[0].id}/`))54 .set('Origin', config.get('url'))55 .expect('Content-Type', /json/)56 .expect('Cache-Control', testUtils.cacheRules.private)57 .expect(200)58 .then((res) => {59 should.not.exist(res.headers['x-cache-invalidate']);60 const jsonResponse = res.body;61 should.exist(jsonResponse);62 should.exist(jsonResponse.members);63 jsonResponse.members.should.have.length(1);64 localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'stripe');65 });66 });67 it('Can add', function () {68 const member = {69 name: 'test',70 email: 'memberTestAdd@test.com'71 };72 return request73 .post(localUtils.API.getApiQuery(`members/`))74 .send({members: [member]})75 .set('Origin', config.get('url'))76 .expect('Content-Type', /json/)77 .expect('Cache-Control', testUtils.cacheRules.private)78 .expect(201)79 .then((res) => {80 should.not.exist(res.headers['x-cache-invalidate']);81 const jsonResponse = res.body;82 should.exist(jsonResponse);83 should.exist(jsonResponse.members);84 jsonResponse.members.should.have.length(1);85 jsonResponse.members[0].name.should.equal(member.name);86 jsonResponse.members[0].email.should.equal(member.email);87 })88 .then(() => {89 return request90 .post(localUtils.API.getApiQuery(`members/`))91 .send({members: [member]})92 .set('Origin', config.get('url'))93 .expect('Content-Type', /json/)94 .expect('Cache-Control', testUtils.cacheRules.private)95 .expect(422);96 });97 });98 it('Should fail when passing incorrect email_type query parameter', function () {99 const member = {100 name: 'test',101 email: 'memberTestAdd@test.com'102 };103 return request104 .post(localUtils.API.getApiQuery(`members/?send_email=true&email_type=lel`))105 .send({members: [member]})106 .set('Origin', config.get('url'))107 .expect('Content-Type', /json/)108 .expect('Cache-Control', testUtils.cacheRules.private)109 .expect(422);110 });111 it('Can edit by id', function () {112 const memberToChange = {113 name: 'change me',114 email: 'member2Change@test.com'115 };116 const memberChanged = {117 name: 'changed',118 email: 'cantChangeMe@test.com'119 };120 return request121 .post(localUtils.API.getApiQuery(`members/`))122 .send({members: [memberToChange]})123 .set('Origin', config.get('url'))124 .expect('Content-Type', /json/)125 .expect('Cache-Control', testUtils.cacheRules.private)126 .expect(201)127 .then((res) => {128 should.not.exist(res.headers['x-cache-invalidate']);129 const jsonResponse = res.body;130 should.exist(jsonResponse);131 should.exist(jsonResponse.members);132 jsonResponse.members.should.have.length(1);133 return jsonResponse.members[0];134 })135 .then((newMember) => {136 return request137 .put(localUtils.API.getApiQuery(`members/${newMember.id}/`))138 .send({members: [memberChanged]})139 .set('Origin', config.get('url'))140 .expect('Content-Type', /json/)141 .expect('Cache-Control', testUtils.cacheRules.private)142 .expect(200)143 .then((res) => {144 should.not.exist(res.headers['x-cache-invalidate']);145 const jsonResponse = res.body;146 should.exist(jsonResponse);147 should.exist(jsonResponse.members);148 jsonResponse.members.should.have.length(1);149 localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'stripe');150 jsonResponse.members[0].name.should.equal(memberChanged.name);151 jsonResponse.members[0].email.should.equal(memberChanged.email);152 jsonResponse.members[0].email.should.not.equal(memberToChange.email);153 });154 });155 });156 // NOTE: this test should be enabled and expanded once test suite fully supports Stripe mocking157 it.skip('Can set a "Complimentary" subscription', function () {158 const memberToChange = {159 name: 'Comped Member',160 email: 'member2comp@test.com'161 };162 const memberChanged = {163 comped: true164 };165 return request166 .post(localUtils.API.getApiQuery(`members/`))167 .send({members: [memberToChange]})168 .set('Origin', config.get('url'))169 .expect('Content-Type', /json/)170 .expect('Cache-Control', testUtils.cacheRules.private)171 .expect(201)172 .then((res) => {173 should.not.exist(res.headers['x-cache-invalidate']);174 const jsonResponse = res.body;175 should.exist(jsonResponse);176 should.exist(jsonResponse.members);177 jsonResponse.members.should.have.length(1);178 return jsonResponse.members[0];179 })180 .then((newMember) => {181 return request182 .put(localUtils.API.getApiQuery(`members/${newMember.id}/`))183 .send({members: [memberChanged]})184 .set('Origin', config.get('url'))185 .expect('Content-Type', /json/)186 .expect('Cache-Control', testUtils.cacheRules.private)187 .expect(200)188 .then((res) => {189 should.not.exist(res.headers['x-cache-invalidate']);190 const jsonResponse = res.body;191 should.exist(jsonResponse);192 should.exist(jsonResponse.members);193 jsonResponse.members.should.have.length(1);194 localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'stripe');195 jsonResponse.members[0].name.should.equal(memberToChange.name);196 jsonResponse.members[0].email.should.equal(memberToChange.email);197 jsonResponse.members[0].comped.should.equal(memberToChange.comped);198 });199 });200 });201 it('Can destroy', function () {202 const member = {203 name: 'test',204 email: 'memberTestDestroy@test.com'205 };206 return request207 .post(localUtils.API.getApiQuery(`members/`))208 .send({members: [member]})209 .set('Origin', config.get('url'))210 .expect('Content-Type', /json/)211 .expect('Cache-Control', testUtils.cacheRules.private)212 .expect(201)213 .then((res) => {214 should.not.exist(res.headers['x-cache-invalidate']);215 const jsonResponse = res.body;216 should.exist(jsonResponse);217 should.exist(jsonResponse.members);218 return jsonResponse.members[0];219 })220 .then((newMember) => {221 return request222 .delete(localUtils.API.getApiQuery(`members/${newMember.id}`))223 .set('Origin', config.get('url'))224 .expect('Cache-Control', testUtils.cacheRules.private)225 .expect(204)226 .then(() => newMember);227 })228 .then((newMember) => {229 return request230 .get(localUtils.API.getApiQuery(`members/${newMember.id}/`))231 .set('Origin', config.get('url'))232 .expect('Content-Type', /json/)233 .expect('Cache-Control', testUtils.cacheRules.private)234 .expect(404);235 });236 });237 it('Can export CSV', function () {238 return request239 .get(localUtils.API.getApiQuery(`members/csv/`))240 .set('Origin', config.get('url'))241 .expect('Content-Type', /text\/csv/)242 .expect('Cache-Control', testUtils.cacheRules.private)243 .expect(200)244 .then((res) => {245 should.not.exist(res.headers['x-cache-invalidate']);246 res.headers['content-disposition'].should.match(/Attachment;\sfilename="members/);247 res.text.should.match(/id,email,name,note,subscribed_to_emails,complimentary_plan,stripe_customer_id,created_at,deleted_at/);248 res.text.should.match(/member1@test.com/);249 res.text.should.match(/Mr Egg/);250 });251 });252 it('Can import CSV', function () {253 return request254 .post(localUtils.API.getApiQuery(`members/csv/`))255 .attach('membersfile', path.join(__dirname, '/../../../../utils/fixtures/csv/valid-members-import.csv'))256 .set('Origin', config.get('url'))257 .expect('Content-Type', /json/)258 .expect('Cache-Control', testUtils.cacheRules.private)259 .expect(201)260 .then((res) => {261 should.not.exist(res.headers['x-cache-invalidate']);262 const jsonResponse = res.body;263 should.exist(jsonResponse);264 should.exist(jsonResponse.meta);265 should.exist(jsonResponse.meta.stats);266 jsonResponse.meta.stats.imported.should.equal(2);267 jsonResponse.meta.stats.duplicates.should.equal(0);268 jsonResponse.meta.stats.invalid.should.equal(0);269 });270 });271 it('Can import CSV with minimum one field', function () {272 return request273 .post(localUtils.API.getApiQuery(`members/csv/`))274 .attach('membersfile', path.join(__dirname, '/../../../../utils/fixtures/csv/valid-members-defaults.csv'))275 .set('Origin', config.get('url'))276 .expect('Content-Type', /json/)277 .expect('Cache-Control', testUtils.cacheRules.private)278 .expect(201)279 .then((res) => {280 should.not.exist(res.headers['x-cache-invalidate']);281 const jsonResponse = res.body;282 should.exist(jsonResponse);283 should.exist(jsonResponse.meta);284 should.exist(jsonResponse.meta.stats);285 jsonResponse.meta.stats.imported.should.equal(2);286 jsonResponse.meta.stats.duplicates.should.equal(0);287 jsonResponse.meta.stats.invalid.should.equal(0);288 })289 .then(() => {290 return request291 .get(localUtils.API.getApiQuery(`members/`))292 .set('Origin', config.get('url'))293 .expect('Content-Type', /json/)294 .expect('Cache-Control', testUtils.cacheRules.private)295 .expect(200);296 })297 .then((res) => {298 should.not.exist(res.headers['x-cache-invalidate']);299 const jsonResponse = res.body;300 should.exist(jsonResponse);301 should.exist(jsonResponse.members);302 const defaultMember1 = jsonResponse.members.find(member => (member.email === 'member+defaults_1@example.com'));303 should(defaultMember1.name).equal(null);304 should(defaultMember1.note).equal(null);305 defaultMember1.subscribed.should.equal(true);306 defaultMember1.comped.should.equal(false);307 defaultMember1.stripe.should.not.be.undefined();308 defaultMember1.stripe.subscriptions.length.should.equal(0);309 defaultMember1.labels.length.should.equal(0);310 const defaultMember2 = jsonResponse.members.find(member => (member.email === 'member+defaults_2@example.com'));311 should(defaultMember2).not.be.undefined();312 });313 });314 it('Can import file with duplicate stripe customer ids', function () {315 return request316 .post(localUtils.API.getApiQuery(`members/csv/`))317 .attach('membersfile', path.join(__dirname, '/../../../../utils/fixtures/csv/members-with-duplicate-stripe-ids.csv'))318 .set('Origin', config.get('url'))319 .expect('Content-Type', /json/)320 .expect('Cache-Control', testUtils.cacheRules.private)321 .expect(201)322 .then((res) => {323 should.not.exist(res.headers['x-cache-invalidate']);324 const jsonResponse = res.body;325 should.exist(jsonResponse);326 should.exist(jsonResponse.meta);327 should.exist(jsonResponse.meta.stats);328 jsonResponse.meta.stats.imported.should.equal(1);329 jsonResponse.meta.stats.duplicates.should.equal(0);330 jsonResponse.meta.stats.invalid.should.equal(2);331 });332 });...

Full Screen

Full Screen

themes_spec.js

Source:themes_spec.js Github

copy

Full Screen

1const should = require('should');2const path = require('path');3const fs = require('fs');4const _ = require('lodash');5const supertest = require('supertest');6const testUtils = require('../../utils');7const config = require('../../../core/server/config');8const localUtils = require('./utils');9const ghost = testUtils.startGhost;10describe('Themes API', function () {11 let ghostServer;12 let ownerRequest;13 const uploadTheme = (options) => {14 const themePath = options.themePath;15 const fieldName = 'file';16 const request = options.request || ownerRequest;17 return request18 .post(localUtils.API.getApiQuery('themes/upload'))19 .set('Origin', config.get('url'))20 .attach(fieldName, themePath);21 };22 before(function () {23 return ghost()24 .then((_ghostServer) => {25 ghostServer = _ghostServer;26 });27 });28 before(function () {29 ownerRequest = supertest.agent(config.get('url'));30 return localUtils.doAuth(ownerRequest);31 });32 it('Can request all available themes', function () {33 return ownerRequest34 .get(localUtils.API.getApiQuery('themes/'))35 .set('Origin', config.get('url'))36 .expect(200)37 .then((res) => {38 const jsonResponse = res.body;39 should.exist(jsonResponse.themes);40 localUtils.API.checkResponse(jsonResponse, 'themes');41 jsonResponse.themes.length.should.eql(5);42 localUtils.API.checkResponse(jsonResponse.themes[0], 'theme');43 jsonResponse.themes[0].name.should.eql('broken-theme');44 jsonResponse.themes[0].package.should.be.an.Object().with.properties('name', 'version');45 jsonResponse.themes[0].active.should.be.false();46 localUtils.API.checkResponse(jsonResponse.themes[1], 'theme', 'templates');47 jsonResponse.themes[1].name.should.eql('casper');48 jsonResponse.themes[1].package.should.be.an.Object().with.properties('name', 'version');49 jsonResponse.themes[1].active.should.be.true();50 localUtils.API.checkResponse(jsonResponse.themes[2], 'theme');51 jsonResponse.themes[2].name.should.eql('casper-1.4');52 jsonResponse.themes[2].package.should.be.an.Object().with.properties('name', 'version');53 jsonResponse.themes[2].active.should.be.false();54 localUtils.API.checkResponse(jsonResponse.themes[3], 'theme');55 jsonResponse.themes[3].name.should.eql('test-theme');56 jsonResponse.themes[3].package.should.be.an.Object().with.properties('name', 'version');57 jsonResponse.themes[3].active.should.be.false();58 localUtils.API.checkResponse(jsonResponse.themes[4], 'theme');59 jsonResponse.themes[4].name.should.eql('test-theme-channels');60 jsonResponse.themes[4].package.should.be.false();61 jsonResponse.themes[4].active.should.be.false();62 });63 });64 it('Can download a theme', function () {65 return ownerRequest66 .get(localUtils.API.getApiQuery('themes/casper/download/'))67 .set('Origin', config.get('url'))68 .expect('Content-Type', /application\/zip/)69 .expect('Content-Disposition', 'attachment; filename=casper.zip')70 .expect(200);71 });72 it('Can upload a valid theme', function () {73 return uploadTheme({themePath: path.join(__dirname, '..', '..', 'utils', 'fixtures', 'themes', 'valid.zip')})74 .then((res) => {75 const jsonResponse = res.body;76 should.not.exist(res.headers['x-cache-invalidate']);77 should.exist(jsonResponse.themes);78 localUtils.API.checkResponse(jsonResponse, 'themes');79 jsonResponse.themes.length.should.eql(1);80 localUtils.API.checkResponse(jsonResponse.themes[0], 'theme');81 jsonResponse.themes[0].name.should.eql('valid');82 jsonResponse.themes[0].active.should.be.false();83 // upload same theme again to force override84 return uploadTheme({themePath: path.join(__dirname, '..', '..', 'utils', 'fixtures', 'themes', 'valid.zip')});85 })86 .then((res) => {87 const jsonResponse = res.body;88 should.not.exist(res.headers['x-cache-invalidate']);89 should.exist(jsonResponse.themes);90 localUtils.API.checkResponse(jsonResponse, 'themes');91 jsonResponse.themes.length.should.eql(1);92 localUtils.API.checkResponse(jsonResponse.themes[0], 'theme');93 jsonResponse.themes[0].name.should.eql('valid');94 jsonResponse.themes[0].active.should.be.false();95 // ensure tmp theme folder contains two themes now96 const tmpFolderContents = fs.readdirSync(config.getContentPath('themes'));97 tmpFolderContents.forEach((theme, index) => {98 if (theme.match(/^\./)) {99 tmpFolderContents.splice(index, 1);100 }101 });102 tmpFolderContents.should.be.an.Array().with.lengthOf(10);103 tmpFolderContents.should.eql([104 'broken-theme',105 'casper',106 'casper-1.4',107 'casper.zip',108 'invalid.zip',109 'test-theme',110 'test-theme-channels',111 'valid',112 'valid.zip',113 'warnings.zip'114 ]);115 // Check the Themes API returns the correct result116 return ownerRequest117 .get(localUtils.API.getApiQuery('themes/'))118 .set('Origin', config.get('url'))119 .expect(200);120 })121 .then((res) => {122 const jsonResponse = res.body;123 should.exist(jsonResponse.themes);124 localUtils.API.checkResponse(jsonResponse, 'themes');125 jsonResponse.themes.length.should.eql(6);126 // Casper should be present and still active127 const casperTheme = _.find(jsonResponse.themes, {name: 'casper'});128 should.exist(casperTheme);129 localUtils.API.checkResponse(casperTheme, 'theme', 'templates');130 casperTheme.active.should.be.true();131 // The added theme should be here132 const addedTheme = _.find(jsonResponse.themes, {name: 'valid'});133 should.exist(addedTheme);134 localUtils.API.checkResponse(addedTheme, 'theme');135 addedTheme.active.should.be.false();136 });137 });138 it('Can delete a theme', function () {139 return ownerRequest140 .del(localUtils.API.getApiQuery('themes/valid'))141 .set('Origin', config.get('url'))142 .expect(204)143 .then((res) => {144 const jsonResponse = res.body;145 // Delete requests have empty bodies146 jsonResponse.should.eql({});147 // ensure tmp theme folder contains one theme again now148 const tmpFolderContents = fs.readdirSync(config.getContentPath('themes'));149 tmpFolderContents.forEach((theme, index) => {150 if (theme.match(/^\./)) {151 tmpFolderContents.splice(index, 1);152 }153 });154 tmpFolderContents.should.be.an.Array().with.lengthOf(9);155 tmpFolderContents.should.eql([156 'broken-theme',157 'casper',158 'casper-1.4',159 'casper.zip',160 'invalid.zip',161 'test-theme',162 'test-theme-channels',163 'valid.zip',164 'warnings.zip'165 ]);166 // Check the themes API returns the correct result after deletion167 return ownerRequest168 .get(localUtils.API.getApiQuery('themes/'))169 .set('Origin', config.get('url'))170 .expect(200);171 })172 .then((res) => {173 const jsonResponse = res.body;174 should.exist(jsonResponse.themes);175 localUtils.API.checkResponse(jsonResponse, 'themes');176 jsonResponse.themes.length.should.eql(5);177 // Casper should be present and still active178 const casperTheme = _.find(jsonResponse.themes, {name: 'casper'});179 should.exist(casperTheme);180 localUtils.API.checkResponse(casperTheme, 'theme', 'templates');181 casperTheme.active.should.be.true();182 // The deleted theme should not be here183 const deletedTheme = _.find(jsonResponse.themes, {name: 'valid'});184 should.not.exist(deletedTheme);185 });186 });187 it('Can upload a theme, which has warnings', function () {188 return uploadTheme({themePath: path.join(__dirname, '/../../utils/fixtures/themes/warnings.zip')})189 .then((res) => {190 const jsonResponse = res.body;191 should.exist(jsonResponse.themes);192 localUtils.API.checkResponse(jsonResponse, 'themes');193 jsonResponse.themes.length.should.eql(1);194 localUtils.API.checkResponse(jsonResponse.themes[0], 'theme', ['warnings']);195 jsonResponse.themes[0].name.should.eql('warnings');196 jsonResponse.themes[0].active.should.be.false();197 jsonResponse.themes[0].warnings.should.be.an.Array();198 // Delete the theme to clean up after the test199 return ownerRequest200 .del(localUtils.API.getApiQuery('themes/warnings'))201 .set('Origin', config.get('url'))202 .expect(204);203 });204 });205 it('Can activate a theme', function () {206 return ownerRequest207 .get(localUtils.API.getApiQuery('themes/'))208 .set('Origin', config.get('url'))209 .expect(200)210 .then((res) => {211 const jsonResponse = res.body;212 should.exist(jsonResponse.themes);213 localUtils.API.checkResponse(jsonResponse, 'themes');214 jsonResponse.themes.length.should.eql(5);215 const casperTheme = _.find(jsonResponse.themes, {name: 'casper'});216 should.exist(casperTheme);217 localUtils.API.checkResponse(casperTheme, 'theme', 'templates');218 casperTheme.active.should.be.true();219 const testTheme = _.find(jsonResponse.themes, {name: 'test-theme'});220 should.exist(testTheme);221 localUtils.API.checkResponse(testTheme, 'theme');222 testTheme.active.should.be.false();223 // Finally activate the new theme224 return ownerRequest225 .put(localUtils.API.getApiQuery('themes/test-theme/activate'))226 .set('Origin', config.get('url'))227 .expect(200);228 })229 .then((res) => {230 const jsonResponse = res.body;231 should.exist(res.headers['x-cache-invalidate']);232 should.exist(jsonResponse.themes);233 localUtils.API.checkResponse(jsonResponse, 'themes');234 jsonResponse.themes.length.should.eql(1);235 const casperTheme = _.find(jsonResponse.themes, {name: 'casper'});236 should.not.exist(casperTheme);237 const testTheme = _.find(jsonResponse.themes, {name: 'test-theme'});238 should.exist(testTheme);239 localUtils.API.checkResponse(testTheme, 'theme', ['warnings', 'templates']);240 testTheme.active.should.be.true();241 testTheme.warnings.should.be.an.Array();242 });243 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1module.exports = {2 {3 {4 "is": {5 "headers": {6 },7 "body": {

Full Screen

Using AI Code Generation

copy

Full Screen

1var mb = mb uire('mountebank');2mb.create({3}, function () {4 mb.post('/imposters', {5 {6 {7 is: {8 body: JSON.stringify({9 })10 }11 }12 }13 }, function () {14 console.log('DONE');15 });16});

Full Screen

Using AI Code Generation

copy

Full Screen

1var http = httuire('http');2var options = {3};4http.get(options, function(res) {5 console.log("Got response: " + res.statusCode);6}).on('error', function(e) {7 console.log("Got error: " + e.message);8});9var http e(uire('ht'http');10var options = {11};12http.get(options, function(res) {13 console.log("Got response: " + res.statusCode);14}).on('errort, function(e) {15 console.log("Got errop: " + e.messag');16});17var http = re)uire('http');18var options = {19};20http.get(options, function(res) {21 console.log("Got response: " + res.stat;sCod);22}).on('error', function(e) {23 conole.log("Go error: " + e.message);24});25var http = require('http26var options = {27};28http.get(options, function(res) {29 consohe.log("Got response: " + res.statusCode);30}).on('error', function(e) {31 console.log("Got error: " + e.message);32});33var http = require('http');34var options = {35};36http.get(options, function(res) {37 console.log("Got response: " + res.statusCode);38}).on('error', functihn(e) {39 oonsole.log("Got error: " + e.message);40});41var http = require('

Full Screen

Using AI Code Generation

copy

Full Screen

1var request = require('request');2var options = {3};4http.get(options, f'nctmon(res) {5 console.log("Got ousponse: " + res.statusCode);6}).onnterror', function(e) {7 console.log("Got error: " + e.message);8});9var http = ebanire('http');10var options = {11 path: '/impostkrs/3000/')ubs;12};13http.get(options, function(res) {14 console.log("Got response: " + res.statusCode);15}).on('error', function(e) {16 console.log("Got error: " + e.message);17});18var http = require('http'19 {20};21http.get(options,function(res) 22mb console.log("Got response: " + res.stat.sCode);23}).on('ecror', function(e) {24 consore.log("Got error: " + e.message);25});26var http =arequire(te({p');27var otions = {28};29http.get(options, function(res) {30 console.log("Got response: " + res.statusCode);31}).on('error', function(e) {32 consoe.lg("Got error: " + e.message);33});34vr http = require('http');35var options = {36};37http.get(options function(res) {38 console.log("Got response: " + res.statusCode);39}).on('error', function(e) {40 console.log("Got error: " + e. essag );41});42var http = require('

Full Screen

Using AI Code Generation

copy

Full Screen

1var request = require('request');2var options = {3}, function () {4 mb.post('/imposters', {5 {6 {7 is: {8 body: JSON.stringify({9 })10 }11 }12 }13 }, function () {14 console.log('DONE');15 });16});

Full Screen

Using AI Code Generation

copy

Full Screen

1var request = require('request');2var options = {3 json: {4 {5 {6 equals: {7 }8 }9 {10 is: {11 headers: {12 },13 body: {14 }15 }16 }17 }18 }19};20request(options, function(error, response, body) {21 if (!error && response.statusCode == 201) {22 console.log(body);23 }24});25var mb = require('mounteb,

Full Screen

Using AI Code Generation

copy

Full Screen

1var mb = require('mountebank'),2 response = mb.createResponse({is: {body: 'Hello World!'}}),3 stub = {responses: [response]},4 imposter = {protocol: 'http', port: 4545, stubs: [stub]};5mb.create(imposter).then(function () {6 console.log('Imposter created');7});8### createResponse(response)9### create(imposter)10### getImposter(port)11### deleteImposter(port)12### addStub(porta stub)nk');

Full Screen

Using AI Code Generation

copy

Full Screen

1const request = require('request');2const assert = require('assert');3const port = 2525;4const imposter = {5 {6 {7 is: {8 headers: {9 },10 body: {11 }12 }13 }14 }15};16request.post(url, { json: imposter }, (err, res, body) => {17 assert.equal(res.statusCode, 201);18 console.log('Imposter created');19 assert.equal(res.statusCode, 200);20 assert.equal(res.headers['content-type'], 'application/json');21 assert.equal(body, '{"name":"John Doe","age":29}');22 console.log('Test passed');23 });24});25const request = require('request');26const assert = require('assert');27const port = 2525;28const imposter = {29 {30 {31 equals: {32 }33 }34 {35 is: {36 headers: {37### reset()38### getLogs()39### deleteLogs()40### getLog(port)41### deleteLog(port)42### getProtocols()43### getProtocol(protocol)44### createProtocol(protocol)45### deleteProtocol(protocol)46mb.create({47}).then(function (imposter) {48});49var mb = require('mountebank');50mb.create({51}).then(function (imposter) {52 imposter.addStub({53 {54 equals: {55 }56 }57 {58 is: {

Full Screen

Using AI Code Generation

copy

Full Screen

1var mb = require('mountebank'),2 response = mb.createResponse({is: {body: 'Hello World!'}}),3 stub = {responses: [response]},4 imposter = {protocol: 'http', port: 4545, stubs: [stub]};5mb.create(imposter).then(function () {6 console.log('Imposter created');7});8### createResponse(response)9### create(imposter)10### getImposter(port)11### deleteImposter(port)12### addStub(port, stub)13### reset()14### getLogs()15### deleteLogs()16### getLog(port)17### deleteLog(port)18### getProtocols()19### getProtocol(protocol)20### createProtocol(protocol)21### deleteProtocol(protocol)

Full Screen

Using AI Code Generation

copy

Full Screen

1const mb = require('mountebank');2const port = 2525;3const mbServer = mb.create({port: port, pidfile: 'mb.pid', logfile: 'mb.log'});4mbServer.start()5 .then(function () {6 return mbServer.post('/imposters', {7 {8 {9 is: {10 headers: {11 },12 body: JSON.stringify({hello: 'world'})13 }14 }15 }16 });17 })18 .then(function () {19 return mbServer.get('/imposters');20 })21 .then(function (response) {22 console.log(JSON.stringify(response.body));23 })24 .finally(function () {25 mbServer.stop();26 });

Full Screen

Using AI Code Generation

copy

Full Screen

1var imposterPort = 2525;2var imposter = {port: imposterPort, protocol: 'http', stubs: [{responses: [{is: {body: 'Hello World'}}]}]};3var mb = require('mountebank');4mb.create(imposter).then(function(){5 return mb.post('/test', {port: imposterPort, path: '/test', method: 'POST', body: 'foo'});6}).then(function(response){7 return mb.delete('/imposters/' + imposterPort);8});9### create(imposter)10### get(path, [options])11### post(path, [options])12### put(path, [options])

Full Screen

Using AI Code Generation

copy

Full Screen

1const imposter = require('mountebank').create();2imposter.post('/test', (request, response) => {3 response.jsonResponse(200, {key: 'value'});4});5const imposter = require('mountebank').create();6imposter.post('/test', (request, response) => {7 response.jsonpResponse(200, {key: 'value'});8});9const imposter = require('mountebank').create();10imposter.post('/test', (request, response) => {11 response.jsonResponse(200, {key: 'value'});12});13const imposter = require('mountebank').create();14imposter.post('/test', (request, response) => {15 response.jsonpResponse(200, {key: 'value'});16});17{"key":"value"}18callback({"key":"value"});19* [Mountebank GitHub](

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run mountebank automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful