-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathGetOctopusUserTests.cs
169 lines (140 loc) · 5.47 KB
/
GetOctopusUserTests.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using NUnit.Framework;
using Octoposh.Cmdlets;
using Octoposh.Model;
using Octopus.Client.Model;
namespace Octoposh.Tests
{
[TestFixture]
public class GetOctopusUserTests
{
private static readonly string CmdletName = "Get-OctopusUser";
private static readonly Type CmdletType = typeof(GetOctopusUser);
private static readonly string User1 = "UserTests_User1";
private static readonly string User2 = "UserTests_User2";
[Test]
public void GetUserBySingleName()
{
var userName = User1;
var parameters = new List<CmdletParameter>
{
new CmdletParameter()
{
Name = "UserName",
SingleValue = userName
}
};
Console.WriteLine("Looking for User [{0}]", userName);
var powershell = new CmdletRunspace().CreatePowershellcmdlet(CmdletName, CmdletType, parameters);
var results = powershell.Invoke<OutputOctopusUser>();
Assert.AreEqual(1, results.Count);
Assert.AreEqual(results[0].UserName, userName);
Console.WriteLine("User [{0}] found", userName);
}
[Test]
public void GetUserByMultipleNames()
{
var userName1 = User1;
var userName2 = User2;
bool isUser1 = false;
bool isUser2 = false;
var parameters = new List<CmdletParameter>
{
new CmdletParameter()
{
Name = "UserName",
MultipleValue = new string[]{userName1,userName2}
}
};
Console.WriteLine("Looking for Users [{0}] and [{1}]", userName1, userName2);
var powershell = new CmdletRunspace().CreatePowershellcmdlet(CmdletName, CmdletType, parameters);
var results = powershell.Invoke<OutputOctopusUser>();
Assert.AreEqual(2, results.Count);
foreach (var item in results)
{
if (item.UserName == userName1)
{
isUser1 = true;
}
else if (item.UserName == userName2)
{
isUser2 = true;
}
else
{
Console.WriteLine("User found with name that was not expected: [{0}]", item.UserName);
throw new Exception();
}
}
Assert.IsTrue(isUser1);
Console.WriteLine("User [{0}] found", userName1);
Assert.IsTrue(isUser2);
Console.WriteLine("User [{0}] found", userName2);
}
[Test]
public void GetUserByNameUsingWildcard()
{
var namePattern = "UserTests_*";
var parameters = new List<CmdletParameter> {
new CmdletParameter(){
Name = "Name",
SingleValue = namePattern
}
};
Console.WriteLine("Looking for resources with name pattern: [{0}]", namePattern);
var pattern = new WildcardPattern(namePattern);
var powershell = new CmdletRunspace().CreatePowershellcmdlet(CmdletName, CmdletType, parameters);
var results = powershell.Invoke<OutputOctopusUser>();
Assert.AreEqual(2, results.Count);
Console.WriteLine("Resources found: [{0}]", results.Count);
foreach (var item in results)
{
Console.WriteLine("Resource name: [{0}]", item.UserName);
Assert.IsTrue(pattern.IsMatch(item.UserName));
}
Console.WriteLine("All resources found match pattern [{0}]", namePattern);
}
[Test]
public void DontGetUserIfNameDoesntMatch()
{
var resourceName = "TotallyANameThatYoullNeverPutToAResource";
var parameters = new List<CmdletParameter> {new CmdletParameter()
{
Name = "Name",
SingleValue = resourceName
}};
var powershell = new CmdletRunspace().CreatePowershellcmdlet(CmdletName, CmdletType, parameters);
var results = powershell.Invoke<OutputOctopusUser>();
Assert.AreEqual(results.Count, 0);
Console.WriteLine("No resources found with name [{0}]", resourceName);
}
[Test]
public void GetUserUsingResourceOnlyReturnsRawResource()
{
var userName = User1;
var parameters = new List<CmdletParameter>
{
new CmdletParameter()
{
Name = "resourceOnly"
},
new CmdletParameter()
{
Name = "UserName",
SingleValue = userName
}
};
var powershell = new CmdletRunspace().CreatePowershellcmdlet(CmdletName, CmdletType, parameters);
var results = powershell.Invoke<List<UserResource>>()[0];
//If [results] has at least one item, It'll be of the resource type declared on the powershell.Invoke line, meaning the test passed
Assert.AreEqual(1, results.Count);
;
}
}
}