microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Apps.Tests/Activities/Events/ReadReceiptEventTests.cs
150lines · modecode
| 1 | using Microsoft.Teams.Api; |
| 2 | using Microsoft.Teams.Api.Activities; |
| 3 | using Microsoft.Teams.Api.Activities.Events; |
| 4 | using Microsoft.Teams.Api.Auth; |
| 5 | using Microsoft.Teams.Apps.Activities.Events; |
| 6 | using Microsoft.Teams.Apps.Annotations; |
| 7 | using Microsoft.Teams.Apps.Testing.Plugins; |
| 8 | |
| 9 | using static Microsoft.Teams.Apps.Activities.Events.Event; |
| 10 | |
| 11 | namespace Microsoft.Teams.Apps.Tests.Activities.Events; |
| 12 | |
| 13 | public class ReadReceiptEventTests |
| 14 | { |
| 15 | private readonly App _app = new(); |
| 16 | private readonly TestPlugin _plugin = new(); |
| 17 | private readonly ReadReceiptController _controller = new(); |
| 18 | private readonly IToken _token = Globals.Token; |
| 19 | |
| 20 | public ReadReceiptEventTests() |
| 21 | { |
| 22 | _app.AddPlugin(_plugin); |
| 23 | _app.AddController(_controller); |
| 24 | _token = Globals.Token; |
| 25 | } |
| 26 | |
| 27 | [Fact] |
| 28 | public async Task Should_CallHandler_OnReadReceiptEvent() |
| 29 | { |
| 30 | // Arrange |
| 31 | var handlerCalled = false; |
| 32 | var eventContext = default(IContext<ReadReceiptActivity>); |
| 33 | |
| 34 | _app.OnReadReceipt(context => |
| 35 | { |
| 36 | handlerCalled = true; |
| 37 | eventContext = context; |
| 38 | return Task.FromResult<object?>(null); |
| 39 | }); |
| 40 | |
| 41 | // Create a ReadReceiptActivity |
| 42 | var ReadReceiptActivity = new ReadReceiptActivity |
| 43 | { |
| 44 | Id = "readReceiptId", |
| 45 | }; |
| 46 | |
| 47 | // Act |
| 48 | var res = await _plugin.Do(_token, ReadReceiptActivity); |
| 49 | |
| 50 | // Assert |
| 51 | Assert.Equal(System.Net.HttpStatusCode.OK, res.Status); |
| 52 | Assert.True(handlerCalled, "The ReadReceipt event handler should be called"); |
| 53 | Assert.NotNull(eventContext); |
| 54 | Assert.IsType<ReadReceiptActivity>(eventContext.Activity); |
| 55 | } |
| 56 | |
| 57 | [Fact] |
| 58 | public async Task Should_NotCallHandler_ForOtherEventTypes() |
| 59 | { |
| 60 | // Arrange |
| 61 | var handlerCalled = false; |
| 62 | |
| 63 | _app.OnReadReceipt(context => |
| 64 | { |
| 65 | handlerCalled = true; |
| 66 | return Task.FromResult<object?>(null); |
| 67 | }); |
| 68 | |
| 69 | // Act - Send a different activity type |
| 70 | var res = await _plugin.Do(_token, new MessageActivity("hello world")); |
| 71 | |
| 72 | // Assert |
| 73 | Assert.Equal(System.Net.HttpStatusCode.OK, res.Status); |
| 74 | Assert.False(handlerCalled, "The ReadReceipt event handler should not be called for other activity types"); |
| 75 | } |
| 76 | |
| 77 | [Fact] |
| 78 | public void ReadReceiptAttribute_Select_ReturnsTrueForReadReceiptActivity() |
| 79 | { |
| 80 | // Arrange |
| 81 | var attribute = new ReadReceiptAttribute(); |
| 82 | var activity = new ReadReceiptActivity |
| 83 | { |
| 84 | Id = "readReceiptId", |
| 85 | Conversation = new Api.Conversation() |
| 86 | { |
| 87 | Id = "conversationId", |
| 88 | Name = "conversationName", |
| 89 | Type = new ConversationType("group"), |
| 90 | }, |
| 91 | ChannelId = new ChannelId("webchat"), |
| 92 | Recipient = new Account() |
| 93 | { |
| 94 | Id = "recipientId", |
| 95 | Name = "recipientName", |
| 96 | }, |
| 97 | ReplyToId = "replyToId", |
| 98 | }; |
| 99 | |
| 100 | // Act |
| 101 | var result = attribute.Select(activity); |
| 102 | |
| 103 | // Assert |
| 104 | Assert.True(result); |
| 105 | } |
| 106 | |
| 107 | [Fact] |
| 108 | public void ReadReceiptAttribute_Select_ReturnsFalseForOtherActivities() |
| 109 | { |
| 110 | // Arrange |
| 111 | var attribute = new ReadReceiptAttribute(); |
| 112 | var activity = new MessageActivity("hello world"); |
| 113 | |
| 114 | // Act |
| 115 | var result = attribute.Select(activity); |
| 116 | |
| 117 | // Assert |
| 118 | Assert.False(result); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | [Fact] |
| 123 | public async Task ReadReceiptAttribute_Controller_Call() |
| 124 | { |
| 125 | var activity = new ReadReceiptActivity |
| 126 | { |
| 127 | Id = "readReceiptId", |
| 128 | ReplyToId = "replyToId", |
| 129 | }; |
| 130 | |
| 131 | var res = await _app.Process<TestPlugin>(_token, activity); |
| 132 | |
| 133 | Assert.Equal(System.Net.HttpStatusCode.OK, res.Status); |
| 134 | Assert.Equal("readReceiptMethod", _controller.MethodCalled); |
| 135 | |
| 136 | } |
| 137 | |
| 138 | [TeamsController] |
| 139 | public class ReadReceiptController |
| 140 | { |
| 141 | public string MethodCalled { get; set; } = string.Empty; |
| 142 | |
| 143 | [ReadReceipt] |
| 144 | public async Task Method1(IContext<ReadReceiptActivity> context, [Context] IContext.Next next) |
| 145 | { |
| 146 | MethodCalled = "readReceiptMethod"; |
| 147 | await next(); |
| 148 | } |
| 149 | } |
| 150 | } |