microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Apps.Tests/Events/ActivityEventTests.cs
112lines · modecode
| 1 | using Microsoft.Teams.Api.Activities; |
| 2 | using Microsoft.Teams.Api.Auth; |
| 3 | using Microsoft.Teams.Apps.Events; |
| 4 | using Microsoft.Teams.Apps.Testing.Plugins; |
| 5 | |
| 6 | namespace Microsoft.Teams.Apps.Tests.Events; |
| 7 | |
| 8 | public class ActivityEventTests |
| 9 | { |
| 10 | private readonly App _app; |
| 11 | private readonly TestPlugin _plugin; |
| 12 | private readonly IToken _token; |
| 13 | |
| 14 | public ActivityEventTests() |
| 15 | { |
| 16 | _app = new App(); |
| 17 | _plugin = new TestPlugin(); |
| 18 | _app.AddPlugin(_plugin); |
| 19 | _token = Globals.Token; |
| 20 | } |
| 21 | |
| 22 | [Fact] |
| 23 | public async Task Should_CallHandler_OnActivityEvent() |
| 24 | { |
| 25 | var calls = 0; |
| 26 | |
| 27 | _app.OnEvent("activity", (sender, @event) => |
| 28 | { |
| 29 | calls++; |
| 30 | Assert.True(@event is ActivityEvent); |
| 31 | }); |
| 32 | |
| 33 | _app.OnActivity((sender, @event) => |
| 34 | { |
| 35 | calls++; |
| 36 | Assert.True(@event is ActivityEvent); |
| 37 | }); |
| 38 | |
| 39 | var res = await _plugin.Do(_token, new MessageActivity("hello world")); |
| 40 | |
| 41 | Assert.Equal(System.Net.HttpStatusCode.OK, res.Status); |
| 42 | Assert.Equal(2, calls); |
| 43 | } |
| 44 | |
| 45 | [Fact] |
| 46 | public async Task Should_PassExtra_OnActivityEvent() |
| 47 | { |
| 48 | IDictionary<string, object?>? extra = null; |
| 49 | _app.OnEvent("activity", (sender, @event) => |
| 50 | { |
| 51 | Assert.True(@event is ActivityEvent); |
| 52 | extra = ((ActivityEvent)@event).Extra; |
| 53 | }); |
| 54 | |
| 55 | var extraFromParameter = new Dictionary<string, object?> |
| 56 | { |
| 57 | { "paramContextKey", "value" } |
| 58 | }; |
| 59 | var res = await _plugin.Do(_token, new MessageActivity("hello world"), extraFromParameter); |
| 60 | |
| 61 | Assert.Equal(System.Net.HttpStatusCode.OK, res.Status); |
| 62 | Assert.Equal(extra!["paramContextKey"], extraFromParameter["paramContextKey"]); |
| 63 | } |
| 64 | |
| 65 | [Fact] |
| 66 | public async Task Should_CallHandler_OnActivityResponseEvent() |
| 67 | { |
| 68 | var calls = 0; |
| 69 | |
| 70 | _app.OnEvent("activity.response", (sender, @event) => |
| 71 | { |
| 72 | calls++; |
| 73 | Assert.True(@event is ActivityResponseEvent); |
| 74 | }); |
| 75 | |
| 76 | _app.OnActivityResponse((sender, @event) => |
| 77 | { |
| 78 | calls++; |
| 79 | Assert.True(@event is ActivityResponseEvent); |
| 80 | }); |
| 81 | |
| 82 | var res = await _plugin.Do(_token, new MessageActivity("hello world")); |
| 83 | |
| 84 | Assert.Equal(System.Net.HttpStatusCode.OK, res.Status); |
| 85 | Assert.Equal(2, calls); |
| 86 | } |
| 87 | |
| 88 | [Fact] |
| 89 | public async Task Should_CallHandler_OnActivityResponseEvent_Async() |
| 90 | { |
| 91 | var calls = 0; |
| 92 | |
| 93 | _app.OnEvent("activity.response", (sender, @event, _) => |
| 94 | { |
| 95 | calls++; |
| 96 | Assert.True(@event is ActivityResponseEvent); |
| 97 | return Task.CompletedTask; |
| 98 | }); |
| 99 | |
| 100 | _app.OnActivityResponse((sender, @event, _) => |
| 101 | { |
| 102 | calls++; |
| 103 | Assert.True(@event is ActivityResponseEvent); |
| 104 | return Task.CompletedTask; |
| 105 | }); |
| 106 | |
| 107 | var res = await _plugin.Do(_token, new MessageActivity("hello world")); |
| 108 | |
| 109 | Assert.Equal(System.Net.HttpStatusCode.OK, res.Status); |
| 110 | Assert.Equal(2, calls); |
| 111 | } |
| 112 | } |