Skip to content

Commit

Permalink
Merge pull request #39 from AnyFetch/new-api-post-subcompany
Browse files Browse the repository at this point in the history
New `POST /subcompanies` behavior
  • Loading branch information
Neamar committed Jun 27, 2014
2 parents e618b40 + 778f172 commit fb4e85c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 27 deletions.
7 changes: 2 additions & 5 deletions config/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ module.exports = {
},
fakeCompany: {
name: 'the_fake_subcompany',
hydraters: [
'http://plaintext.hydrater.anyfetch.com/hydrate',
'http://pdf.hydrater.anyfetch.com/hydrate',
]
hydraters: []
},
fakeDocument: {
identifier: 'the "unique" document identifier (éüà)',
Expand Down Expand Up @@ -93,4 +90,4 @@ module.exports = {
contentType: 'image/jpeg'
}
}
};
};
1 change: 1 addition & 0 deletions config/json/api-descriptors.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"endpoint": "/subcompanies",
"verb": "POST",
"body": [
"user",
"name",
"hydraters"
]
Expand Down
17 changes: 8 additions & 9 deletions lib/utility/create-subcompany-with-admin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use strict';

var async = require('async');
var rarity = require('rarity');

var rarity = require('rarity');

/**
* Create a new user and move it to a new subcompany.
Expand All @@ -19,14 +16,16 @@ module.exports = function createSubcompanyWithAdmin(subcompany, newAdmin, finalC
async.waterfall([
// Create the admin user who will be named admin of the new subcompany
function createAdmin(cb) {
self.postUser(newAdmin, rarity.slice(1, cb));
self.postUser(newAdmin, cb);
},

function createSubcompany(cb) {
var newAdminFetch = new self.constructor(newAdmin.email, newAdmin.password);
newAdminFetch.postSubcompany(subcompany, function(err, res) {
function extractId(res, cb) {
cb(null, res.body.id);
},
function createSubcompany(adminId, cb) {
subcompany.user = adminId;
self.postSubcompany(subcompany, function(err, res) {
cb(err, res.body);
});
},
], finalCb);
};
};
3 changes: 2 additions & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ describe('<High-level helper functions>', function() {
it('should have created the new user and moved it to the subcompany', function(done) {
var newAdminFetch = new Anyfetch(admin.email, admin.password);
newAdminFetch.getCompany(function(err, res) {
var company = res.body;
should(err).not.be.ok;

var company = res.body;
should(company).be.ok;
company.should.have.property('id').and.equal(subcompanyId);

Expand Down
22 changes: 10 additions & 12 deletions test/mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('<Low-level mapping functions>', function() {
before(function() {
anyfetch = new Anyfetch(accessToken);
});

var testEndpoint = function(name) {
describe(name, function() {
var expected = configuration.apiDescriptors[name];
Expand Down Expand Up @@ -92,11 +92,11 @@ describe('<Low-level mapping functions>', function() {
it('should use the correct verb', function() {
res.req.method.should.equal(expected.verb);
});

it('should target the correct endpoint', function() {
res.req.path.should.startWith(expected.endpoint);
});

it('should have the expected return code', function() {
res.res.statusCode.should.equal(expected.expectedStatus);
});
Expand Down Expand Up @@ -149,17 +149,17 @@ describe('<Low-level mapping functions>', function() {

it('should post file created with `fs.createReadStream`', function(done) {
hash.file = fs.createReadStream(hash.path);
subFunctions.postFile(hash, done);
subFunctions.postFile(hash, done);
});

it('should post file without knowing mime-type', function(done) {
var file = fs.createReadStream(hash.path);
subFunctions.postFile({ file: file }, done);
subFunctions.postFile({ file: file }, done);
});

it('should post file from a path', function(done) {
var filename = __dirname + '/samples/hello.md';
subFunctions.postFile({ file: filename }, done);
subFunctions.postFile({ file: filename }, done);
});
});

Expand Down Expand Up @@ -207,7 +207,7 @@ describe('<Low-level mapping functions>', function() {
done();
});
});

// Delete phony document
it('...delete phony document', function(done) {
anyfetch.deleteDocumentById(documentId, done);
Expand Down Expand Up @@ -246,20 +246,18 @@ describe('<Low-level mapping functions>', function() {
var companyInfos = {
name: 'the-fake-company'
};
var userId = null;
var subcompanyId = null;

before(function(done) {
// Setup: an admin user who will be named admin of the new subcompany
anyfetch.postUser(userInfos, function(err, res) {
userId = res.body.id;
companyInfos.user = res.body.id;
done(err);
});
});

it('should create a subcompany as the new user', function(done) {
var chuckFetch = new Anyfetch(userInfos.email, userInfos.password);
chuckFetch.postSubcompanies(companyInfos, function(err, res) {
it('should create a subcompany with the new user', function(done) {
anyfetch.postSubcompanies(companyInfos, function(err, res) {
subcompanyId = res.body.id;
done(err);
});
Expand Down

0 comments on commit fb4e85c

Please sign in to comment.