|
| 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 | +} |
0 commit comments