Ejemplo n.º 1
0
        public async Task <IEnumerable <DialogError> > Handle(DialogSubmission dialog)
        {
            if (dialog.CallbackId == DialogDemoBase.EchoDialog)
            {
                await _slack.Chat.PostMessage(new Message
                {
                    Channel     = dialog.Channel.Id,
                    Attachments =
                    {
                        new Attachment
                        {
                            Text   = "Dialog fields",
                            Fields = dialog.Submission
                                     .Select(e => new Field
                            {
                                Title = e.Key,
                                Value = e.Value
                            })
                                     .ToList()
                        }
                    }
                }).ConfigureAwait(false);

                return(null);
            }
            else
            {
                return(dialog.Submission.Keys
                       .Select(name => new DialogError {
                    Name = name, Error = $"Not {name}-y enough!"
                }));
            }
        }
Ejemplo n.º 2
0
        public async Task Route_InvocationIsAddAnswerDialogSubmission_ShouldCallAppropriateServices()
        {
            // Arrange
            var dialog = new DialogSubmission <AddAnswerSubmission>
            {
                Type       = MessageType.DialogSubmission,
                CallbackId = "callbackId"
            };

            var payloadRaw = JsonConvert.SerializeObject(dialog, SlackSerializerSettings.DefaultSettings);

            _submissionSelectServiceMock.Setup(m => m.Choose(It.IsAny <string>()))
            .Returns(typeof(DialogSubmission <AddAnswerSubmission>));
            _slackExecutorServiceMock.Setup(m => m.ExecuteSubmission(It.IsAny <Type>(), It.IsAny <object[]>()))
            .Returns(Task.CompletedTask);

            // Act
            await _service.Route(payloadRaw);

            // Assert
            _submissionSelectServiceMock.Verify(m => m.Choose(It.Is <string>(c => c == dialog.CallbackId)), Times.Once);
            _submissionSelectServiceMock.VerifyNoOtherCalls();
            _slackExecutorServiceMock.Verify(
                m => m.ExecuteSubmission(It.Is <Type>(t => t == typeof(DialogSubmission <AddAnswerSubmission>)), It.IsAny <object[]>()),
                Times.Once);
            _slackExecutorServiceMock.VerifyNoOtherCalls();
            _interactiveMessageServiceMock.VerifyNoOtherCalls();
        }
Ejemplo n.º 3
0
        public async Task ProcessSubmission(DialogSubmission <AddAnswerSubmission> submission)
        {
            if (submission == null)
            {
                throw new ArgumentNullException(nameof(submission));
            }

            var customParams = _callbackIdCustomParamsWrappingService.Unwrap(submission.CallbackId);
            var answer       = submission.Submission.ExpertsAnswer.Trim('\n', ' ');
            var questionId   = customParams.FirstOrDefault();

            if (questionId == null)
            {
                throw new ArgumentNullException(nameof(questionId));
            }

            await _questionService.AppendAnswerAsync(questionId, answer);

            LogNewAnswer(submission.User, questionId, answer);

            var question = await _questionService.GetQuestionAsync(questionId);

            await UpdateMessageForUser(answer, question.Text, submission).ConfigureAwait(false);

            await NotifyWatchers(question, answer, submission.User.Id).ConfigureAwait(false);
        }
        public async Task ProcessSubmission_CallbackIdDoesNotContainQuestionId_ArgumentNullException()
        {
            // Arrange
            const string expectedQuestionId = "questionId";
            var          dialog             = new DialogSubmission <AddAnswerSubmission>
            {
                User = new ItemInfo
                {
                    Id   = "userId",
                    Name = "userName"
                },
                Channel = new ItemInfo
                {
                    Id   = "channelId",
                    Name = "ChannelName"
                },
                CallbackId = expectedQuestionId,
                Submission = new AddAnswerSubmission
                {
                    ExpertsAnswer = "Answer goes here"
                }
            };

            _callbackIdCustomParamsWrapperMock.Setup(m => m.Unwrap(It.IsAny <string>()))
            .Returns(new List <string>());

            // Act-Assert
            await Assert.ThrowsAsync <ArgumentNullException>(() => _service.ProcessSubmission(dialog));
        }
