public void It_Should_Generate_An_Email_Message()
            {
                var scriptRunResult = new ScriptRunResultModel
                {
                    UserName        = "******",
                    ScriptKey       = "test-script-1",
                    ScriptStartedAt = new DateTime(2015, 12, 11, 12, 0, 0),
                    ScriptEndedAt   = new DateTime(2015, 12, 11, 12, 0, 3),
                    ScriptOutput    = new List <ScriptOutputModel>
                    {
                        new ScriptOutputStdModel
                        {
                            Timestamp = new DateTime(2015, 12, 11, 12, 0, 1),
                            Value     = "output 1"
                        },
                        new ScriptOutputStdModel
                        {
                            Timestamp = new DateTime(2015, 12, 11, 12, 0, 2),
                            Value     = "output 2"
                        }
                    }
                };
                var emailMessage = _scriptRunEmailBuilder.Build(scriptRunResult);

                Assert.AreEqual("*****@*****.**", emailMessage.From.Address);
                Assert.AreEqual("subject - user-1 - test-script-1", emailMessage.Subject);
                Assert.AreEqual(JsonConvert.SerializeObject(scriptRunResult,
                                                            Formatting.Indented), emailMessage.Body);
                Assert.AreEqual(3, emailMessage.To.Count);
                Assert.AreEqual("*****@*****.**", emailMessage.To[0].Address);
                Assert.AreEqual("*****@*****.**", emailMessage.To[1].Address);
                Assert.AreEqual("*****@*****.**", emailMessage.To[2].Address);
            }
Exemple #2
0
        public object Run(string key)
        {
            try
            {
                var user       = User.Identity as WindowsIdentity;
                var userScript = GetUserScript(user, key);

                if (userScript != null)
                {
                    var scriptRunResult = _scriptRunnerService.RunScript(user.Name, userScript);
                    _emailService.SendEmail(_scriptRunEmailBuilder.Build(scriptRunResult));

                    return(new ScriptRunResultViewModel
                    {
                        UserName = scriptRunResult.UserName,
                        ScriptKey = scriptRunResult.ScriptKey,
                        ScriptStartedAt = scriptRunResult.ScriptStartedAt,
                        ScriptEndedAt = scriptRunResult.ScriptEndedAt,
                        ScriptTermination = scriptRunResult.ScriptTermination == ScriptTermination.ExecutionFinished
                            ? "Execution finished"
                            : "Timeout expired",
                        ScriptOutput = scriptRunResult.ScriptOutput.Select(output => string.Format("{0} - {1}: {2}",
                                                                                                   output.Timestamp, output.Type, output.Value))
                    });
                }
                return(new ErrorModel
                {
                    Message = "The script was not found or the user is not authorized",
                });
            }
            catch (DelayNotExpiredException e)
            {
                return(new ErrorModel
                {
                    Message = string.Format(
                        "The script has been run recently. Please wait {0} seconds",
                        e.RemainingDelay),
                });
            }
            catch (Exception e)
            {
                return(new ErrorModel
                {
                    Message = e.Message,
                    Exception = e
                });
            }
        }