Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions src/Authentication/Authentication.Test/Common/GraphSettingsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
namespace Microsoft.Graph.Authentication.Test.Common
{
using Microsoft.Graph.Authentication.Test.Mocks;
using Microsoft.Graph.PowerShell.Authentication;
using Microsoft.Graph.PowerShell.Authentication.Common;
using Microsoft.Graph.PowerShell.Authentication.Interfaces;
using Microsoft.Graph.PowerShell.Authentication.Models;
using System.Linq;
using Xunit;

public class GraphSettingsTests
{
[Fact]
public void ShouldSerializeAndDeserializeSettings()
{
GraphSession.Initialize(() => new GraphSession());

// Arrange
GraphSession.Instance.DataStore = new MockDataStore();
GraphSettings settings = new GraphSettings(ProtectedFileProvider.CreateFileProvider(Constants.SettingFilePath, FileProtection.SharedRead));

GraphEnvironment userDefinedEnv = new GraphEnvironment
{
Name = "TestCloud",
Type = GraphEnvironmentConstants.EnvironmentType.UserDefined,
AzureADEndpoint = "https://tester.com",
GraphEndpoint = "https://tester.com"
};

settings.EnvironmentTable[userDefinedEnv.Name] = userDefinedEnv;

// Act
string serializedSettings = settings.ToString();
settings.TryDeserializeObject(serializedSettings, out GraphSettings deserializedSettings, new GraphSettingsConverter());
deserializedSettings.TryGetEnvironment(userDefinedEnv.Name, out IGraphEnvironment deserializedEnv);

// Assert
Assert.NotNull(deserializedSettings);
Assert.NotNull(deserializedEnv);
Assert.Equal(serializedSettings, deserializedSettings.ToString());
Assert.Equal(userDefinedEnv.GraphEndpoint, deserializedEnv.GraphEndpoint);

GraphSession.Reset();
}

[Fact]
public void ShouldSaveSettingsToConfiguredDataStore()
{
GraphSession.Initialize(() => new GraphSession());

// Arrange
GraphSession.Instance.DataStore = new MockDataStore();
GraphSettings settings = new GraphSettings(ProtectedFileProvider.CreateFileProvider(Constants.SettingFilePath, FileProtection.SharedRead));

GraphEnvironment userDefinedEnv = new GraphEnvironment
{
Name = "TestCloud",
Type = GraphEnvironmentConstants.EnvironmentType.UserDefined,
AzureADEndpoint = "https://tester.com",
GraphEndpoint = "https://tester.com"
};
string expectedSettingsContent = @"{
""EnvironmentTable"": {
""TestCloud"": {
""Name"": ""TestCloud"",
""AzureADEndpoint"": ""https://tester.com"",
""GraphEndpoint"": ""https://tester.com"",
""Type"": ""User-defined""
}
}
}";

// Act
// Saves settings to disk store.
settings.TrySetEnvironment(userDefinedEnv, out IGraphEnvironment savedEnvironment);
string settingsContent = GraphSession.Instance.DataStore.ReadFileAsText(Constants.SettingFilePath).Substring(1).TrimEnd(new[] { '\0' });

// Assert
Assert.NotEmpty(settingsContent);
Assert.Equal(expectedSettingsContent, settingsContent);

GraphSession.Reset();
}

[Fact]
public void ShouldLoadSettingsFromConfiguredDataStore()
{
GraphSession.Initialize(() => new GraphSession());

// Arrange
GraphSession.Instance.DataStore = new MockDataStore();
string settingsContent = @"{
""EnvironmentTable"": {
""MyNewCloud"": {
""Name"": ""MyNewCloud"",
""AzureADEndpoint"": ""https://login.MyNewCloud.com"",
""GraphEndpoint"": ""https://graph.MyNewCloud.com"",
""Type"": ""User-defined""
},
""TrialCloud"": {
""Name"": ""MyNewCloud"",
""AzureADEndpoint"": ""https://login.TrialCloud.com"",
""GraphEndpoint"": ""https://graph.TrialCloud.com"",
""Type"": ""User-defined""
}
}
}";
GraphSession.Instance.DataStore.WriteFile(Constants.SettingFilePath, settingsContent);

// Act
// Loads settings from disk store.
GraphSettings settings = new GraphSettings(ProtectedFileProvider.CreateFileProvider(Constants.SettingFilePath, FileProtection.SharedRead));

settings.TryGetEnvironment("MyNewCloud", out IGraphEnvironment loadedEnvironment);

// Assert
Assert.NotNull(loadedEnvironment);
// 5 built-in + 2 user-defined
Assert.Equal(7, settings.Environments.Count());
Assert.Equal("https://login.MyNewCloud.com", loadedEnvironment.AzureADEndpoint);
Assert.Equal("https://graph.MyNewCloud.com", loadedEnvironment.GraphEndpoint);
Assert.Equal(GraphEnvironmentConstants.EnvironmentType.UserDefined, loadedEnvironment.Type);

GraphSession.Reset();
}

