text: /*
   * Copyright 2013 Apereo Foundation (AF) Licensed under the
   * Educational Community License, Version 2.0 (the "License"); you may
   * not use this file except in compliance with the License. You may
   * obtain a copy of the License at
   *
   *     http://opensource.org/licenses/ECL-2.0
   *
   * Unless required by applicable law or agreed to in writing,
   * software distributed under the License is distributed on an "AS IS"
   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
   * or implied. See the License for the specific language governing
   * permissions and limitations under the License.
   */
  var _ = require('underscore');
  var assert = require('assert');
  
  var RestAPI = require('oae-rest');
  var RestContext = require('oae-rest/lib/model').RestContext;
  var TestsUtil = require('oae-tests');
  
  var TenantsAPI = require('oae-tenants');
  
  
  describe('Tenants', function() {
  
      // Rest context that can be used every time we need to make a request as an anonymous user
      var anonymousCamRestContext = null;
      // Rest context that can be used for anonymous requests on the global tenant
      var anonymousGlobalRestContext = null;
      // Rest context that can be used every time we need to make a request as a Cambridge tenant admin
      var camAdminRestContext = null;
      // Rest context that can be used every time we need to use a request as a global admin
      var globalAdminRestContext = null;
  
      /**
       * Function that will fill up the anonymous and the tenant admin context
       */
      before(function(callback) {
          // Fill up anonymous rest context
          anonymousCamRestContext = TestsUtil.createTenantRestContext(global.oaeTests.tenants.cam.host);
          // Fill up the anonymous global rest context
          anonymousGlobalRestContext = TestsUtil.createGlobalRestContext();
          // Fill up Cam tenant admin rest context
          camAdminRestContext = TestsUtil.createTenantAdminRestContext(global.oaeTests.tenants.cam.host);
          // Fill up the global admin rest context
          globalAdminRestContext = TestsUtil.createGlobalAdminRestContext();
          callback();
      });
  
  
      describe('Get tenant', function() {
  
          /**
           * Test that verifies that all tenants can be retrieved
           */
          it('verify get all tenants', function(callback) {
              // Get all tenants, check that there are 2
              RestAPI.Tenants.getTenants(globalAdminRestContext, function(err, tenants) {
                  assert.ok(!err);
                  assert.ok(tenants);
                  assert.ok(tenants['camtest']);
                  assert.equal(tenants['camtest'].host, 'cambridge.oae.com');
                  assert.ok(tenants['gttest']);
                  assert.equal(tenants['gttest'].host, 'gt.oae.com');
  
                  // Create a new tenant
                  RestAPI.Tenants.createTenant(globalAdminRestContext, 'oxfordtest', 'Oxford University', 'oxford.oae.com', function(err) {
                      assert.ok(!err);
                      // Get all tenants, check that there are 3
                      RestAPI.Tenants.getTenants(globalAdminRestContext, function(err, tenants) {
                          assert.ok(tenants);
                          assert.ok(tenants['camtest']);
                          assert.equal(tenants['camtest'].host, 'cambridge.oae.com');
                          assert.ok(tenants['gttest']);
                          assert.equal(tenants['gttest'].host, 'gt.oae.com');
                          assert.equal(tenants['camtest'].host, 'cambridge.oae.com');
                          assert.ok(tenants['oxfordtest']);
                          assert.equal(tenants['oxfordtest'].host, 'oxford.oae.com');
                          // Verify that the global admin tenant is not included
                          assert.ok(!tenants['admin']);
  
                          // Delete the previously created tenant
                          RestAPI.Tenants.deleteTenant(globalAdminRestContext, 'oxfordtest', function(err) {
                              assert.ok(!err);
                              RestAPI.Tenants.getTenants(globalAdminRestContext, function(err, tenants) {
                                  assert.ok(!err);
                                  assert.ok(tenants);
                                  assert.ok(!tenants['oxfordtest']);
                                  assert.ok(tenants['camtest']);
                                  assert.equal(tenants['camtest'].host, 'cambridge.oae.com');
                                  assert.ok(tenants['gttest']);
                                  assert.equal(tenants['gttest'].host, 'gt.oae.com');
                                  callback();
                              });
                          });
                      });
                  });
              });
          });
  
          /**
           * Test that verifies that the current tenant's information can be retrieved
           */
          it('verify get tenant', function(callback) {
              RestAPI.Tenants.getTenant(anonymousCamRestContext, null, function(err, tenant) {
                  assert.ok(!err);
                  assert.ok(tenant);
                  assert.equal(tenant.alias, 'camtest');
                  assert.equal(tenant.host, 'cambridge.oae.com');
  
                  // Verify that the tenant information is available through the global tenant
                  RestAPI.Tenants.getTenant(globalAdminRestContext, 'camtest', function(err, tenant) {
                      assert.ok(!err);
                      assert.ok(tenant);
                      assert.equal(tenant.alias, 'camtest');
                      assert.equal(tenant.host, 'cambridge.oae.com');
  
                      // Get the tenant by host name
                      var tenantByHost = TenantsAPI.getTenantByHost('cambridge.oae.com');
                      assert.ok(tenantByHost);
                      assert.equal(tenantByHost.alias, 'camtest');
                      assert.equal(tenantByHost.host, 'cambridge.oae.com');
                      callback();
                  });
              });
          });
  
          /**
           *  Test that verifies that getting the global tenant succeeds
           */
          it('verify get global tenant', function(callback) {
              RestAPI.Tenants.getTenant(globalAdminRestContext, null, function(err, tenant) {
                  assert.ok(!err);
                  assert.ok(tenant);
                  assert.equal(tenant.isGlobalAdminServer, true);
                  assert.equal(tenant.alias, 'admin');
  
                  // Get the global admin tenant by host name
                  var globalAdminTenant = TenantsAPI.getTenantByHost('localhost:2000');
                  assert.ok(globalAdminTenant);
                  assert.equal(globalAdminTenant.isGlobalAdminServer, true);
                  assert.equal(globalAdminTenant.alias, 'admin');
                  callback();
              });
          });
  
          /**
           * Test that verifies that getting the tenant information through the global server requires a valid alias
           */
          it('verify get tenant validation', function(callback) {
              RestAPI.Tenants.getTenant(globalAdminRestContext, ' ', function(err, tenant) {
                  assert.ok(err);
                  assert.equal(err.code, 404);
                  callback();
              });
          });
  
          /**
           * Test that verifies that a tenant can be retrieved by its mapped alias through the internal API.
           */
          it('verify get tenant by alias', function(callback) {
              // Get the Cambridge tenant
              var tenant = TenantsAPI.getTenant('camtest');
              assert.ok(tenant);
              assert.equal(tenant.alias, 'camtest');
              assert.equal(tenant.displayName, 'Cambridge University Test');
              assert.equal(tenant.host, 'cambridge.oae.com');
  
              // Get the GT tenant
              tenant = TenantsAPI.getTenant('gttest');
              assert.ok(tenant);
              assert.equal(tenant.alias, 'gttest');
              assert.equal(tenant.displayName, 'Georgia Tech Test');
              assert.equal(tenant.host, 'gt.oae.com');
  
              // Get the global admin tenant
              tenant = TenantsAPI.getTenant('admin');
              assert.ok(tenant);
              assert.equal(tenant.alias, 'admin');
              assert.equal(tenant.displayName, 'Global admin server');
              assert.equal(tenant.host, 'localhost:2000');
              assert.equal(tenant.isGlobalAdminServer, true);
  
              // Get non-existing tenant
              tenant = TenantsAPI.getTenant('non-existing');
              assert.ok(!tenant);
              callback();
          });
  
          /**
           * Test that verifies that a tenant can be retrieved by its mapped host name. This uses the internal
           * API as there is no REST feed available that offers this functionality.
           */
          it('verify get tenant by host', function(callback) {
              // Get the Cambridge tenant
              var tenant = TenantsAPI.getTenantByHost('cambridge.oae.com');
              assert.ok(tenant);
              assert.equal(tenant.alias, 'camtest');
              assert.equal(tenant.displayName, 'Cambridge University Test');
              assert.equal(tenant.host, 'cambridge.oae.com');
  
              // Get the GT tenant
              tenant = TenantsAPI.getTenantByHost('gt.oae.com');
              assert.ok(tenant);
              assert.equal(tenant.alias, 'gttest');
              assert.equal(tenant.displayName, 'Georgia Tech Test');
              assert.equal(tenant.host, 'gt.oae.com');
  
              // Get the global admin tenant
              tenant = TenantsAPI.getTenantByHost('localhost:2000');
              assert.ok(tenant);
              assert.equal(tenant.alias, 'admin');
              assert.equal(tenant.displayName, 'Global admin server');
              assert.equal(tenant.host, 'localhost:2000');
              assert.equal(tenant.isGlobalAdminServer, true);
  
              // Get non-existing tenant
              tenant = TenantsAPI.getTenantByHost('nonexisting.oae.com');
              assert.ok(!tenant);
              callback();
          });
      });
  
  
      describe('Tenant actions', function() {
  
          /**
           * Test that verifies that a tenant can not be deleted by an anonymous user
           */
          it('verify create tenant as anonymous user fails', function(callback) {
              // Try to create a tenant as an anonymous user
              RestAPI.Tenants.createTenant(anonymousGlobalRestContext, 'nyutest', 'New York University', 'nyu.oae.com', function(err) {
                  assert.ok(err);
                  assert.equal(err.code, 401);
                  callback();
              });
          });
  
          /**
           * Test that verifies that it is possible to create a new tenant
           */
          it('verify create tenant', function(callback) {
              RestAPI.Tenants.createTenant(globalAdminRestContext, 'nyutest', 'New York University', 'nyu.oae.com', function(err, tenant) {
                  assert.ok(!err);
                  assert.ok(tenant);
                  assert.equal(tenant.alias, 'nyutest');
                  assert.equal(tenant.host, 'nyu.oae.com');
  
                  // Get the tenant
                  var nyuRestContext = TestsUtil.createTenantRestContext('nyu.oae.com');
                  RestAPI.Tenants.getTenant(nyuRestContext, null, function(err, tenant) {
                      assert.ok(!err);
                      assert.ok(tenant);
                      assert.equal(tenant.alias, 'nyutest');
                      assert.equal(tenant.host, 'nyu.oae.com');
  
                      // Get the tenant by host
                      var nyuTenant = TenantsAPI.getTenantByHost('nyu.oae.com');
                      assert.ok(nyuTenant);
                      assert.equal(nyuTenant.alias, 'nyutest');
                      assert.equal(nyuTenant.host, 'nyu.oae.com');
                      callback();
                  });
              });
          });
  
          /**
           * Test that verifies that creating a tenant needs an alias, a displayName and a host specified
           */
          it('verify create tenant validation', function(callback) {
              // Try creating a tenant with no alias
              RestAPI.Tenants.createTenant(globalAdminRestContext, null, 'AAR', 'aar.oae.com', function(err) {
                  assert.ok(err);
  
                  // Try creating a tenant with an invalid alias, using a colon in the alias
                  RestAPI.Tenants.createTenant(globalAdminRestContext, 'American Academic of Religion', 'AAR', 'aar.oae.com', function(err) {
                      assert.ok(err);
  
                      // Try creating a tenant with an invalid alias, using a colon in the alias
                      RestAPI.Tenants.createTenant(globalAdminRestContext, 'aar:test', 'AAR', 'aar.oae.com', function(err) {
                          assert.ok(err);
  
                          // Try creating a tenant with no displayName
                          RestAPI.Tenants.createTenant(globalAdminRestContext, 'aartest', null, 'aar.oae.com', function(err) {
                              assert.ok(err);
  
                              // Try creating a tenant with no base URL
                              RestAPI.Tenants.createTenant(globalAdminRestContext, 'aartest', 'AAR', null, function(err) {
                                  assert.ok(err);
  
                                  // Verify that the tenant does not exist
                                  var aarRestContext = TestsUtil.createTenantRestContext('aar.oae.com');
  
                                  RestAPI.Tenants.getTenant(aarRestContext, null, function(err, tenant) {
                                      assert.ok(err);
                                      assert.ok(!tenant);
                                      callback();
                                  });
                              });
                          });
                      });
                  });
              });
          });
  
          /**
           * Test that verifies that a tenant cannot be created with a duplicate alias
           */
          it('verify create tenant duplicate alias', function(callback) {
              RestAPI.Tenants.createTenant(globalAdminRestContext, 'camtest', 'AAR', 'camtest.oae.com', function(err) {
                  assert.ok(err);
  
                  // Verify that the existing tenant is still running
                  RestAPI.Tenants.getTenant(anonymousCamRestContext, null, function(err, tenant) {
                      assert.ok(!err);
                      assert.ok(tenant);
                      assert.equal(tenant.alias, 'camtest');
                      assert.equal(tenant.host, 'cambridge.oae.com');
                      callback();
                  });
              });
          });
  
          /**
           * Test that verifies that a tenant cannot be created with a duplicate host
           */
          it('verify create tenant duplicate host', function(callback) {
              RestAPI.Tenants.createTenant(globalAdminRestContext, 'angliaruskin', 'Anglia Ruskin University', 'cambridge.oae.com', function(err) {
                  assert.ok(err);
  
                  // Verify that the tenant with that hostname is still running
                  RestAPI.Tenants.getTenant(anonymousCamRestContext, null, function(err, tenant) {
                      assert.ok(!err);
                      assert.ok(tenant);
                      assert.equal(tenant.alias, 'camtest');
                      assert.equal(tenant.host, 'cambridge.oae.com');
                      callback();
                  });
              });
          });
  
          /**
           * Test that verifies that a tenant can be stopped
           */
          it('verify stop tenant', function(callback) {
              // Create a new tenant
              RestAPI.Tenants.createTenant(globalAdminRestContext, 'aartest', 'AAR', 'aar.oae.com', function(err, tenant) {
                  assert.ok(!err);
                  var aarRestContext = TestsUtil.createTenantRestContext('aar.oae.com');
  
                  // Verify that the tenant is running
                  RestAPI.Tenants.getTenant(aarRestContext, null, function(err, tenant) {
                      assert.ok(!err);
                      assert.ok(tenant);
                      assert.equal(tenant.alias, 'aartest');
  
                      // Verify it's in the list of running tenant aliases
                      assert.ok(TenantsAPI.getTenants(true)['aartest']);
  
                      // Stop the tenant
                      RestAPI.Tenants.stopTenant(globalAdminRestContext, 'aartest', function(err) {
                          assert.ok(!err);
  
                          // Verify that the tenant is no longer running
                          RestAPI.Tenants.getTenant(aarRestContext, null, function(err, tenant) {
                              assert.ok(err);
                              assert.equal(err.code, 503);
                              assert.ok(!tenant);
  
                              // Verify it is no longer in the list of running tenant aliases
                              assert.ok(!TenantsAPI.getTenants(true)['aartest']);
                              // Verify it is in the list of disabled tenant aliases
                              assert.ok(TenantsAPI.getTenants()['aartest']);
                              assert.equal(TenantsAPI.getTenants()['aartest'].active, false);
  
                              // Verify that it's still part of the all tenants feed
                              RestAPI.Tenants.getTenants(globalAdminRestContext, function(err, tenants) {
                                  assert.ok(!err);
                                  assert.ok(tenants);
                                  assert.ok(tenants['aartest']);
                                  assert.equal(tenants['aartest'].host, 'aar.oae.com');
                                  assert.equal(tenants['aartest'].active, false);
                                  callback();
                              });
                          });
                      });
                  });
              });
          });
  
          /**
           * Test that verifies that a tenant can not be stopped by an anonymous user
           */
          it('verify stop tenant as anonymous user fails', function(callback) {
              // Try to stop the tenant as an anonymous user
              RestAPI.Tenants.stopTenant(anonymousGlobalRestContext, 'aartest', function(err) {
                  assert.ok(err);
                  assert.equal(err.code, 401);
                  callback();
              });
          });
  
          /**
           * Test that verifes that a non-existing tenant cannot be stopped
           */
          it('verify stop non-existing tenant', function(callback) {
              // Stop tenant with no alias
              RestAPI.Tenants.stopTenant(globalAdminRestContext, null, function(err) {
                  assert.ok(err);
                  callback();
              });
          });
  
          /**
           * Test that verifies that a stopped tenant can be started
           */
          it('verify start tenant', function(callback) {
              // Re-start the tenant from the previous test
              RestAPI.Tenants.startTenant(globalAdminRestContext, 'aartest', function(err) {
                  assert.ok(!err);
  
                  // Verify that the tenant is running again
                  var aarRestContext = TestsUtil.createTenantRestContext('aar.oae.com');
                  RestAPI.Tenants.getTenant(aarRestContext, null, function(err, tenant) {
                      assert.ok(!err);
                      assert.ok(tenant);
                      assert.equal(tenant.alias, 'aartest');
                      assert.equal(tenant.host, 'aar.oae.com');
                      callback();
                  });
              });
          });
  
          /**
           * Test that verifies that a tenant can not be started by an anonymous user
           */
          it('verify start tenant as anonymous user fails', function(callback) {
              // Try to stop the tenant as an anonymous user
              RestAPI.Tenants.startTenant(anonymousGlobalRestContext, 'aartest', function(err) {
                  assert.ok(err);
                  assert.equal(err.code, 401);
                  callback();
              });
          });
  
          /**
           * Test that verifes that a non-existing tenant cannot be started
           */
          it('verify stop non-existing tenant', function(callback) {
              // Stop tenant with no alias
              RestAPI.Tenants.startTenant(globalAdminRestContext, null, function(err) {
                  assert.ok(err);
                  callback();
              });
          });
  
          /**
           * Test that verifies that a tenant can not be deleted by an anonymous user
           */
          it('verify delete tenant as anonymous user fails', function(callback) {
              // Try to stop the tenant as an anonymous user
              RestAPI.Tenants.deleteTenant(anonymousGlobalRestContext, 'aartest', function(err) {
                  assert.ok(err);
                  assert.equal(err.code, 401);
                  callback();
              });
          });
  
          /**
           * Test that verifies that a tenant can be deleted
           */
          it('verify delete tenant', function(callback) {
              // Try deleting a non-existing tenant
              RestAPI.Tenants.deleteTenant(globalAdminRestContext, null, function(err) {
                  assert.ok(err);
  
                  // Delete the tenant from the previous test
                  RestAPI.Tenants.deleteTenant(globalAdminRestContext, 'aartest', function(err) {
                      assert.ok(!err);
  
                      // Verify that the tenant is no longer running
                      var aarRestContext = TestsUtil.createTenantRestContext('aar.oae.com');
                      RestAPI.Tenants.getTenant(aarRestContext, null, function(err, tenant) {
                          assert.ok(err);
                          assert.ok(!tenant);
  
                          // Verify that it's no longer a part of the all tenants feed
                          RestAPI.Tenants.getTenants(globalAdminRestContext, function(err, tenants) {
                              assert.ok(!err);
                              assert.ok(tenants);
                              assert.ok(!tenants['aartest']);
                              callback();
                          });
                      });
                  });
              });
          });
  
          /**
           * Test that verifes that a non-existing tenant cannot be deleted
           */
          it('verify delete non-existing tenant', function(callback) {
              // Stop tenant with no alias
              RestAPI.Tenants.deleteTenant(globalAdminRestContext, null, function(err) {
                  assert.ok(err);
                  callback();
              });
          });
      });
  
      describe('Update tenant', function() {
  
          /**
           * Test that verifies that a tenant's displayName can not be updated by an anonymous user or non-admin user
           */
          it('verify update tenant as non-admin user', function(callback) {
              // Try to update the tenant's display name as an anonymous user on the global admin tenant
              RestAPI.Tenants.updateTenant(anonymousGlobalRestContext, 'camtest', {'displayName': 'Anglia Ruskin University'}, function(err) {
                  assert.ok(err);
                  assert.equal(err.code, 401);
  
                  // Try to update the tenant's host as an anonymous user on the global admin tenant
                  RestAPI.Tenants.updateTenant(anonymousGlobalRestContext, 'camtest', {'host': 'newcamtest.oae.com'}, function(err) {
                      assert.ok(err);
                      assert.equal(err.code, 401);
  
                      // Try to update tenant's display name and host as an anonymous user on the global admin tenant
                      RestAPI.Tenants.updateTenant(anonymousGlobalRestContext, 'camtest', {'displayName': 'Anglia Ruskin University', 'host': 'newcamtest.oae.com'}, function(err) {
                          assert.ok(err);
                          assert.equal(err.code, 401);
  
                          // Try to update the tenant's display name as an anonymous user on a user tenant
                          RestAPI.Tenants.updateTenant(anonymousCamRestContext, null, {'displayName': 'Anglia Ruskin University'}, function(err) {
                              assert.ok(err);
                              assert.equal(err.code, 401);
  
                              // Try to update the tenant's host as an anonymous user on a user tenant
                              RestAPI.Tenants.updateTenant(anonymousCamRestContext, null, {'host': 'newcamtest.oae.com'}, function(err) {
                                  assert.ok(err);
                                  assert.equal(err.code, 401);
  
                                  // Try to update tenant's display name and host as an anonymous user on a user tenant
                                  RestAPI.Tenants.updateTenant(anonymousCamRestContext, null, {'displayName': 'Anglia Ruskin University', 'host': 'newcamtest.oae.com'}, function(err) {
                                      assert.ok(err);
                                      assert.equal(err.code, 401);
  
                                      // Create a regular non-admin user
                                      var userId = TestsUtil.generateTestUserId('john');
                                      RestAPI.User.createUser(camAdminRestContext, userId, 'password', 'John Doe', null, function(err, createdUser) {
                                          johnCamRestContext = TestsUtil.createTenantRestContext(global.oaeTests.tenants.cam.host, userId, 'password');
  
                                          // Try to update the tenant's display name as a non-admin user on a user tenant
                                          RestAPI.Tenants.updateTenant(johnCamRestContext, null, {'displayName': 'Anglia Ruskin University'}, function(err) {
                                              assert.ok(err);
                                              assert.equal(err.code, 401);
  
                                              // Try to update the tenant's host as a non-admin user on a user tenant
                                              RestAPI.Tenants.updateTenant(johnCamRestContext, null, {'host': 'newcamtest.oae.com'}, function(err) {
                                                  assert.ok(err);
                                                  assert.equal(err.code, 401);
  
                                                  // Try to update tenant's display name and host as a non-admin useron a user tenant
                                                  RestAPI.Tenants.updateTenant(johnCamRestContext, null, {'displayName': 'Anglia Ruskin University', 'host': 'newcamtest.oae.com'}, function(err) {
                                                      assert.ok(err);
                                                      assert.equal(err.code, 401);
                                                      callback();
                                                  });
                                              });
                                          });
                                      });
                                  });
                              });
                          });
                      });
                  });
              });
          });
  
          /**
           * Test that verifies that a tenant can only be updated when at least 1 correct update value has been supplied
           */
          it('verify update tenant validation', function(callback) {
              // Verify through the global admin tenant
              RestAPI.Tenants.updateTenant(globalAdminRestContext, 'camtest', null, function(err) {
                  assert.ok(err);
                  assert.equal(err.code, 400);
                  // Verify update with an invalid property
                  RestAPI.Tenants.updateTenant(globalAdminRestContext, 'camtest', {'alias': 'foobar'}, function(err) {
                      assert.ok(err);
                      assert.equal(err.code, 400);
                      // Verify update with a valid and an invalid property
                      RestAPI.Tenants.updateTenant(globalAdminRestContext, 'camtest', {'alias': 'foobar', 'displayName': 'Anglia Ruskin University'}, function(err) {
                          assert.ok(err);
                          assert.equal(err.code, 400);
  
                          // Verify through a user tenant
                          RestAPI.Tenants.updateTenant(camAdminRestContext, null, null, function(err) {
                              assert.ok(err);
                              assert.equal(err.code, 400);
                              // Verify update with an invalid property
                              RestAPI.Tenants.updateTenant(camAdminRestContext, null, {'alias': 'foobar'}, function(err) {
                                  assert.ok(err);
                                  assert.equal(err.code, 400);
                                  // Verify update with a valid and an invalid property
                                  RestAPI.Tenants.updateTenant(camAdminRestContext, null, {'alias': 'foobar', 'displayName': 'Anglia Ruskin University'}, function(err) {
                                      assert.ok(err);
                                      assert.equal(err.code, 400);
                                      callback();
                                  });
                              });
                          });
                      });
                  });
              });
          });
  
          /**
           * Test that verifies that a tenant displayName can be updated
           */
          it('verify update tenant', function(callback) {
              // Update the tenant display name as the global admin
              RestAPI.Tenants.updateTenant(globalAdminRestContext, 'camtest', {'displayName': 'Anglia Ruskin University'}, function(err) {
                  assert.ok(!err);
  
                  // Check if the update was successful
                  RestAPI.Tenants.getTenant(camAdminRestContext, null, function(err, tenant) {
                      assert.ok(!err);
                      assert.ok(tenant);
                      assert.equal(tenant.alias, 'camtest');
                      assert.equal(tenant.host, 'cambridge.oae.com');
                      assert.equal(tenant.displayName, 'Anglia Ruskin University');
  
                      // Update the tenant display name as the tenant admin
                      RestAPI.Tenants.updateTenant(camAdminRestContext, null, {'displayName': 'Queens College'}, function(err) {
                          assert.ok(!err);
  
                          // Check if the update was successful
                          RestAPI.Tenants.getTenant(camAdminRestContext, null, function(err, tenant) {
                              assert.ok(!err);
                              assert.ok(tenant);
                              assert.equal(tenant.alias, 'camtest');
                              assert.equal(tenant.host, 'cambridge.oae.com');
                              assert.equal(tenant.displayName, 'Queens College');
  
                              // Update the tenant host as the global admin
                              RestAPI.Tenants.updateTenant(globalAdminRestContext, 'camtest', {'host': 'oxford.oae.com'}, function(err) {
                                  assert.ok(!err);
  
                                  // Check if the update was successful.
                                  // The old host name should no longer be accepting requests
                                  RestAPI.Tenants.getTenant(camAdminRestContext, null, function(err, tenant) {
                                      assert.ok(err);
                                      assert.equal(err.code, 418);
                                      // The new host name should now be responding to requests
                                      var oxfordCamAdminRestContext = TestsUtil.createTenantAdminRestContext('oxford.oae.com');
                                      RestAPI.Tenants.getTenant(oxfordCamAdminRestContext, null, function(err, tenant) {
                                          assert.ok(!err);
                                          assert.ok(tenant);
                                          assert.equal(tenant.alias, 'camtest');
                                          assert.equal(tenant.host, 'oxford.oae.com');
                                          assert.equal(tenant.displayName, 'Queens College');
  
                                          // Update the tenant host as the tenant admin
                                          RestAPI.Tenants.updateTenant(oxfordCamAdminRestContext, null, {'host': 'hull.oae.com'}, function(err) {
                                              assert.ok(!err);
  
                                              // Check if the update was successful.
                                              // The old host name should no longer be accepting requests
                                              RestAPI.Tenants.getTenant(oxfordCamAdminRestContext, null, function(err, tenant) {
                                                  assert.ok(err);
                                                  assert.equal(err.code, 418);
                                                  // The new host name should now be responding to requests
                                                  var hullCamAdminRestContext = TestsUtil.createTenantAdminRestContext('hull.oae.com');
                                                  RestAPI.Tenants.getTenant(hullCamAdminRestContext, null, function(err, tenant) {
                                                      assert.ok(!err);
                                                      assert.ok(tenant);
                                                      assert.equal(tenant.alias, 'camtest');
                                                      assert.equal(tenant.host, 'hull.oae.com');
                                                      assert.equal(tenant.displayName, 'Queens College');
  
                                                      // Update the tenant display name and host as the tenant admin
                                                      RestAPI.Tenants.updateTenant(hullCamAdminRestContext, null, {'displayName': 'Swansea University', 'host': 'swansea.oae.com'}, function(err) {
                                                          assert.ok(!err);
  
                                                          // Check if the update was successful.
                                                          // The old host name should no longer be accepting requests
                                                          RestAPI.Tenants.getTenant(hullCamAdminRestContext, null, function(err, tenant) {
                                                              assert.ok(err);
                                                              assert.equal(err.code, 418);
                                                              // The new host name should now be responding to requests
                                                              var swanseaCamAdminRestContext = TestsUtil.createTenantAdminRestContext('swansea.oae.com');
                                                              RestAPI.Tenants.getTenant(swanseaCamAdminRestContext, null, function(err, tenant) {
                                                                  assert.ok(!err);
                                                                  assert.ok(tenant);
                                                                  assert.equal(tenant.alias, 'camtest');
                                                                  assert.equal(tenant.host, 'swansea.oae.com');
                                                                  assert.equal(tenant.displayName, 'Swansea University');
  
                                                                  // Update the tenant display name and host as the tenant admin
                                                                  RestAPI.Tenants.updateTenant(swanseaCamAdminRestContext, null, {'displayName': 'Cambridge University Test', 'host': 'cambridge.oae.com'}, function(err) {
                                                                      assert.ok(!err);
  
                                                                      // Check if the update was successful
                                                                      // The old host name should no longer be accepting requests
                                                                      RestAPI.Tenants.getTenant(swanseaCamAdminRestContext, null, function(err, tenant) {
                                                                          assert.ok(err);
                                                                          assert.equal(err.code, 418);
                                                                          // The new host name should now be responding to requests
                                                                          RestAPI.Tenants.getTenant(camAdminRestContext, null, function(err, tenant) {
                                                                              assert.ok(!err);
                                                                              assert.ok(tenant);
                                                                              assert.equal(tenant.alias, 'camtest');
                                                                              assert.equal(tenant.host, 'cambridge.oae.com');
                                                                              assert.equal(tenant.displayName, 'Cambridge University Test');
                                                                              callback();
                                                                          });
                                                                      });
                                                                  });
                                                              });
                                                          });
                                                      });
                                                  });
                                              });
                                          });
                                      });
                                  });
                              });
                          });
                      });
                  });
              });
          });
      });
  
  
      describe('Non-existing tenants', function() {
  
          it('verify non-existing tenant', function(callback) {
              // Get the me feed on an existing tenant
              RestAPI.User.getMe(anonymousCamRestContext, function(err, meObj) {
                  assert.ok(!err);
                  assert.equal(meObj.anon, true);
  
                  // Get the me feed on a non-existing tenant
                  anonymousNonExistingRestContext = TestsUtil.createTenantRestContext('harvard.oae.com');
                  RestAPI.User.getMe(anonymousNonExistingRestContext, function(err, meObj) {
                      assert.ok(err);
                      assert.equal(err.code, 418);
                      callback();
                  });
              });
          });
      });
  });
  