Beispiel #1
0
        public ActionResult TestFailedBuild(int Id)
        {
            var user    = database.Users.First(u => u.UserName == User.Identity.Name);
            var project = database.Projects.FirstOrDefault(p => p.ProjectId == Id && user.UserId == p.User.UserId);

            Notification n = new Notification()
            {
                application = new Application()
                {
                    name = project.ProjectName
                },
                build = new Build()
                {
                    commit = new Commit()
                    {
                        id      = Guid.NewGuid().ToString(),
                        message = "A successful build test from TweetHarbor"
                    },
                    status = "failed"
                }
            };

            NotifyController c = new NotifyController(database, twitterService, textMessageService);
            var res            = c.New(user.UserName, user.UniqueId, n);

            //TODO: Make a real post work
            //var notificationValue = JsonConvert.SerializeObject(n);
            //string url = Request.Url.Scheme + "://"
            //    + Request.Url.Host
            //    + (Request.Url.Host.ToLower() == "localhost" ? ":" + Request.Url.Port : "")
            //    + "/notify/new/" + user.UserName + "?token=" + user.UniqueId;

            //WebRequest wr = WebRequest.Create(url);
            //string parms = "notification=" + notificationValue;
            //wr.Method = "POST";
            //byte[] data = System.Text.Encoding.UTF8.GetBytes(parms);
            //wr.ContentLength = data.Length;
            //wr.ContentType = "application/json";

            //var sw = wr.GetRequestStream();
            //sw.Write(data, 0, data.Length);
            //sw.Close();

            //var response = wr.GetResponse();
            //StreamReader sr = new StreamReader(response.GetResponseStream());
            //var val = sr.ReadToEnd();
            //sr.Close();

            return(RedirectToAction("Index", new { Controller = "Account" }));
        }
        public ActionResult TestSuccessfulBuild(int Id)
        {
            var user = database.Users.First(u => u.UserName == User.Identity.Name);
            var project = database.Projects.FirstOrDefault(p => p.ProjectId == Id && user.UserId == p.User.UserId);

            Notification n = new Notification()
            {
                application = new Application()
                {
                    name = project.ProjectName

                },
                build = new Build()
                {
                    commit = new Commit()
                    {
                        id = Guid.NewGuid().ToString(),
                        message = "A successful build test from TweetHarbor"
                    },
                    status = "succeeded"
                }
            };

            NotifyController c = new NotifyController(database, twitterService, textMessageService);
            var res = c.New(user.UserName, user.UniqueId, n);

            //TODO: Make a real post work
            //var notificationValue = JsonConvert.SerializeObject(n);
            //string url = Request.Url.Scheme + "://"
            //    + Request.Url.Host
            //    + (Request.Url.Host.ToLower() == "localhost" ? ":" + Request.Url.Port : "")
            //    + "/notify/new/" + user.UserName + "?token=" + user.UniqueId;

            //WebRequest wr = WebRequest.Create(url);
            //string parms = "notification=" + notificationValue;
            //wr.Method = "POST";
            //byte[] data = System.Text.Encoding.UTF8.GetBytes(parms);
            //wr.ContentLength = data.Length;
            //wr.ContentType = "application/json";

            //var sw = wr.GetRequestStream();
            //sw.Write(data, 0, data.Length);
            //sw.Close();

            //var response = wr.GetResponse();
            //StreamReader sr = new StreamReader(response.GetResponseStream());
            //var val = sr.ReadToEnd();
            //sr.Close();

            return RedirectToAction("Index", new { Controller = "Account" });
        }
        public void TestBuildFailure()
        {
            string testStr = "{\"application\": { \"name\": \"Test Project 1\" },   \"build\": {    \"commit\": {      \"id\": \"" + Guid.NewGuid() + "\", \"message\": \"Implement foo\"  }, \"status\": \"succeeded\" } }";
            var o = JsonConvert.DeserializeObject<Notification>(testStr);

            var db = new TestTweetHarborDbContext();
            var user = new User()
            {
                EmailAddress = "*****@*****.**",
                OAuthToken = "<FakeOauthToken>",
                OAuthTokenSecret = "<FakeOauthTokenSecret>",
                UniqueId = "db7a3a64156d0b33beae93fe99ca599e",
                SendPrivateTweet = true,
                SendPublicTweet = false,
                TwitterUserName = "******"
            };
            db.Users.Add(user);

            var proj = new Project()
            {
                ProjectName = "The Test Project",
                SendPrivateTweetOnFailure = true,
                SendPrivateTweetOnSuccess = true,
                SendPublicTweetOnFailure = false,
                SendPublicTweetOnSuccess = true,
                User = user
            };

            db.Projects.Add(proj);

            user.Projects = new Collection<Project>();
            user.Projects.Add(proj);

            var m = new Mock<ITweetHarborTextMessageService>();

            m.Setup(a => a.SendText("", ""));

            var controller = new NotifyController(db, new TestTweetHarborTwitterService(), m.Object);

            MvcMockHelpers.SetFakeControllerContext(controller);

            var res = controller.New(user.TwitterUserName, user.UniqueId, o);

            Assert.IsInstanceOfType(res, typeof(JsonResult));
            Assert.IsInstanceOfType((res as JsonResult).Data, typeof(JsonResultModel));

            Assert.AreEqual(true, ((res as JsonResult).Data as JsonResultModel).Success);
        }
        public void TestSendSmsMessages_OutboundNotificationCreated()
        {
            string testStr = "{\"application\": { \"name\": \"Test Project 1\" },   \"build\": {    \"commit\": {      \"id\": \"" + Guid.NewGuid() + "\", \"message\": \"Implement foo\"  }, \"status\": \"succeeded\" } }";
            var o = JsonConvert.DeserializeObject<Notification>(testStr);

            TestTweetHarborDbContext db = new TestTweetHarborDbContext();

            var m = new Mock<ITweetHarborTextMessageService>();
            m.Setup(a => a.SendText("", ""));

            var user = new User()
            {
                EmailAddress = "*****@*****.**",
                OAuthToken = "<FakeOauthToken>",
                OAuthTokenSecret = "<FakeOauthTokenSecret>",
                UniqueId = "db7a3a64156d0b33beae93fe99ca599e",
                SendPrivateTweet = true,
                SendPublicTweet = false,
                TwitterUserName = "******",
                SendSMS = true
            };
            db.Users.Add(user);

            var proj = new Project()
                {
                    ProjectName = o.application.name,
                    SendPrivateTweetOnFailure = true,
                    SendPrivateTweetOnSuccess = true,
                    SendPublicTweetOnFailure = false,
                    SendPublicTweetOnSuccess = true,
                    SendTextOnSuccess = true,
                    SendTextOnFailure = false,
                    User = user
                };
            user.Projects.Add(proj);
            db.Projects.Add(proj);
            var tmr = new TextMessageRecipient()
            {
                Name = "App Test",
                PhoneNumber = "5201235678",
            };
            proj.TextMessageRecipients.Add(tmr);

            var dmr = new TwitterMessageRecipient()
            {
                ScreenName = "testuser",
            };

            proj.MessageRecipients.Add(dmr);

            var controller = new NotifyController(db, new TestTweetHarborTwitterService(), m.Object);

            var res = controller.New(user.TwitterUserName, user.UniqueId, o);

            Assert.IsInstanceOfType(res, typeof(JsonResult));
            Assert.IsInstanceOfType((res as JsonResult).Data, typeof(JsonResultModel));

            Assert.AreEqual(true, ((res as JsonResult).Data as JsonResultModel).Success);

            Assert.AreNotEqual(0, proj.OutboundNotifications.Count);
            Assert.AreNotEqual(0, proj.OutboundNotifications.First().Message.Length);
            Assert.AreEqual(2, proj.OutboundNotifications.Count);

            var nots = proj.OutboundNotifications.OrderBy(pj => pj.NotificationType);

            Assert.AreEqual("SMS", nots.First().NotificationType);
            Assert.AreEqual("5201235678", nots.First().Recipient);

            Assert.AreEqual("Twitter", nots.ElementAt(1).NotificationType);
            Assert.AreEqual(dmr.ScreenName, nots.ElementAt(1).Recipient);
        }
        public void TestSendSmsMessages_InboundNotificationCreated()
        {
            string testStr = "{\"application\": { \"name\": \"Test Project 1\" },   \"build\": {    \"commit\": {      \"id\": \"" + Guid.NewGuid() + "\", \"message\": \"Implement foo\"  }, \"status\": \"succeeded\" } }";
            var o = JsonConvert.DeserializeObject<Notification>(testStr);

            TestTweetHarborDbContext db = new TestTweetHarborDbContext();

            var m = new Mock<ITweetHarborTextMessageService>();
            m.Setup(a => a.SendText("", ""));

            var user = UserHelper.ArrangeNewUserDefault();
            db.Users.Add(user);

            var proj = new Project()
            {
                ProjectName = o.application.name,
                SendPrivateTweetOnFailure = true,
                SendPrivateTweetOnSuccess = true,
                SendPublicTweetOnFailure = false,
                SendPublicTweetOnSuccess = true,
                SendTextOnSuccess = true,
                SendTextOnFailure = false,
                User = user
            };
            user.Projects.Add(proj);
            db.Projects.Add(proj);
            var tmr = new TextMessageRecipient()
            {
                Name = "App Test",
                PhoneNumber = "5201235678",
            };
            proj.TextMessageRecipients.Add(tmr);

            var controller = new NotifyController(db, new TestTweetHarborTwitterService(), m.Object);

            var res = controller.New(user.UserName, user.UniqueId, o);

            Assert.IsInstanceOfType(res, typeof(JsonResult));
            Assert.IsInstanceOfType((res as JsonResult).Data, typeof(JsonResultModel));

            Assert.AreEqual(true, ((res as JsonResult).Data as JsonResultModel).Success);

            Assert.AreNotEqual(0, proj.ProjectNotifications.Count);
            Assert.AreNotEqual(0, proj.ProjectNotifications.First().Build.commit.message.Length);
        }
        public void TestBuildSuccess_MutedMessage()
        {
            string testStr = "{\"application\": { \"name\": \"Test Project 1\" },   \"build\": {    \"commit\": {      \"id\": \"" + Guid.NewGuid() + "\", \"message\": \"Implement foo-\"  }, \"status\": \"succeeded\" } }";
            var deserializedJsonObject = JsonConvert.DeserializeObject<Notification>(testStr);

            var db = new TestTweetHarborDbContext();
            var user = UserHelper.ArrangeNewUserDefault();
            db.Users.Add(user);

            var proj = new Project()
            {
                ProjectName = "The Test Project",
                SendPrivateTweetOnFailure = true,
                SendPrivateTweetOnSuccess = true,
                SendPublicTweetOnFailure = false,
                SendPublicTweetOnSuccess = true,
                User = user
            };

            db.Projects.Add(proj);

            proj.MessageRecipients.Add(new TwitterMessageRecipient() { ScreenName = "sethwebster" });

            user.Projects = new Collection<Project>();
            user.Projects.Add(proj);

            var m = new Mock<ITweetHarborTextMessageService>();

            m.Setup(a => a.SendText("", ""));

            var controller = new NotifyController(db, new TestTweetHarborTwitterService(), m.Object);
            MvcMockHelpers.SetFakeControllerContext(controller);

            var res = controller.New(user.UserName, user.UniqueId, deserializedJsonObject);

            proj = user.Projects.FirstOrDefault(p => p.ProjectName == deserializedJsonObject.application.name);

            Assert.IsInstanceOfType(res, typeof(JsonResult));
            Assert.IsInstanceOfType((res as JsonResult).Data, typeof(JsonResultModel));

            Assert.AreNotEqual(0, proj.ProjectNotifications.Count());
            Assert.AreEqual(0, proj.OutboundNotifications.Count());

            Assert.AreEqual(true, ((res as JsonResult).Data as JsonResultModel).Success);
        }