Skip to content

Commit

Permalink
[communication] Update @azure/communication-sms to ESM/vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
mpodwysocki committed Nov 14, 2024
1 parent adcf77e commit 86555c0
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 143 deletions.
24 changes: 20 additions & 4 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
*/

import type { Recorder } from "@azure-tools/test-recorder";
import { isLiveMode, isPlaybackMode } from "@azure-tools/test-recorder";
import { env, isLiveMode, isPlaybackMode } from "@azure-tools/test-recorder";
import { matrix } from "@azure-tools/test-utils-vitest";
import type { SmsClient } from "../../src/index.js";
import type { SmsClient, SmsSendOptions, SmsSendRequest } from "../../src/index.js";
import { Uuid } from "../../src/utils/uuid.js";
import sendSmsSuites from "../public/suites/smsClient.send.js";
import {
createRecordedSmsClient,
createRecordedSmsClientWithToken,
} from "../public/utils/recordedClient.js";
import { describe, vi, beforeEach, afterEach } from "vitest";
import { assertIsFailureResult, assertIsSuccessResult } from "../public/utils/assertHelpers.js";
import { describe, it, assert, vi, beforeEach, afterEach } from "vitest";

matrix([[true, false]], async function (useAad: boolean) {
describe(`SmsClient [Playback/Record]${useAad ? " [AAD]" : ""}`, async () => {
Expand Down Expand Up @@ -49,8 +49,125 @@ matrix([[true, false]], async function (useAad: boolean) {
}
});

describe("when sending SMS", () => {
sendSmsSuites(client);
describe("when sending SMS", async () => {
it("can send an SMS message", { timeout: 5000 }, async () => {
const fromNumber = env.AZURE_PHONE_NUMBER as string;
const validToNumber = env.AZURE_PHONE_NUMBER as string;
const results = await client.send({
from: fromNumber,
to: [validToNumber],
message: "test message",
});

assert.lengthOf(results, 1, "must return as many results as there were recipients");
assertIsSuccessResult(results[0], validToNumber);
});

it("can send an SMS message with options passed in", { timeout: 4000 }, async () => {
const fromNumber = env.AZURE_PHONE_NUMBER as string;
const validToNumber = env.AZURE_PHONE_NUMBER as string;
const results = await client.send(
{
from: fromNumber,
to: [validToNumber],
message: "test message",
},
{
enableDeliveryReport: true,
tag: "SMS_LIVE_TEST",
deliveryReportTimeoutInSeconds: 300,
},
);

assert.lengthOf(results, 1, "must return as many results as there were recipients");
assertIsSuccessResult(results[0], validToNumber);
});

it("sends a new message each time send is called", { timeout: 4000 }, async () => {
const fromNumber = env.AZURE_PHONE_NUMBER as string;
const validToNumber = env.AZURE_PHONE_NUMBER as string;

const sendRequest: SmsSendRequest = {
from: fromNumber,
to: [validToNumber],
message: "test message",
};
const options: SmsSendOptions = {
enableDeliveryReport: true,
tag: "SMS_LIVE_TEST",
};

const firstResults = await client.send(sendRequest, options);
const secondResults = await client.send(sendRequest, options);

assertIsSuccessResult(firstResults[0], validToNumber);
assertIsSuccessResult(secondResults[0], validToNumber);
assert.notEqual(firstResults[0].messageId, secondResults[0].messageId);
});

it("can send an SMS message to multiple recipients", { timeout: 4000 }, async () => {
const fromNumber = env.AZURE_PHONE_NUMBER as string;
const validToNumber = env.AZURE_PHONE_NUMBER as string;
const invalidToNumber = "+1425555012345"; // invalid number that's too long
const recipients = [validToNumber, invalidToNumber];

const results = await client.send({
from: fromNumber,
to: recipients,
message: "test message",
});

assert.lengthOf(
results,
recipients.length,
"must return as many results as there were recipients",
);

assertIsSuccessResult(results[0], validToNumber);
assertIsFailureResult(results[1], invalidToNumber, "Unknown country code.");
});

it("throws an exception when sending from a number you don't own", async () => {
const fromNumber = "+14255550123";
const validToNumber = env.AZURE_PHONE_NUMBER as string;
try {
await client.send(
{
from: fromNumber,
to: [validToNumber],
message: "test message",
},
{
enableDeliveryReport: true,
tag: "SMS_LIVE_TEST",
},
);
assert.fail("Should have thrown an error");
} catch (e: any) {
assert.equal(e.statusCode, 401);
}
});

it("throws an exception when sending from an invalid number", async () => {
const fromNumber = "+1425555012345"; // invalid number that's too long
const validToNumber = env.AZURE_PHONE_NUMBER as string;
try {
await client.send(
{
from: fromNumber,
to: [validToNumber],
message: "test message",
},
{
enableDeliveryReport: true,
tag: "SMS_LIVE_TEST",
},
);
assert.fail("Should have thrown an error");
} catch (e: any) {
assert.equal(e.statusCode, 401);
}
});
});
});
});
127 changes: 122 additions & 5 deletions sdk/communication/communication-sms/test/public/smsClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import type { Recorder } from "@azure-tools/test-recorder";
import { env, isPlaybackMode } from "@azure-tools/test-recorder";
import { matrix } from "@azure-tools/test-utils-vitest";
import type { SmsClient } from "../../src/index.js";
import type { SmsClient, SmsSendOptions, SmsSendRequest } from "../../src/index.js";
import { Uuid } from "../../src/utils/uuid.js";
import sendSmsSuites from "./suites/smsClient.send.js";
import {
createRecordedSmsClient,
createRecordedSmsClientWithToken,
} from "./utils/recordedClient.js";
import { describe, vi, beforeEach, afterEach } from "vitest";
import { assertIsFailureResult, assertIsSuccessResult } from "./utils/assertHelpers.js";
import { describe, it, assert, vi, beforeEach, afterEach } from "vitest";

matrix([[true, false]], async function (useAad: boolean) {
const skipIntSMSTests = env.COMMUNICATION_SKIP_INT_SMS_TEST === "true";
Expand Down Expand Up @@ -49,8 +49,125 @@ matrix([[true, false]], async function (useAad: boolean) {
}
});

describe("test send method", () => {
sendSmsSuites(client);
describe("test send method", async () => {
it("can send an SMS message", { timeout: 5000 }, async () => {
const fromNumber = env.AZURE_PHONE_NUMBER as string;
const validToNumber = env.AZURE_PHONE_NUMBER as string;
const results = await client.send({
from: fromNumber,
to: [validToNumber],
message: "test message",
});

assert.lengthOf(results, 1, "must return as many results as there were recipients");
assertIsSuccessResult(results[0], validToNumber);
});

it("can send an SMS message with options passed in", { timeout: 4000 }, async () => {
const fromNumber = env.AZURE_PHONE_NUMBER as string;
const validToNumber = env.AZURE_PHONE_NUMBER as string;
const results = await client.send(
{
from: fromNumber,
to: [validToNumber],
message: "test message",
},
{
enableDeliveryReport: true,
tag: "SMS_LIVE_TEST",
deliveryReportTimeoutInSeconds: 300,
},
);

assert.lengthOf(results, 1, "must return as many results as there were recipients");
assertIsSuccessResult(results[0], validToNumber);
});

it("sends a new message each time send is called", { timeout: 4000 }, async () => {
const fromNumber = env.AZURE_PHONE_NUMBER as string;
const validToNumber = env.AZURE_PHONE_NUMBER as string;

const sendRequest: SmsSendRequest = {
from: fromNumber,
to: [validToNumber],
message: "test message",
};
const options: SmsSendOptions = {
enableDeliveryReport: true,
tag: "SMS_LIVE_TEST",
};

const firstResults = await client.send(sendRequest, options);
const secondResults = await client.send(sendRequest, options);

assertIsSuccessResult(firstResults[0], validToNumber);
assertIsSuccessResult(secondResults[0], validToNumber);
assert.notEqual(firstResults[0].messageId, secondResults[0].messageId);
});

it("can send an SMS message to multiple recipients", { timeout: 4000 }, async () => {
const fromNumber = env.AZURE_PHONE_NUMBER as string;
const validToNumber = env.AZURE_PHONE_NUMBER as string;
const invalidToNumber = "+1425555012345"; // invalid number that's too long
const recipients = [validToNumber, invalidToNumber];

const results = await client.send({
from: fromNumber,
to: recipients,
message: "test message",
});

assert.lengthOf(
results,
recipients.length,
"must return as many results as there were recipients",
);

assertIsSuccessResult(results[0], validToNumber);
assertIsFailureResult(results[1], invalidToNumber, "Unknown country code.");
});

it("throws an exception when sending from a number you don't own", async () => {
const fromNumber = "+14255550123";
const validToNumber = env.AZURE_PHONE_NUMBER as string;
try {
await client.send(
{
from: fromNumber,
to: [validToNumber],
message: "test message",
},
{
enableDeliveryReport: true,
tag: "SMS_LIVE_TEST",
},
);
assert.fail("Should have thrown an error");
} catch (e: any) {
assert.equal(e.statusCode, 401);
}
});

it("throws an exception when sending from an invalid number", async () => {
const fromNumber = "+1425555012345"; // invalid number that's too long
const validToNumber = env.AZURE_PHONE_NUMBER as string;
try {
await client.send(
{
from: fromNumber,
to: [validToNumber],
message: "test message",
},
{
enableDeliveryReport: true,
tag: "SMS_LIVE_TEST",
},
);
assert.fail("Should have thrown an error");
} catch (e: any) {
assert.equal(e.statusCode, 401);
}
});
});
},
);
Expand Down
Loading

0 comments on commit 86555c0

Please sign in to comment.