Exemplo n.º 1
0
        public void TestQuestionCommand()
        {
            SlackRequestDoc requestDoc = new SlackRequestDoc
            {
                Text = "  question   What    does ATM stand for?   "
            };
            SlackResponseDoc responseDoc = new SlackResponseDoc();

            triviaGameService.Setup(x => x.SubmitQuestion(It.IsAny <SlackRequestDoc>(), It.IsAny <string>())).Returns(responseDoc);

            SlackResponseDoc result = cut.ProcessSlashCommand(requestDoc);

            Assert.AreSame(responseDoc, result);

            triviaGameService.Verify(x => x.SubmitQuestion(requestDoc, "What    does ATM stand for?"));
        }
Exemplo n.º 2
0
        public void TestStopCommand()
        {
            SlackRequestDoc requestDoc = new SlackRequestDoc
            {
                Text = "  stop  "
            };
            SlackResponseDoc responseDoc = new SlackResponseDoc();

            triviaGameService.Setup(x => x.Stop(It.IsAny <SlackRequestDoc>())).Returns(responseDoc);

            SlackResponseDoc result = cut.ProcessSlashCommand(requestDoc);

            Assert.AreSame(responseDoc, result);

            triviaGameService.Verify(x => x.Stop(requestDoc));
        }
Exemplo n.º 3
0
        public void TestStartCommandWithTopic()
        {
            SlackRequestDoc requestDoc = new SlackRequestDoc
            {
                Text = "  start  movie quotes"
            };
            SlackResponseDoc responseDoc = new SlackResponseDoc();

            triviaGameService.Setup(x => x.Start(It.IsAny <SlackRequestDoc>(), It.IsAny <string>())).Returns(responseDoc);

            SlackResponseDoc result = cut.ProcessSlashCommand(requestDoc);

            Assert.AreSame(responseDoc, result);

            triviaGameService.Verify(x => x.Start(requestDoc, "movie quotes"));
        }
Exemplo n.º 4
0
        public void TestMarkCorrectCommandWithAnswer()
        {
            SlackRequestDoc requestDoc = new SlackRequestDoc
            {
                Text = "correct <@12345>   I    do not    know"
            };
            SlackResponseDoc responseDoc = new SlackResponseDoc();

            triviaGameService.Setup(x => x.MarkAnswerCorrect(It.IsAny <SlackRequestDoc>(), It.IsAny <string>(), It.IsAny <string>())).Returns(responseDoc);

            SlackResponseDoc result = cut.ProcessSlashCommand(requestDoc);

            Assert.AreSame(responseDoc, result);

            triviaGameService.Verify(x => x.MarkAnswerCorrect(requestDoc, "<@12345>", "I    do not    know"));
        }
Exemplo n.º 5
0
        public void TestAnswerCommand()
        {
            SlackRequestDoc requestDoc = new SlackRequestDoc
            {
                Text = "  answer   I    do not    know   "
            };
            SlackResponseDoc responseDoc = new SlackResponseDoc();

            triviaGameService.Setup(x => x.SubmitAnswer(It.IsAny <SlackRequestDoc>(), It.IsAny <string>())).Returns(responseDoc);

            SlackResponseDoc result = cut.ProcessSlashCommand(requestDoc);

            Assert.AreSame(responseDoc, result);

            triviaGameService.Verify(x => x.SubmitAnswer(requestDoc, "I    do not    know"));
        }
Exemplo n.º 6
0
        public void TestQuestionCommandWithTooFewArguments()
        {
            SlackRequestDoc requestDoc = new SlackRequestDoc
            {
                Command = "/command",
                Text    = "question"
            };

            SlackResponseDoc result = cut.ProcessSlashCommand(requestDoc);

            Assert.IsNotNull(result);
            Assert.AreEqual(SlackResponseType.EPHEMERAL, result.ResponseType);
            Assert.AreEqual("To submit a question, use `/command question <QUESTION_TEXT>`.\n\nFor example, `/command question In what year did WWII officially begin?`", result.Text);
            Assert.IsNull(result.Attachments);

            triviaGameService.VerifyNoOtherCalls();
        }
