Skip to content

Commit 1996e74

Browse files
committed
📍 OMRSJS-12: Infer swagger.json URL.
1 parent 9c4d962 commit 1996e74

File tree

4 files changed

+186
-5
lines changed

4 files changed

+186
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ coverage-node
44
coverage-browser
55
sauce_connect.log
66
npm-debug.log
7+
lib

src/constants.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* eslint-disable no-unused-vars */
2+
export const OMRSJS_CLASS_NAME = 'OpenMRS';
3+
export const OMRSJS_VERSION = '0.0.1';
4+
5+
export const OMRSJS_DEFAULT_PROTOCOL = 'http';
6+
export const OMRSJS_OPENAPI_SPEC_PATH_SUFFIX = 'module/webservices/rest/swagger.json';

src/openmrs.js

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
import Swagger from 'swagger-client';
2+
import * as constants from './constants';
23

34
export default class OpenMRS {
45

56
constructor(config) {
67
/** Allow configuration to to be initialized on new instance creation */
7-
this.config = config || {};
8+
if (typeof config === 'object') {
9+
this.config = config;
10+
} else if (typeof config === 'string') {
11+
this.config = {
12+
url: config,
13+
};
14+
} else {
15+
this.config = {};
16+
}
817

918
/** Used as the library name. */
10-
this._name = 'OpenMRS';
19+
this._name = constants.OMRSJS_CLASS_NAME;
1120

1221
/** Used as the semantic version number. */
13-
this._version = '0.0.1';
22+
this._version = constants.OMRSJS_VERSION;
1423

1524
this._api = undefined;
1625
}
@@ -35,9 +44,28 @@ export default class OpenMRS {
3544
}
3645
}
3746

