|
9 | 9 | ChannelAccount,
|
10 | 10 | ConversationAccount,
|
11 | 11 | InputHints,
|
| 12 | + SignInConstants, |
12 | 13 | TokenResponse,
|
13 | 14 | )
|
14 | 15 |
|
@@ -260,3 +261,156 @@ async def callback_handler(turn_context: TurnContext):
|
260 | 261 |
|
261 | 262 | await adapter.send("Hello")
|
262 | 263 | self.assertTrue(called)
|
| 264 | + |
| 265 | + async def test_should_end_oauth_prompt_on_invalid_message_when_end_on_invalid_message( |
| 266 | + self, |
| 267 | + ): |
| 268 | + connection_name = "myConnection" |
| 269 | + token = "abc123" |
| 270 | + magic_code = "888999" |
| 271 | + |
| 272 | + async def exec_test(turn_context: TurnContext): |
| 273 | + dialog_context = await dialogs.create_context(turn_context) |
| 274 | + |
| 275 | + results = await dialog_context.continue_dialog() |
| 276 | + |
| 277 | + if results.status == DialogTurnStatus.Empty
9E88
span>: |
| 278 | + await dialog_context.prompt("prompt", PromptOptions()) |
| 279 | + elif results.status == DialogTurnStatus.Complete: |
| 280 | + if results.result and results.result.token: |
| 281 | + await turn_context.send_activity("Failed") |
| 282 | + |
| 283 | + else: |
| 284 | + await turn_context.send_activity("Ended") |
| 285 | + |
| 286 | + await convo_state.save_changes(turn_context) |
| 287 | + |
| 288 | + # Initialize TestAdapter. |
| 289 | + adapter = TestAdapter(exec_test) |
| 290 | + |
| 291 | + # Create ConversationState with MemoryStorage and register the state as middleware. |
| 292 | + convo_state = ConversationState(MemoryStorage()) |
| 293 | + |
| 294 | + # Create a DialogState property, DialogSet and AttachmentPrompt. |
| 295 | + dialog_state = convo_state.create_property("dialog_state") |
| 296 | + dialogs = DialogSet(dialog_state) |
| 297 | + dialogs.add( |
| 298 | + OAuthPrompt( |
| 299 | + "prompt", |
| 300 | + OAuthPromptSettings(connection_name, "Login", None, 300000, None, True), |
| 301 | + ) |
| 302 | + ) |
| 303 | + |
| 304 | + def inspector( |
| 305 | + activity: Activity, description: str = None |
| 306 | + ): # pylint: disable=unused-argument |
| 307 | + assert len(activity.attachments) == 1 |
| 308 | + assert ( |
| 309 | + activity.attachments[0].content_type |
| 310 | + == CardFactory.content_types.oauth_card |
| 311 | + ) |
| 312 | + |
| 313 | + # send a mock EventActivity back to the bot with the token |
| 314 | + adapter.add_user_token( |
| 315 | + connection_name, |
| 316 | + activity.channel_id, |
| 317 | + activity.recipient.id, |
| 318 | + token, |
| 319 | + magic_code, |
| 320 | + ) |
| 321 | + |
| 322 | + step1 = await adapter.send("Hello") |
| 323 | + step2 = await step1.assert_reply(inspector) |
| 324 | + step3 = await step2.send("test invalid message") |
| 325 | + await step3.assert_reply("Ended") |
| 326 | + |
| 327 | + async def test_should_timeout_oauth_prompt_with_message_activity(self,): |
| 328 | + activity = Activity(type=ActivityTypes.message, text="any") |
| 329 | + await self.run_timeout_test(activity) |
| 330 | + |
| 331 | + async def test_should_timeout_oauth_prompt_with_token_response_event_activity( |
| 332 | + self, |
| 333 | + ): |
| 334 | + activity = Activity( |
| 335 | + type=ActivityTypes.event, name=SignInConstants.token_response_event_name |
| 336 | + ) |
| 337 | + await self.run_timeout_test(activity) |
| 338 | + |
| 339 | + async def test_should_timeout_oauth_prompt_with_verify_state_operation_activity( |
| 340 | + self, |
| 341 | + ): |
| 342 | + activity = Activity( |
| 343 | + type=ActivityTypes.invoke, name=SignInConstants.verify_state_operation_name |
| 344 | + ) |
| 345 | + await self.run_timeout_test(activity) |
| 346 | + |
| 347 | + async def test_should_not_timeout_oauth_prompt_with_custom_event_activity(self,): |
| 348 | + activity = Activity(type=ActivityTypes.event, name="custom event name") |
| 349 | + await self.run_timeout_test(activity, False, "Ended", "Failed") |
| 350 | + |
| 351 | + async def run_timeout_test( |
| 352 | + self, |
| 353 | + activity: Activity, |
| 354 | + should_succeed: bool = True, |
| 355 | + token_response: str = "Failed", |
| 356 | + no_token_resonse="Ended", |
| 357 | + ): |
| 358 | + connection_name = "myConnection" |
| 359 | + token = "abc123" |
| 360 | + magic_code = "888999" |
| 361 | + |
| 362 | + async def exec_test(turn_context: TurnContext): |
| 363 | + dialog_context = await dialogs.create_context(turn_context) |
| 364 | + |
| 365 | + results = await dialog_context.continue_dialog() |
| 366 | + |
| 367 | + if results.status == DialogTurnStatus.Empty: |
| 368 | + await dialog_context.prompt("prompt", PromptOptions()) |
| 369 | + elif results.status == DialogTurnStatus.Complete or ( |
| 370 | + results.status == DialogTurnStatus.Waiting and not should_succeed |
| 371 | + ): |
| 372 | + if results.result and results.result.token: |
| 373 | + await turn_context.send_activity(token_response) |
| 374 | + |
| 375 | + else: |
| 376 | + await turn_context.send_activity(no_token_resonse) |
| 377 | + |
| 378 | + await convo_state.save_changes(turn_context) |
| 379 | + |
| 380 | + # Initialize TestAdapter. |
| 381 | + adapter = TestAdapter(exec_test) |
| 382 | + |
| 383 | + # Create ConversationState with MemoryStorage and register the state as middleware. |
| 384 | + convo_state = ConversationState(MemoryStorage()) |
| 385 | + |
| 386 | + # Create a DialogState property, DialogSet and AttachmentPrompt. |
| 387 | + dialog_state = convo_state.create_property("dialog_state") |
| 388 | + dialogs = DialogSet(dialog_state) |
| 389 | + dialogs.add( |
| 390 | + OAuthPrompt( |
| 391 | + "prompt", OAuthPromptSettings(connection_name, "Login", None, 1), |
| 392 | + ) |
| 393 | + ) |
| 394 | + |
| 395 | + def inspector( |
| 396 | + activity: Activity, description: str = None |
| 397 | + ): # pylint: disable=unused-argument |
| 398 | + assert len(activity.attachments) == 1 |
| 399 | + assert ( |
| 400 | + activity.attachments[0].content_type |
| 401 | + == CardFactory.content_types.oauth_card |
| 402 | + ) |
| 403 | + |
| 404 | + # send a mock EventActivity back to the bot with the token |
| 405 | + adapter.add_user_token( |
| 406 | + connection_name, |
| 407 | + activity.channel_id, |
| 408 | + activity.recipient.id, |
| 409 | + token, |
| 410 | + magic_code, |
| 411 | + ) |
| 412 | + |
| 413 | + step1 = await adapter.send("Hello") |
| 414 | + step2 = await step1.assert_reply(inspector) |
| 415 | + step3 = await step2.send(activity) |
| 416 | + await step3.assert_reply(no_token_resonse) |
0 commit comments