Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/github_actions/production-c23a41af91
Browse files Browse the repository at this point in the history
  • Loading branch information
corinagum authored Feb 22, 2024
2 parents ade565d + 5e693b0 commit f831c57
Show file tree
Hide file tree
Showing 52 changed files with 579 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
1 change: 0 additions & 1 deletion js/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"plugin:import/typescript",
"plugin:import/recommended",
"plugin:jsdoc/recommended",
"plugin:security/recommended",
"plugin:prettier/recommended" // Recommended to be last
],
"plugins": ["@typescript-eslint", "jsdoc", "mocha", "only-warn", "prettier"],
Expand Down
3 changes: 2 additions & 1 deletion js/.nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"**/coverage/**",
"**/*.d.ts",
"**/*.spec.ts",
"packages/**/src/**/index.ts"
"packages/**/src/**/index.ts",
"packages/teams-ai/src/internals/Colorize.ts"
],
"reporter": ["html", "text", "lcov"],
"all": true,
Expand Down
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"eslint-plugin-mocha": "^10.2.0",
"eslint-plugin-only-warn": "^1.1.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-security": "^1.7.1",
"exorcist": "^2.0.0",
"express": "^4.18.2",
"mocha": "^10.3.0",
"mocha-junit-reporter": "^2.0.0",
"ms-rest-azure": "^3.0.2",
Expand Down
1 change: 0 additions & 1 deletion js/packages/teams-ai/src/AI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ export class AI<TState extends TurnState = TurnState> {
break;
}