Exemplo n.º 7
0
        public void TestAnswerCommandWithTooFewArguments()
        {
            SlackRequestDoc requestDoc = new SlackRequestDoc
            {
                Command = "/command",
                Text    = "answer"
            };

            SlackResponseDoc result = cut.ProcessSlashCommand(requestDoc);

            Assert.IsNotNull(result);
            Assert.AreEqual(SlackResponseType.EPHEMERAL, result.ResponseType);
            Assert.AreEqual("To submit an answer, use `/command answer <ANSWER_TEXT>`.\n\nFor example, `/command answer Blue skies`", result.Text);
            Assert.IsNull(result.Attachments);

            triviaGameService.VerifyNoOtherCalls();
        }
Exemplo n.º 8
0
        public SlackResponseDoc SubmitAnswer(SlackRequestDoc requestDoc, string answer)
        {
            try
            {
                _workflowService.OnAnswerSubmitted(
                    requestDoc.ChannelId,
                    requestDoc.UserId,
                    requestDoc.Username,
                    answer,
                    requestDoc.RequestTime
                    );
            }
            catch (GameNotStartedException)
            {
                return(SlackResponseDoc.Failure(String.Format(GAME_NOT_STARTED_FORMAT, requestDoc.Command)));
            }
            catch (WorkflowException e)
            {
                return(SlackResponseDoc.Failure(e.Message));
            }

            SlackUser user = new SlackUser(requestDoc.UserId, requestDoc.Username);

            _scoreService.CreateUserIfNotExists(requestDoc.ChannelId, user);

            SlackResponseDoc delayedResponseDoc = new SlackResponseDoc
            {
                ResponseType = SlackResponseType.IN_CHANNEL,
                Text         = String.Format("<@{0}> answers:", requestDoc.UserId),
                Attachments  = new List <SlackAttachment> {
                    new SlackAttachment(answer, false)
                }
            };

            _delayedSlackService.sendResponse(requestDoc.ResponseUrl, delayedResponseDoc);

            return(new SlackResponseDoc
            {
                ResponseType = SlackResponseType.EPHEMERAL,
                Text = "Answer submitted."
            });
        }
Exemplo n.º 9
0
        public SlackResponseDoc Pass(SlackRequestDoc requestDoc, string target)
        {
            string userId = SlackUtils.NormalizeId(target);

            try
            {
                bool userExists = _scoreService.DoesUserExist(requestDoc.ChannelId, userId);

                if (!userExists)
                {
                    SlackResponseDoc responseDoc = SlackResponseDoc.Failure("User " + target + " does not exist. Please choose a valid user.");
                    responseDoc.Attachments = new List <SlackAttachment> {
                        new SlackAttachment("Usage: `" + requestDoc.Command + " pass @jsmith`")
                    };
                    return(responseDoc);
                }

                _workflowService.OnTurnChanged(requestDoc.ChannelId, requestDoc.UserId, userId);
            }
            catch (GameNotStartedException)
            {
                return(SlackResponseDoc.Failure(String.Format(GAME_NOT_STARTED_FORMAT, requestDoc.Command)));
            }
            catch (WorkflowException e)
            {
                return(SlackResponseDoc.Failure(e.Message));
            }

            SlackResponseDoc delayedResponseDoc = new SlackResponseDoc
            {
                ResponseType = SlackResponseType.IN_CHANNEL,
                Text         = String.Format("<@{0}> has decided to pass his/her turn to <@{1}>.\n\nOK, <@{1}>, it's your turn to ask a question!", requestDoc.UserId, userId)
            };

            _delayedSlackService.sendResponse(requestDoc.ResponseUrl, delayedResponseDoc);

            return(new SlackResponseDoc
            {
                ResponseType = SlackResponseType.EPHEMERAL,
                Text = "Turn passed to <@" + userId + ">."
            });
        }
Exemplo n.º 10
0
        public void TestMarkCorrectCommandWithTooFewArguments()
        {
            SlackRequestDoc requestDoc = new SlackRequestDoc
            {
                Command = "/command",
                Text    = "correct"
            };

            SlackResponseDoc result = cut.ProcessSlashCommand(requestDoc);

            Assert.IsNotNull(result);
            Assert.AreEqual(SlackResponseType.EPHEMERAL, result.ResponseType);
            Assert.AreEqual(
                "To mark an answer correct, use `/command correct <USERNAME>`.\n" +
                "Optional: To include the correct answer, use `/command correct <USERNAME> <CORRECT_ANSWER>`.\n\n" +
                "For example, `/command correct @jsmith Chris Farley`",
                result.Text
                );
            Assert.IsNull(result.Attachments);

            triviaGameService.VerifyNoOtherCalls();
        }
