microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/close-pull-request

Branches

Tags

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

Clone

HTTPS

Download ZIP

Tests/Microsoft.Teams.Apps.Tests/Activities/Events/MeetingLeaveEventTests.cs

170lines · modecode

1using Microsoft.Teams.Api;
2using Microsoft.Teams.Api.Activities;
3using Microsoft.Teams.Api.Activities.Events;
4using Microsoft.Teams.Api.Auth;
5using Microsoft.Teams.Apps.Activities.Events;
6using Microsoft.Teams.Apps.Testing.Plugins;
7
8using static Microsoft.Teams.Apps.Activities.Events.Event;
9
10
11
12namespace Microsoft.Teams.Apps.Tests.Activities.Events;
13
14public class MeetingLeaveEventTests
15{
16 private readonly App _app = new();
17 private readonly TestPlugin _plugin = new();
18 private readonly MeetingActivityController _controller = new();
19 private readonly IToken _token = Globals.Token;
20
21 public MeetingLeaveEventTests()
22 {
23 _app.AddPlugin(_plugin);
24 _app.AddController(_controller);
25 _token = Globals.Token;
26 }
27
28 [Fact]
29 public async Task Should_CallHandler_OnMeetingLeaveEvent()
30 {
31 // Arrange
32 var handlerCalled = false;
33 var eventContext = default(IContext<MeetingParticipantLeaveActivity>);
34
35 _app.OnMeetingLeave(context =>
36 {
37 handlerCalled = true;
38 eventContext = context;
39 return Task.FromResult<object?>(null);
40 });
41
42 // Create a MeetingLeaveActivity
43 var meetingLeaveActivity = new MeetingParticipantLeaveActivity
44 {
45 Value = new MeetingParticipantLeaveActivityValue
46 {
47 Members = new List<MeetingParticipantLeaveActivityValue.Member>
48 {
49 new MeetingParticipantLeaveActivityValue.Member
50 {
51 User = new Account { Id = "user1", Name = "Test User" },
52 Meeting = new MeetingParticipantLeaveActivityValue.Meeting
53 {
54 InMeeting = true,
55 Role = Role.User
56 }
57 },
58
59 }
60 }
61 };
62
63 // Act
64 var res = await _plugin.Do(_token, meetingLeaveActivity);
65
66 // Assert
67 Assert.Equal(System.Net.HttpStatusCode.OK, res.Status);
68 Assert.True(handlerCalled, "The MeetingLeave event handler should be called");
69 Assert.NotNull(eventContext);
70 Assert.IsType<MeetingParticipantLeaveActivity>(eventContext.Activity);
71 Assert.Single(eventContext.Activity.Value.Members);
72 }
73
74 [Fact]
75 public async Task Should_NotCallHandler_ForOtherEventTypes()
76 {
77 // Arrange
78 var handlerCalled = false;
79
80 _app.OnMeetingLeave(context =>
81 {
82 handlerCalled = true;
83 return Task.FromResult<object?>(null);
84 });
85
86 // Act - Leave a different activity type
87 var res = await _plugin.Do(_token, new ReadReceiptActivity());
88
89 // Assert
90 Assert.Equal(System.Net.HttpStatusCode.OK, res.Status);
91 Assert.False(handlerCalled, "The MeetingLeave event handler should not be called for other activity types");
92 }
93
94 [Fact]
95 public void MeetingLeaveAttribute_Select_ReturnsTrueForMeetingLeaveActivity()
96 {
97 // Arrange
98 var attribute = new MeetingLeaveAttribute();
99 var activity = new MeetingParticipantLeaveActivity
100 {
101 Value = new MeetingParticipantLeaveActivityValue
102 {
103 Members = new List<MeetingParticipantLeaveActivityValue.Member>
104 {
105 new MeetingParticipantLeaveActivityValue.Member
106 {
107 User = new Account { Id = "user1", Name = "Test User" },
108 Meeting = new MeetingParticipantLeaveActivityValue.Meeting
109 {
110 InMeeting = true,
111 Role = Role.User
112 }
113 },
114
115 }
116 }
117 };
118
119 // Act
120 var result = attribute.Select(activity);
121
122 // Assert
123 Assert.True(result);
124 }
125
126 [Fact]
127 public void MeetingLeaveAttribute_Select_ReturnsFalseForOtherActivities()
128 {
129 // Arrange
130 var attribute = new MeetingLeaveAttribute();
131 var activity = new MessageActivity("hello world");
132
133 // Act
134 var result = attribute.Select(activity);
135
136 // Assert
137 Assert.False(result);
138 }
139
140
141 [Fact]
142 public async Task MeetingLeaveAttribute_Controller_Call()
143 {
144 var activity = new MeetingParticipantLeaveActivity
145 {
146 Value = new MeetingParticipantLeaveActivityValue
147 {
148 Members = new List<MeetingParticipantLeaveActivityValue.Member>
149 {
150 new MeetingParticipantLeaveActivityValue.Member
151 {
152 User = new Account { Id = "user1", Name = "Test User" },
153 Meeting = new MeetingParticipantLeaveActivityValue.Meeting
154 {
155 InMeeting = true,
156 Role = Role.User
157 }
158 },
159
160 }
161 }
162 };
163
164 var res = await _app.Process<TestPlugin>(_token, activity);
165
166 Assert.Equal(System.Net.HttpStatusCode.OK, res.Status);
167 Assert.Equal("meetingLeaveMethod", _controller.MethodCalled);
168 }
169}