-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathTestCommon.cs
148 lines (126 loc) · 4.65 KB
/
TestCommon.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using Microsoft.SharePoint.Client;
using System;
using System.Configuration;
using System.Security;
using System.Net;
using Core = PnP.Framework;
using System.Threading;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using PnP.PowerShell.Commands.Utilities;
namespace PnP.PowerShell.Tests
{
static class TestCommon
{
#region Constructor
static TestCommon()
{
Configuration = new Configuration();
}
#endregion
public static Configuration Configuration { get; set; }
public static string WebHookTestUrl { get; set; }
private static PSTestScope testScope;
public static PSTestScope GetTestScope()
{
if (testScope == null)
{
testScope = new PSTestScope();
}
return testScope;
}
#region Methods
public static ClientContext CreateClientContext()
{
return CreateContext(Configuration.SiteUrl, Configuration.Credentials);
}
public static ClientContext CreateTenantClientContext()
{
var tenantUrl = UrlUtilities.GetTenantAdministrationUrl(Configuration.SiteUrl);
return CreateContext(tenantUrl, Configuration.Credentials);
}
public static ClientContext CreateClientContext(string siteUrl)
{
return CreateContext(siteUrl, Configuration.Credentials);
}
private static ClientContext CreateContext(string contextUrl, PSCredential credentials)
{
ClientContext context = null;
using (var am = PnP.Framework.AuthenticationManager.CreateWithCredentials(credentials.UserName, credentials.Password))
{
context = am.GetContext(contextUrl);
}
context.RequestTimeout = Timeout.Infinite;
return context;
}
public static string GetTenantRootUrl(ClientContext ctx)
{
var uri = new Uri(ctx.Url);
return $"https://{uri.DnsSafeHost}";
}
#endregion
}
internal class Configuration
{
public string SiteUrl { get; set; }
public PSCredential Credentials { get; set; }
public Configuration()
{
SiteUrl = Environment.GetEnvironmentVariable("PnPTests_SiteUrl");
if (string.IsNullOrEmpty(SiteUrl))
{
throw new ConfigurationErrorsException("Please set PnPTests_SiteUrl environment variable, or run Run-Tests.ps1 in the build root folder");
}
else
{
SiteUrl = SiteUrl.TrimEnd(new[] { '/' });
}
var credLabel = Environment.GetEnvironmentVariable("PnPTests_CredentialManagerLabel");
if (string.IsNullOrEmpty(credLabel))
{
var username = Environment.GetEnvironmentVariable("PnPTests_Username");
var password = Environment.GetEnvironmentVariable("PnPTests_Password");
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
Credentials = new PSCredential(username, ConvertFromBase64String(password));
}
}
else
{
Credentials = PnP.PowerShell.Commands.Utilities.CredentialManager.GetCredential(credLabel);
}
if (Credentials == null)
{
throw new ConfigurationErrorsException("Please set PnPTests_CredentialManagerLabel or PnPTests_Username and PnPTests_Password, or run Run-Tests.ps1 in the build root folder");
}
}
private SecureString ConvertToSecureString(string input)
{
var secureString = new SecureString();
foreach (char c in input)
{
secureString.AppendChar(c);
}
secureString.MakeReadOnly();
return secureString;
}
private SecureString ConvertFromBase64String(string input)
{
var iss = InitialSessionState.CreateDefault();
using (var rs = RunspaceFactory.CreateRunspace(iss))
{
rs.Open();
var pipeLine = rs.CreatePipeline();
var cmd = new Command("ConvertTo-SecureString");
cmd.Parameters.Add("String", input);
pipeLine.Commands.Add(cmd);
var results = pipeLine.Invoke();
if (results.Count > 0)
{
return results[0].BaseObject as SecureString;
}
}
return null;
}
}
}