Exemplo n.º 11
0
        public SlackResponseDoc Start(SlackRequestDoc requestDoc, string topic)
        {
            string channelId = requestDoc.ChannelId;
            string userId    = requestDoc.UserId;

            try
            {
                _workflowService.OnGameStarted(channelId, userId, topic);
            }
            catch (GameNotStartedException)
            {
                return(SlackResponseDoc.Failure(String.Format(GAME_NOT_STARTED_FORMAT, requestDoc.Command)));
            }
            catch (WorkflowException e)
            {
                return(SlackResponseDoc.Failure(e.Message));
            }

            return(new SlackResponseDoc
            {
                ResponseType = SlackResponseType.IN_CHANNEL,
                Text = String.Format("OK, <@{0}>, please ask a question.", userId)
            });
        }
Exemplo n.º 12
0
        public SlackResponseDoc MarkAnswerCorrect(SlackRequestDoc requestDoc, string target, string answer)
        {
            string text;

            try
            {
                _workflowService.OnCorrectAnswerSelected(requestDoc.ChannelId, requestDoc.UserId);

                if (target.Equals(NO_CORRECT_ANSWER_TARGET, StringComparison.InvariantCultureIgnoreCase))
                {
                    //"Change" back to the original host to reset the workflow state
                    _workflowService.OnTurnChanged(requestDoc.ChannelId, requestDoc.UserId, requestDoc.UserId);

                    if (answer == null)
                    {
                        text = String.Format(
                            "It looks like no one was able to answer that one!\n\n{0}\n\nOK, <@{1}>, let's try another one!",
                            generateScoreText(requestDoc),
                            requestDoc.UserId
                            );
                    }
                    else
                    {
                        text = String.Format(
                            "It looks like no one was able to answer that one! The correct answer was {0}.\n\n{1}\n\nOK, <@{2}>, let's try another one!",
                            answer,
                            generateScoreText(requestDoc),
                            requestDoc.UserId
                            );
                    }
                }
                else
                {
                    string userId = SlackUtils.NormalizeId(target);

                    _scoreService.IncrementScore(requestDoc.ChannelId, userId);
                    _workflowService.OnTurnChanged(requestDoc.ChannelId, requestDoc.UserId, userId);

                    if (answer == null)
                    {
                        text = String.Format(
                            "<@{1}> is correct!\n\n{0}\n\nOK, <@{1}>, you're up!",
                            generateScoreText(requestDoc),
                            userId
                            );
                    }
                    else
                    {
                        text = String.Format(
                            "<@{2}> is correct with {0}!\n\n{1}\n\nOK, <@{2}>, you're up!",
                            answer,
                            generateScoreText(requestDoc),
                            userId
                            );
                    }
                }
            }
            catch (GameNotStartedException)
            {
                return(SlackResponseDoc.Failure(String.Format(GAME_NOT_STARTED_FORMAT, requestDoc.Command)));
            }
            catch (WorkflowException e)
            {
                return(SlackResponseDoc.Failure(e.Message));
            }
            catch (ScoreException)
            {
                SlackResponseDoc responseDoc = SlackResponseDoc.Failure("User " + target + " does not exist. Please choose a valid user.");
                responseDoc.Attachments = new List <SlackAttachment> {
                    new SlackAttachment("Usage: `" + requestDoc.Command + " correct @jsmith Blue skies`")
                };
                return(responseDoc);
            }

            SlackResponseDoc delayedResponseDoc = new SlackResponseDoc
            {
                ResponseType = SlackResponseType.IN_CHANNEL,
                Text         = text
            };

            _delayedSlackService.sendResponse(requestDoc.ResponseUrl, delayedResponseDoc);

            return(new SlackResponseDoc
            {
                ResponseType = SlackResponseType.EPHEMERAL,
                Text = "Score updated."
            });
        }
Exemplo n.º 13
0
 public void sendResponse(string url, SlackResponseDoc responseDoc)
 {
     Task.Run(() => client.PostAsJsonAsync(url, responseDoc));
 }