microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
feature/oauthflow-fixes

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

core/test/Microsoft.Teams.Core.UnitTests/Schema/CoreActivityTests.cs

333lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using Microsoft.Teams.Core.Schema;
5
6namespace Microsoft.Teams.Core.UnitTests.Schema;
7
8public class CoreCoreActivityTests
9{
10 [Fact]
11 public void Ctor_And_Nulls()
12 {
13 CoreActivity a1 = new();
14 Assert.NotNull(a1);
15 Assert.Equal(ActivityType.Message, a1.Type);
16
17 CoreActivity a2 = new()
18 {
19 Type = "mytype"
20 };
21 Assert.NotNull(a2);
22 Assert.Equal("mytype", a2.Type);
23 }
24
25 [Fact]
26 public void Json_Nulls_Not_Deserialized()
27 {
28 string json = """
29 {
30 "type": "message",
31 "text": null
32 }
33 """;
34 CoreActivity act = CoreActivity.FromJsonString(json);
35 Assert.NotNull(act);
36 Assert.Equal("message", act.Type);
37
38 string json2 = """
39 {
40 "type": "message"
41 }
42 """;
43 CoreActivity act2 = CoreActivity.FromJsonString(json2);
44 Assert.NotNull(act2);
45 Assert.Equal("message", act2.Type);
46
47 }
48
49 [Fact]
50 public void Accept_Unkown_Primitive_Fields()
51 {
52 string json = """
53 {
54 "type": "message",
55 "text": "hello",
56 "unknownString": "some string",
57 "unknownInt": 123,
58 "unknownBool": true,
59 "unknownNull": null
60 }
61 """;
62 CoreActivity act = CoreActivity.FromJsonString(json);
63 Assert.NotNull(act);
64 Assert.Equal("message", act.Type);
65 Assert.True(act.Properties.ContainsKey("unknownString"));
66 Assert.True(act.Properties.ContainsKey("unknownInt"));
67 Assert.True(act.Properties.ContainsKey("unknownBool"));
68 Assert.True(act.Properties.ContainsKey("unknownNull"));
69 Assert.Equal("some string", act.Properties["unknownString"]?.ToString());
70 Assert.Equal(123, ((JsonElement)act.Properties["unknownInt"]!).GetInt32());
71 Assert.True(((JsonElement)act.Properties["unknownBool"]!).GetBoolean());
72 Assert.Null(act.Properties["unknownNull"]);
73 }
74
75 [Fact]
76 public void Serialize_Unkown_Primitive_Fields()
77 {
78 CoreActivity act = new()
79 {
80 Type = ActivityType.Message,
81 };
82 act.Properties["unknownString"] = "some string";
83 act.Properties["unknownInt"] = 123;
84 act.Properties["unknownBool"] = true;
85 act.Properties["unknownNull"] = null;
86 act.Properties["unknownLong"] = 1L;
87 act.Properties["unknownDouble"] = 1.0;
88
89 string json = act.ToJson();
90 Assert.Contains("\"type\": \"message\"", json);
91 Assert.Contains("\"unknownString\": \"some string\"", json);
92 Assert.Contains("\"unknownInt\": 123", json);
93 Assert.Contains("\"unknownBool\": true", json);
94 Assert.Contains("\"unknownNull\": null", json);
95 Assert.Contains("\"unknownLong\": 1", json);
96 Assert.Contains("\"unknownDouble\": 1", json);
97 }
98
99 [Fact]
100 public void Deserialize_Unkown__Fields_In_KnownObjects()
101 {
102 string json = """
103 {
104 "type": "message",
105 "text": "hello",
106 "from": {
107 "id": "1",
108 "name": "tester",
109 "aadObjectId": "123"
110 }
111 }
112 """;
113 CoreActivity act = CoreActivity.FromJsonString(json);
114 Assert.NotNull(act);
115 Assert.Equal("message", act.Type);
116 Assert.NotNull(act.From);
117 Assert.Equal("1", act.From.Id);
118 Assert.Equal("tester", act.From.Name);
119 Assert.Equal("123", act.From.Properties["aadObjectId"]?.ToString());
120 }
121
122 [Fact]
123 public void Deserialize_Serialize_Unkown__Fields_In_KnownObjects()
124 {
125 string json = """
126 {
127 "type": "message",
128 "text": "hello",
129 "from": {
130 "id": "1",
131 "name": "tester",
132 "aadObjectId": "123"
133 }
134 }
135 """;
136 CoreActivity act = CoreActivity.FromJsonString(json);
137 string json2 = act.ToJson();
138 Assert.Contains("\"type\": \"message\"", json2);
139 Assert.Contains("\"text\": \"hello\"", json2);
140 Assert.Contains("\"from\":", json2);
141 Assert.Contains("\"id\": \"1\"", json2);
142 Assert.Contains("\"name\": \"tester\"", json2);
143 Assert.Contains("\"aadObjectId\": \"123\"", json2);
144 }
145
146 [Fact]
147 public void Deserialize_Serialize_Entities()
148 {
149 string json = """
150 {
151 "type": "message",
152 "text": "hello",
153 "entities": [
154 {
155 "mentioned": {
156 "id": "28:0b6fe6d1-fece-44f7-9a48-56465e2d5ab8",
157 "name": "ridotest"
158 },
159 "text": "\u003Cat\u003Eridotest\u003C/at\u003E",
160 "type": "mention"
161 },
162 {
163 "locale": "en-US",
164 "country": "US",
165 "platform": "Web",
166 "timezone": "America/Los_Angeles",
167 "type": "clientInfo"
168 }
169 ]
170 }
171 """;
172 CoreActivity act = CoreActivity.FromJsonString(json);
173 string json2 = act.ToJson();
174 Assert.Contains("\"type\": \"message\"", json2);
175 Assert.True(act.Properties.ContainsKey("entities"));
176 Assert.IsType<JsonElement>(act.Properties["entities"]);
177 JsonElement entitiesElement = (JsonElement)act.Properties["entities"]!;
178 Assert.Equal(JsonValueKind.Array, entitiesElement.ValueKind);
179 Assert.Equal(2, entitiesElement.GetArrayLength());
180
181 }
182
183
184 [Fact]
185 public void Handling_Nulls_from_default_serializer()
186 {
187 string json = """
188 {
189 "type": "message",
190 "text": null,
191 "unknownString": null
192 }
193 """;
194 CoreActivity? act = JsonSerializer.Deserialize<CoreActivity>(json); //without default options
195 Assert.NotNull(act);
196 Assert.Equal("message", act.Type);
197 Assert.Null(act.Properties["text"]);
198 Assert.Null(act.Properties["unknownString"]!);
199
200 string json2 = JsonSerializer.Serialize(act); //without default options
201 Assert.Contains("\"type\":\"message\"", json2);
202 Assert.Contains("\"text\":null", json2);
203 Assert.Contains("\"unknownString\":null", json2);
204 }
205
206 [Fact]
207 public void Serialize_With_Properties_Initialized()
208 {
209 CoreActivity act = new()
210 {
211 Type = ActivityType.Message,
212 From = new ConversationAccount { Id = "user1", Properties = { { "fromCustomField", "fromCustomValue" } } },
213 Recipient = new ConversationAccount { Id = "bot1", Properties = { { "recipientCustomField", "recipientCustomValue" } } },
214 Properties =
215 {
216 { "customField", "customValue" },
217 { "channelData", new ChannelData { Properties = { { "channelCustomField", "channelCustomValue" } } } },
218 { "conversation", new Conversation { Properties = { { "conversationCustomField", "conversationCustomValue" } } } },
219 }
220 };
221 string json = act.ToJson();
222 Assert.Contains("\"type\": \"message\"", json);
223 Assert.Contains("\"customField\": \"customValue\"", json);
224 Assert.Contains("\"channelCustomField\": \"channelCustomValue\"", json);
225 Assert.Contains("\"conversationCustomField\": \"conversationCustomValue\"", json);
226 Assert.Contains("\"fromCustomField\": \"fromCustomValue\"", json);
227 Assert.Contains("\"recipientCustomField\": \"recipientCustomValue\"", json);
228 }
229
230
231 [Fact]
232 public async Task DeserializeAsync()
233 {
234 string json = """
235 {
236 "type": "message",
237 "text": "hello",
238 "from": {
239 "id": "1",
240 "name": "tester",
241 "aadObjectId": "123"
242 }
243 }
244 """;
245 using MemoryStream ms = new(System.Text.Encoding.UTF8.GetBytes(json));
246 CoreActivity? act = await CoreActivity.FromJsonStreamAsync(ms);
247 Assert.NotNull(act);
248 Assert.Equal("message", act.Type);
249 Assert.Equal("hello", act.Properties["text"]?.ToString());
250 Assert.NotNull(act.From);
251 Assert.Equal("1", act.From.Id);
252 Assert.Equal("tester", act.From.Name);
253 Assert.Equal("123", act.From.Properties["aadObjectId"]?.ToString());
254 }
255
256
257 [Fact]
258 public async Task DeserializeInvokeWithValueAsync()
259 {
260 string json = """
261 {
262 "type": "invoke",
263 "value": {
264 "key1": "value1",
265 "key2": 2
266 }
267 }
268 """;
269 using MemoryStream ms = new(System.Text.Encoding.UTF8.GetBytes(json));
270 CoreActivity? act = await CoreActivity.FromJsonStreamAsync(ms);
271 Assert.NotNull(act);
272 Assert.Equal("invoke", act.Type);
273 // Value is no longer on CoreActivity — it lands in Properties via [JsonExtensionData]
274 Assert.True(act.Properties.ContainsKey("value"));
275 JsonElement valueElement = Assert.IsType<JsonElement>(act.Properties["value"]);
276 Assert.Equal("value1", valueElement.GetProperty("key1").GetString());
277 Assert.Equal(2, valueElement.GetProperty("key2").GetInt32());
278 }
279
280 [Fact]
281 public void IsTargeted_DefaultsToNull()
282 {
283 ConversationAccount account = new();
284
285 Assert.Null(account.IsTargeted);
286 }
287
288 [Fact]
289 public void IsTargeted_CanBeSetToTrue()
290 {
291 ConversationAccount account = new()
292 {
293 IsTargeted = true
294 };
295
296 Assert.True(account.IsTargeted);
297 }
298
299 [Fact]
300 public void IsTargeted_IsSerializedToJson()
301 {
302 CoreActivity activity = new()
303 {
304 Type = ActivityType.Message,
305 Recipient = new ConversationAccount { Id = "user-123", IsTargeted = true }
306 };
307
308 string json = activity.ToJson();
309
310 // IsTargeted is serialized in the recipient object
311 Assert.Contains("isTargeted", json, StringComparison.OrdinalIgnoreCase);
312 }
313
314 [Fact]
315 public void IsTargeted_DeserializedFromJson()
316 {
317 string json = """
318 {
319 "type": "message",
320 "recipient": {
321 "id": "user-123",
322 "isTargeted": true
323 }
324 }
325 """;
326
327 CoreActivity activity = CoreActivity.FromJsonString(json);
328
329 Assert.NotNull(activity.Recipient);
330 Assert.Equal("user-123", activity.Recipient.Id);
331 Assert.True(activity.Recipient.IsTargeted);
332 }
333}
334