Skip to content

Commit

Permalink
Added Azure Mixed Reality Authentication shared code tests
Browse files Browse the repository at this point in the history
  - This change adds 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.
  • Loading branch information
Craig Treasure committed Feb 4, 2021
1 parent 67bc005 commit 4c9f9ce
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Core.TestFramework",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared.Azure.MixedReality.Authentication", "shared\Shared.Azure.MixedReality.Authentication.csproj", "{BB9881FB-C892-4EFA-93F0-3A1B4DE018F6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared.Azure.MixedReality.Authentication.Tests", "tests\Shared.Azure.MixedReality.Authentication.Tests\Shared.Azure.MixedReality.Authentication.Tests.csproj", "{A128CD7B-EABC-48C7-BAD2-216007EF130D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -33,6 +35,10 @@ Global
{BB9881FB-C892-4EFA-93F0-3A1B4DE018F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB9881FB-C892-4EFA-93F0-3A1B4DE018F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB9881FB-C892-4EFA-93F0-3A1B4DE018F6}.Release|Any CPU.Build.0 = Release|Any CPU
{A128CD7B-EABC-48C7-BAD2-216007EF130D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A128CD7B-EABC-48C7-BAD2-216007EF130D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A128CD7B-EABC-48C7-BAD2-216007EF130D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A128CD7B-EABC-48C7-BAD2-216007EF130D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static Uri ConstructFromDomain(string accountDomain)
{
Argument.AssertNotNullOrWhiteSpace(accountDomain, nameof(accountDomain));

if (!Uri.TryCreate($"https://sts.{accountDomain}", UriKind.Absolute, out Uri result))
if (!Uri.TryCreate($"https://sts.{accountDomain}", UriKind.Absolute, out Uri? result))
{
throw new ArgumentException("The value could not be used to construct a valid endpoint.", nameof(accountDomain));
}
Expand Down
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);
}
}
}
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);
}
}
}
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);
}
}
}
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>
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);
}
}
}

0 comments on commit 4c9f9ce

Please sign in to comment.