From 4c9f9ceb9941a0eabd34db4ebab8ad2f1eafda5d Mon Sep 17 00:00:00 2001 From: Craig Treasure Date: Wed, 3 Feb 2021 13:17:45 -0800 Subject: [PATCH] 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. --- .../Azure.MixedReality.Authentication.sln | 6 ++ .../shared/AuthenticationEndpoint.cs | 2 +- .../AuthenticationEndpointTests.cs | 33 ++++++++++ .../MixedRealityAccountKeyCredentialTests.cs | 62 +++++++++++++++++++ .../MixedRealityTokenCredentialTests.cs | 34 ++++++++++ ...e.MixedReality.Authentication.Tests.csproj | 38 ++++++++++++ .../StaticAccessTokenCredentialTests.cs | 41 ++++++++++++ 7 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/AuthenticationEndpointTests.cs create mode 100644 sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/MixedRealityAccountKeyCredentialTests.cs create mode 100644 sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/MixedRealityTokenCredentialTests.cs create mode 100644 sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/Shared.Azure.MixedReality.Authentication.Tests.csproj create mode 100644 sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/StaticAccessTokenCredentialTests.cs diff --git a/sdk/mixedreality/Azure.MixedReality.Authentication/Azure.MixedReality.Authentication.sln b/sdk/mixedreality/Azure.MixedReality.Authentication/Azure.MixedReality.Authentication.sln index f4361fb52d35..4d77138fb33f 100644 --- a/sdk/mixedreality/Azure.MixedReality.Authentication/Azure.MixedReality.Authentication.sln +++ b/sdk/mixedreality/Azure.MixedReality.Authentication/Azure.MixedReality.Authentication.sln @@ -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 @@ -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 diff --git a/sdk/mixedreality/Azure.MixedReality.Authentication/shared/AuthenticationEndpoint.cs b/sdk/mixedreality/Azure.MixedReality.Authentication/shared/AuthenticationEndpoint.cs index 0d87500f304d..1cb3026e044b 100644 --- a/sdk/mixedreality/Azure.MixedReality.Authentication/shared/AuthenticationEndpoint.cs +++ b/sdk/mixedreality/Azure.MixedReality.Authentication/shared/AuthenticationEndpoint.cs @@ -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)); } diff --git a/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/AuthenticationEndpointTests.cs b/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/AuthenticationEndpointTests.cs new file mode 100644 index 000000000000..75ca6e28d831 --- /dev/null +++ b/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/AuthenticationEndpointTests.cs @@ -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(() => AuthenticationEndpoint.ConstructFromDomain(null!)); + Assert.AreEqual("accountDomain", ex.ParamName); + + ex = Assert.Throws(() => AuthenticationEndpoint.ConstructFromDomain(string.Empty)); + Assert.AreEqual("accountDomain", ex.ParamName); + + ex = Assert.Throws(() => AuthenticationEndpoint.ConstructFromDomain(" ")); + Assert.AreEqual("accountDomain", ex.ParamName); + } + } +} diff --git a/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/MixedRealityAccountKeyCredentialTests.cs b/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/MixedRealityAccountKeyCredentialTests.cs new file mode 100644 index 000000000000..b9db2d50963b --- /dev/null +++ b/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/MixedRealityAccountKeyCredentialTests.cs @@ -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(() => new MixedRealityAccountKeyCredential(Guid.Empty, s_testAccountKey)); + Assert.AreEqual("accountId", ex.ParamName); + + ex = Assert.Throws(() => new MixedRealityAccountKeyCredential(s_testAccountId, (string)null!)); + Assert.AreEqual("key", ex.ParamName); + + ex = Assert.Throws(() => new MixedRealityAccountKeyCredential(s_testAccountId, "")); + Assert.AreEqual("key", ex.ParamName); + + ex = Assert.Throws(() => 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); + } + } +} diff --git a/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/MixedRealityTokenCredentialTests.cs b/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/MixedRealityTokenCredentialTests.cs new file mode 100644 index 000000000000..854cc617695a --- /dev/null +++ b/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/MixedRealityTokenCredentialTests.cs @@ -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); + } + } +} diff --git a/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/Shared.Azure.MixedReality.Authentication.Tests.csproj b/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/Shared.Azure.MixedReality.Authentication.Tests.csproj new file mode 100644 index 000000000000..841108535874 --- /dev/null +++ b/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/Shared.Azure.MixedReality.Authentication.Tests.csproj @@ -0,0 +1,38 @@ + + + true + true + true + Enable + + + + + + $(RequiredTargetFrameworks) + Azure.MixedReality.Authentication.Tests + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/StaticAccessTokenCredentialTests.cs b/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/StaticAccessTokenCredentialTests.cs new file mode 100644 index 000000000000..53b6a2f34b4d --- /dev/null +++ b/sdk/mixedreality/Azure.MixedReality.Authentication/tests/Shared.Azure.MixedReality.Authentication.Tests/StaticAccessTokenCredentialTests.cs @@ -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); + } + } +}