-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Communication] - Authentication - SmsClient auth using token credent…
…ial (#13260) * [smsClient] introduce new constructor * [smsClientWithToken] flesh out the unit test for sms client * [smsClientWithToken.spec.ts] adding new replacable variables for token auth * [smsClientWithToken.spec] Adding new sms recordings and skipping tests if credentials cannot be created. Mainly for browser mode * [recording] adding browser recordings * [recordedClient] introducing new utils for sms test
- Loading branch information
Showing
11 changed files
with
205 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...ers/smsclientwithtoken_playbacklive/recording_successfully_issues_a_token_for_a_user.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
12 changes: 7 additions & 5 deletions
12
...ommunication/communication-sms/recordings/node/smsclient/recording_sends_a_sms_message.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
.../node/smsclientwithtoken_playbacklive/recording_successfully_issues_a_token_for_a_user.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
sdk/communication/communication-sms/test/smsClientWithToken.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { record, Recorder, env } from "@azure/test-utils-recorder"; | ||
import { SendRequest, SmsClient } from "../src/smsClient"; | ||
import { assert } from "chai"; | ||
import { isNode } from "@azure/core-http"; | ||
import * as dotenv from "dotenv"; | ||
import { createCredential, recorderConfiguration } from "./utils/recordedClient"; | ||
|
||
if (isNode) { | ||
dotenv.config(); | ||
} | ||
|
||
describe("SmsClientWithToken [Playback/Live]", async () => { | ||
let recorder: Recorder; | ||
|
||
beforeEach(async function() { | ||
recorder = record(this, recorderConfiguration); | ||
}); | ||
|
||
afterEach(async function() { | ||
if (!this.currentTest?.isPending()) { | ||
await recorder.stop(); | ||
} | ||
}); | ||
|
||
it("successfully issues a token for a user", async function() { | ||
const credential = createCredential(); | ||
|
||
if (!credential) { | ||
this.skip(); | ||
} | ||
|
||
const endpoint = env.COMMUNICATION_ENDPOINT; | ||
const fromNumber = env.AZURE_PHONE_NUMBER; | ||
const toNumber = env.AZURE_PHONE_NUMBER; | ||
|
||
const smsClient = new SmsClient(endpoint, credential); | ||
const sendRequest: SendRequest = { | ||
from: fromNumber, | ||
to: [toNumber], | ||
message: "test message" | ||
}; | ||
|
||
const response = await smsClient.send(sendRequest); | ||
assert.equal(response._response.status, 200); | ||
}).timeout(5000); | ||
}); |
35 changes: 35 additions & 0 deletions
35
sdk/communication/communication-sms/test/utils/recordedClient.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { DefaultAzureCredential, TokenCredential } from "@azure/identity"; | ||
import { isPlaybackMode, RecorderEnvironmentSetup } from "@azure/test-utils-recorder"; | ||
|
||
export const recorderConfiguration: RecorderEnvironmentSetup = { | ||
replaceableVariables: { | ||
AZURE_COMMUNICATION_LIVETEST_CONNECTION_STRING: "endpoint=https://endpoint/;accesskey=banana", | ||
AZURE_PHONE_NUMBER: "+18005551234", | ||
COMMUNICATION_ENDPOINT: "https://endpoint/", | ||
AZURE_CLIENT_ID: "SomeClientId", | ||
AZURE_CLIENT_SECRET: "SomeClientSecret", | ||
AZURE_TENANT_ID: "SomeTenantId" | ||
}, | ||
customizationsOnRecordings: [ | ||
(recording: string): string => recording.replace(/(https:\/\/)([^\/',]*)/, "$1endpoint"), | ||
(recording: string): string => | ||
recording.replace(/"messageId"\s?:\s?"[^"]*"/g, `"messageId":"Sanitized"`) | ||
], | ||
queryParametersToSkip: [] | ||
}; | ||
|
||
export function createCredential(): TokenCredential | undefined { | ||
if (isPlaybackMode()) { | ||
return { | ||
getToken: async (_scopes) => { | ||
return { token: "testToken", expiresOnTimestamp: 11111 }; | ||
} | ||
}; | ||
} else { | ||
try { | ||
return new DefaultAzureCredential(); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
} |