microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Api.Tests/Clients/ActivityClientTests.cs
308lines · modecode
| 1 | using System.Net; |
| 2 | using System.Text.Json; |
| 3 | |
| 4 | using Microsoft.Teams.Api.Clients; |
| 5 | using Microsoft.Teams.Common.Http; |
| 6 | |
| 7 | using Moq; |
| 8 | |
| 9 | using static Microsoft.Teams.Api.Activities.Invokes.Configs; |
| 10 | |
| 11 | namespace Microsoft.Teams.Api.Tests.Clients; |
| 12 | |
| 13 | public class ActivityClientTests |
| 14 | { |
| 15 | [Fact] |
| 16 | public async Task ActivityClient_CreateAsync() |
| 17 | { |
| 18 | Resource responseResource = new Resource() { Id = "activityId" }; |
| 19 | |
| 20 | var responseMessage = new HttpResponseMessage(); |
| 21 | responseMessage.Headers.Add("Custom-Header", "HeaderValue"); |
| 22 | var responseBody = new Resource() { Id = "activityId" }; |
| 23 | var mockHandler = new Mock<IHttpClient>(); |
| 24 | mockHandler |
| 25 | .Setup(handler => handler.SendAsync(It.IsAny<IHttpRequest>(), It.IsAny<CancellationToken>())) |
| 26 | .ReturnsAsync(new HttpResponse<string>() |
| 27 | { |
| 28 | Body = JsonSerializer.Serialize(responseResource, new JsonSerializerOptions { WriteIndented = true }), |
| 29 | Headers = responseMessage.Headers, |
| 30 | StatusCode = HttpStatusCode.OK |
| 31 | }); |
| 32 | |
| 33 | string serviceUrl = "https://serviceurl.com/"; |
| 34 | var activityClient = new ActivityClient(serviceUrl, mockHandler.Object); |
| 35 | string conversationId = "conversationId"; |
| 36 | var value = new Cards.HeroCard() |
| 37 | { |
| 38 | Title = "test card", |
| 39 | SubTitle = "test fetch config activity" |
| 40 | }; |
| 41 | var activity = new FetchActivity(value); |
| 42 | var response = await activityClient.CreateAsync(conversationId, activity); |
| 43 | |
| 44 | Assert.Equal(responseResource.Id, response!.Id); |
| 45 | string expecteUrl = "https://serviceurl.com/v3/conversations/conversationId/activities"; |
| 46 | HttpMethod expectedMethod = HttpMethod.Post; |
| 47 | mockHandler.Verify(x => x.SendAsync( |
| 48 | It.Is<IHttpRequest>(arg => arg.Url == expecteUrl && arg.Method == expectedMethod), |
| 49 | It.IsAny<CancellationToken>()), |
| 50 | Times.Once); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | [Fact] |
| 55 | public async Task ActivityClient_CreateAsync_NullResponse() |
| 56 | { |
| 57 | var responseMessage = new HttpResponseMessage(); |
| 58 | responseMessage.Headers.Add("Custom-Header", "HeaderValue"); |
| 59 | var responseBody = new Resource() { Id = "activityId" }; |
| 60 | var mockHandler = new Mock<IHttpClient>(); |
| 61 | mockHandler |
| 62 | .Setup(handler => handler.SendAsync(It.IsAny<IHttpRequest>(), It.IsAny<CancellationToken>())) |
| 63 | .ReturnsAsync(new HttpResponse<string>() |
| 64 | { |
| 65 | Body = String.Empty, |
| 66 | Headers = responseMessage.Headers, |
| 67 | StatusCode = HttpStatusCode.OK |
| 68 | }); |
| 69 | |
| 70 | string serviceUrl = "https://serviceurl.com/"; |
| 71 | var activityClient = new ActivityClient(serviceUrl, mockHandler.Object); |
| 72 | string conversationId = "conversationId"; |
| 73 | var value = new Cards.HeroCard() |
| 74 | { |
| 75 | Title = "test card", |
| 76 | SubTitle = "test fetch config activity" |
| 77 | }; |
| 78 | var activity = new FetchActivity(value); |
| 79 | var response = await activityClient.CreateAsync(conversationId, activity); |
| 80 | |
| 81 | Assert.Null(response); |
| 82 | string expecteUrl = "https://serviceurl.com/v3/conversations/conversationId/activities"; |
| 83 | HttpMethod expectedMethod = HttpMethod.Post; |
| 84 | mockHandler.Verify(x => x.SendAsync( |
| 85 | It.Is<IHttpRequest>(arg => arg.Url == expecteUrl && arg.Method == expectedMethod), |
| 86 | It.IsAny<CancellationToken>()), |
| 87 | Times.Once); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | [Fact] |
| 92 | public async Task ActivityClient_UpdateAsync() |
| 93 | { |
| 94 | Resource responseResource = new Resource() { Id = "activityId" }; |
| 95 | |
| 96 | var responseMessage = new HttpResponseMessage(); |
| 97 | responseMessage.Headers.Add("Custom-Header", "HeaderValue"); |
| 98 | var responseBody = new Resource() { Id = "activityId" }; |
| 99 | var mockHandler = new Mock<IHttpClient>(); |
| 100 | mockHandler |
| 101 | .Setup(handler => handler.SendAsync(It.IsAny<IHttpRequest>(), It.IsAny<CancellationToken>())) |
| 102 | .ReturnsAsync(new HttpResponse<string>() |
| 103 | { |
| 104 | Body = JsonSerializer.Serialize(responseResource, new JsonSerializerOptions { WriteIndented = true }), |
| 105 | Headers = responseMessage.Headers, |
| 106 | StatusCode = HttpStatusCode.OK |
| 107 | }); |
| 108 | |
| 109 | string serviceUrl = "https://serviceurl.com/"; |
| 110 | var activityClient = new ActivityClient(serviceUrl, mockHandler.Object); |
| 111 | string conversationId = "conversationId"; |
| 112 | var value = new Cards.HeroCard() |
| 113 | { |
| 114 | Title = "test card", |
| 115 | SubTitle = "test fetch config activity" |
| 116 | }; |
| 117 | var activity = new FetchActivity(value); |
| 118 | var response = await activityClient.UpdateAsync(conversationId, responseResource.Id, activity); |
| 119 | |
| 120 | Assert.Equal(responseResource.Id, response!.Id); |
| 121 | string expecteUrl = "https://serviceurl.com/v3/conversations/conversationId/activities/activityId"; |
| 122 | HttpMethod expectedMethod = HttpMethod.Put; |
| 123 | mockHandler.Verify(x => x.SendAsync( |
| 124 | It.Is<IHttpRequest>(arg => arg.Url == expecteUrl && arg.Method == expectedMethod), |
| 125 | It.IsAny<CancellationToken>()), |
| 126 | Times.Once); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | [Fact] |
| 131 | public async Task ActivityClient_ReplyAsync() |
| 132 | { |
| 133 | Resource responseResource = new Resource() { Id = "activityId" }; |
| 134 | |
| 135 | var responseMessage = new HttpResponseMessage(); |
| 136 | responseMessage.Headers.Add("Custom-Header", "HeaderValue"); |
| 137 | var responseBody = new Resource() { Id = "activityId" }; |
| 138 | var mockHandler = new Mock<IHttpClient>(); |
| 139 | mockHandler |
| 140 | .Setup(handler => handler.SendAsync(It.IsAny<IHttpRequest>(), It.IsAny<CancellationToken>())) |
| 141 | .ReturnsAsync(new HttpResponse<string>() |
| 142 | { |
| 143 | Body = JsonSerializer.Serialize(responseResource, new JsonSerializerOptions { WriteIndented = true }), |
| 144 | Headers = responseMessage.Headers, |
| 145 | StatusCode = HttpStatusCode.OK |
| 146 | }); |
| 147 | |
| 148 | string serviceUrl = "https://serviceurl.com/"; |
| 149 | var activityClient = new ActivityClient(serviceUrl, mockHandler.Object); |
| 150 | string conversationId = "conversationId"; |
| 151 | var value = new Cards.HeroCard() |
| 152 | { |
| 153 | Title = "test card", |
| 154 | SubTitle = "test fetch config activity" |
| 155 | }; |
| 156 | var activity = new FetchActivity(value); |
| 157 | var response = await activityClient.ReplyAsync(conversationId, responseResource.Id, activity); |
| 158 | |
| 159 | Assert.Equal(responseResource.Id, response!.Id); |
| 160 | string expecteUrl = "https://serviceurl.com/v3/conversations/conversationId/activities/activityId"; |
| 161 | HttpMethod expectedMethod = HttpMethod.Post; |
| 162 | mockHandler.Verify(x => x.SendAsync( |
| 163 | It.Is<IHttpRequest>(arg => arg.Url == expecteUrl && arg.Method == expectedMethod), |
| 164 | It.IsAny<CancellationToken>()), |
| 165 | Times.Once); |
| 166 | } |
| 167 | |
| 168 | [Fact] |
| 169 | public async Task ActivityClient_DeleteAsync() |
| 170 | { |
| 171 | Resource responseResource = new Resource() { Id = "activityId" }; |
| 172 | |
| 173 | var responseMessage = new HttpResponseMessage(); |
| 174 | responseMessage.Headers.Add("Custom-Header", "HeaderValue"); |
| 175 | var responseBody = new Resource() { Id = "activityId" }; |
| 176 | var mockHandler = new Mock<IHttpClient>(); |
| 177 | mockHandler |
| 178 | .Setup(handler => handler.SendAsync(It.IsAny<IHttpRequest>(), It.IsAny<CancellationToken>())) |
| 179 | .ReturnsAsync(new HttpResponse<string>() |
| 180 | { |
| 181 | Body = String.Empty, |
| 182 | Headers = responseMessage.Headers, |
| 183 | StatusCode = HttpStatusCode.OK |
| 184 | }); |
| 185 | |
| 186 | string serviceUrl = "https://serviceurl.com/"; |
| 187 | var activityClient = new ActivityClient(serviceUrl, mockHandler.Object); |
| 188 | string conversationId = "conversationId"; |
| 189 | var value = new Cards.HeroCard() |
| 190 | { |
| 191 | Title = "test card", |
| 192 | SubTitle = "test fetch config activity" |
| 193 | }; |
| 194 | var activity = new FetchActivity(value); |
| 195 | await activityClient.DeleteAsync(conversationId, responseResource.Id); |
| 196 | |
| 197 | string expecteUrl = "https://serviceurl.com/v3/conversations/conversationId/activities/activityId"; |
| 198 | HttpMethod expectedMethod = HttpMethod.Delete; |
| 199 | mockHandler.Verify(x => x.SendAsync( |
| 200 | It.Is<IHttpRequest>(arg => arg.Url == expecteUrl && arg.Method == expectedMethod), |
| 201 | It.IsAny<CancellationToken>()), |
| 202 | Times.Once); |
| 203 | } |
| 204 | |
| 205 | [Fact] |
| 206 | public async Task ActivityClient_CreateTargetedAsync() |
| 207 | { |
| 208 | Resource responseResource = new Resource() { Id = "activityId" }; |
| 209 | |
| 210 | var responseMessage = new HttpResponseMessage(); |
| 211 | responseMessage.Headers.Add("Custom-Header", "HeaderValue"); |
| 212 | var mockHandler = new Mock<IHttpClient>(); |
| 213 | mockHandler |
| 214 | .Setup(handler => handler.SendAsync(It.IsAny<IHttpRequest>(), It.IsAny<CancellationToken>())) |
| 215 | .ReturnsAsync(new HttpResponse<string>() |
| 216 | { |
| 217 | Body = JsonSerializer.Serialize(responseResource, new JsonSerializerOptions { WriteIndented = true }), |
| 218 | Headers = responseMessage.Headers, |
| 219 | StatusCode = HttpStatusCode.OK |
| 220 | }); |
| 221 | |
| 222 | string serviceUrl = "https://serviceurl.com/"; |
| 223 | var activityClient = new ActivityClient(serviceUrl, mockHandler.Object); |
| 224 | string conversationId = "conversationId"; |
| 225 | var value = new Cards.HeroCard() |
| 226 | { |
| 227 | Title = "test card", |
| 228 | SubTitle = "test targeted activity" |
| 229 | }; |
| 230 | var activity = new FetchActivity(value); |
| 231 | var response = await activityClient.CreateTargetedAsync(conversationId, activity); |
| 232 | |
| 233 | Assert.Equal(responseResource.Id, response!.Id); |
| 234 | string expectedUrl = "https://serviceurl.com/v3/conversations/conversationId/activities?isTargetedActivity=true"; |
| 235 | HttpMethod expectedMethod = HttpMethod.Post; |
| 236 | mockHandler.Verify(x => x.SendAsync( |
| 237 | It.Is<IHttpRequest>(arg => arg.Url == expectedUrl && arg.Method == expectedMethod), |
| 238 | It.IsAny<CancellationToken>()), |
| 239 | Times.Once); |
| 240 | } |
| 241 | |
| 242 | [Fact] |
| 243 | public async Task ActivityClient_UpdateTargetedAsync() |
| 244 | { |
| 245 | Resource responseResource = new Resource() { Id = "activityId" }; |
| 246 | |
| 247 | var responseMessage = new HttpResponseMessage(); |
| 248 | responseMessage.Headers.Add("Custom-Header", "HeaderValue"); |
| 249 | var mockHandler = new Mock<IHttpClient>(); |
| 250 | mockHandler |
| 251 | .Setup(handler => handler.SendAsync(It.IsAny<IHttpRequest>(), It.IsAny<CancellationToken>())) |
| 252 | .ReturnsAsync(new HttpResponse<string>() |
| 253 | { |
| 254 | Body = JsonSerializer.Serialize(responseResource, new JsonSerializerOptions { WriteIndented = true }), |
| 255 | Headers = responseMessage.Headers, |
| 256 | StatusCode = HttpStatusCode.OK |
| 257 | }); |
| 258 | |
| 259 | string serviceUrl = "https://serviceurl.com/"; |
| 260 | var activityClient = new ActivityClient(serviceUrl, mockHandler.Object); |
| 261 | string conversationId = "conversationId"; |
| 262 | var value = new Cards.HeroCard() |
| 263 | { |
| 264 | Title = "test card", |
| 265 | SubTitle = "test targeted activity update" |
| 266 | }; |
| 267 | var activity = new FetchActivity(value); |
| 268 | var response = await activityClient.UpdateTargetedAsync(conversationId, responseResource.Id, activity); |
| 269 | |
| 270 | Assert.Equal(responseResource.Id, response!.Id); |
| 271 | string expectedUrl = "https://serviceurl.com/v3/conversations/conversationId/activities/activityId?isTargetedActivity=true"; |
| 272 | HttpMethod expectedMethod = HttpMethod.Put; |
| 273 | mockHandler.Verify(x => x.SendAsync( |
| 274 | It.Is<IHttpRequest>(arg => arg.Url == expectedUrl && arg.Method == expectedMethod), |
| 275 | It.IsAny<CancellationToken>()), |
| 276 | Times.Once); |
| 277 | } |
| 278 | |
| 279 | [Fact] |
| 280 | public async Task ActivityClient_DeleteTargetedAsync() |
| 281 | { |
| 282 | Resource responseResource = new Resource() { Id = "activityId" }; |
| 283 | |
| 284 | var responseMessage = new HttpResponseMessage(); |
| 285 | responseMessage.Headers.Add("Custom-Header", "HeaderValue"); |
| 286 | var mockHandler = new Mock<IHttpClient>(); |
| 287 | mockHandler |
| 288 | .Setup(handler => handler.SendAsync(It.IsAny<IHttpRequest>(), It.IsAny<CancellationToken>())) |
| 289 | .ReturnsAsync(new HttpResponse<string>() |
| 290 | { |
| 291 | Body = String.Empty, |
| 292 | Headers = responseMessage.Headers, |
| 293 | StatusCode = HttpStatusCode.OK |
| 294 | }); |
| 295 | |
| 296 | string serviceUrl = "https://serviceurl.com/"; |
| 297 | var activityClient = new ActivityClient(serviceUrl, mockHandler.Object); |
| 298 | string conversationId = "conversationId"; |
| 299 | await activityClient.DeleteTargetedAsync(conversationId, responseResource.Id); |
| 300 | |
| 301 | string expectedUrl = "https://serviceurl.com/v3/conversations/conversationId/activities/activityId?isTargetedActivity=true"; |
| 302 | HttpMethod expectedMethod = HttpMethod.Delete; |
| 303 | mockHandler.Verify(x => x.SendAsync( |
| 304 | It.Is<IHttpRequest>(arg => arg.Url == expectedUrl && arg.Method == expectedMethod), |
| 305 | It.IsAny<CancellationToken>()), |
| 306 | Times.Once); |
| 307 | } |
| 308 | } |