Repository: auth0/node-auth0
Pull request #27 (2015-04-09 22:49:07)

Regions


Patch:
diff --git a/README.md b/README.md
index d59d8af71..1cffb2892 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,18 @@ Initialize your client class with an API v2 token (you can generate one [here](h
 
 ~~~js
 var token = '{YOUR_API_V2_TOKEN}';
-var auth0 = require('auth0')(token);
+var auth0 = require('auth0')({
+  token: token
+});
+~~~
+
+By default the code assumes your account is running in the US West region. If you are running in Europe you can specify:
+~~~js
+var token = '{YOUR_API_V2_TOKEN}';
+var auth0 = require('auth0')({
+  token: token,
+  region: 'eu'
+});
 ~~~
 
 ### User's metadata
diff --git a/package.json b/package.json
index 228f9844c..75ea3023a 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "auth0",
-  "version": "2.0.0-alpha.0",
+  "version": "2.0.0-alpha.1",
   "description": "node.js SDK for Auth0 API v2",
   "main": "index.js",
   "scripts": {
@@ -21,9 +21,10 @@
   },
   "homepage": "https://github.com/auth0/node-auth0",
   "dependencies": {
-    "url-join": "0.0.1",
+    "bluebird": "^2.9.24",
+    "mocha": "^2.2.4",
     "superagent": "^1.1.0",
-    "bluebird": "^2.9.24"
+    "url-join": "0.0.1"
   },
   "devDependencies": {
     "mocha": "^2.2.1",
diff --git a/src/apiError.js b/src/apiError.js
deleted file mode 100644
index 27cb587ce..000000000
--- a/src/apiError.js
+++ /dev/null
@@ -1,11 +0,0 @@
-var util = require('util');
-
-var ApiError = function(statusCode, error, message){
-  this.error = error;
-  this.message = message;
-  this.statusCode = statusCode;
-};
-
-util.inherits(ApiError, Error);
-
-module.exports = ApiError;
\ No newline at end of file
diff --git a/src/client.js b/src/client.js
index c0ff67059..be1e4b413 100644
--- a/src/client.js
+++ b/src/client.js
@@ -1,9 +1,28 @@
 var User = require('./user');
-var BASE_URL = 'https://login.auth0.com/api/v2';
+var util = require('util');
+var DEFAULT_REGION = 'us';
+var BASE_URL_FORMAT = 'https://%s/api/v2';
+var ArgumentError = require('./errors').ArgumentError;
 
-function Client(accessToken){
-  this.accessToken = accessToken;
-  this.baseUrl = BASE_URL;
+var REGIONS_TO_DOMAINS = {
+  us: 'login.auth0.com',
+  eu: 'login.eu.auth0.com'
+};
+
+function Client(options){
+  if (!options.token){
+    throw new ArgumentError('Missing token');
+  }
+
+  var domain = REGIONS_TO_DOMAINS[options.region || DEFAULT_REGION];
+
+  if (!domain){
+    throw new ArgumentError('The region is invalid, valid values are any of: "' +
+      Object.keys(REGIONS_TO_DOMAINS).join('","') + '"');
+  }
+
+  this.accessToken = options.token;
+  this.baseUrl = util.format(BASE_URL_FORMAT, domain);
 }
 
 Client.prototype.user = function(id){
diff --git a/src/errors.js b/src/errors.js
new file mode 100644
index 000000000..97162e018
--- /dev/null
+++ b/src/errors.js
@@ -0,0 +1,21 @@
+var util = require('util');
+
+var ApiError = function(statusCode, error, message){
+  this.error = error;
+  this.message = message;
+  this.statusCode = statusCode;
+};
+
+util.inherits(ApiError, Error);
+
+var ArgumentError = function(message){
+  this.message = message;
+};
+
+util.inherits(ApiError, Error);
+util.inherits(ArgumentError, Error);
+
+module.exports = {
+  ApiError: ApiError,
+  ArgumentError: ArgumentError
+};
\ No newline at end of file
diff --git a/src/utils.js b/src/utils.js
index a9910ca9c..b7a16e278 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -1,4 +1,4 @@
-var ApiError = require('./apiError');
+var ApiError = require('./errors').ApiError;
 
 var utils = module.exports = {};
 


Test patch:
diff --git a/test/appMetadata.tests.js b/test/appMetadata.tests.js
index fde3ed910..7096bb22a 100644
--- a/test/appMetadata.tests.js
+++ b/test/appMetadata.tests.js
@@ -4,11 +4,11 @@ var util = require('util');
 var constants = require('./constants');
 
 var TOKEN = 'token';
-var auth0 = require('..')(TOKEN);
+var auth0 = require('..')({ token: TOKEN });
 
 describe('app metadata', function(){
   describe('update', function(){
-    var user_id = 'google-oauth|1234';
+    var user_id = 'google-oauth%7C1234';
     var url = util.format(constants.USER_SUB_ROUTE, user_id);
     var update = {
       roles: ['reader', 'writer'],
diff --git a/test/constants.js b/test/constants.js
index d8cdc4604..f51369f11 100644
--- a/test/constants.js
+++ b/test/constants.js
@@ -1,4 +1,5 @@
 var constants = module.exports = {};
 
 constants.BASE_API_URL = 'https://login.auth0.com/api/v2/';
+constants.EU_BASE_API_URL = 'https://login.eu.auth0.com/api/v2/';
 constants.USER_SUB_ROUTE = '/users/%s';
\ No newline at end of file
diff --git a/test/errors.tests.js b/test/errors.tests.js
new file mode 100644
index 000000000..0202ea814
--- /dev/null
+++ b/test/errors.tests.js
@@ -0,0 +1,16 @@
+var expect = require('chai').expect;
+
+var Auth0 = require('..');
+var ArgumentError = require('../src/errors').ArgumentError;
+
+describe('errors', function(){
+  it('should fail if region is invalid', function(){
+    expect(Auth0.bind(null, { token: 'token', region: 'sa' }))
+      .to.throw(ArgumentError, 'The region is invalid, valid values are any of: "us","eu"');
+  });
+
+  it('should fail if token is not provided', function(){
+    expect(Auth0.bind(null, { token: '' }))
+      .to.throw(ArgumentError, 'Missing token');
+  });
+});
\ No newline at end of file
diff --git a/test/regions.tests.js b/test/regions.tests.js
new file mode 100644
index 000000000..bd4462389
--- /dev/null
+++ b/test/regions.tests.js
@@ -0,0 +1,51 @@
+var expect = require('chai').expect;
+var nock = require('nock');
+var util = require('util');
+var constants = require('./constants');
+
+var TOKEN = 'token';
+var auth0 = require('..')({
+  token: TOKEN,
+  region: 'eu'
+});
+
+describe('regions', function(){
+  var user_id = 'google-oauth%7C1234';
+  var url = util.format(constants.USER_SUB_ROUTE, user_id);
+  var update = {
+    roles: ['reader', 'writer'],
+    permissions: null
+  };
+
+  var body = {
+    app_metadata: update
+  };
+
+  var localNock;
+
+  before(function(){
+    localNock = nock(constants.EU_BASE_API_URL, {
+      reqheaders: {
+        'Authorization': 'Bearer ' + TOKEN
+      }
+    })
+    .patch(url, body)
+    .reply(401, {
+      'statusCode': 401,
+      'error': 'Unauthorized',
+      'message': 'Invalid token'
+    });
+  });
+
+  after(nock.cleanAll);
+
+  it ('should perform request against correct domain', function(){
+    return auth0.user(user_id).appMetadata.update(update).catch(function(err){
+      expect(err).to.not.be.undefined;
+      expect(err.statusCode).to.equal(401);
+      expect(err.error).to.equal('Unauthorized');
+      expect(err.message).to.equal('Invalid token');
+      localNock.done();
+    });
+  });
+});
\ No newline at end of file
diff --git a/test/userMetadata.tests.js b/test/userMetadata.tests.js
index 80934c26d..221ef382c 100644
--- a/test/userMetadata.tests.js
+++ b/test/userMetadata.tests.js
@@ -4,11 +4,11 @@ var util = require('util');
 var constants = require('./constants');
 
 var TOKEN = 'token';
-var auth0 = require('..')(TOKEN);
+var auth0 = require('..')({ token: TOKEN });
 
 describe('user metadata', function(){
   describe('update', function(){
-    var user_id = 'google-oauth|1234';
+    var user_id = 'google-oauth%7C1234';
     var url = util.format(constants.USER_SUB_ROUTE, user_id);
     var update = {
       hobby: 'surf',