Skip to content

Commit bd7e99a

Browse files
author
macbookpro
committed
API-3 - Extend API Info response
1 parent 037935f commit bd7e99a

File tree

5 files changed

+97
-71
lines changed

5 files changed

+97
-71
lines changed

test/unit/helpers/rt.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ export function createMockRTServer(appId = APP_ID) {
9595
socket.on(RTSocketEvents.SUB_ON, data => onMessage(RTSocketEvents.SUB_ON, data))
9696
socket.on(RTSocketEvents.SUB_OFF, data => onMessage(RTSocketEvents.SUB_OFF, data))
9797

98-
onMessage(RTSocketEvents.CONNECT, { apiKey, clientId, userToken: userToken === 'null' ? null : userToken })
98+
onMessage(RTSocketEvents.CONNECT, {
99+
apiKey, clientId, userToken: userToken === 'null' ? null : userToken, connectionId: socket.id
100+
})
99101
})
100102

101103
const emit = (type, data) => {

test/unit/specs/data/rt.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai'
22
import { describe, it } from 'mocha'
33

4-
import Backendless, { forTest, prepareMockRequest, createMockRTServer, Utils } from '../../helpers/sandbox'
4+
import Backendless, { forTest, createMockRTServer, Utils } from '../../helpers/sandbox'
55

66
describe('<Data> RT', function() {
77

@@ -11,6 +11,7 @@ describe('<Data> RT', function() {
1111

1212
const tableName = 'TEST_TABLE_NAME'
1313
const relationColumnName = 'TEST_REL_COLUMN_NAME'
14+
const rtURL = 'http://localhost:12345'
1415

1516
let rtClient
1617

@@ -20,7 +21,9 @@ describe('<Data> RT', function() {
2021
beforeEach(async () => {
2122
rtClient = await createMockRTServer()
2223

23-
prepareMockRequest(rtClient.host)
24+
Backendless.appInfoPromise = chai.spy(() =>
25+
Promise.resolve({ rtURL })
26+
)
2427

2528
dataStore = Backendless.Data.of(tableName)
2629
rtHandlers = dataStore.rt()

test/unit/specs/logging.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -570,16 +570,20 @@ describe('<Logging>', function() {
570570
})
571571

572572
it('should apply log levels to logging config', async () => {
573-
prepareMockRequest([
574-
{
575-
name : loggerName,
576-
level: 'FATAL'
577-
},
578-
{
579-
name : 'Global logger',
580-
level: 'ERROR'
581-
}
582-
])
573+
Backendless.appInfoPromise = chai.spy(() =>
574+
Promise.resolve({
575+
loggers: [
576+
{
577+
name : loggerName,
578+
level: 'FATAL'
579+
},
580+
{
581+
name : 'Global logger',
582+
level: 'ERROR'
583+
}
584+
]
585+
})
586+
)
583587

584588
Backendless.initApp({
585589
appId : APP_ID,
@@ -620,16 +624,20 @@ describe('<Logging>', function() {
620624
})
621625

622626
it('should ignore all logs when global is OFF', async () => {
623-
prepareMockRequest([
624-
{
625-
name : loggerName,
626-
level: 'INFO'
627-
},
628-
{
629-
name : 'Global logger',
630-
level: 'OFF'
631-
}
632-
])
627+
Backendless.appInfoPromise = chai.spy(() =>
628+
Promise.resolve({
629+
loggers: [
630+
{
631+
name : loggerName,
632+
level: 'INFO'
633+
},
634+
{
635+
name : 'Global logger',
636+
level: 'OFF'
637+
}
638+
]
639+
})
640+
)
633641

634642
Backendless.initApp({
635643
appId : APP_ID,

test/unit/specs/messaging/channel.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('<Messaging> Channel', function() {
1111

1212
const channelName = 'TEST_CHANNEL_NAME'
1313
const message = 'TEST_MESSAGE'
14+
const rtURL = 'http://localhost:12345'
1415

1516
const fakeResult = { foo: 123 }
1617

@@ -22,7 +23,9 @@ describe('<Messaging> Channel', function() {
2223
beforeEach(async () => {
2324
rtClient = await createMockRTServer()
2425

25-
prepareMockRequest(rtClient.host)
26+
Backendless.appInfoPromise = chai.spy(() =>
27+
Promise.resolve({ rtURL })
28+
)
2629

2730
channel = Backendless.Messaging.subscribe(channelName)
2831

test/unit/specs/rt/basic.js

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { expect } from 'chai'
1+
import chai, { expect } from 'chai'
2+
import spies from 'chai-spies'
23
import { describe, it } from 'mocha'
34

5+
chai.use(spies)
6+
47
import Backendless, {
58
forTest,
69
prepareMockRequest,
710
createMockRTServer,
8-
APP_PATH,
911
API_KEY,
1012
Utils
1113
} from '../../helpers/sandbox'
@@ -19,12 +21,17 @@ describe('<RT> Basic', function() {
1921
let rtClient
2022

2123
const channelName = 'TEST_CHANNEL_NAME'
24+
const host = 'http://localhost:12345'
2225

2326
describe('with AppID and API Key', function() {
2427
const backendlessApp = Backendless
2528

2629
before(async () => {
2730
rtClient = await createMockRTServer()
31+
32+
backendlessApp.appInfoPromise = chai.spy(() =>
33+
Promise.resolve({ rtURL: host })
34+
)
2835
})
2936

3037
after(async () => {
@@ -33,8 +40,8 @@ describe('<RT> Basic', function() {
3340
rtClient.stop()
3441
})
3542

36-
it('lookup', async () => {
37-
const req1 = prepareMockRequest(rtClient.host)
43+
it('connect and subscribe', async () => {
44+
expect(backendlessApp.appInfoPromise).to.not.have.been.called
3845

3946
backendlessApp.Messaging.subscribe(channelName)
4047

@@ -46,17 +53,13 @@ describe('<RT> Basic', function() {
4653
expect(con1.userToken).to.be.equal(null)
4754
expect(con1.clientId).to.be.a('string')
4855

49-
expect(req1).to.deep.include({
50-
method : 'GET',
51-
path : `${APP_PATH}/rt/lookup`,
52-
headers: {},
53-
body : undefined
54-
})
56+
expect(backendlessApp.appInfoPromise).to.have.been.called
5557
})
5658

57-
it('lookup with user token', async () => {
59+
it('connected with user token', async () => {
60+
expect(backendlessApp.appInfoPromise).to.not.have.been.called
61+
5862
prepareMockRequest({ objectId: 'test-id', 'user-token': 'test-token-1' })
59-
prepareMockRequest(rtClient.host)
6063

6164
await backendlessApp.UserService.login('login', 'password')
6265

@@ -67,12 +70,11 @@ describe('<RT> Basic', function() {
6770
expect(con1.apiKey).to.be.equal(API_KEY)
6871
expect(con1.userToken).to.be.equal('test-token-1')
6972
expect(con1.clientId).to.be.a('string')
73+
expect(backendlessApp.appInfoPromise).to.have.been.called
7074
})
7175

7276
it('reconnects after changing debug mode', async () => {
73-
prepareMockRequest(rtClient.host)
74-
prepareMockRequest(rtClient.host)
75-
prepareMockRequest(rtClient.host)
77+
expect(backendlessApp.appInfoPromise).to.not.have.been.called
7678

7779
backendlessApp.Messaging.subscribe(channelName)
7880

@@ -90,6 +92,10 @@ describe('<RT> Basic', function() {
9092
.to.be.equal(con2.clientId)
9193
.to.be.equal(con3.clientId)
9294

95+
expect(con1.connectionId)
96+
.to.not.equal(con2.connectionId)
97+
.to.not.equal(con3.connectionId)
98+
9399
expect(con1.apiKey).to.be.equal(API_KEY)
94100
expect(con1.userToken).to.be.equal(null)
95101
expect(con1.clientId).to.be.a('string')
@@ -101,10 +107,13 @@ describe('<RT> Basic', function() {
101107
expect(con3.apiKey).to.be.equal(API_KEY)
102108
expect(con3.userToken).to.be.equal(null)
103109
expect(con3.clientId).to.be.a('string')
110+
111+
expect(backendlessApp.appInfoPromise).to.have.been.called
104112
})
105113

106114
it('updates connection user-token after login/logout', async () => {
107-
prepareMockRequest(rtClient.host)
115+
expect(backendlessApp.appInfoPromise).to.not.have.been.called
116+
108117
prepareMockRequest({ objectId: 'test-id-1', 'user-token': 'test-token-1' })
109118
prepareMockRequest({ objectId: 'test-id-2', 'user-token': 'test-token-2' })
110119
prepareMockRequest()
@@ -144,11 +153,12 @@ describe('<RT> Basic', function() {
144153
expect(met1.id).to.be.not.equal(met2.id)
145154
expect(met1.id).to.be.not.equal(met3.id)
146155
expect(met2.id).to.be.not.equal(met3.id)
156+
157+
expect(backendlessApp.appInfoPromise).to.have.been.called
147158
})
148159

149160
it('should update connection user-token after setCurrentUserToken', async () => {
150-
prepareMockRequest(rtClient.host)
151-
prepareMockRequest()
161+
expect(backendlessApp.appInfoPromise).to.not.have.been.called
152162

153163
backendlessApp.Messaging.subscribe(channelName)
154164

@@ -173,6 +183,8 @@ describe('<RT> Basic', function() {
173183
expect(met2.id).to.be.a('string')
174184
expect(met2.name).to.be.equal('SET_USER_TOKEN')
175185
expect(met2.options).to.be.eql({ userToken: null })
186+
187+
expect(backendlessApp.appInfoPromise).to.have.been.called
176188
})
177189

178190
it('should not set user-token after setCurrentUserToken', async () => {
@@ -196,6 +208,10 @@ describe('<RT> Basic', function() {
196208

197209
before(async () => {
198210
rtClient = await createMockRTServer(rtAppId)
211+
212+
backendlessApp.appInfoPromise = chai.spy(() =>
213+
Promise.resolve({ appId: rtAppId, apiKey: rtAPIKey, rtURL: host })
214+
)
199215
})
200216

201217
afterEach(async () => {
@@ -208,9 +224,8 @@ describe('<RT> Basic', function() {
208224
rtClient.stop()
209225
})
210226

211-
it('lookup', async () => {
212-
const req1 = prepareMockRequest(rtClient.host)
213-
const req2 = prepareMockRequest({ appId: rtAppId, apiKey: rtAPIKey })
227+
it('connected', async () => {
228+
expect(backendlessApp.appInfoPromise).to.not.have.been.called
214229

215230
backendlessApp.Messaging.subscribe(channelName)
216231

@@ -222,25 +237,13 @@ describe('<RT> Basic', function() {
222237
expect(con1.userToken).to.be.equal(null)
223238
expect(con1.clientId).to.be.a('string')
224239

225-
expect(req1).to.deep.include({
226-
method : 'GET',
227-
path : `${domain}/api/rt/lookup`,
228-
headers: {},
229-
body : undefined
230-
})
231-
232-
expect(req2).to.deep.include({
233-
method : 'GET',
234-
path : `${domain}/api/info`,
235-
headers: {},
236-
body : undefined
237-
})
240+
expect(backendlessApp.appInfoPromise).to.have.been.called
238241
})
239242

240-
it('lookup with user token', async () => {
243+
it('connection with user token', async () => {
244+
expect(backendlessApp.appInfoPromise).to.not.have.been.called
245+
241246
prepareMockRequest({ objectId: 'test-id', 'user-token': 'test-token-1' })
242-
prepareMockRequest(rtClient.host)
243-
prepareMockRequest({ appId: rtAppId, apiKey: rtAPIKey })
244247

245248
await backendlessApp.UserService.login('login', 'password')
246249

@@ -251,15 +254,12 @@ describe('<RT> Basic', function() {
251254
expect(con1.apiKey).to.be.equal(rtAPIKey)
252255
expect(con1.userToken).to.be.equal('test-token-1')
253256
expect(con1.clientId).to.be.a('string')
257+
258+
expect(backendlessApp.appInfoPromise).to.have.been.called
254259
})
255260

256261
it('reconnects after changing debug mode', async () => {
257-
prepareMockRequest(rtClient.host)
258-
prepareMockRequest({ appId: rtAppId, apiKey: rtAPIKey })
259-
prepareMockRequest(rtClient.host)
260-
prepareMockRequest({ appId: rtAppId, apiKey: rtAPIKey })
261-
prepareMockRequest(rtClient.host)
262-
prepareMockRequest({ appId: rtAppId, apiKey: rtAPIKey })
262+
expect(backendlessApp.appInfoPromise).to.not.have.been.called
263263

264264
backendlessApp.Messaging.subscribe(channelName)
265265

@@ -277,6 +277,10 @@ describe('<RT> Basic', function() {
277277
.to.be.equal(con2.clientId)
278278
.to.be.equal(con3.clientId)
279279

280+
expect(con1.connectionId)
281+
.to.not.equal(con2.connectionId)
282+
.to.not.equal(con3.connectionId)
283+
280284
expect(con1.apiKey).to.be.equal(rtAPIKey)
281285
expect(con1.userToken).to.be.equal(null)
282286
expect(con1.clientId).to.be.a('string')
@@ -288,11 +292,13 @@ describe('<RT> Basic', function() {
288292
expect(con3.apiKey).to.be.equal(rtAPIKey)
289293
expect(con3.userToken).to.be.equal(null)
290294
expect(con3.clientId).to.be.a('string')
295+
296+
expect(backendlessApp.appInfoPromise).to.have.been.called
291297
})
292298

293299
it('updates connection user-token after login/logout', async () => {
294-
prepareMockRequest(rtClient.host)
295-
prepareMockRequest({ appId: rtAppId, apiKey: rtAPIKey })
300+
expect(backendlessApp.appInfoPromise).to.not.have.been.called
301+
296302
prepareMockRequest({ objectId: 'test-id-1', 'user-token': 'test-token-1' })
297303
prepareMockRequest({ objectId: 'test-id-2', 'user-token': 'test-token-2' })
298304
prepareMockRequest()
@@ -332,11 +338,13 @@ describe('<RT> Basic', function() {
332338
expect(met1.id).to.be.not.equal(met2.id)
333339
expect(met1.id).to.be.not.equal(met3.id)
334340
expect(met2.id).to.be.not.equal(met3.id)
341+
342+
expect(backendlessApp.appInfoPromise).to.have.been.called
335343
})
336344

337345
it('should update connection user-token after setCurrentUserToken', async () => {
338-
prepareMockRequest(rtClient.host)
339-
prepareMockRequest({ appId: rtAppId, apiKey: rtAPIKey })
346+
expect(backendlessApp.appInfoPromise).to.not.have.been.called
347+
340348
prepareMockRequest()
341349

342350
backendlessApp.Messaging.subscribe(channelName)
@@ -362,6 +370,8 @@ describe('<RT> Basic', function() {
362370
expect(met2.id).to.be.a('string')
363371
expect(met2.name).to.be.equal('SET_USER_TOKEN')
364372
expect(met2.options).to.be.eql({ userToken: null })
373+
374+
expect(backendlessApp.appInfoPromise).to.have.been.called
365375
})
366376

367377
it('should not set user-token after setCurrentUserToken', async () => {

0 commit comments

Comments
 (0)