From 598b1e6b61b463a5b19d33b33b60ed24341fb0c3 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 18 Dec 2013 11:49:09 -0800 Subject: [PATCH] Reformat the code using 2 space identation --- lib/models/application.js | 211 ++++++++++++------------ test/model.application.test.js | 293 ++++++++++++++++----------------- 2 files changed, 251 insertions(+), 253 deletions(-) diff --git a/lib/models/application.js b/lib/models/application.js index 03c98bb6..9cca6c77 100644 --- a/lib/models/application.js +++ b/lib/models/application.js @@ -3,84 +3,83 @@ var assert = require('assert'); // Authentication schemes var AuthenticationSchemeSchema = { - scheme: String, // local, facebook, google, twitter, linkedin, github - credential: Object // Scheme-specific credentials + scheme: String, // local, facebook, google, twitter, linkedin, github + credential: Object // Scheme-specific credentials }; // See https://github.com/argon/node-apn/blob/master/doc/apn.markdown var APNSSettingSchema = { - production: Boolean, // production or development mode - certData: String, // The certificate data - keyData: String, // The key data + production: Boolean, // production or development mode + certData: String, // The certificate data + keyData: String, // The key data - pushOptions: {type: { - gateway: {type: String, default: 'gateway.sandbox.push.apple.com'}, - port: {type: Number, default: 2195} - }}, + pushOptions: {type: { + gateway: {type: String, default: 'gateway.sandbox.push.apple.com'}, + port: {type: Number, default: 2195} + }}, - feedbackOptions: {type: { - gateway: {type: String, default: 'feedback.sandbox.push.apple.com'}, - port: {type: Number, default: 2196}, - batchFeedback: Boolean, - interval: Number - }} + feedbackOptions: {type: { + gateway: {type: String, default: 'feedback.sandbox.push.apple.com'}, + port: {type: Number, default: 2196}, + batchFeedback: Boolean, + interval: Number + }} }; var GcmSettingsSchema = { - serverApiKey: String + serverApiKey: String }; // Push notification settings var PushNotificationSettingSchema = { - apns: APNSSettingSchema, - gcm: GcmSettingsSchema + apns: APNSSettingSchema, + gcm: GcmSettingsSchema }; /** * Data model for Application */ var ApplicationSchema = { - id: {type: String, id: true, generated: true}, - // Basic information - name: {type: String, required: true}, // The name - description: String, // The description - icon: String, // The icon image url + id: {type: String, id: true, generated: true}, + // Basic information + name: {type: String, required: true}, // The name + description: String, // The description + icon: String, // The icon image url - owner: String, // The user id of the developer who registers the application - collaborators: [String], // A list of users ids who have permissions to work on this app + owner: String, // The user id of the developer who registers the application + collaborators: [String], // A list of users ids who have permissions to work on this app - // EMail - email: String, // e-mail address - emailVerified: Boolean, // Is the e-mail verified + // EMail + email: String, // e-mail address + emailVerified: Boolean, // Is the e-mail verified - // oAuth 2.0 settings - url: String, // The application url - callbackUrls: [String], // oAuth 2.0 code/token callback url - permissions: [String], // A list of permissions required by the application + // oAuth 2.0 settings + url: String, // The application url + callbackUrls: [String], // oAuth 2.0 code/token callback url + permissions: [String], // A list of permissions required by the application - // Keys - clientKey: String, - javaScriptKey: String, - restApiKey: String, - windowsKey: String, - masterKey: String, + // Keys + clientKey: String, + javaScriptKey: String, + restApiKey: String, + windowsKey: String, + masterKey: String, - // Push notification - pushSettings: PushNotificationSettingSchema, + // Push notification + pushSettings: PushNotificationSettingSchema, - // User Authentication - authenticationEnabled: {type: Boolean, default: true}, - anonymousAllowed: {type: Boolean, default: true}, - authenticationSchemes: [AuthenticationSchemeSchema], + // User Authentication + authenticationEnabled: {type: Boolean, default: true}, + anonymousAllowed: {type: Boolean, default: true}, + authenticationSchemes: [AuthenticationSchemeSchema], - status: {type: String, default: 'sandbox'}, // Status of the application, production/sandbox/disabled + status: {type: String, default: 'sandbox'}, // Status of the application, production/sandbox/disabled - // Timestamps - created: {type: Date, default: Date}, - modified: {type: Date, default: Date} + // Timestamps + created: {type: Date, default: Date}, + modified: {type: Date, default: Date} }; - /** * Application management functions */ @@ -88,13 +87,13 @@ var ApplicationSchema = { var crypto = require('crypto'); function generateKey(hmacKey, algorithm, encoding) { - hmacKey = hmacKey || 'loopback'; - algorithm = algorithm || 'sha256'; - encoding = encoding || 'base64'; - var hmac = crypto.createHmac(algorithm, hmacKey); - var buf = crypto.randomBytes(64); - hmac.update(buf); - return hmac.digest('base64'); + hmacKey = hmacKey || 'loopback'; + algorithm = algorithm || 'sha256'; + encoding = encoding || 'base64'; + var hmac = crypto.createHmac(algorithm, hmacKey); + var buf = crypto.randomBytes(64); + hmac.update(buf); + return hmac.digest('base64'); } var Application = loopback.createModel('Application', ApplicationSchema); @@ -104,15 +103,15 @@ var Application = loopback.createModel('Application', ApplicationSchema); * @param next */ Application.beforeCreate = function (next) { - var app = this; - app.created = app.modified = new Date(); - app.id = generateKey('id', 'sha1'); - app.clientKey = generateKey('client'); - app.javaScriptKey = generateKey('javaScript'); - app.restApiKey = generateKey('restApi'); - app.windowsKey = generateKey('windows'); - app.masterKey = generateKey('master'); - next(); + var app = this; + app.created = app.modified = new Date(); + app.id = generateKey('id', 'sha1'); + app.clientKey = generateKey('client'); + app.javaScriptKey = generateKey('javaScript'); + app.restApiKey = generateKey('restApi'); + app.windowsKey = generateKey('windows'); + app.masterKey = generateKey('master'); + next(); }; /** @@ -123,34 +122,34 @@ Application.beforeCreate = function (next) { * @param cb Callback function */ Application.register = function (owner, name, options, cb) { - assert(owner, 'owner is required'); - assert(name, 'name is required'); + assert(owner, 'owner is required'); + assert(name, 'name is required'); - if(typeof options === 'function' && !cb) { - cb = options; - options = {}; + if (typeof options === 'function' && !cb) { + cb = options; + options = {}; + } + var props = {owner: owner, name: name}; + for (var p in options) { + if (!(p in props)) { + props[p] = options[p]; } - var props = {owner: owner, name: name}; - for(var p in options) { - if(!(p in props)) { - props[p] = options[p]; - } - } - Application.create(props, cb); + } + Application.create(props, cb); }; /** * Reset keys for the application instance * @param cb */ -Application.prototype.resetKeys = function(cb) { - this.clientKey = generateKey('client'); - this.javaScriptKey = generateKey('javaScript'); - this.restApiKey = generateKey('restApi'); - this.windowsKey = generateKey('windows'); - this.masterKey = generateKey('master'); - this.modified = new Date(); - this.save(cb); +Application.prototype.resetKeys = function (cb) { + this.clientKey = generateKey('client'); + this.javaScriptKey = generateKey('javaScript'); + this.restApiKey = generateKey('restApi'); + this.windowsKey = generateKey('windows'); + this.masterKey = generateKey('master'); + this.modified = new Date(); + this.save(cb); }; /** @@ -158,14 +157,14 @@ Application.prototype.resetKeys = function(cb) { * @param appId * @param cb */ -Application.resetKeys = function(appId, cb) { - Application.findById(appId, function(err, app) { - if(err) { - cb && cb(err, app); - return; - } - app.resetKeys(cb); - }); +Application.resetKeys = function (appId, cb) { + Application.findById(appId, function (err, app) { + if (err) { + cb && cb(err, app); + return; + } + app.resetKeys(cb); + }); }; /** @@ -174,20 +173,20 @@ Application.resetKeys = function(appId, cb) { * @param key * @param cb */ -Application.authenticate = function(appId, key, cb) { - Application.findById(appId, function(err, app) { - if(err || !app) { - cb && cb(err, null); - return; - } - var matched = null; - ['clientKey', 'javaScriptKey', 'restApiKey', 'windowsKey', 'masterKey'].forEach(function(k) { - if(app[k] === key) { - matched = k; - } - }); - cb && cb(null, matched); +Application.authenticate = function (appId, key, cb) { + Application.findById(appId, function (err, app) { + if (err || !app) { + cb && cb(err, null); + return; + } + var matched = null; + ['clientKey', 'javaScriptKey', 'restApiKey', 'windowsKey', 'masterKey'].forEach(function (k) { + if (app[k] === key) { + matched = k; + } }); + cb && cb(null, matched); + }); }; module.exports = Application; diff --git a/test/model.application.test.js b/test/model.application.test.js index b249f6b1..2401429c 100644 --- a/test/model.application.test.js +++ b/test/model.application.test.js @@ -3,165 +3,164 @@ var assert = require('assert'); var Application = loopback.Application; describe('Application', function () { - var registeredApp = null; + var registeredApp = null; - it('Create a new application', function (done) { - Application.create({owner: 'rfeng', name: 'MyApp1', - description: 'My first mobile application'}, function (err, result) { - var app = result; - assert.equal(app.owner, 'rfeng'); - assert.equal(app.name, 'MyApp1'); - assert.equal(app.description, 'My first mobile application'); - assert(app.clientKey); - assert(app.javaScriptKey); - assert(app.restApiKey); - assert(app.windowsKey); - assert(app.masterKey); - assert(app.created); - assert(app.modified); - done(err, result); - }); + it('Create a new application', function (done) { + Application.create({owner: 'rfeng', name: 'MyApp1', + description: 'My first mobile application'}, function (err, result) { + var app = result; + assert.equal(app.owner, 'rfeng'); + assert.equal(app.name, 'MyApp1'); + assert.equal(app.description, 'My first mobile application'); + assert(app.clientKey); + assert(app.javaScriptKey); + assert(app.restApiKey); + assert(app.windowsKey); + assert(app.masterKey); + assert(app.created); + assert(app.modified); + done(err, result); }); + }); - it('Create a new application', function (done) { - Application.create({owner: 'rfeng', name: 'MyAppWithPush', - description: 'My push mobile application', - pushSettings: { - apns: { - production: false, - certData: 'cert', - keyData: 'key', - pushOptions: { - }, - feedbackOptions: { - interval: 300, - batchFeedback: true - } - }, - gcm: { - serverApiKey: 'serverKey' - } - }}, - function (err, result) { - var app = result; - assert.equal(app.owner, 'rfeng'); - assert.equal(app.name, 'MyAppWithPush'); - assert.equal(app.description, 'My push mobile application'); - assert.deepEqual(app.pushSettings.toObject(), { - apns: { - production: false, - certData: 'cert', - keyData: 'key', - pushOptions: { - gateway: 'gateway.sandbox.push.apple.com', - port: 2195 - }, - feedbackOptions: { - gateway: 'feedback.sandbox.push.apple.com', - port: 2196, - interval: 300, - batchFeedback: true - } + it('Create a new application', function (done) { + Application.create({owner: 'rfeng', name: 'MyAppWithPush', + description: 'My push mobile application', + pushSettings: { + apns: { + production: false, + certData: 'cert', + keyData: 'key', + pushOptions: { }, - gcm: { - serverApiKey: 'serverKey' + feedbackOptions: { + interval: 300, + batchFeedback: true } - }); - done(err, result); + }, + gcm: { + serverApiKey: 'serverKey' + } + }}, + function (err, result) { + var app = result; + assert.equal(app.owner, 'rfeng'); + assert.equal(app.name, 'MyAppWithPush'); + assert.equal(app.description, 'My push mobile application'); + assert.deepEqual(app.pushSettings.toObject(), { + apns: { + production: false, + certData: 'cert', + keyData: 'key', + pushOptions: { + gateway: 'gateway.sandbox.push.apple.com', + port: 2195 + }, + feedbackOptions: { + gateway: 'feedback.sandbox.push.apple.com', + port: 2196, + interval: 300, + batchFeedback: true + } + }, + gcm: { + serverApiKey: 'serverKey' + } }); + done(err, result); + }); + }); + + beforeEach(function (done) { + Application.register('rfeng', 'MyApp2', + {description: 'My second mobile application'}, function (err, result) { + var app = result; + assert.equal(app.owner, 'rfeng'); + assert.equal(app.name, 'MyApp2'); + assert.equal(app.description, 'My second mobile application'); + assert(app.clientKey); + assert(app.javaScriptKey); + assert(app.restApiKey); + assert(app.windowsKey); + assert(app.masterKey); + assert(app.created); + assert(app.modified); + registeredApp = app; + done(err, result); + }); + }); + + it('Reset keys', function (done) { + Application.resetKeys(registeredApp.id, function (err, result) { + var app = result; + assert.equal(app.owner, 'rfeng'); + assert.equal(app.name, 'MyApp2'); + assert.equal(app.description, 'My second mobile application'); + assert(app.clientKey); + assert(app.javaScriptKey); + assert(app.restApiKey); + assert(app.windowsKey); + assert(app.masterKey); + + assert(app.clientKey !== registeredApp.clientKey); + assert(app.javaScriptKey !== registeredApp.javaScriptKey); + assert(app.restApiKey !== registeredApp.restApiKey); + assert(app.windowsKey !== registeredApp.windowsKey); + assert(app.masterKey !== registeredApp.masterKey); + + assert(app.created); + assert(app.modified); + registeredApp = app; + done(err, result); }); + }); - beforeEach(function (done) { - Application.register('rfeng', 'MyApp2', - {description: 'My second mobile application'}, function (err, result) { - var app = result; - assert.equal(app.owner, 'rfeng'); - assert.equal(app.name, 'MyApp2'); - assert.equal(app.description, 'My second mobile application'); - assert(app.clientKey); - assert(app.javaScriptKey); - assert(app.restApiKey); - assert(app.windowsKey); - assert(app.masterKey); - assert(app.created); - assert(app.modified); - registeredApp = app; - done(err, result); - }); - }); + it('Authenticate with application id & clientKey', function (done) { + Application.authenticate(registeredApp.id, registeredApp.clientKey, + function (err, result) { + assert.equal(result, 'clientKey'); + done(err, result); + }); + }); - it('Reset keys', function (done) { - Application.resetKeys(registeredApp.id, function (err, result) { - var app = result; - assert.equal(app.owner, 'rfeng'); - assert.equal(app.name, 'MyApp2'); - assert.equal(app.description, 'My second mobile application'); - assert(app.clientKey); - assert(app.javaScriptKey); - assert(app.restApiKey); - assert(app.windowsKey); - assert(app.masterKey); + it('Authenticate with application id & javaScriptKey', function (done) { + Application.authenticate(registeredApp.id, registeredApp.javaScriptKey, + function (err, result) { + assert.equal(result, 'javaScriptKey'); + done(err, result); + }); + }); - assert(app.clientKey !== registeredApp.clientKey); - assert(app.javaScriptKey !== registeredApp.javaScriptKey); - assert(app.restApiKey !== registeredApp.restApiKey); - assert(app.windowsKey !== registeredApp.windowsKey); - assert(app.masterKey !== registeredApp.masterKey); + it('Authenticate with application id & restApiKey', function (done) { + Application.authenticate(registeredApp.id, registeredApp.restApiKey, + function (err, result) { + assert.equal(result, 'restApiKey'); + done(err, result); + }); + }); - assert(app.created); - assert(app.modified); - registeredApp = app; - done(err, result); - }); - }); + it('Authenticate with application id & masterKey', function (done) { + Application.authenticate(registeredApp.id, registeredApp.masterKey, + function (err, result) { + assert.equal(result, 'masterKey'); + done(err, result); + }); + }); - it('Authenticate with application id & clientKey', function (done) { - Application.authenticate(registeredApp.id, registeredApp.clientKey, - function (err, result) { - assert.equal(result, 'clientKey'); - done(err, result); - }); - }); + it('Authenticate with application id & windowsKey', function (done) { + Application.authenticate(registeredApp.id, registeredApp.windowsKey, + function (err, result) { + assert.equal(result, 'windowsKey'); + done(err, result); + }); + }); - it('Authenticate with application id & javaScriptKey', function (done) { - Application.authenticate(registeredApp.id, registeredApp.javaScriptKey, - function (err, result) { - assert.equal(result, 'javaScriptKey'); - done(err, result); - }); - }); - - it('Authenticate with application id & restApiKey', function (done) { - Application.authenticate(registeredApp.id, registeredApp.restApiKey, - function (err, result) { - assert.equal(result, 'restApiKey'); - done(err, result); - }); - }); - - it('Authenticate with application id & masterKey', function (done) { - Application.authenticate(registeredApp.id, registeredApp.masterKey, - function (err, result) { - assert.equal(result, 'masterKey'); - done(err, result); - }); - }); - - - it('Authenticate with application id & windowsKey', function (done) { - Application.authenticate(registeredApp.id, registeredApp.windowsKey, - function (err, result) { - assert.equal(result, 'windowsKey'); - done(err, result); - }); - }); - - it('Fail to authenticate with application id & invalid key', function (done) { - Application.authenticate(registeredApp.id, 'invalid-key', - function (err, result) { - assert(!result); - done(err, result); - }); - }); + it('Fail to authenticate with application id & invalid key', function (done) { + Application.authenticate(registeredApp.id, 'invalid-key', + function (err, result) { + assert(!result); + done(err, result); + }); + }); });