forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mixed Reality Authentication: This change builds and tests the shared…
… code (Azure#18397) This change does the following: - Added a new Share.Azure.MixedReality.Authentication.Tests project to test the shared code. I did find 1 bug that presented when compiling with .NET 5.0 due to better nullable annotations. I had to pull the shared code in as source because I couldn't get `InternalsVisisbleTo` to work in this repo. - This change adds a project file to build the Mixed Reality Authentication shared code to ensure it stays healthy. - It took a while to figure out how to add a project that wasn't going to be packaged and shipped, but could also access shared code and what not. You'll see me defining the `AzureCoreSharedSources` property manually because that property is only set for official client libraries which have certain criteria like starting with "Azure.".
- Loading branch information
1 parent
618afcf
commit eeebae4
Showing
14 changed files
with
243 additions
and
3 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
20 changes: 20 additions & 0 deletions
20
.../Azure.MixedReality.Authentication/shared/Shared.Azure.MixedReality.Authentication.csproj
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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<LangVersion>latest</LangVersion> | ||
<Nullable>Enable</Nullable> | ||
<AzureCoreSharedSources>$(RepoRoot)/sdk/core/Azure.Core/src/Shared/</AzureCoreSharedSources> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\src\Azure.MixedReality.Authentication.csproj" /> | ||
</ItemGroup> | ||
|
||
<!-- Shared source from Azure.Core --> | ||
<ItemGroup> | ||
<Compile Include="$(AzureCoreSharedSources)Argument.cs" Link="Shared\%(RecursiveDir)\%(Filename)%(Extension)" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions
33
...ation/tests/Shared.Azure.MixedReality.Authentication.Tests/AuthenticationEndpointTests.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,33 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.MixedReality.Authentication.Tests | ||
{ | ||
public class AuthenticationEndpointTests | ||
{ | ||
[Test] | ||
public void ConstructFromDomain() | ||
{ | ||
Uri expected = new Uri("https://sts.eastus2.mixedreality.com"); | ||
Uri actual = AuthenticationEndpoint.ConstructFromDomain("eastus2.mixedreality.com"); | ||
|
||
Assert.AreEqual(expected, actual); | ||
} | ||
|
||
[Test] | ||
public void ConstructFromDomainWithInvalidParameters() | ||
{ | ||
ArgumentException ex = Assert.Throws<ArgumentNullException>(() => AuthenticationEndpoint.ConstructFromDomain(null!)); | ||
Assert.AreEqual("accountDomain", ex.ParamName); | ||
|
||
ex = Assert.Throws<ArgumentException>(() => AuthenticationEndpoint.ConstructFromDomain(string.Empty)); | ||
Assert.AreEqual("accountDomain", ex.ParamName); | ||
|
||
ex = Assert.Throws<ArgumentException>(() => AuthenticationEndpoint.ConstructFromDomain(" ")); | ||
Assert.AreEqual("accountDomain", ex.ParamName); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...s/Shared.Azure.MixedReality.Authentication.Tests/MixedRealityAccountKeyCredentialTests.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,62 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Azure.Core; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.MixedReality.Authentication.Tests | ||
{ | ||
public class MixedRealityAccountKeyCredentialTests | ||
{ | ||
private const string ExpectedTestToken = "87e9abb1-79b9-4502-bbae-cfae8c610f23:my_account_key"; | ||
|
||
private static readonly Guid s_testAccountId = Guid.Parse("87e9abb1-79b9-4502-bbae-cfae8c610f23"); | ||
|
||
private static readonly string s_testAccountKey = "my_account_key"; | ||
|
||
private static readonly AzureKeyCredential s_testKeyCredential = new AzureKeyCredential(s_testAccountKey); | ||
|
||
[Test] | ||
public void Create() | ||
{ | ||
new MixedRealityAccountKeyCredential(s_testAccountId, s_testAccountKey); | ||
new MixedRealityAccountKeyCredential(s_testAccountId, s_testKeyCredential); | ||
} | ||
|
||
[Test] | ||
public void CreateWithInvalidParameters() | ||
{ | ||
ArgumentException ex = Assert.Throws<ArgumentException>(() => new MixedRealityAccountKeyCredential(Guid.Empty, s_testAccountKey)); | ||
Assert.AreEqual("accountId", ex.ParamName); | ||
|
||
ex = Assert.Throws<ArgumentNullException>(() => new MixedRealityAccountKeyCredential(s_testAccountId, (string)null!)); | ||
Assert.AreEqual("key", ex.ParamName); | ||
|
||
ex = Assert.Throws<ArgumentException>(() => new MixedRealityAccountKeyCredential(s_testAccountId, "")); | ||
Assert.AreEqual("key", ex.ParamName); | ||
|
||
ex = Assert.Throws<ArgumentNullException>(() => new MixedRealityAccountKeyCredential(s_testAccountId, (AzureKeyCredential)null!)); | ||
Assert.AreEqual("keyCredential", ex.ParamName); | ||
} | ||
|
||
[Test] | ||
public void GetToken() | ||
{ | ||
MixedRealityAccountKeyCredential credential = new MixedRealityAccountKeyCredential(s_testAccountId, s_testKeyCredential); | ||
AccessToken token = credential.GetToken(default, default); | ||
Assert.AreEqual(ExpectedTestToken, token.Token); | ||
Assert.AreEqual(DateTimeOffset.MaxValue, token.ExpiresOn); | ||
} | ||
|
||
[Test] | ||
public async Task GetTokenAsync() | ||
{ | ||
MixedRealityAccountKeyCredential credential = new MixedRealityAccountKeyCredential(s_testAccountId, s_testKeyCredential); | ||
AccessToken token = await credential.GetTokenAsync(default, default); | ||
Assert.AreEqual(ExpectedTestToken, token.Token); | ||
Assert.AreEqual(DateTimeOffset.MaxValue, token.ExpiresOn); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
.../tests/Shared.Azure.MixedReality.Authentication.Tests/MixedRealityTokenCredentialTests.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,34 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using Azure.Core; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.MixedReality.Authentication.Tests | ||
{ | ||
public class MixedRealityTokenCredentialTests | ||
{ | ||
private static readonly Guid s_testAccountId = Guid.Parse("87e9abb1-79b9-4502-bbae-cfae8c610f23"); | ||
|
||
private static readonly Uri s_testEndpoint = new Uri("https://sts.my.mixedreality.endpoint.com"); | ||
|
||
[Test] | ||
public void GetMixedRealityCredential() | ||
{ | ||
MixedRealityAccountKeyCredential credential = new MixedRealityAccountKeyCredential(s_testAccountId, "my_account_key"); | ||
TokenCredential actual = MixedRealityTokenCredential.GetMixedRealityCredential(s_testAccountId, s_testEndpoint, credential); | ||
|
||
Assert.AreNotEqual(credential, actual); | ||
} | ||
|
||
[Test] | ||
public void GetMixedRealityCredentialWithStatic() | ||
{ | ||
StaticAccessTokenCredential credential = new StaticAccessTokenCredential(default); | ||
TokenCredential actual = MixedRealityTokenCredential.GetMixedRealityCredential(s_testAccountId, s_testEndpoint, credential); | ||
|
||
Assert.AreEqual(credential, actual); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...e.MixedReality.Authentication.Tests/Shared.Azure.MixedReality.Authentication.Tests.csproj
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,38 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<IsClientLibrary>true</IsClientLibrary> | ||
<IsDataPlaneProject>true</IsDataPlaneProject> | ||
<IsTestProject>true</IsTestProject> | ||
<Nullable>Enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<Import Project="$(RepoEngPath)\Directory.Build.Data.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks> | ||
<RootNamespace>Azure.MixedReality.Authentication.Tests</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.Identity" /> | ||
<PackageReference Include="NUnit" /> | ||
<PackageReference Include="NUnit3TestAdapter" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="Moq" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Azure.MixedReality.Authentication.csproj" /> | ||
</ItemGroup> | ||
|
||
<!-- Shared source from Azure.Core --> | ||
<ItemGroup> | ||
<Compile Include="$(AzureCoreSharedSources)Argument.cs" Link="Shared\%(RecursiveDir)\%(Filename)%(Extension)" /> | ||
</ItemGroup> | ||
|
||
<!-- Shared source from Azure.MixedReality.Authentication --> | ||
<ItemGroup> | ||
<Compile Include="$(AzureMixedRealityAuthenticationSharedSources)\*.cs" Link="Shared\%(RecursiveDir)\%(Filename)%(Extension)" /> | ||
</ItemGroup> | ||
|
||
</Project> |
41 changes: 41 additions & 0 deletions
41
.../tests/Shared.Azure.MixedReality.Authentication.Tests/StaticAccessTokenCredentialTests.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,41 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Azure.Core; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.MixedReality.Authentication.Tests | ||
{ | ||
public class StaticAccessTokenCredentialTests | ||
{ | ||
private const string ExpectedTestToken = "my_access_token"; | ||
|
||
private static readonly AccessToken s_fakeAccessToken = new AccessToken(ExpectedTestToken, DateTimeOffset.MaxValue); | ||
|
||
[Test] | ||
public void Create() | ||
{ | ||
new StaticAccessTokenCredential(s_fakeAccessToken); | ||
} | ||
|
||
[Test] | ||
public void GetToken() | ||
{ | ||
StaticAccessTokenCredential credential = new StaticAccessTokenCredential(s_fakeAccessToken); | ||
AccessToken token = credential.GetToken(default, default); | ||
Assert.AreEqual(ExpectedTestToken, token.Token); | ||
Assert.AreEqual(DateTimeOffset.MaxValue, token.ExpiresOn); | ||
} | ||
|
||
[Test] | ||
public async Task GetTokenAsync() | ||
{ | ||
StaticAccessTokenCredential credential = new StaticAccessTokenCredential(s_fakeAccessToken); | ||
AccessToken token = await credential.GetTokenAsync(default, default); | ||
Assert.AreEqual(ExpectedTestToken, token.Token); | ||
Assert.AreEqual(DateTimeOffset.MaxValue, token.ExpiresOn); | ||
} | ||
} | ||
} |