microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/test/Microsoft.Teams.Apps.UnitTests/CreateDerivedContextTests.cs
176lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.AspNetCore.Http; |
| 5 | using Microsoft.Extensions.Configuration; |
| 6 | using Microsoft.Extensions.Logging.Abstractions; |
| 7 | using Microsoft.Teams.Apps.Api.Clients; |
| 8 | using Microsoft.Teams.Apps.Handlers; |
| 9 | using Microsoft.Teams.Apps.Schema; |
| 10 | using Microsoft.Teams.Apps.State; |
| 11 | using Microsoft.Teams.Core; |
| 12 | using Moq; |
| 13 | |
| 14 | namespace Microsoft.Teams.Apps.UnitTests; |
| 15 | |
| 16 | /// <summary> |
| 17 | /// Unit tests for <see cref="Context{TActivity}.CreateDerivedContext()"/> |
| 18 | /// and <see cref="Context{TActivity}.CreateDerivedContext{TNew}(TNew)"/>. |
| 19 | /// </summary> |
| 20 | public class CreateDerivedContextTests |
| 21 | { |
| 22 | // ===== Parameterless overload ===== |
| 23 | |
| 24 | [Fact] |
| 25 | public void Parameterless_PreservesBotApplication() |
| 26 | { |
| 27 | TeamsBotApplication app = CreateApp(); |
| 28 | Context<TeamsActivity> source = CreateContext(app); |
| 29 | |
| 30 | Context<TeamsActivity> derived = source.CreateDerivedContext(); |
| 31 | |
| 32 | Assert.Same(app, derived.TeamsBotApplication); |
| 33 | } |
| 34 | |
| 35 | [Fact] |
| 36 | public void Parameterless_PreservesActivity() |
| 37 | { |
| 38 | TeamsBotApplication app = CreateApp(); |
| 39 | InvokeActivity activity = new() { Type = TeamsActivityType.Invoke, Name = "test" }; |
| 40 | Context<TeamsActivity> source = new(app, activity); |
| 41 | |
| 42 | Context<TeamsActivity> derived = source.CreateDerivedContext(); |
| 43 | |
| 44 | Assert.Same(activity, derived.Activity); |
| 45 | } |
| 46 | |
| 47 | [Fact] |
| 48 | public void Parameterless_PropagatesState() |
| 49 | { |
| 50 | TeamsBotApplication app = CreateApp(); |
| 51 | Context<TeamsActivity> source = CreateContext(app); |
| 52 | TurnStateContainer state = CreateState(); |
| 53 | source.State = state; |
| 54 | |
| 55 | Context<TeamsActivity> derived = source.CreateDerivedContext(); |
| 56 | |
| 57 | Assert.Same(state, derived.State); |
| 58 | } |
| 59 | |
| 60 | [Fact] |
| 61 | public void Parameterless_WithoutState_DoesNotSetState() |
| 62 | { |
| 63 | TeamsBotApplication app = CreateApp(); |
| 64 | Context<TeamsActivity> source = CreateContext(app); |
| 65 | |
| 66 | Context<TeamsActivity> derived = source.CreateDerivedContext(); |
| 67 | |
| 68 | Assert.False(derived.HasState); |
| 69 | } |
| 70 | |
| 71 | // ===== Typed overload ===== |
| 72 | |
| 73 | [Fact] |
| 74 | public void Typed_PreservesBotApplication() |
| 75 | { |
| 76 | TeamsBotApplication app = CreateApp(); |
| 77 | Context<TeamsActivity> source = CreateContext(app); |
| 78 | InvokeActivity typedActivity = new() { Type = TeamsActivityType.Invoke, Name = "test" }; |
| 79 | |
| 80 | Context<InvokeActivity> derived = source.CreateDerivedContext(typedActivity); |
| 81 | |
| 82 | Assert.Same(app, derived.TeamsBotApplication); |
| 83 | } |
| 84 | |
| 85 | [Fact] |
| 86 | public void Typed_UsesNewActivity() |
| 87 | { |
| 88 | TeamsBotApplication app = CreateApp(); |
| 89 | Context<TeamsActivity> source = CreateContext(app); |
| 90 | InvokeActivity typedActivity = new() { Type = TeamsActivityType.Invoke, Name = "test" }; |
| 91 | |
| 92 | Context<InvokeActivity> derived = source.CreateDerivedContext(typedActivity); |
| 93 | |
| 94 | Assert.Same(typedActivity, derived.Activity); |
| 95 | Assert.NotSame(source.Activity, derived.Activity); |
| 96 | } |
| 97 | |
| 98 | [Fact] |
| 99 | public void Typed_PropagatesState() |
| 100 | { |
| 101 | TeamsBotApplication app = CreateApp(); |
| 102 | Context<TeamsActivity> source = CreateContext(app); |
| 103 | TurnStateContainer state = CreateState(); |
| 104 | source.State = state; |
| 105 | InvokeActivity typedActivity = new() { Type = TeamsActivityType.Invoke, Name = "test" }; |
| 106 | |
| 107 | Context<InvokeActivity> derived = source.CreateDerivedContext(typedActivity); |
| 108 | |
| 109 | Assert.Same(state, derived.State); |
| 110 | } |
| 111 | |
| 112 | [Fact] |
| 113 | public void Typed_WithoutState_DoesNotSetState() |
| 114 | { |
| 115 | TeamsBotApplication app = CreateApp(); |
| 116 | Context<TeamsActivity> source = CreateContext(app); |
| 117 | InvokeActivity typedActivity = new() { Type = TeamsActivityType.Invoke, Name = "test" }; |
| 118 | |
| 119 | Context<InvokeActivity> derived = source.CreateDerivedContext(typedActivity); |
| 120 | |
| 121 | Assert.False(derived.HasState); |
| 122 | } |
| 123 | |
| 124 | [Fact] |
| 125 | public void Typed_WidensActivityType() |
| 126 | { |
| 127 | TeamsBotApplication app = CreateApp(); |
| 128 | InvokeActivity invokeActivity = new() { Type = TeamsActivityType.Invoke, Name = "test" }; |
| 129 | Context<InvokeActivity> source = new(app, invokeActivity); |
| 130 | TurnStateContainer state = CreateState(); |
| 131 | source.State = state; |
| 132 | |
| 133 | Context<TeamsActivity> derived = source.CreateDerivedContext((TeamsActivity)invokeActivity); |
| 134 | |
| 135 | Assert.Same(invokeActivity, derived.Activity); |
| 136 | Assert.Same(state, derived.State); |
| 137 | } |
| 138 | |
| 139 | // ===== Helpers ===== |
| 140 | |
| 141 | private static TurnStateContainer CreateState() |
| 142 | { |
| 143 | TurnState convState = new(); |
| 144 | convState.Set("test-key", "test-value"); |
| 145 | return new TurnStateContainer(convState, new TurnState()); |
| 146 | } |
| 147 | |
| 148 | private static Context<TeamsActivity> CreateContext(TeamsBotApplication app) |
| 149 | { |
| 150 | TeamsActivity activity = new() { Type = TeamsActivityType.Message }; |
| 151 | return new Context<TeamsActivity>(app, activity); |
| 152 | } |
| 153 | |
| 154 | private static TeamsBotApplication CreateApp() |
| 155 | { |
| 156 | Mock<UserTokenClient> mockUserTokenClient = new( |
| 157 | new HttpClient(), |
| 158 | new Mock<IConfiguration>().Object, |
| 159 | NullLogger<UserTokenClient>.Instance); |
| 160 | |
| 161 | Mock<ConversationClient> mockConversationClient = new( |
| 162 | new HttpClient(), |
| 163 | NullLogger<ConversationClient>.Instance); |
| 164 | |
| 165 | ApiClient apiClient = new( |
| 166 | new HttpClient(), |
| 167 | mockConversationClient.Object, |
| 168 | mockUserTokenClient.Object); |
| 169 | |
| 170 | return new TeamsBotApplication( |
| 171 | apiClient, |
| 172 | new HttpContextAccessor(), |
| 173 | NullLogger<TeamsBotApplication>.Instance, |
| 174 | new TeamsBotApplicationOptions { AppId = "test-app-id" }); |
| 175 | } |
| 176 | } |
| 177 | |