-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathPSObjectJsonConverterTests.cs
More file actions
148 lines (130 loc) · 5.42 KB
/
Copy pathPSObjectJsonConverterTests.cs
File metadata and controls
148 lines (130 loc) · 5.42 KB
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
using System.Collections;
using System.Management.Automation;
using System.Net.Http;
using Microsoft.Graph.PowerShell.Authentication.Cmdlets;
using Microsoft.Graph.PowerShell.Authentication.Helpers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;
namespace Microsoft.Graph.Authentication.Test.Helpers
{
public class PSObjectJsonConverterTests
{
[Fact]
public void ShouldSerializePSObjectWrappedStringAsPlainString()
{
// Mimics a value produced by the PowerShell pipeline, e.g. bare $_ in ForEach-Object.
// See https://github.com/microsoftgraph/msgraph-sdk-powershell/issues/3654.
var body = new Hashtable
{
{
"message", new Hashtable
{
{
"toRecipients", new object[]
{
new Hashtable
{
{
"emailAddress", new Hashtable
{
{ "address", PSObject.AsPSObject("recipient@example.com") }
}
}
}
}
}
}
},
{ "saveToSentItems", true }
};
var json = JsonConvert.SerializeObject(body, new PSObjectJsonConverter());
var parsedBody = JObject.Parse(json);
Assert.Equal("recipient@example.com",
parsedBody.SelectToken("message.toRecipients[0].emailAddress.address").Value<string>());
Assert.True(parsedBody.SelectToken("saveToSentItems").Value<bool>());
}
[Fact]
public void ShouldSerializePSObjectWrappedPrimitivesAsUnderlyingValues()
{
var body = new Hashtable
{
{ "count", PSObject.AsPSObject(42) },
{ "enabled", PSObject.AsPSObject(true) }
};
var json = JsonConvert.SerializeObject(body, new PSObjectJsonConverter());
var parsedBody = JObject.Parse(json);
Assert.Equal(42, parsedBody.SelectToken("count").Value<int>());
Assert.True(parsedBody.SelectToken("enabled").Value<bool>());
}
[Fact]
public void ShouldSerializePSObjectWrappedDictionary()
{
var body = new Hashtable
{
{
"emailAddress", PSObject.AsPSObject(new Hashtable
{
{ "address", PSObject.AsPSObject("recipient@example.com") }
})
}
};
var json = JsonConvert.SerializeObject(body, new PSObjectJsonConverter());
var parsedBody = JObject.Parse(json);
Assert.Equal("recipient@example.com",
parsedBody.SelectToken("emailAddress.address").Value<string>());
}
[Fact]
public void ShouldSerializePSCustomObjectAsJsonObject()
{
var emailAddress = new PSObject();
emailAddress.Properties.Add(new PSNoteProperty("address", PSObject.AsPSObject("recipient@example.com")));
var body = new Hashtable
{
{ "emailAddress", emailAddress }
};
var json = JsonConvert.SerializeObject(body, new PSObjectJsonConverter());
var parsedBody = JObject.Parse(json);
Assert.Equal("recipient@example.com",
parsedBody.SelectToken("emailAddress.address").Value<string>());
}
[Fact]
public void SetRequestContentShouldSerializeDictionaryWithPSObjectWrappedValues()
{
var cmdlet = new InvokeMgGraphRequest();
using (var request = new HttpRequestMessage(HttpMethod.Post,
"https://graph.microsoft.com/v1.0/users/sender@example.com/sendMail"))
{
var body = new Hashtable
{
{
"message", new Hashtable
{
{
"toRecipients", new object[]
{
new Hashtable
{
{
"emailAddress", new Hashtable
{
{ "address", PSObject.AsPSObject("recipient@example.com") }
}
}
}
}
}
}
},
{ "saveToSentItems", true }
};
var contentLength = cmdlet.SetRequestContent(request, body);
Assert.True(contentLength > 0);
var json = request.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var parsedBody = JObject.Parse(json);
Assert.Equal("recipient@example.com",
parsedBody.SelectToken("message.toRecipients[0].emailAddress.address").Value<string>());
}
}
}
}