Ejemplo n.º 5
0
 public async Task <IEnumerable <DialogError> > Handle(DialogSubmission dialog)
 {
     return(dialog.Submission.Keys
            .Select(name => new DialogError {
         Name = name, Error = $"Not {name}-y enough!"
     }));
 }
Ejemplo n.º 6
0
        private async Task <SlackResult> HandleDialogSubmission(DialogSubmission dialog)
        {
            var errors = (await _dialogSubmissionHandler.Handle(dialog).ConfigureAwait(false))?.ToList()
                         ?? new List <DialogError>();

            var body = new DialogErrorResponse {
                Errors = errors
            };

            return(errors.Any()
                ? (SlackResult) new JsonResult(_jsonSettings, HttpStatusCode.OK, body)
                : new EmptyResult(HttpStatusCode.OK));
        }
Ejemplo n.º 7
0
        public void RegisterDialogSubmissionHandler_HandleSubmissions()
        {
            // Arrange
            var handler = Substitute.For <IDialogSubmissionHandler>();
            var otherDialogSubmission = new DialogSubmission {
                CallbackId = "other"
            };
            var dialogSubmission = new DialogSubmission {
                CallbackId = "key"
            };

            var sut = Configure(c => c.RegisterDialogSubmissionHandler("key", handler));

            // Act
            HandleLegacyDialogSubmissions(sut, new[] { otherDialogSubmission, dialogSubmission });

            // Assert
            handler.DidNotReceive().Handle(otherDialogSubmission);
            handler.Received().Handle(dialogSubmission);
        }
Ejemplo n.º 8
0
        public async Task <IEnumerable <DialogError> > Handle(DialogSubmission dialog)
        {
            await _slack.Chat.PostMessage(new Message
            {
                Channel     = dialog.Channel.Id,
                Attachments =
                {
                    new Attachment
                    {
                        Text   = "Dialog fields",
                        Fields = dialog.Submission
                                 .Select(e => new Field
                        {
                            Title = e.Key,
                            Value = e.Value
                        })
                                 .ToList()
                    }
                }
            }).ConfigureAwait(false);

            return(null);
        }
