-
Notifications
You must be signed in to change notification settings - Fork 492
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
fix(service-client): Update support for AzureSasCredential for a better user experience #1797
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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
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
121 changes: 121 additions & 0 deletions
121
iothub/service/tests/IotHubSasCredentialPropertiesTests.cs
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,121 @@ | ||
// 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 Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Net; | ||
using System.Globalization; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Amqp; | ||
using FluentAssertions; | ||
|
||
#if !NET451 | ||
|
||
using Azure; | ||
|
||
#endif | ||
|
||
namespace Microsoft.Azure.Devices.Tests | ||
{ | ||
[TestClass] | ||
[TestCategory("Unit")] | ||
public class IotHubSasCredentialPropertiesTests | ||
{ | ||
private const string _hostName = "myiothub.azure-devices.net"; | ||
|
||
#if !NET451 | ||
|
||
[TestMethod] | ||
public async Task TestCbsTokenGeneration_Succeeds() | ||
{ | ||
// arrange | ||
DateTime expiresAtUtc = DateTime.UtcNow; | ||
DateTime updatedExpiresAtUtc = DateTime.UtcNow.AddDays(1); | ||
|
||
string token = string.Format( | ||
CultureInfo.InvariantCulture, | ||
"SharedAccessSignature sr={0}&sig={1}&se={2}", | ||
WebUtility.UrlEncode(_hostName), | ||
WebUtility.UrlEncode("signature"), | ||
expiresAtUtc); | ||
|
||
string updatedToken = string.Format( | ||
CultureInfo.InvariantCulture, | ||
"SharedAccessSignature sr={0}&sig={1}&se={2}", | ||
WebUtility.UrlEncode(_hostName), | ||
WebUtility.UrlEncode("signature"), | ||
updatedExpiresAtUtc); | ||
|
||
var azureSasCredential = new AzureSasCredential(token); | ||
var iotHubSasCredentialProperties = new IotHubSasCredentialProperties(_hostName, azureSasCredential); | ||
|
||
// act | ||
|
||
CbsToken cbsToken = await iotHubSasCredentialProperties.GetTokenAsync(null, null, null).ConfigureAwait(false); | ||
azureSasCredential.Update(updatedToken); | ||
CbsToken updatedCbsToken = await iotHubSasCredentialProperties.GetTokenAsync(null, null, null).ConfigureAwait(false); | ||
|
||
// assert | ||
cbsToken.ExpiresAtUtc.ToString().Should().Be(expiresAtUtc.ToString()); | ||
updatedCbsToken.ExpiresAtUtc.ToString().Should().Be(updatedExpiresAtUtc.ToString()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestCbsTokenGeneration_InvalidExpirationDateTimeFormat_Fails() | ||
{ | ||
// arrange | ||
string token = string.Format( | ||
CultureInfo.InvariantCulture, | ||
"SharedAccessSignature sr={0}&sig={1}&se={2}", | ||
WebUtility.UrlEncode(_hostName), | ||
WebUtility.UrlEncode("signature"), | ||
"01:01:2021"); | ||
|
||
var azureSasCredential = new AzureSasCredential(token); | ||
var iotHubSasCredentialProperties = new IotHubSasCredentialProperties(_hostName, azureSasCredential); | ||
|
||
try | ||
{ | ||
// act | ||
await iotHubSasCredentialProperties.GetTokenAsync(null, null, null).ConfigureAwait(false); | ||
|
||
Assert.Fail("The parsing of date time in invalid format on the SAS token should have caused an exception."); | ||
} | ||
catch (InvalidOperationException ex) | ||
{ | ||
// assert | ||
ex.Message.Should().Be($"Invalid expiration time on {nameof(AzureSasCredential)} signature."); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestCbsTokenGeneration_MissingExpiration_Fails() | ||
{ | ||
// arrange | ||
string token = string.Format( | ||
CultureInfo.InvariantCulture, | ||
"SharedAccessSignature sr={0}&sig={1}", | ||
WebUtility.UrlEncode(_hostName), | ||
WebUtility.UrlEncode("signature")); | ||
|
||
var azureSasCredential = new AzureSasCredential(token); | ||
var iotHubSasCredentialProperties = new IotHubSasCredentialProperties(_hostName, azureSasCredential); | ||
|
||
try | ||
{ | ||
// act | ||
await iotHubSasCredentialProperties.GetTokenAsync(null, null, null).ConfigureAwait(false); | ||
|
||
Assert.Fail("The missing expiry on the SAS token should have caused an exception."); | ||
} | ||
catch (InvalidOperationException ex) | ||
{ | ||
// assert | ||
ex.Message.Should().Be($"There is no expiration time on {nameof(AzureSasCredential)} signature."); | ||
} | ||
} | ||
|
||
#endif | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty cool use of linq here!