Skip to content

Commit

Permalink
Adding in test for authentication via SAS key for ServiceBus
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpark-msft committed Sep 2, 2020
1 parent 673afa5 commit 1f4b461
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions sdk/servicebus/service-bus/test/auth.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { ServiceBusConnectionStringModel, SharedKeyCredential } from "@azure/core-amqp";
import chai from "chai";
import { parseConnectionString } from "rhea-promise";
import { ServiceBusReceiver } from "../src/receivers/receiver";
import { ServiceBusClient } from "../src/serviceBusClient";
import { ReceivedMessage } from "../src/serviceBusMessage";
import { getEnvVars } from "./utils/envVarUtils";
import { TestClientType } from "./utils/testUtils";
import {
createServiceBusClientForTests,
ServiceBusClientForTests,
ServiceBusTestHelpers
} from "./utils/testutils2";
const assert = chai.assert;

type UnpackReturnType<T extends (...args: any) => any> = ReturnType<T> extends Promise<infer U>
? U
: never;

[TestClientType.UnpartitionedQueue, TestClientType.UnpartitionedSubscription].forEach(
(entityType) => {
describe(`Authentication via SAS to ${TestClientType[entityType]}`, () => {
let tempClient: ServiceBusClientForTests;
let entities: UnpackReturnType<ServiceBusTestHelpers["createTestEntities"]>;
let sasConnectionString: string;

before(async () => {
tempClient = await createServiceBusClientForTests();
entities = await tempClient.test.createTestEntities(entityType);

const { SERVICEBUS_CONNECTION_STRING: serviceBusConnectionString } = getEnvVars();

const { Endpoint: fqdn } = parseConnectionString<ServiceBusConnectionStringModel>(
serviceBusConnectionString
);

sasConnectionString = getSasConnectionString(
serviceBusConnectionString,
entities.queue ?? `${entities.topic!}`,
fqdn.replace(/\/+$/, "")
);
});

after(async () => {
await tempClient.test.afterEach();
await tempClient.test.after();
});

it("ServiceBusClient", async () => {
const client = new ServiceBusClient(sasConnectionString);

const sender = await tempClient.createSender(entities.queue ?? entities.topic!);

await sender.sendMessages({
body: "Hello"
});

await sender.close();

let receiver: ServiceBusReceiver<ReceivedMessage>;

if (entities.queue) {
receiver = client.createReceiver(entities.queue!, {
receiveMode: "receiveAndDelete"
});
} else {
receiver = client.createReceiver(entities.topic!, entities.subscription!, {
receiveMode: "receiveAndDelete"
});
}

const messages = await receiver.receiveMessages(1, {
maxWaitTimeInMs: 10 * 1000
});

await receiver.close();

assert.equal(messages.length, 1, "Should have received at least one message");
await client.close();
});

function getSasConnectionString(
connectionString: string,
path: string,
fqdn: string
): string {
const sas = SharedKeyCredential.fromConnectionString(connectionString).getToken(
`${fqdn}/${path}`
).token;

return `Endpoint=${fqdn};SharedAccessSignature=${sas}`;
}
});
}
);

0 comments on commit 1f4b461

Please sign in to comment.