Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Service Bus] Tests for SBClient token credential overload #9347

Merged
Merged
138 changes: 70 additions & 68 deletions sdk/servicebus/service-bus/test/serviceBusClient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { EnvironmentCredential } from "@azure/identity";
import chai from "chai";
import chaiAsPromised from "chai-as-promised";
import * as dotenv from "dotenv";
import Long from "long";
import { MessagingError, Receiver, ServiceBusClient, SessionReceiver } from "../src";
import { Sender } from "../src/sender";
import { getReceiverClosedErrorMsg, getSenderClosedErrorMsg } from "../src/util/errors";
import { TestClientType, TestMessage, checkWithTimeout, isMessagingError } from "./utils/testUtils";
import {
DispositionType,
ReceivedMessageWithLock,
ServiceBusMessage
} from "../src/serviceBusMessage";

const should = chai.should();
chai.use(chaiAsPromised);

import { getEnvVars, isNode } from "../test/utils/envVarUtils";
import * as dotenv from "dotenv";
dotenv.config();

// import { EnvironmentCredential } from "@azure/identity";
import { getReceiverClosedErrorMsg, getSenderClosedErrorMsg } from "../src/util/errors";
import { EnvVarNames, getEnvVars, isNode } from "../test/utils/envVarUtils";
import { checkWithTimeout, isMessagingError, TestClientType, TestMessage } from "./utils/testUtils";
import {
createServiceBusClientForTests,
EntityName,
ServiceBusClientForTests,
createServiceBusClientForTests,
testPeekMsgsLength
} from "./utils/testutils2";

const should = chai.should();
chai.use(chaiAsPromised);

dotenv.config();

describe("Create ServiceBusClient", function(): void {
let sbClient: ServiceBusClient;

Expand Down Expand Up @@ -283,29 +282,28 @@ describe("Test ServiceBusClient creation", function(): void {
"Endpoint=sb://((.*).servicebus.windows.net)"
) || "")[1];

// `keytar` being used in `@azure/identity` is causing the build to fail when imported for the tests.
// /**
// * Utility to create EnvironmentCredential using `@azure/identity`
// */
// function getDefaultTokenCredential() {
// should.exist(
// env[EnvVarNames.AZURE_CLIENT_ID],
// "define AZURE_CLIENT_ID in your environment before running integration tests."
// );
// should.exist(
// env[EnvVarNames.AZURE_TENANT_ID],
// "define AZURE_TENANT_ID in your environment before running integration tests."
// );
// should.exist(
// env[EnvVarNames.AZURE_CLIENT_SECRET],
// "define AZURE_CLIENT_SECRET in your environment before running integration tests."
// );
// should.exist(
// env[EnvVarNames.SERVICEBUS_CONNECTION_STRING],
// "define SERVICEBUS_CONNECTION_STRING in your environment before running integration tests."
// );
// return new EnvironmentCredential();
// }
/**
* Utility to create EnvironmentCredential using `@azure/identity`
*/
function getDefaultTokenCredential() {
should.exist(
env[EnvVarNames.AZURE_CLIENT_ID],
"define AZURE_CLIENT_ID in your environment before running integration tests."
);
should.exist(
env[EnvVarNames.AZURE_TENANT_ID],
"define AZURE_TENANT_ID in your environment before running integration tests."
);
should.exist(
env[EnvVarNames.AZURE_CLIENT_SECRET],
"define AZURE_CLIENT_SECRET in your environment before running integration tests."
);
should.exist(
env[EnvVarNames.SERVICEBUS_CONNECTION_STRING],
"define SERVICEBUS_CONNECTION_STRING in your environment before running integration tests."
);
return new EnvironmentCredential();
}

it("throws error for invalid tokenCredentials", async function(): Promise<void> {
try {
Expand Down Expand Up @@ -337,39 +335,43 @@ describe("Test ServiceBusClient creation", function(): void {
should.equal(errorWasThrown, true, "Error thrown flag must be true");
});

// if (isNode) {
// it("Coerces input to string for host in credential based constructor", async function(): Promise<
// void
// > {
// const tokenCreds = getDefaultTokenCredential();
// sbClient = new ServiceBusClient(123 as any, tokenCreds);
// should.equal(sbClient.name, "sb://123/", "Name of the namespace is different than expected");
// });

// it("sends a message to the ServiceBus entity", async function(): Promise<void> {
// const tokenCreds = getDefaultTokenCredential();

// const serviceBusClient = createServiceBusClientForTests();
// const entities = await serviceBusClient.test.createTestEntities(
// TestClientType.UnpartitionedQueue
// );
// await serviceBusClient.close();

// const sbClient = new ServiceBusClient(serviceBusEndpoint, tokenCreds);
// sbClient.should.be.an.instanceof(ServiceBusClient);

// const sender = sbClient.createSender(entities.queue!);
// const receiver = await sbClient.createReceiver(entities.queue!, "peekLock");
// const testMessages = TestMessage.getSample();
// await sender.send(testMessages);
// const msgs = await receiver.receiveBatch(1);

// should.equal(Array.isArray(msgs), true, "`ReceivedMessages` is not an array");
// should.equal(msgs[0].body, testMessages.body, "MessageBody is different than expected");
// should.equal(msgs.length, 1, "Unexpected number of messages");
// await sbClient.close();
// });
// }
if (isNode) {
it("throws error for invalid host name", async function(): Promise<void> {
try {
new ServiceBusClient(123 as any, getDefaultTokenCredential());
} catch (error) {
errorWasThrown = true;
should.equal(
error.message,
"`host` parameter is not a string",
"ErrorMessage is different than expected"
);
}
should.equal(errorWasThrown, true, "Error thrown flag must be true");
});

it("sends a message to the ServiceBus entity", async function(): Promise<void> {
const tokenCreds = getDefaultTokenCredential();

const serviceBusClient = createServiceBusClientForTests();
const entities = await serviceBusClient.test.createTestEntities(
TestClientType.UnpartitionedQueue
);
await serviceBusClient.close();

const sbClient = new ServiceBusClient(serviceBusEndpoint, tokenCreds);
const sender = sbClient.createSender(entities.queue!);
const receiver = sbClient.createReceiver(entities.queue!, "peekLock");
const testMessages = TestMessage.getSample();
await sender.send(testMessages);
const msgs = await receiver.receiveBatch(1);

should.equal(Array.isArray(msgs), true, "`ReceivedMessages` is not an array");
should.equal(msgs[0].body, testMessages.body, "MessageBody is different than expected");
should.equal(msgs.length, 1, "Unexpected number of messages");
await sbClient.close();
});
}
});

describe("Errors after close()", function(): void {
Expand Down