Ejemplo n.º 9
0
        private async Task <HttpResponse> HandleDialogSubmission(HttpContext context, DialogSubmission dialog)
        {
            var errors = (await _dialogSubmissionHandler.Handle(dialog).ConfigureAwait(false))?.ToList()
                         ?? new List <DialogError>();

            return(errors.Any()
                ? await context.Respond(HttpStatusCode.OK, "application/json", Serialize(new DialogErrorResponse {
                Errors = errors
            })).ConfigureAwait(false)
                : await context.Respond(HttpStatusCode.OK).ConfigureAwait(false));
        }
 public Task <IEnumerable <DialogError> > Handle(DialogSubmission dialog) => ResolvedHandle(h => h.Handle(dialog));
        public async Task ProcessSubmission_JustInvoked_ShouldSendMessageToChannel()
        {
            // Arrange
            const string expectedQuestionId = "questionId";
            var          dialog             = new DialogSubmission <AddAnswerSubmission>
            {
                User = new ItemInfo
                {
                    Id   = "userId",
                    Name = "userName"
                },
                Channel = new ItemInfo
                {
                    Id   = "channelId",
                    Name = "ChannelName"
                },
                CallbackId = expectedQuestionId,
                Submission = new AddAnswerSubmission
                {
                    ExpertsAnswer = "Answer goes here"
                }
            };

            var question = new Question
            {
                Text          = "text",
                AskedUsersIds = new List <string>()
            };

            string actualChannelId      = null;
            string actualQuestionId     = null;
            string actualAppendedAnswer = null;

            _callbackIdCustomParamsWrapperMock.Setup(m => m.Unwrap(It.IsAny <string>()))
            .Returns(new List <string> {
                expectedQuestionId
            });
            _questionServiceMock.Setup(m => m.AppendAnswerAsync(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(Task.CompletedTask)
            .Callback((string questionId, string answer) =>
            {
                actualQuestionId     = questionId;
                actualAppendedAnswer = answer;
            });
            _questionServiceMock.Setup(s => s.GetQuestionAsync(It.IsAny <string>())).ReturnsAsync(question);

            _slackHttpClientMock.Setup(m =>
                                       m.UpdateMessageAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <List <AttachmentDto> >()))
            .Returns(Task.CompletedTask)
            .Callback((string timeStamp, string channelId, string message, List <AttachmentDto> attachments) => actualChannelId = channelId);

            // Act
            await _service.ProcessSubmission(dialog);

            // Assert
            Assert.Equal(actualChannelId, dialog.Channel.Id);
            Assert.Equal(actualAppendedAnswer, dialog.Submission.ExpertsAnswer);
            Assert.Equal(actualQuestionId, expectedQuestionId);
            _questionServiceMock.Verify(m => m.AppendAnswerAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Once);
            _questionServiceMock.Verify(s => s.GetQuestionAsync(It.IsAny <string>()), Times.Once);
            _questionServiceMock.VerifyNoOtherCalls();
            _slackHttpClientMock.Verify(
                m => m.UpdateMessageAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <List <AttachmentDto> >()),
                Times.Once);
            _slackHttpClientMock.VerifyNoOtherCalls();
        }
        public async Task ProcessSubmission_JustInvoked_ShouldSendNotificationForAllSubscribers()
        {
            //Arrange
            var users = new List <string>
            {
                "1",
                "2"
            };

            var channelIds = new List <ChannelDto>
            {
                new ChannelDto
                {
                    Id = "channel1"
                },
                new ChannelDto
                {
                    Id = "channel2"
                }
            };


            var dialog = new DialogSubmission <AddAnswerSubmission>
            {
                User = new ItemInfo
                {
                    Id   = "userId",
                    Name = "userName"
                },
                Channel = new ItemInfo
                {
                    Id   = "channelId",
                    Name = "ChannelName"
                },
                CallbackId = "questionId",
                Submission = new AddAnswerSubmission
                {
                    ExpertsAnswer = "Answer goes here"
                }
            };

            var question = new Question
            {
                Text          = "text",
                AskedUsersIds = users
            };

            _callbackIdCustomParamsWrapperMock.Setup(m => m.Unwrap(It.IsAny <string>()))
            .Returns(new List <string> {
                "Id"
            });

            _questionServiceMock
            .Setup(s => s.GetQuestionAsync(It.IsAny <string>()))
            .ReturnsAsync(question);

            _slackHttpClientMock
            .Setup(s => s.OpenDirectMessageChannelAsync(users[0]))
            .ReturnsAsync(channelIds[0]);
            _slackHttpClientMock
            .Setup(s => s.OpenDirectMessageChannelAsync(users[1]))
            .ReturnsAsync(channelIds[1]);

            // Act
            await _service.ProcessSubmission(dialog);

            //Assert
            _questionServiceMock.Verify(s => s.GetQuestionAsync(It.IsAny <string>()), Times.Once);


            _slackHttpClientMock
            .Verify(s => s.OpenDirectMessageChannelAsync(It.IsAny <string>()), Times.Exactly(2));

            _slackHttpClientMock
            .Verify(s => s.SendMessageAsync(channelIds[0].Id, It.IsAny <string>(), It.IsAny <List <AttachmentDto> >()), Times.Once);
            _slackHttpClientMock
            .Verify(s => s.SendMessageAsync(channelIds[1].Id, It.IsAny <string>(), It.IsAny <List <AttachmentDto> >()), Times.Once);
        }
Ejemplo n.º 13
0
 public async Task <IEnumerable <DialogError> > Handle(DialogSubmission dialog) => Enumerable.Empty <DialogError>();
Ejemplo n.º 14
0
 public Task <IEnumerable <DialogError> > Handle(DialogSubmission dialog) => Task.FromResult(Enumerable.Empty <DialogError>());