// eslint-disable-next-line security/detect-object-injection
let output: string;
const cmd = plan.commands[i];
switch (cmd.type) {
Expand Down
3 changes: 2 additions & 1 deletion js/packages/teams-ai/src/AdaptiveCards.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ describe('AdaptiveCards', () => {
let selector: RouteSelector;
let handler: any;
let addRouteStub: sinon.SinonStub;
const adapter = new TestAdapter();
let adapter: TestAdapter;

beforeEach(() => {
adapter = new TestAdapter();
app = new Application();
adaptiveCards = new AdaptiveCards(app);
selector = null as unknown as RouteSelector;
Expand Down
2 changes: 0 additions & 2 deletions js/packages/teams-ai/src/AdaptiveCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
// TODO:
/* eslint-disable security/detect-object-injection */
import {
TurnContext,
ActivityTypes,
Expand Down
10 changes: 0 additions & 10 deletions js/packages/teams-ai/src/Application.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable security/detect-object-injection */
/**
* @module teams-ai
*/
Expand Down Expand Up @@ -707,8 +706,6 @@ export class Application<TState extends TurnState = TurnState> {
// Invoke Activities from Teams need to be responded to in less than 5 seconds.
if (context.activity.type === ActivityTypes.Invoke) {
for (let i = 0; i < this._invokeRoutes.length; i++) {
// TODO: fix security/detect-object-injection
// eslint-disable-next-line security/detect-object-injection
const route = this._invokeRoutes[i];
if (await route.selector(context)) {
// Execute route handler
Expand All @@ -728,8 +725,6 @@ export class Application<TState extends TurnState = TurnState> {

// All other ActivityTypes and any unhandled Invokes are run through the remaining routes.
for (let i = 0; i < this._routes.length; i++) {
// TODO:
// eslint-disable-next-line security/detect-object-injection
const route = this._routes[i];
if (await route.selector(context)) {
// Execute route handler
Expand Down Expand Up @@ -835,8 +830,6 @@ export class Application<TState extends TurnState = TurnState> {
// Listen for any messages to be sent from the bot
if (timerRunning) {
for (let i = 0; i < activities.length; i++) {
// TODO:
// eslint-disable-next-line security/detect-object-injection
if (activities[i].type == ActivityTypes.Message) {
// Stop the timer
this.stopTypingTimer();
Expand Down Expand Up @@ -957,8 +950,6 @@ export class Application<TState extends TurnState = TurnState> {
handlers: ApplicationEventHandler<TState>[]
): Promise<boolean> {
for (let i = 0; i < handlers.length; i++) {
// TODO:
// eslint-disable-next-line security/detect-object-injection
const continueExecution = await handlers[i](context, state);
if (!continueExecution) {
return false;
Expand Down Expand Up @@ -991,7 +982,6 @@ export class Application<TState extends TurnState = TurnState> {
try {
// Copy original activity to new context
for (const key in context.activity) {
// eslint-disable-next-line security/detect-object-injection
(ctx.activity as any)[key] = (context.activity as any)[key];
}

Expand Down
15 changes: 9 additions & 6 deletions js/packages/teams-ai/src/MemoryFork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ export class MemoryFork implements Memory {
*/
public deleteValue(path: string): void {
const { scope, name } = this.getScopeAndName(path);
if (this._fork.hasOwnProperty(scope) && this._fork[scope].hasOwnProperty(name)) {
if (
Object.prototype.hasOwnProperty.call(this._fork, scope) &&
Object.prototype.hasOwnProperty.call(this._fork[scope], name)
) {
delete this._fork[scope][name];
}
}
Expand All @@ -87,8 +90,8 @@ export class MemoryFork implements Memory {
*/
public hasValue(path: string): boolean {
const { scope, name } = this.getScopeAndName(path);
if (this._fork.hasOwnProperty(scope)) {
return this._fork[scope].hasOwnProperty(name);
if (Object.prototype.hasOwnProperty.call(this._fork, scope)) {
return Object.prototype.hasOwnProperty.call(this._fork[scope], name);
} else {
return this._memory.hasValue(path);
}
Expand All @@ -104,8 +107,8 @@ export class MemoryFork implements Memory {
*/
public getValue<TValue = unknown>(path: string): TValue {
const { scope, name } = this.getScopeAndName(path);
if (this._fork.hasOwnProperty(scope)) {
if (this._fork[scope].hasOwnProperty(name)) {
if (Object.prototype.hasOwnProperty.call(this._fork, scope)) {
if (Object.prototype.hasOwnProperty.call(this._fork[scope], name)) {
return this._fork[scope][name] as TValue;
}
}
Expand All @@ -122,7 +125,7 @@ export class MemoryFork implements Memory {
*/
public setValue(path: string, value: unknown): void {
const { scope, name } = this.getScopeAndName(path);
if (!this._fork.hasOwnProperty(scope)) {
if (!Object.prototype.hasOwnProperty.call(this._fork, scope)) {
this._fork[scope] = {};
}

Expand Down
14 changes: 2 additions & 12 deletions js/packages/teams-ai/src/TaskModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,7 @@ function createTaskSelector(
const isTeams = context.activity.channelId == Channels.Msteams;
const isInvoke = context?.activity?.type == ActivityTypes.Invoke && context?.activity?.name == invokeName;
const data = context?.activity?.value?.data;
if (
isInvoke &&
isTeams &&
typeof data == 'object' &&
// eslint-disable-next-line security/detect-object-injection
typeof data[filterField] == 'string'
) {
// eslint-disable-next-line security/detect-object-injection
if (isInvoke && isTeams && typeof data == 'object' && typeof data[filterField] == 'string') {
return Promise.resolve(verb.test(data[filterField]));
} else {
return Promise.resolve(false);
Expand All @@ -355,10 +348,7 @@ function createTaskSelector(
return (context: TurnContext) => {
const isInvoke = context?.activity?.type == ActivityTypes.Invoke && context?.activity?.name == invokeName;
const data = context?.activity?.value?.data;
return Promise.resolve(
// eslint-disable-next-line security/detect-object-injection
isInvoke && typeof data == 'object' && data[filterField] == verb
);
return Promise.resolve(isInvoke && typeof data == 'object' && data[filterField] == verb);
};
}
}
6 changes: 3 additions & 3 deletions js/packages/teams-ai/src/TeamsAdapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ describe('TeamsAdapter', () => {
sandbox = sinon.createSandbox();
adapter = new TeamsAdapter();

app.post('/api/messages', async (req, res) => {
app.post('/api/messages', async (req: any, res: any) => {
await adapter.process(req, res, async () => {});
});

server = app.listen(80);
server = app.listen(9876);
});

afterEach(() => {
Expand All @@ -34,7 +34,7 @@ describe('TeamsAdapter', () => {
let userAgent: string | undefined;

try {
const res = await axios.post('/api/messages', {
const res = await axios.post('http://127.0.0.1:9876/api/messages', {
type: 'invoke',
localTimezone: 'America/Los_Angeles',
callerId: 'test',
Expand Down
16 changes: 7 additions & 9 deletions js/packages/teams-ai/src/TeamsAttachmentDownloader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { strict as assert } from 'assert';

import { Application } from './Application';
import { TeamsAdapter } from './TeamsAdapter';
import { TeamsAttachmentDownloader } from './TeamsAttachmentDownloader';
import { Activity, Attachment, CallerIdConstants } from 'botbuilder';

import { createTestTurnContextAndState } from './internals/testing/TestUtilities';
import sinon, { createSandbox } from 'sinon';

import axios from 'axios';
import { Activity, Attachment, CallerIdConstants } from 'botbuilder';
import {
AppCredentials,
AuthenticatorResult,
GovernmentConstants,
PasswordServiceClientCredentialFactory
} from 'botframework-connector';
import sinon, { createSandbox } from 'sinon';

import { Application } from './Application';
import { TeamsAdapter } from './TeamsAdapter';
import { TeamsAttachmentDownloader } from './TeamsAttachmentDownloader';
import { createTestTurnContextAndState } from './internals/testing/TestUtilities';

describe('TeamsAttachmentDownloader', () => {
const mockAxios = axios;
Expand Down
159 changes: 159 additions & 0 deletions js/packages/teams-ai/src/TurnState.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { strict as assert } from 'assert';
import { TurnState } from './TurnState';
import { createTestTurnContextAndState } from './internals/testing/TestUtilities';
import { TeamsAdapter } from './TeamsAdapter';
import { Activity } from 'botbuilder';

describe('TurnState', () => {
let adapter: TeamsAdapter;
let activity: Partial<Activity>;
let turnState: TurnState;

beforeEach(() => {
turnState = new TurnState();
activity = {
type: 'message',
text: 'Here is the attachment'
};
});

describe('conversation', () => {
it("should throw an error if TurnState hasn't been loaded", () => {
assert.throws(() => turnState.conversation, new Error("TurnState hasn't been loaded. Call load() first."));
});

it('should get and set the conversation state', async () => {
const [context, _] = await createTestTurnContextAndState(adapter, activity);
const conversationState = { prop: 'value' };

await turnState.load(context);

// Set the conversation state
turnState.conversation = conversationState;
// Get the conversation state
const retrievedConversationState = turnState.conversation;

// Assert that the retrieved conversation state is the same as the original conversation state
assert.equal(retrievedConversationState, conversationState);
});
});

describe('temp', () => {
it("should throw an error if TurnState hasn't been loaded", () => {
assert.throws(() => turnState.temp, new Error("TurnState hasn't been loaded. Call load() first."));
});

it('should get and set the temp state', async () => {
const [context, turnState] = await createTestTurnContextAndState(adapter, activity);
turnState.load(context);
const tempState = {
actionOutputs: {},
authTokens: {},
input: context.activity.text,
inputFiles: undefined,
lastOutput: ''
};
// Get the temp state
const retrievedTempState = turnState.temp;

// Assert that the retrieved temp state is the same as the original temp state
assert.deepEqual(retrievedTempState, tempState);
});
});

describe('user', () => {
it("should throw an error if TurnState hasn't been loaded", () => {
assert.throws(() => turnState.user, new Error("TurnState hasn't been loaded. Call load() first."));
});

it('should get and set the user state', async () => {
const [context, turnState] = await createTestTurnContextAndState(adapter, activity);
// Mock the user state
turnState.load(context);
const userState = { prop: 'value' };
// Set the user state
turnState.user = userState;

// Get the user state
const retrievedUserState = turnState.user;

// Assert that the retrieved user state is the same as the original user state
assert.equal(retrievedUserState, userState);
});
});

describe('deleteConversationState', () => {
it("should throw an error if TurnState hasn't been loaded", () => {
assert.throws(
() => turnState.deleteConversationState(),
new Error("TurnState hasn't been loaded. Call load() first.")
);
});

it('should delete the conversation state', async () => {
const [context, turnState] = await createTestTurnContextAndState(adapter, activity);
// Mock the user state
turnState.load(context);
// Mock the conversation state
const conversationState = { prop: 'value' };

// Set the conversation state
turnState.conversation = conversationState;

// Delete the conversation state
turnState.deleteConversationState();

// Get the conversation state
const retrievedConversationState = turnState.conversation;

// Assert that the conversation state is undefined
assert.deepEqual(retrievedConversationState, {});
});
});

describe('deleteTempState', () => {
it("should throw an error if TurnState hasn't been loaded", () => {
assert.throws(
() => turnState.deleteTempState(),
new Error("TurnState hasn't been loaded. Call load() first.")
);
});

it('should delete the temp state', async () => {
const [context, turnState] = await createTestTurnContextAndState(adapter, activity);

turnState.load(context);

// Delete the temp state
turnState.deleteTempState();

// Get the temp state
const retrievedTempState = turnState.temp;

// Assert that the temp state is undefined
assert.deepEqual(retrievedTempState, {});
});
});

describe('deleteUserState', () => {
it('should delete the user state', async () => {
const [context, turnState] = await createTestTurnContextAndState(adapter, activity);

turnState.load(context);
// Mock the user state
const userState = { prop: 'value' };

// Set the user state
turnState.user = userState;

// Delete the user state
turnState.deleteUserState();

// Get the user state
const retrievedUserState = turnState.user;

// Assert that the user state is undefined
assert.deepEqual(retrievedUserState, {});
});
});
});
Loading

0 comments on commit f831c57

Please sign in to comment.