Skip to content

Commit

Permalink
Revert "Deprecate API v2" (#7801)
Browse files Browse the repository at this point in the history
  • Loading branch information
paglias authored Jul 17, 2016
1 parent 45c31a2 commit 590adb3
Show file tree
Hide file tree
Showing 193 changed files with 11,487 additions and 230 deletions.
7 changes: 7 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ website/client/

# Temporarilly disabled. These should be removed when the linting errors are fixed
common/script/content/index.js
common/script/public/**/*.js

website/server/**/api-v2/**/*.js
website/server/routes/payments.js
website/server/routes/pages.js
website/server/middlewares/apiThrottle.js
website/server/middlewares/forceRefresh.js

debug-scripts/*
scripts/*
Expand Down
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ module.exports = function(grunt) {
grunt.registerTask('build:test', ['test:prepare:translations', 'build:dev']);

grunt.registerTask('test:prepare:translations', function() {
var i18n = require('./website/server/libs/i18n'),
var i18n = require('./website/server/libs/api-v3/i18n'),
fs = require('fs');
fs.writeFileSync('test/spec/mocks/translations.js',
"if(!window.env) window.env = {};\n" +
Expand Down
1 change: 1 addition & 0 deletions common/locales/en/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"deleteLocalAccountText": "Are you sure? This will delete your account forever, and it can never be restored! You will need to register a new account to use Habitica again. Banked or spent Gems will not be refunded. If you're absolutely certain, type your password into the text box below.",
"API": "API",
"APIv3": "API v3",
"APIv2": "API v2 - Deprecated",
"APIText": "Copy these for use in third party applications. However, think of your API Token like a password, and do not share it publicly. You may occasionally be asked for your User ID, but never post your API Token where others can see it, including on Github.",
"APIToken": "API Token (this is a password - see warning above!)",
"thirdPartyApps": "Third Party Apps",
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,13 @@
"scripts": {
"lint": "eslint .",
"test": "npm run lint && gulp test",
"test:api-v2:unit": "mocha test/server_side",
"test:api-v2:integration": "mocha test/api/v2 --recursive",
"test:api-v3": "gulp test:api-v3",
"test:api-v3:unit": "gulp test:api-v3:unit",
"test:api-v3:integration": "gulp test:api-v3:integration",
"test:api-v3:integration:separate-server": "NODE_ENV=test gulp test:api-v3:integration:separate-server",
"test:api-v3:integration:separate-server": "gulp test:api-v3:integration:separate-server",
"test:api-legacy": "istanbul cover -i \"website/server/**\" --dir coverage/api ./node_modules/mocha/bin/_mocha test/api-legacy",
"test:common": "mocha test/common --recursive",
"test:content": "mocha test/content --recursive",
"test:karma": "karma start --single-run",
Expand Down
2 changes: 1 addition & 1 deletion tasks/gulp-console.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mongoose from 'mongoose';
import autoinc from 'mongoose-id-autoinc';
import logger from '../website/server/libs/logger';
import logger from '../website/server/libs/api-v3/logger';
import nconf from 'nconf';
import repl from 'repl';
import gulp from 'gulp';
Expand Down
184 changes: 181 additions & 3 deletions tasks/gulp-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ let server;

const TEST_DB_URI = nconf.get('TEST_DB_URI');

const API_V2_TEST_COMMAND = 'npm run test:api-v2:integration';
const API_V3_TEST_COMMAND = 'npm run test:api-v3';
const LEGACY_API_TEST_COMMAND = 'npm run test:api-legacy';
const COMMON_TEST_COMMAND = 'npm run test:common';
const CONTENT_TEST_COMMAND = 'npm run test:content';
const CONTENT_OPTIONS = {maxBuffer: 1024 * 500};
const KARMA_TEST_COMMAND = 'npm run test:karma';
const SERVER_SIDE_TEST_COMMAND = 'npm run test:api-v2:unit';
const ISTANBUL_TEST_COMMAND = 'npm run test:api-legacy';

/* Helper methods for reporting test summary */
let testResults = [];
Expand Down Expand Up @@ -181,6 +185,43 @@ gulp.task('test:server_side:safe', ['test:prepare:build'], (cb) => {
pipe(runner);
});

gulp.task('test:api-legacy', ['test:prepare:mongo'], (cb) => {
let runner = exec(
testBin(ISTANBUL_TEST_COMMAND),
(err, stdout, stderr) => {
cb(err);
}
);
pipe(runner);
});

gulp.task('test:api-legacy:safe', ['test:prepare:mongo'], (cb) => {
let runner = exec(
testBin(ISTANBUL_TEST_COMMAND),
(err, stdout, stderr) => {
testResults.push({
suite: 'API (legacy) Specs',
pass: testCount(stdout, /(\d+) passing/),
fail: testCount(stdout, /(\d+) failing/),
pend: testCount(stdout, /(\d+) pending/)
});
cb();
}
);
pipe(runner);
});

gulp.task('test:api-legacy:clean', (cb) => {
pipe(exec(testBin(LEGACY_API_TEST_COMMAND), () => cb()));
});

gulp.task('test:api-legacy:watch', [
'test:prepare:mongo',
'test:api-legacy:clean'
], () => {
gulp.watch(['website/server/**', 'test/api-legacy/**'], ['test:api-legacy:clean']);
});

gulp.task('test:karma', ['test:prepare:build'], (cb) => {
let runner = exec(
testBin(KARMA_TEST_COMMAND),
Expand Down Expand Up @@ -268,6 +309,46 @@ gulp.task('test:e2e:safe', ['test:prepare', 'test:prepare:server'], (cb) => {
});
});

/*gulp.task('test:api-v2', ['test:prepare:server'], (done) => {
process.env.API_VERSION = 'v2';
awaitPort(TEST_SERVER_PORT).then(() => {
runMochaTests('./test/api/v2/**//*.js', server, done)
});
});
gulp.task('test:api-v2:watch', ['test:prepare:server'], () => {
process.env.RUN_INTEGRATION_TEST_FOREVER = true;
gulp.watch(['website/server/**', 'test/api/v2/**'], ['test:api-v2']);
});
gulp.task('test:api-v2:safe', ['test:prepare:server'], (done) => {
awaitPort(TEST_SERVER_PORT).then(() => {
let runner = exec(
testBin(API_V2_TEST_COMMAND),
(err, stdout, stderr) => {
testResults.push({
suite: 'API V2 Specs\t',
pass: testCount(stdout, /(\d+) passing/),
fail: testCount(stderr, /(\d+) failing/),
pend: testCount(stdout, /(\d+) pending/)
});
done();
}
);
pipe(runner);
});
});*/

gulp.task('test:api-v2:integration', (done) => {
let runner = exec(
testBin('mocha test/api/v2 --recursive'),
{maxBuffer: 500*1024},
(err, stdout, stderr) => done(err)
)

pipe(runner);
});

gulp.task('test:api-v3:unit', (done) => {
let runner = exec(
testBin('mocha test/api/v3/unit --recursive'),
Expand All @@ -278,7 +359,7 @@ gulp.task('test:api-v3:unit', (done) => {
});

gulp.task('test:api-v3:unit:watch', () => {
gulp.watch(['website/server/libs/*', 'test/api/v3/unit/**/*', 'website/server/controllers/**/*'], ['test:api-v3:unit']);
gulp.watch(['website/server/libs/api-v3/*', 'test/api/v3/unit/**/*', 'website/server/controllers/**/*'], ['test:api-v3:unit']);
});

gulp.task('test:api-v3:integration', (done) => {
Expand All @@ -292,7 +373,7 @@ gulp.task('test:api-v3:integration', (done) => {
});

gulp.task('test:api-v3:integration:watch', () => {
gulp.watch(['website/server/controllers/api-v3/**/*', 'common/script/ops/*', 'website/server/libs/*.js',
gulp.watch(['website/server/controllers/api-v3/**/*', 'common/script/ops/*', 'website/server/libs/api-v3/*.js',
'test/api/v3/integration/**/*'], ['test:api-v3:integration']);
});

Expand All @@ -312,6 +393,7 @@ gulp.task('test', (done) => {
'test:karma',
'test:api-v3:unit',
'test:api-v3:integration',
'test:api-v2:integration',
done
);
});
Expand All @@ -322,4 +404,100 @@ gulp.task('test:api-v3', (done) => {
'test:api-v3:integration',
done
);
});
});

// Old tests tasks
/*
gulp.task('test:api-v3', ['test:api-v3:unit', 'test:api-v3:integration']);
gulp.task('test:api-v3:watch', ['test:api-v3:unit:watch', 'test:api-v3:integration:watch']);
gulp.task('test:api-v3:unit', (done) => {*/
// runMochaTests('./test/api/v3/unit/**/*.js', null, done)
/*});
gulp.task('test:api-v3:unit:watch', () => {
gulp.watch(['website/server/**', 'test/api/v3/unit/**'], ['test:api-v3:unit']);
});
gulp.task('test:api-v3:integration', ['test:prepare:server'], (done) => {
process.env.API_VERSION = 'v3';
awaitPort(TEST_SERVER_PORT).then(() => {*/
// runMochaTests('./test/api/v3/integration/**/*.js', server, done)
/* });
});
gulp.task('test:api-v3:integration:watch', ['test:prepare:server'], () => {
process.env.RUN_INTEGRATION_TEST_FOREVER = true;
gulp.watch(['website/server/**', 'test/api/v3/integration/**'], ['test:api-v3:integration']);
});
gulp.task('test:api-v3:safe', ['test:prepare:server'], (done) => {
awaitPort(TEST_SERVER_PORT).then(() => {
let runner = exec(
testBin(API_V3_TEST_COMMAND),
(err, stdout, stderr) => {
testResults.push({
suite: 'API V3 Specs\t',
pass: testCount(stdout, /(\d+) passing/),
fail: testCount(stdout, /(\d+) failing/),
pend: testCount(stdout, /(\d+) pending/)
});
done();
}
);
pipe(runner);
});
});
gulp.task('test:all', (done) => {
runSequence(
//'test:e2e:safe',
//'test:common:safe',
//'test:content:safe',
// 'test:server_side:safe',
//'test:karma:safe',
//'test:api-legacy:safe',
//'test:api-v2:safe',
'test:api-v3:safe',
done);
});
gulp.task('test', ['test:all'], () => {
let totals = [0,0,0];
console.log('\n\x1b[36m\x1b[4mHabitica Test Summary\x1b[0m\n');
testResults.forEach((s) => {
totals[0] = totals[0] + s.pass;
totals[1] = totals[1] + s.fail;
totals[2] = totals[2] + s.pend;
console.log(
`\x1b[33m\x1b[4m${s.suite}\x1b[0m\t`,
`\x1b[32mPassing: ${s.pass},\t`,
`\x1b[31mFailed: ${s.fail},\t`,
`\x1b[36mPending: ${s.pend}\t`
);
if (s.pass === 0) {
console.error('ERROR: Detected a test suite with 0 passing tests. Something may be wrong causing the build to error.');
process.exit(1);
}
});
console.log(
'\n\x1b[33m\x1b[4mTotal:\x1b[0m\t\t\t',
`\x1b[32mPassing: ${totals[0]},\t`,
`\x1b[31mFailed: ${totals[1]},\t`,
`\x1b[36mPending: ${totals[2]}\t`
);
kill(server);
if (totals[1] > 0) {
console.error('ERROR: There are failing tests!');
process.exit(1);
} else {
console.log('\n\x1b[36mThanks for helping keep Habitica clean!\x1b[0m');
process.exit();
}
});*/
5 changes: 5 additions & 0 deletions test/api-legacy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Legacy API Tests

These tests have been deprecated. Any tests that you see in this folder should be converted to ES2015 and ported to the `/test/api` directory.

Once all tests have been ported, this directory will be removed.
92 changes: 92 additions & 0 deletions test/api-legacy/api-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
require('babel-core/register');
var path, superagentDefaults;

superagentDefaults = require("superagent-defaults");

global.request = superagentDefaults();

global.mongoose = require("mongoose");
var Bluebird = require('bluebird');
mongoose.Promise = Bluebird;

global.moment = require("moment");

global.async = require("async");

global._ = require("lodash");

global.shared = require("../../common");

global.User = require("../../website/server/models/user").model;

global.chai = require("chai");

chai.use(require("sinon-chai"));

global.expect = chai.expect;

path = require("path");

global.conf = require("nconf");

conf.argv().env().file({
file: path.join(__dirname, "../config.json")
}).defaults();

conf.set("PORT", "1337");

process.env.NODE_DB_URI = "mongodb://localhost/habitrpg_test_api_legacy";

global.baseURL = "http://localhost:" + conf.get("PORT") + "/api/v2";

global.user = void 0;

global.expectCode = function(res, code) {
if (code === 200) {
expect(res.body.err).to.not.exist;
}
return expect(res.statusCode).to.equal(code);
};

global.registerNewUser = function(cb, main) {
var password, randomID, username;
if (main == null) {
main = true;
}
randomID = shared.uuid();
if (main) {
username = password = randomID;
}
return request.post(baseURL + "/register").set("Accept", "application/json").set("X-API-User", null).set("X-API-Key", null).send({
username: randomID,
password: randomID,
confirmPassword: randomID,
email: randomID + "@gmail.com"
}).end(function(err, res) {
var _id, apiToken;
if (!main) {
return cb(null, res.body);
}
_id = res.body._id;
apiToken = res.body.apiToken;
return User.findOne({
_id: _id,
apiToken: apiToken
}, function(err, _user) {
expect(err).to.not.be.ok;
global.user = _user;
request.set("Accept", "application/json").set("X-API-User", _id).set("X-API-Key", apiToken);
return cb(null, res.body);
});
});
};

global.registerManyUsers = function(number, callback) {
return async.times(number, function(n, next) {
return registerNewUser(function(err, user) {
return next(err, user);
}, false);
}, function(err, users) {
return callback(err, users);
});
};
Loading

0 comments on commit 590adb3

Please sign in to comment.