-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Azure Mixed Reality Authentication shared code tests
- 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
Showing
7 changed files
with
215 additions
and
1 deletion.
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
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); | ||
} | ||
} | ||
} |