Files
js-openstack-lib/test/unit/keystoneTest.js
Michael Krotscheck abd0508c30 Created HTTP wrapper
This utility class provides an abstraction layer for HTTP calls via fetch(). Its purpose is
to provide common, SDK-wide behavior for all HTTP requests. Included are:

- Providing a common extension point for request and response manipulation.
- Access to default headers.

In the future, this class chould also be extended to provide:

- Some form of progress() support for large uploads and downloads (perhaps via introduction of Q)
- Convenience decoding of the response body, depending on Content-Type.
- Internal error handling (At this time, HTTP errors are passed to then() rather than catch()).
- Other features.

Change-Id: I8173554b1b7ef052e2a74504b98f4ec574ce6136
2016-08-18 13:08:14 -07:00

70 lines
1.6 KiB
JavaScript

import Keystone from '../../src/keystone.js';
import fetchMock from 'fetch-mock';
describe('Openstack connection test', () => {
it('should export a class', () => {
const keystone = new Keystone(aCloudsConfig('cloud1'), 'cloud1');
expect(keystone).toBeDefined();
});
it('should throw an error for an empty config', () => {
try {
const keystone = new Keystone();
keystone.authenticate();
} catch (e) {
expect(e.message).toEqual('A configuration is required.');
}
});
it('should authenticate', (done) => {
const cloudsConfig = aCloudsConfig('cloud1');
const authUrl = cloudsConfig.clouds.cloud1.auth.auth_url;
fetchMock
.post(authUrl, {
body: {
catalog: {
foo: 'bar'
}
},
headers: {
'X-Subject-Token': 'the-token'
}
});
const keystone = new Keystone(cloudsConfig.clouds.cloud1);
keystone.authenticate()
.then(() => {
expect(fetchMock.called(authUrl)).toEqual(true);
expect(typeof keystone.token).toEqual('string');
expect(keystone.token).toEqual('the-token');
expect(keystone.catalog).toEqual({foo: 'bar'});
done();
})
.catch((reason) => {
expect(reason).toBeUndefined();
done();
});
});
function aCloudsConfig (name) {
const cloudsConfig = {
clouds: {}
};
cloudsConfig.clouds[name] = {
region_name: 'Region1',
auth: {
username: 'user',
password: 'pass',
project_name: 'js-openstack-lib',
auth_url: 'http://keystone/'
}
};
return cloudsConfig;
}
});