public void AuthorizeCallback_ExistingUser() { var db = new TestTweetHarborDbContext(); var ts = new Mock<ITweetHarborTwitterService>(); string token = Guid.NewGuid().ToString(); string verifier = Guid.NewGuid().ToString(); string TestUsername = "******"; string imageUrl = "http://test.com/image.jpg"; db.Users.Add(new User() { DateCreated = DateTime.Now.AddDays(-1), TwitterUserName = TestUsername, UserProfilePicUrl = "http://oldurl.com" }); ts.Setup<OAuthAccessToken>(a => a.GetAccessToken(It.IsAny<OAuthRequestToken>(), It.IsAny<string>())).Returns(new OAuthAccessToken() { Token = token, TokenSecret = verifier }); ts.Setup<TwitterUser>(a => a.VerifyCredentials()).Returns(new TwitterUser() { ScreenName = TestUsername, ProfileImageUrl = imageUrl }); var auth = new Mock<IFormsAuthenticationWrapper>(); var controller = new AccountController(db, ts.Object, auth.Object); controller.SetFakeControllerContext(); var val = controller.AuthorizeCallback(token, verifier); Assert.AreEqual(1, db.Users.Count()); Assert.AreEqual(token, db.Users.First().OAuthToken); Assert.AreEqual(verifier, db.Users.First().OAuthTokenSecret); Assert.AreEqual(TestUsername, db.Users.First().TwitterUserName); Assert.AreEqual(imageUrl, db.Users.First().UserProfilePicUrl); }
public void AuthorizeCallback_ExistingUser() { var db = new TestTweetHarborDbContext(); var ts = new Mock<ITweetHarborTwitterService>(); string token = Guid.NewGuid().ToString(); string verifier = Guid.NewGuid().ToString(); string TestUsername = "******"; string imageUrl = "http://test.com/image.jpg"; var user = UserHelper.ArrangeNewUserDefault(); db.Users.Add(user); ts.Setup<OAuthAccessToken>(a => a.GetAccessToken(It.IsAny<OAuthRequestToken>(), It.IsAny<string>())).Returns(new OAuthAccessToken() { Token = token, TokenSecret = verifier }); ts.Setup<TwitterUser>(a => a.VerifyCredentials()).Returns(new TwitterUser() { ScreenName = TestUsername, ProfileImageUrl = imageUrl }); var auth = new Mock<IFormsAuthenticationWrapper>(); var controller = new AccountController(db, ts.Object, auth.Object); controller.SetFakeControllerContext(); var val = controller.OAuthComplete(null, "Twitter", null); Assert.AreEqual(1, db.Users.Count()); Assert.AreEqual(token, db.Users.First().AuthenticationAccounts.FirstOrDefault(ac => ac.AccountProvider == "twitter").OAuthToken); Assert.AreEqual(verifier, db.Users.First().AuthenticationAccounts.FirstOrDefault(ac => ac.AccountProvider == "twitter").OAuthTokenSecret); Assert.AreEqual(TestUsername, db.Users.First().AuthenticationAccounts.FirstOrDefault(ac => ac.AccountProvider == "twitter").UserName); Assert.AreEqual(imageUrl, db.Users.First().UserProfilePicUrl); }
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 AuthorizeAppHarbor_TestAuthorizeReturnsCorretRedirectResult() { var db = new TestTweetHarborDbContext(); var ts = new Mock<ITweetHarborTwitterService>(); var auth = new Mock<IFormsAuthenticationWrapper>(); var c = new AccountController(db, ts.Object, auth.Object); c.SetFakeControllerContext(); var authResponse = c.Authorize("appharbor"); Assert.IsInstanceOfType(authResponse, typeof(RedirectResult)); RedirectResult redirRes = (authResponse as RedirectResult); var data = HttpUtility.ParseQueryString(redirRes.Url.ToString().Substring(redirRes.Url.ToString().IndexOf('?'))); Assert.AreNotEqual(0, data.Count, "No query string parameters found"); Assert.AreNotEqual(0, data["redirect_uri"].Length, "redirect_uri not found in url"); var data2 = HttpUtility.ParseQueryString(new Uri(data["redirect_uri"]).Query); Assert.IsTrue(data2.Keys[0].ToLower() == "client"); Assert.IsTrue(data2["client"].ToLower() == "appharbor"); }
public void GlobalNotificationToggle_SendPublicTweet_True_Correct() { 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); var ts = new TestTweetHarborTwitterService(); var auth = new Mock<IFormsAuthenticationWrapper>(); var controller = new AccountController(db, ts, auth.Object); var ident = new GenericIdentity("localtestuser"); System.Security.Principal.GenericPrincipal c = new System.Security.Principal.GenericPrincipal(ident, new string[] { }); controller.SetFakeControllerContext(c); var res = controller.GlobalNotificationToggle("SendPublicTweet", true); Assert.IsInstanceOfType(res.Data, typeof(JsonResultModel)); var rm = (JsonResultModel)res.Data; Assert.IsTrue(rm.Success); Assert.IsTrue(rm.Message == "Value has been updated"); Assert.AreEqual(true, db.Users.FirstOrDefault(u => u.UserName == ident.Name).SendPublicTweet); }
public void AuthorizeTwitter_TestAuthorizeReturnsCorretTwitterRedirectResult() { var db = new TestTweetHarborDbContext(); var ts = new Mock<ITweetHarborTwitterService>(); var token = new OAuthRequestToken() { Token = Guid.NewGuid().ToString(), TokenSecret = Guid.NewGuid().ToString() }; ts.Setup(m => m.GetRequestToken("http://localhost:9090/Account/OAuthComplete/?Client=twitter")).Returns(token); ts.Setup(m => m.GetAuthorizationUri(token)).Returns(new Uri("http://twitter.com/OAuth")); var auth = new Mock<IFormsAuthenticationWrapper>(); var c = new AccountController(db, ts.Object, auth.Object); c.SetFakeControllerContext(); var authResponse = c.Authorize("twitter"); Assert.IsInstanceOfType(authResponse, typeof(RedirectResult)); RedirectResult redirRes = (authResponse as RedirectResult); //var data = HttpUtility.ParseQueryString(redirRes.Url.ToString().Substring(redirRes.Url.ToString() .IndexOf('?'))); //Assert.AreNotEqual(0, data.Count, "No query string parameters found"); //Assert.AreNotEqual(0, data["redirect_uri"].Length, "redirect_uri not found in url"); //var data2 = HttpUtility.ParseQueryString(new Uri(data["redirect_uri"]).Query); //Assert.IsTrue(data2.Keys[0].ToLower() == "client"); //Assert.IsTrue(data2["client"].ToLower() == "appharbor"); }
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 AuthorizeCallback_NewUser() { var db = new TestTweetHarborDbContext(); var ts = new Mock<ITweetHarborTwitterService>(); string token = Guid.NewGuid().ToString(); string verifier = Guid.NewGuid().ToString(); string TestUsername = "******"; ts.Setup<OAuthAccessToken>(a => a.GetAccessToken(It.IsAny<OAuthRequestToken>(), It.IsAny<string>())).Returns(new OAuthAccessToken() { Token = token, TokenSecret = verifier }); ts.Setup<TwitterUser>(a => a.VerifyCredentials()).Returns(new TwitterUser() { ScreenName = TestUsername }); var auth = new Mock<IFormsAuthenticationWrapper>(); var controller = new AccountController(db, ts.Object, auth.Object); controller.SetFakeControllerContext(); var val = controller.AuthorizeCallback(token, verifier); Assert.AreNotEqual(0, db.Users.Count()); Assert.AreEqual(token, db.Users.First().OAuthToken); Assert.AreEqual(verifier, db.Users.First().OAuthTokenSecret); Assert.AreEqual(TestUsername, db.Users.First().TwitterUserName); }
public void GlobalNotificationToggle_SendPublicTweet_False_UserDoesNotExist() { var db = new TestTweetHarborDbContext(); var user = new User() { EmailAddress = "*****@*****.**", OAuthToken = "<FakeOauthToken>", OAuthTokenSecret = "<FakeOauthTokenSecret>", UniqueId = "db7a3a64156d0b33beae93fe99ca599e", SendPrivateTweet = true, SendPublicTweet = true, 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); var ts = new TestTweetHarborTwitterService(); var auth = new Mock<IFormsAuthenticationWrapper>(); var controller = new AccountController(db, ts, auth.Object); var ident = new GenericIdentity("localtestusermissing"); System.Security.Principal.GenericPrincipal c = new System.Security.Principal.GenericPrincipal(ident, new string[] { }); controller.SetFakeControllerContext(c); var res = controller.GlobalNotificationToggle("SendPublicTweet", true); Assert.IsInstanceOfType(res.Data, typeof(JsonResultModel)); var rm = (JsonResultModel)res.Data; Assert.IsFalse(rm.Success); Assert.IsTrue(rm.Error == "User Not Found"); }
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); }