47+
getOpenAPISpecURL(url) {
48+
let specUrl = url;
49+
50+
// make sure a protocol is specified
51+
if (url.substring(0, 3) !== constants.OMRSJS_DEFAULT_PROTOCOL) {
52+
specUrl = `${constants.OMRSJS_DEFAULT_PROTOCOL}://${specUrl}`;
53+
}
54+
55+
// make sure the url string points to the OpenAPI spec
56+
if (url.endsWith(constants.OMRSJS_OPENAPI_SPEC_PATH_SUFFIX)) {
57+
/** noop */
58+
} else {
59+
specUrl = url.endsWith('/') ? `${specUrl}${constants.OMRSJS_OPENAPI_SPEC_PATH_SUFFIX}` :
60+
`${specUrl}/${constants.OMRSJS_OPENAPI_SPEC_PATH_SUFFIX}`;
61+
}
62+
63+
return specUrl;
64+
}
65+
3866
login(user = this.config.user, pass = this.config.pass, url = this.config.url) {
3967
return new Swagger({
40-
url,
68+
url: this.getOpenAPISpecURL(url),
4169
usePromise: true,
4270
authorizations: {
4371
easyapi_basic: new Swagger.PasswordAuthorization(user, pass),

test/openmrs.spec.js

+147-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let lib;
1212
chai.expect();
1313
chai.use(chaiAsPromised);
1414

15-
describe('After contructing a new instance of OpenMRS', () => {
15+
describe('After contructing a new instance of OpenMRS with no constructor parameters', () => {
1616
before(() => {
1717
lib = new OpenMRS();
1818
});
@@ -96,3 +96,149 @@ describe('After contructing a new instance of OpenMRS', () => {
9696
});
9797
});
9898
});
99+
100+
describe('Contructing a new instance of OpenMRS with the full URL constructor parameter', () => {
101+
before(() => {
102+
lib = new OpenMRS('http://localhost:8080/openmrs/module/webservices/rest/swagger.json');
103+
});
104+
105+
describe('and logging in', () => {
106+
beforeEach(() => {
107+
fauxJax.install();
108+
});
109+
110+
afterEach(() => {
111+
fauxJax.restore();
112+
});
113+
114+
it('should succeed with just the user and password', () => {
115+
fauxJax.on('request', (req) => {
116+
req.respond(200, {
117+
'Content-Type': 'application/json',
118+
}, JSON.stringify(swaggerSpec));
119+
});
120+
121+
const deferred = lib.login('admin', 'Admin123');
122+
123+
return expect(deferred).to.eventually.be.fulfilled;
124+
});
125+
});
126+
});
127+
128+
describe('Contructing a new instance of OpenMRS with the base URL and protocol', () => {
129+
before(() => {
130+
lib = new OpenMRS('http://localhost:8080/openmrs/module/webservices/rest/swagger.json');
131+
});
132+
133+
describe('and logging in', () => {
134+
beforeEach(() => {
135+
fauxJax.install();
136+
});
137+
138+
afterEach(() => {
139+
fauxJax.restore();
140+
});
141+
142+
it('should succeed with just the user and password', () => {
143+
fauxJax.on('request', (req) => {
144+
req.respond(200, {
145+
'Content-Type': 'application/json',
146+
}, JSON.stringify(swaggerSpec));
147+
});
148+
149+
const deferred = lib.login('admin', 'Admin123');
150+
151+
return expect(deferred).to.eventually.be.fulfilled;
152+
});
153+
});
154+
});
155+
156+
describe('Contructing a new instance of OpenMRS with the full URL without protocol', () => {
157+
before(() => {
158+
lib = new OpenMRS('localhost:8080/openmrs/module/webservices/rest/swagger.json');
159+
});
160+
161+
describe('and logging in', () => {
162+
beforeEach(() => {
163+
fauxJax.install();
164+
});
165+
166+
afterEach(() => {
167+
fauxJax.restore();
168+
});
169+
170+
it('should succeed with just the user and password', () => {
171+
fauxJax.on('request', (req) => {
172+
req.respond(200, {
173+
'Content-Type': 'application/json',
174+
}, JSON.stringify(swaggerSpec));
175+
});
176+
177+
const deferred = lib.login('admin', 'Admin123');
178+
179+
return expect(deferred).to.eventually.be.fulfilled;
180+
});
181+
});
182+
});
183+
184+
describe('Contructing a new instance of OpenMRS with the base URL without protocol', () => {
185+
before(() => {
186+
lib = new OpenMRS('localhost:8080/openmrs');
187+
});
188+
189+
describe('and logging in', () => {
190+
beforeEach(() => {
191+
fauxJax.install();
192+
});
193+
194+
afterEach(() => {
195+
fauxJax.restore();
196+
});
197+
198+
it('should succeed with just the user and password', () => {
199+
fauxJax.on('request', (req) => {
200+
req.respond(200, {
201+
'Content-Type': 'application/json',
202+
}, JSON.stringify(swaggerSpec));
203+
});
204+
205+
const deferred = lib.login('admin', 'Admin123');
206+
207+
return expect(deferred).to.eventually.be.fulfilled;
208+
});
209+
});
210+
});
211+
212+
describe('Contructing a new instance of OpenMRS with the config object', () => {
213+
before(() => {
214+
const config = {
215+
user: 'admin',
216+
pass: 'Admin123',
217+
url: 'localhost:8080/openmrs',
218+
};
219+
220+
lib = new OpenMRS(config);
221+
});
222+
223+
describe('and logging in', () => {
224+
beforeEach(() => {
225+
fauxJax.install();
226+
});
227+
228+
afterEach(() => {
229+
fauxJax.restore();
230+
});
231+
232+
it('should succeed with no parameters', () => {
233+
fauxJax.on('request', (req) => {
234+
req.respond(200, {
235+
'Content-Type': 'application/json',
236+
}, JSON.stringify(swaggerSpec));
237+
});
238+
239+
const deferred = lib.login();
240+
241+
return expect(deferred).to.eventually.be.fulfilled;
242+
});
243+
});
244+
});

0 commit comments

Comments
 (0)