[Fact]
public void ShouldRemoveSettingsFromConfiguredDataStore()
{
GraphSession.Initialize(() => new GraphSession());

// Arrange
GraphSession.Instance.DataStore = new MockDataStore();
GraphSettings settings = new GraphSettings(ProtectedFileProvider.CreateFileProvider(Constants.SettingFilePath, FileProtection.SharedRead));
GraphEnvironment myNewCloudEnv = new GraphEnvironment
{
Name = "MyNewCloud",
Type = GraphEnvironmentConstants.EnvironmentType.UserDefined,
AzureADEndpoint = "https://login.MyNewCloud.com",
GraphEndpoint = "https://graph.MyNewCloud.com"
};
GraphEnvironment trialCloudEnv = new GraphEnvironment
{
Name = "TrialCloud",
Type = GraphEnvironmentConstants.EnvironmentType.UserDefined,
AzureADEndpoint = "https://login.TrialCloud.com",
GraphEndpoint = "https://graph.TrialCloud.com"
};
settings.TrySetEnvironment(myNewCloudEnv, out IGraphEnvironment mergedMyNewCloudEnv);
settings.TrySetEnvironment(trialCloudEnv, out IGraphEnvironment mergedTrialCloudEnv);

// Act
settings.RemoveEnvironment(trialCloudEnv.Name);
string settingsContent = GraphSession.Instance.DataStore.ReadFileAsText(Constants.SettingFilePath);

// Assert
Assert.NotEmpty(settingsContent);
// 5 built-in + 1 user-defined
Assert.Equal(6, settings.Environments.Count());

GraphSession.Reset();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
namespace Microsoft.Graph.Authentication.Test
{
using Microsoft.Graph.PowerShell.Authentication;
using Microsoft.Graph.PowerShell.Authentication.Handlers;
using Microsoft.Graph.PowerShell.Authentication.Models;
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

public class NationalCloudHandlerTests : IDisposable
{
private HttpMessageInvoker _invoker;
private FakeSuccessHandler _fakeSuccessHandler;
private NationalCloudHandler _nationalCloudHandler;
private string topParam = "$top=5";
private string selectParam = "Select=DisplayName%2CId%2CMail%2CUserType";

public NationalCloudHandlerTests()
{
this._fakeSuccessHandler = new FakeSuccessHandler();
this._nationalCloudHandler = new NationalCloudHandler(new ODataQueryOptionsHandler(_fakeSuccessHandler));
this._invoker = new HttpMessageInvoker(_nationalCloudHandler);
}

public void Dispose()
{
Dispose(true);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this._invoker.Dispose();
}
}


[Fact]
public async Task ShouldUseGlobalCloudWhenEnvironmentIsNotSet()
{
GraphSession.Initialize(() => new GraphSession());

// Arrange
GraphSession.Instance.Environment = null;
string initialRequestUrl = $"https://graph.microsoft.com/v1.0/users?{topParam}&{selectParam}";
Uri requestUrl = new Uri(initialRequestUrl);
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUrl);

// Act
var response = await this._invoker.SendAsync(httpRequestMessage, new CancellationToken());
var sentRequestQuery = response.RequestMessage.RequestUri.Query;

// Assert
Assert.Equal(requestUrl.Scheme, response.RequestMessage.RequestUri.Scheme);
Assert.Equal(requestUrl.Host, response.RequestMessage.RequestUri.Host);
Assert.Contains(topParam, sentRequestQuery);
Assert.Contains($"${selectParam}", sentRequestQuery);
Assert.Equal(2, sentRequestQuery.Split('&').Length);

GraphSession.Reset();
}

[Fact]
public async Task ShouldUseGermanyCloudWhenEnvironmentIsSetToGermany()
{
GraphSession.Initialize(() => new GraphSession());

// Arrange
GraphEnvironment germanyEnvironment = GraphEnvironment.BuiltInEnvironments[GraphEnvironmentConstants.EnvironmentName.Germany];
GraphSession.Instance.Environment = germanyEnvironment;
Uri requestUrl = new Uri($"https://graph.microsoft.com/beta/users?{topParam}&{selectParam}");
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUrl);

// Act
var response = await this._invoker.SendAsync(httpRequestMessage, new CancellationToken());
var sentRequestQuery = response.RequestMessage.RequestUri.Query;

// Assert
Assert.Equal(requestUrl.Scheme, response.RequestMessage.RequestUri.Scheme);
Assert.Equal("graph.microsoft.de", response.RequestMessage.RequestUri.Host);
Assert.Contains(topParam, sentRequestQuery);
Assert.Contains(selectParam, sentRequestQuery);
Assert.Equal(2, sentRequestQuery.Split('&').Length);

GraphSession.Reset();
}

[Fact]
public async Task ShouldUseUserDefinedCloudWhenEnvironmentIsSetToAUserDefinedCloud()
{
GraphSession.Initialize(() => new GraphSession());

// Arrange
GraphEnvironment userDefinedEnvironment = new GraphEnvironment
{
Name = "Canary",
AzureADEndpoint = "https://login.microsoftonline.com",
GraphEndpoint = "https://canary.graph.microsoft.com"
};
GraphSession.Instance.Environment = userDefinedEnvironment;
Uri requestUrl = new Uri($"https://graph.microsoft.com/v1.0/users?{topParam}&{selectParam}");
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUrl);

// Act
var response = await this._invoker.SendAsync(httpRequestMessage, new CancellationToken());
var sentRequestQuery = response.RequestMessage.RequestUri.Query;

// Assert
Assert.Equal(requestUrl.Scheme, response.RequestMessage.RequestUri.Scheme);
Assert.Equal("canary.graph.microsoft.com", response.RequestMessage.RequestUri.Host);
Assert.Contains(topParam, sentRequestQuery);
Assert.Contains($"${selectParam}", sentRequestQuery);
Assert.Equal(2, sentRequestQuery.Split('&').Length);

GraphSession.Reset();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Microsoft.Graph.Authentication.Test
{
using Microsoft.Graph.PowerShell.Authentication.Helpers;
using Microsoft.Graph.PowerShell.Authentication.Handlers;
using System;
using System.Linq;
using System.Net.Http;
Expand All @@ -24,9 +24,18 @@ public ODataQueryOptionsHandlerTests()

public void Dispose()
{
this._invoker.Dispose();
Dispose(true);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this._invoker.Dispose();
}
}


[Fact]
public async Task ShouldAddDollarSignToStandardODataQueryOptionsToV1Endpoint()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand Down
Loading