-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests(service-client): E2E tests for aad auth on all our clients
- Loading branch information
Showing
5 changed files
with
175 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
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,138 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Microsoft.Azure.Devices.Client; | ||
using Microsoft.Azure.Devices.Common.Exceptions; | ||
using Microsoft.Azure.Devices.E2ETests.Helpers; | ||
using Microsoft.Azure.Devices.Shared; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
#if !NET451 | ||
|
||
using Microsoft.Rest; | ||
using Azure.Core; | ||
using Azure.Identity; | ||
|
||
#endif | ||
|
||
using ClientOptions = Microsoft.Azure.Devices.Client.ClientOptions; | ||
|
||
namespace Microsoft.Azure.Devices.E2ETests.iothub | ||
{ | ||
/// <summary> | ||
/// Tests to ensure authentication using Azure active directory succeeds in all the clients. | ||
/// </summary> | ||
[TestClass] | ||
[TestCategory("E2E")] | ||
[TestCategory("IoTHub")] | ||
public class TokenCredentialAuthenticationTests : E2EMsTestBase | ||
{ | ||
private readonly string _devicePrefix = $"E2E_{nameof(TokenCredentialAuthenticationTests)}_"; | ||
|
||
#if !NET451 | ||
|
||
[LoggedTestMethod] | ||
public async Task RegistryManager_Http_TokenCredentialAuth_Success() | ||
{ | ||
// arrange | ||
using var registryManager = RegistryManager.Create( | ||
Configuration.IoTHub.GetIotHubHostName(), | ||
Configuration.IoTHub.GetClientSecretCredential()); | ||
var device = new Device(Guid.NewGuid().ToString()); | ||
|
||
// act | ||
Device createdDevice = await registryManager.AddDeviceAsync(device).ConfigureAwait(false); | ||
|
||
// assert | ||
Assert.IsNotNull(createdDevice); | ||
await registryManager.RemoveDeviceAsync(device.Id).ConfigureAwait(false); | ||
} | ||
|
||
[LoggedTestMethod] | ||
public async Task JobClient_Http_TokenCredentialAuth_Success() | ||
{ | ||
// arrange | ||
using var jobClient = JobClient.Create( | ||
Configuration.IoTHub.GetIotHubHostName(), | ||
Configuration.IoTHub.GetClientSecretCredential()); | ||
|
||
string jobId = "JOBSAMPLE" + Guid.NewGuid().ToString(); | ||
string jobDeviceId = "JobsSample_Device"; | ||
string query = $"DeviceId IN ['{jobDeviceId}']"; | ||
Twin twin = new Twin(jobDeviceId); | ||
|
||
try | ||
{ | ||
// act | ||
JobResponse createJobResponse = await jobClient.ScheduleTwinUpdateAsync( | ||
jobId, | ||
query, | ||
twin, | ||
DateTime.UtcNow, | ||
(long)TimeSpan.FromMinutes(2).TotalSeconds).ConfigureAwait(false); | ||
} | ||
catch (ThrottlingException) | ||
{ | ||
// Concurrent jobs can be rejected, but it still means authentication was successful. Ignore the exception. | ||
} | ||
} | ||
|
||
[LoggedTestMethod] | ||
public async Task DigitalTwinClient_Http_TokenCredentialAuth_Success() | ||
{ | ||
// arrange | ||
TestDevice testDevice = await TestDevice.GetTestDeviceAsync(Logger, _devicePrefix).ConfigureAwait(false); | ||
string deviceId = testDevice.Id; | ||
string thermostatModelId = "dtmi:com:example:TemperatureController;1"; | ||
|
||
// Create a device client instance initializing it with the "Thermostat" model. | ||
var options = new ClientOptions | ||
{ | ||
ModelId = thermostatModelId, | ||
}; | ||
using DeviceClient deviceClient = testDevice.CreateDeviceClient(Client.TransportType.Mqtt, options); | ||
|
||
// Call openAsync() to open the device's connection, so that the ModelId is sent over Mqtt CONNECT packet. | ||
await deviceClient.OpenAsync().ConfigureAwait(false); | ||
|
||
using var digitalTwinClient = DigitalTwinClient.Create( | ||
Configuration.IoTHub.GetIotHubHostName(), | ||
Configuration.IoTHub.GetClientSecretCredential()); | ||
|
||
// act | ||
HttpOperationResponse<ThermostatTwin, DigitalTwinGetHeaders> response = await digitalTwinClient | ||
.GetDigitalTwinAsync<ThermostatTwin>(deviceId) | ||
.ConfigureAwait(false); | ||
ThermostatTwin twin = response.Body; | ||
|
||
// assert | ||
twin.Metadata.ModelId.Should().Be(thermostatModelId); | ||
} | ||
|
||
[LoggedTestMethod] | ||
public async Task Service_Amqp_TokenCredentialAuth_Success() | ||
{ | ||
// arrange | ||
TestDevice testDevice = await TestDevice.GetTestDeviceAsync(Logger, _devicePrefix).ConfigureAwait(false); | ||
string deviceId = testDevice.Id; | ||
using DeviceClient deviceClient = testDevice.CreateDeviceClient(Client.TransportType.Mqtt); | ||
await deviceClient.OpenAsync().ConfigureAwait(false); | ||
|
||
using var serviceClient = ServiceClient.Create( | ||
Configuration.IoTHub.GetIotHubHostName(), | ||
Configuration.IoTHub.GetClientSecretCredential(), | ||
TransportType.Amqp); | ||
|
||
// act | ||
await serviceClient.OpenAsync().ConfigureAwait(false); | ||
using var message = new Message(Encoding.ASCII.GetBytes("Hello, Cloud!")); | ||
await serviceClient.SendAsync(deviceId, message); | ||
} | ||
|
||
#endif | ||
} | ||
} |