assignSubscription
const mongoose = require('mongoose');
const { describe, it } = require('mocha');
const { expect } = require('chai');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const fakeNewAgentAiDriver = {
createSubscription() {
},
};
const stubGetAIDriver = sinon.stub().returns(fakeNewAgentAiDriver);
const AgentSchema = proxyquire('../../src/model/mongoose/Agent', {
'../../lib/cognitive-driver-sdk': {
getAIDriver: stubGetAIDriver,
},
});
describe('assignSubscription', () => {
const agentModel = mongoose.model('Agent', AgentSchema);
it('internal functions must accept the specified args and must be a specified result', async () => {
// --- data
const fakeReturnSource = {
source: {
getAzureApi() {
},
},
};
const fakeReturnAzureApi = {
getCognitiveServiceSubscriptionData() {
},
};
const fakeReturnSubscriptionData = {
subscriptionId: '9b5b46980bde4371893d7fd7b4b82d68',
region: 'westus2',
keyPath:
'/subscriptions/9911f06d-b361-49a8-ae5e-a72e0816db00/resourceGroups/CognitiveOpsDevGroup/providers/Microsoft.CognitiveServices/accounts/CognitiveOpsDevLuis',
endpoint: 'https://westus2.api.cognitive.microsoft.com/luis/v2.0',
};
const predictionResource = {
subscriptionId: '9911f06d-b361-49a8-ae5e-a72e0816db00',
resourceGroupName: 'CognitiveOpsDevGroup',
name: 'CognitiveOpsDevLuis',
type: 'Microsoft.CognitiveServices/accounts',
id:
'/subscriptions/9911f06d-b361-49a8-ae5e-a72e0816db00/resourceGroups/CognitiveOpsDevGroup/providers/Microsoft.CognitiveServices/accounts/CognitiveOpsDevLuis'
};
const agentId = '5d8b7d6a94a8e4003b844fec';
const queryKeyIn = '0e1df8210db74eed92c80a82f34c3a8d';
const endpointIn = 'https://westus2.api.cognitive.microsoft.com/luis/v2.0';
const clientId = undefined;
const agentPlatform = 'luis';
const apiKey = '30933a3603df43ae8da2370d2b6dea9b';
const agentModelId = 'a5892dcc-84f4-468c-92ec-7b67deffb0bf';
// --- stubs
const stubFindByIdOrThrow = sinon.stub(agentModel, 'findByIdOrThrow')
.returns(Promise.resolve({
agent: { client: clientId },
model: { id: agentModelId },
platform: agentPlatform,
}));
const stubGetLuisResource = sinon.stub(agentModel, 'getLuisResource').returns(Promise.resolve(fakeReturnSource));
const stubAzureApi = sinon.stub(fakeReturnSource.source, 'getAzureApi').returns(
Promise.resolve(fakeReturnAzureApi),
);
const stubGetCognitiveServiceSubscriptionData = sinon.stub(fakeReturnAzureApi, 'getCognitiveServiceSubscriptionData').returns(
Promise.resolve(fakeReturnSubscriptionData), // endpoint value
);
const stubGetAiApiKey = sinon.stub(agentModel, 'getAiApiKey').returns(
Promise.resolve(apiKey),
);
const stubCreateSubscription = sinon.stub(fakeNewAgentAiDriver, 'createSubscription').returns(Promise.resolve({}));
const stubGetAiQueryKeyByResource = sinon.stub(agentModel, 'getAiQueryKeyByResource').returns(
Promise.resolve(queryKeyIn),
);
// --- test
const { queryKey, endpoint } = await agentModel.assignSubscription(agentId, predictionResource);
// --- verify returns
expect(queryKey).to.be.equal(queryKeyIn);
expect(endpoint).to.be.equal(endpointIn);
// --- verify args
sinon.assert.calledWithExactly(stubFindByIdOrThrow, agentId);
sinon.assert.calledWithExactly(stubGetLuisResource, clientId);
sinon.assert.calledWithExactly(stubAzureApi);
sinon.assert.calledWithExactly(stubGetCognitiveServiceSubscriptionData, predictionResource);
sinon.assert.calledWithExactly(stubGetAiApiKey, clientId);
sinon.assert.calledWithExactly(stubGetAIDriver, agentPlatform, apiKey);
sinon.assert.calledWithExactly(
stubCreateSubscription,
fakeReturnSubscriptionData,
agentModelId,
);
sinon.assert.calledWithExactly(stubGetAiQueryKeyByResource, clientId, predictionResource);
// --- restore stubs
stubFindByIdOrThrow.restore();
stubGetLuisResource.restore();
stubAzureApi.restore();
stubGetCognitiveServiceSubscriptionData.restore();
stubGetAiApiKey.restore();
stubCreateSubscription.restore();
stubGetAiQueryKeyByResource.restore();
});
});