Last active
August 29, 2015 14:10
-
-
Save dwaligora/38b98189db44783facb5 to your computer and use it in GitHub Desktop.
mock internal module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var express = require('express'), | |
hash = require('object-hash'), | |
config = require('../../../../../config'), | |
session = require('../../../../../lib/session'), | |
formatterManager = require('../../../../../lib/formatter'), | |
zipManager = require('../../../../../lib/zip/manager'); | |
var router = express.Router(); | |
module.exports = function(application) { | |
/** | |
* POST /zip - preparing archive unique hash/token | |
*/ | |
router.post( | |
'/', | |
require('../../../../../middleware/validator/zip').Create, | |
require('../../../../../middleware/validator/response')('json.validation'), | |
switchDb, | |
saveHash, | |
sendResponse | |
); | |
return router; | |
}; | |
/** | |
* @param {Request} req | |
* @param {Response} res | |
* @param {Function} next | |
*/ | |
function switchDb(req, res, next) { | |
zipManager.switchStorageDb(10); | |
return next(); | |
} | |
/** | |
* @param {Request} req | |
* @param {Response} res | |
* @param {Function} next | |
*/ | |
function saveHash(req, res, next) { | |
res.locals.hashString = hash(req.body); | |
// store data & set expiration time | |
zipManager.saveArchiveData( | |
formatterManager.get('redis.key').format([ | |
session.getClient(), | |
session.getProvider(), | |
'archive', | |
res.locals.hashString | |
]), | |
req.body, | |
config.zip.ttl | |
); | |
return next(); | |
} | |
/** | |
* @param {Request} req | |
* @param {Response} res | |
* @param {Function} next | |
*/ | |
function sendResponse(req, res, next) { | |
res.writeHead(200, {'Content-Type': 'application/json'}); | |
res.write(JSON.stringify(formatterManager.get('json.response').format( | |
200, | |
'Archive prepared successfully!', | |
{ token: res.locals.hashString } | |
))); | |
res.end(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// core libs | |
var hash = require('object-hash'), | |
expect = require('chai').expect, | |
request = require('request'), | |
rewire = require('rewire'), | |
sinon = require('sinon'), | |
// app libs | |
config = require('../../../../../../config'), | |
appBootstrap = require('../../../../../../app/bootstrap'), | |
// stubs, mocks | |
server = null, | |
serverStubFactory = require('../../../../../stubs/server'), | |
create = rewire('../../../../../../app/api/controllers/v1/zip/create'), | |
// fixtures | |
subdomains = ['rs', 'altitude'], | |
requestBody = { | |
filename: 'mochaTestArchive', | |
files: [ | |
{ | |
container: 'altitude', | |
source: 'licence-disclaimer.no.104c0928cb380a52e10c1be525cffc79.txt', | |
destination: 'mochaTestArchive/test.txt' | |
} | |
] | |
}; | |
describe('API V1 ZIP resource', function () { | |
before(function(done) { | |
server = serverStubFactory.create(appBootstrap.getInstance(), 1025, done); | |
}); | |
after(function(done) { | |
server.close(); | |
done(); | |
}); | |
describe('/POST /api/v1/zip', function () { | |
it('should return 500 unless request is valid', function () { | |
require('../../../../../../lib/util').Request = require('../../../../../stubs/lib/util/request')( | |
server, | |
'/api/v1/zip', | |
subdomains | |
); | |
request( | |
serverStubFactory.provideRequestOpts(server, 'api/v1/zip', 'POST', {}), | |
function(error, response, body) { | |
expect(response.headers['content-type']).to.equal('application/json'); | |
expect(response.statusCode).to.equal(400); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment