Skip to content

Commit e828a22

Browse files
Merge branch 'dev' into BrianTJackett-patch-1
2 parents d214bed + 0994f8f commit e828a22

38 files changed

+2429
-71
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
namespace Microsoft.Graph.Authentication.Test.Common
2+
{
3+
using Microsoft.Graph.Authentication.Test.Mocks;
4+
using Microsoft.Graph.PowerShell.Authentication;
5+
using Microsoft.Graph.PowerShell.Authentication.Common;
6+
using Microsoft.Graph.PowerShell.Authentication.Interfaces;
7+
using Microsoft.Graph.PowerShell.Authentication.Models;
8+
using System.Linq;
9+
using Xunit;
10+
11+
public class GraphSettingsTests
12+
{
13+
[Fact]
14+
public void ShouldSerializeAndDeserializeSettings()
15+
{
16+
GraphSession.Initialize(() => new GraphSession());
17+
18+
// Arrange
19+
GraphSession.Instance.DataStore = new MockDataStore();
20+
GraphSettings settings = new GraphSettings(ProtectedFileProvider.CreateFileProvider(Constants.SettingFilePath, FileProtection.SharedRead));
21+
22+
GraphEnvironment userDefinedEnv = new GraphEnvironment
23+
{
24+
Name = "TestCloud",
25+
Type = GraphEnvironmentConstants.EnvironmentType.UserDefined,
26+
AzureADEndpoint = "https://tester.com",
27+
GraphEndpoint = "https://tester.com"
28+
};
29+
30+
settings.EnvironmentTable[userDefinedEnv.Name] = userDefinedEnv;
31+
32+
// Act
33+
string serializedSettings = settings.ToString();
34+
settings.TryDeserializeObject(serializedSettings, out GraphSettings deserializedSettings, new GraphSettingsConverter());
35+
deserializedSettings.TryGetEnvironment(userDefinedEnv.Name, out IGraphEnvironment deserializedEnv);
36+
37+
// Assert
38+
Assert.NotNull(deserializedSettings);
39+
Assert.NotNull(deserializedEnv);
40+
Assert.Equal(serializedSettings, deserializedSettings.ToString());
41+
Assert.Equal(userDefinedEnv.GraphEndpoint, deserializedEnv.GraphEndpoint);
42+
43+
GraphSession.Reset();
44+
}
45+
46+
[Fact]
47+
public void ShouldSaveSettingsToConfiguredDataStore()
48+
{
49+
GraphSession.Initialize(() => new GraphSession());
50+
51+
// Arrange
52+
GraphSession.Instance.DataStore = new MockDataStore();
53+
GraphSettings settings = new GraphSettings(ProtectedFileProvider.CreateFileProvider(Constants.SettingFilePath, FileProtection.SharedRead));
54+
55+
GraphEnvironment userDefinedEnv = new GraphEnvironment
56+
{
57+
Name = "TestCloud",
58+
Type = GraphEnvironmentConstants.EnvironmentType.UserDefined,
59+
AzureADEndpoint = "https://tester.com",
60+
GraphEndpoint = "https://tester.com"
61+
};
62+
string expectedSettingsContent = @"{
63+
""EnvironmentTable"": {
64+
""TestCloud"": {
65+
""Name"": ""TestCloud"",
66+
""AzureADEndpoint"": ""https://tester.com"",
67+
""GraphEndpoint"": ""https://tester.com"",
68+
""Type"": ""User-defined""
69+
}
70+
}
71+
}";
72+
73+
// Act
74+
// Saves settings to disk store.
75+
settings.TrySetEnvironment(userDefinedEnv, out IGraphEnvironment savedEnvironment);
76+
string settingsContent = GraphSession.Instance.DataStore.ReadFileAsText(Constants.SettingFilePath).Substring(1).TrimEnd(new[] { '\0' });
77+
78+
// Assert
79+
Assert.NotEmpty(settingsContent);
80+
Assert.Equal(expectedSettingsContent, settingsContent);
81+
82+
GraphSession.Reset();
83+
}
84+
85+
[Fact]
86+
public void ShouldLoadSettingsFromConfiguredDataStore()
87+
{
88+
GraphSession.Initialize(() => new GraphSession());
89+
90+
// Arrange
91+
GraphSession.Instance.DataStore = new MockDataStore();
92+
string settingsContent = @"{
93+
""EnvironmentTable"": {
94+
""MyNewCloud"": {
95+
""Name"": ""MyNewCloud"",
96+
""AzureADEndpoint"": ""https://login.MyNewCloud.com"",
97+
""GraphEndpoint"": ""https://graph.MyNewCloud.com"",
98+
""Type"": ""User-defined""
99+
},
100+
""TrialCloud"": {
101+
""Name"": ""MyNewCloud"",
102+
""AzureADEndpoint"": ""https://login.TrialCloud.com"",
103+
""GraphEndpoint"": ""https://graph.TrialCloud.com"",
104+
""Type"": ""User-defined""
105+
}
106+
}
107+
}";
108+
GraphSession.Instance.DataStore.WriteFile(Constants.SettingFilePath, settingsContent);
109+
110+
// Act
111+
// Loads settings from disk store.
112+
GraphSettings settings = new GraphSettings(ProtectedFileProvider.CreateFileProvider(Constants.SettingFilePath, FileProtection.SharedRead));
113+
114+
settings.TryGetEnvironment("MyNewCloud", out IGraphEnvironment loadedEnvironment);
115+
116+
// Assert
117+
Assert.NotNull(loadedEnvironment);
118+
// 5 built-in + 2 user-defined
119+
Assert.Equal(7, settings.Environments.Count());
120+
Assert.Equal("https://login.MyNewCloud.com", loadedEnvironment.AzureADEndpoint);
121+
Assert.Equal("https://graph.MyNewCloud.com", loadedEnvironment.GraphEndpoint);
122+
Assert.Equal(GraphEnvironmentConstants.EnvironmentType.UserDefined, loadedEnvironment.Type);
123+
124+
GraphSession.Reset();
125+
}
126+
127+
[Fact]
128+
public void ShouldRemoveSettingsFromConfiguredDataStore()
129+
{
130+
GraphSession.Initialize(() => new GraphSession());
131+
132+
// Arrange
133+
GraphSession.Instance.DataStore = new MockDataStore();
134+
GraphSettings settings = new GraphSettings(ProtectedFileProvider.CreateFileProvider(Constants.SettingFilePath, FileProtection.SharedRead));
135+
GraphEnvironment myNewCloudEnv = new GraphEnvironment
136+
{
137+
Name = "MyNewCloud",
138+
Type = GraphEnvironmentConstants.EnvironmentType.UserDefined,
139+
AzureADEndpoint = "https://login.MyNewCloud.com",
140+
GraphEndpoint = "https://graph.MyNewCloud.com"
141+
};
142+
GraphEnvironment trialCloudEnv = new GraphEnvironment
143+
{
144+
Name = "TrialCloud",
145+
Type = GraphEnvironmentConstants.EnvironmentType.UserDefined,
146+
AzureADEndpoint = "https://login.TrialCloud.com",
147+
GraphEndpoint = "https://graph.TrialCloud.com"
148+
};
149+
settings.TrySetEnvironment(myNewCloudEnv, out IGraphEnvironment mergedMyNewCloudEnv);
150+
settings.TrySetEnvironment(trialCloudEnv, out IGraphEnvironment mergedTrialCloudEnv);
151+
152+
// Act
153+
settings.RemoveEnvironment(trialCloudEnv.Name);
154+
string settingsContent = GraphSession.Instance.DataStore.ReadFileAsText(Constants.SettingFilePath);
155+
156+
// Assert
157+
Assert.NotEmpty(settingsContent);
158+
// 5 built-in + 1 user-defined
159+
Assert.Equal(6, settings.Environments.Count());
160+
161+
GraphSession.Reset();
162+
}
163+
}
164+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
namespace Microsoft.Graph.Authentication.Test
2+
{
3+
using Microsoft.Graph.PowerShell.Authentication;
4+
using Microsoft.Graph.PowerShell.Authentication.Handlers;
5+
using Microsoft.Graph.PowerShell.Authentication.Models;
6+
using System;
7+
using System.Net.Http;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using Xunit;
11+
12+
public class NationalCloudHandlerTests : IDisposable
13+
{
14+
private HttpMessageInvoker _invoker;
15+
private FakeSuccessHandler _fakeSuccessHandler;
16+
private NationalCloudHandler _nationalCloudHandler;
17+
private string topParam = "$top=5";
18+
private string selectParam = "Select=DisplayName%2CId%2CMail%2CUserType";
19+
20+
public NationalCloudHandlerTests()
21+
{
22+
this._fakeSuccessHandler = new FakeSuccessHandler();
23+
this._nationalCloudHandler = new NationalCloudHandler(new ODataQueryOptionsHandler(_fakeSuccessHandler));
24+
this._invoker = new HttpMessageInvoker(_nationalCloudHandler);
25+
}
26+
27+
public void Dispose()
28+
{
29+
Dispose(true);
30+
}
31+
32+
protected virtual void Dispose(bool disposing)
33+
{
34+
if (disposing)
35+
{
36+
this._invoker.Dispose();
37+
}
38+
}
39+
40+
41+
[Fact]
42+
public async Task ShouldUseGlobalCloudWhenEnvironmentIsNotSet()
43+
{
44+
GraphSession.Initialize(() => new GraphSession());
45+
46+
// Arrange
47+
GraphSession.Instance.Environment = null;
48+
string initialRequestUrl = $"https://graph.microsoft.com/v1.0/users?{topParam}&{selectParam}";
49+
Uri requestUrl = new Uri(initialRequestUrl);
50+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUrl);
51+
52+
// Act
53+
var response = await this._invoker.SendAsync(httpRequestMessage, new CancellationToken());
54+
var sentRequestQuery = response.RequestMessage.RequestUri.Query;
55+
56+
// Assert
57+
Assert.Equal(requestUrl.Scheme, response.RequestMessage.RequestUri.Scheme);
58+
Assert.Equal(requestUrl.Host, response.RequestMessage.RequestUri.Host);
59+
Assert.Contains(topParam, sentRequestQuery);
60+
Assert.Contains($"${selectParam}", sentRequestQuery);
61+
Assert.Equal(2, sentRequestQuery.Split('&').Length);
62+
63+
GraphSession.Reset();
64+
}
65+
66+
[Fact]
67+
public async Task ShouldUseGermanyCloudWhenEnvironmentIsSetToGermany()
68+
{
69+
GraphSession.Initialize(() => new GraphSession());
70+
71+
// Arrange
72+
GraphEnvironment germanyEnvironment = GraphEnvironment.BuiltInEnvironments[GraphEnvironmentConstants.EnvironmentName.Germany];
73+
GraphSession.Instance.Environment = germanyEnvironment;
74+
Uri requestUrl = new Uri($"https://graph.microsoft.com/beta/users?{topParam}&{selectParam}");
75+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUrl);
76+
77+
// Act
78+
var response = await this._invoker.SendAsync(httpRequestMessage, new CancellationToken());
79+
var sentRequestQuery = response.RequestMessage.RequestUri.Query;
80+
81+
// Assert
82+
Assert.Equal(requestUrl.Scheme, response.RequestMessage.RequestUri.Scheme);
83+
Assert.Equal("graph.microsoft.de", response.RequestMessage.RequestUri.Host);
84+
Assert.Contains(topParam, sentRequestQuery);
85+
Assert.Contains(selectParam, sentRequestQuery);
86+
Assert.Equal(2, sentRequestQuery.Split('&').Length);
87+
88+
GraphSession.Reset();
89+
}
90+
91+
[Fact]
92+
public async Task ShouldUseUserDefinedCloudWhenEnvironmentIsSetToAUserDefinedCloud()
93+
{
94+
GraphSession.Initialize(() => new GraphSession());
95+
96+
// Arrange
97+
GraphEnvironment userDefinedEnvironment = new GraphEnvironment
98+
{
99+
Name = "Canary",
100+
AzureADEndpoint = "https://login.microsoftonline.com",
101+
GraphEndpoint = "https://canary.graph.microsoft.com"
102+
};
103+
GraphSession.Instance.Environment = userDefinedEnvironment;
104+
Uri requestUrl = new Uri($"https://graph.microsoft.com/v1.0/users?{topParam}&{selectParam}");
105+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUrl);
106+
107+
// Act
108+
var response = await this._invoker.SendAsync(httpRequestMessage, new CancellationToken());
109+
var sentRequestQuery = response.RequestMessage.RequestUri.Query;
110+
111+
// Assert
112+
Assert.Equal(requestUrl.Scheme, response.RequestMessage.RequestUri.Scheme);
113+
Assert.Equal("canary.graph.microsoft.com", response.RequestMessage.RequestUri.Host);
114+
Assert.Contains(topParam, sentRequestQuery);
115+
Assert.Contains($"${selectParam}", sentRequestQuery);
116+
Assert.Equal(2, sentRequestQuery.Split('&').Length);
117+
118+
GraphSession.Reset();
119+
}
120+
}
121+
}

src/Authentication/Authentication.Test/Helpers/ODataQueryOptionsHandlerTests.cs renamed to src/Authentication/Authentication.Test/Handlers/ODataQueryOptionsHandlerTests.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Microsoft.Graph.Authentication.Test
22
{
3-
using Microsoft.Graph.PowerShell.Authentication.Helpers;
3+
using Microsoft.Graph.PowerShell.Authentication.Handlers;
44
using System;
55
using System.Linq;
66
using System.Net.Http;
@@ -24,9 +24,18 @@ public ODataQueryOptionsHandlerTests()
2424

2525
public void Dispose()
2626
{
27-
this._invoker.Dispose();
27+
Dispose(true);
2828
}
2929

30+
protected virtual void Dispose(bool disposing)
31+
{
32+
if (disposing)
33+
{
34+
this._invoker.Dispose();
35+
}
36+
}
37+
38+
3039
[Fact]
3140
public async Task ShouldAddDollarSignToStandardODataQueryOptionsToV1Endpoint()
3241
{

src/Authentication/Authentication.Test/TestUtils/FakeSuccessHandler.cs renamed to src/Authentication/Authentication.Test/Mocks/FakeSuccessHandler.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Net;
1+
using System.Net;
42
using System.Net.Http;
5-
using System.Text;
63
using System.Threading;
74
using System.Threading.Tasks;
85

0 commit comments

Comments
 (0)