Пример #1
0
 public ActionResult Edit(string Id, User user)
 {
     database.Users.Attach(user);
     (database as System.Data.Entity.DbContext).Entry(user).State = System.Data.EntityState.Modified;
     database.SaveChanges();
     return View(user);
 }
        private User AppHarborCreateOrUpdateAccountIfNeeded(string AccessToken, AppHarbor.Client.User user, User returnUser)
        {
            //TODO: must have some kind of AppHb unique id-- username, etc --see twitter approach (screenname) (for now we used emailaddress)
            if (null == returnUser)
            {
                returnUser = (from u in database.Users
                              where u.AuthenticationAccounts.FirstOrDefault(ac => ac.AccountProvider == "appharbor" && ac.UserName == user.UserName) != null
                              select u).FirstOrDefault();
            }
            if (null == returnUser) // CREATE
            {
                var existingUser = (from u in database.Users where u.UserName.ToLower() == user.UserName.ToLower() select u).FirstOrDefault();
                returnUser = new User();
                returnUser.UserName = null == existingUser ? user.UserName : "";
                returnUser.EmailAddress = user.EmailAddress;
                returnUser.UpdateUniqueId();
                database.Users.Add(returnUser);
            }

            var newAppHarborAccount = returnUser.AuthenticationAccounts.FirstOrDefault(ac => ac.AccountProvider == "appharbor" &&
                ac.UserName == user.UserName);

            if (newAppHarborAccount == null)
            {
                newAppHarborAccount = new UserAuthenticationAccount();
                newAppHarborAccount.AccountProvider = "appharbor";
                newAppHarborAccount.UserName = user.UserName;
                newAppHarborAccount.UserName = user.UserName;
                newAppHarborAccount.ProfilePicUrl = "<not implemented>";
                if (null == returnUser.AuthenticationAccounts)
                    returnUser.AuthenticationAccounts = new Collection<UserAuthenticationAccount>();
                returnUser.AuthenticationAccounts.Add(newAppHarborAccount);
            }

            //returnUser.UserProfilePicUrl = user.ProfileImageUrl;
            var appharborAccount = returnUser.AuthenticationAccounts.First(t => t.AccountProvider == "appharbor");
            appharborAccount.OAuthToken = AccessToken;
            //appharborAccount.OAuthTokenSecret = accessToken.TokenSecret;
            //appharborAccount.ProfilePicUrl = user.ProfileImageUrl;

            try
            {
                database.SaveChanges();
            }
            catch (Exception e)
            {
                Trace.WriteLine("Exception: " + e.Message);
                throw e;
            }
            return returnUser;
        }
Пример #3
0
        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);
        }
Пример #4
0
        public ActionResult AccountSetup(string Id, User user, string ReturnUrl)
        {
            var dbUser = database.Users.FirstOrDefault(u => u.UniqueId == Id);
            if (null != dbUser && null != user)
            {
                user.UserName = user.UserName != null ? user.UserName.Trim() : null;
                user.EmailAddress = user.EmailAddress != null ? user.EmailAddress.Trim() : null;
                if (string.IsNullOrEmpty(user.UserName))
                    ModelState.AddModelError("UserName", "UserName must not be empty");
                if (string.IsNullOrEmpty(user.EmailAddress))
                    ModelState.AddModelError("EmailAddress", "Please enter an email address");

                var usernameTaken = database.Users.FirstOrDefault(u => u.UserName.ToLower() == user.UserName.ToLower() && u.UserId != dbUser.UserId) != null;
                if (usernameTaken)
                {
                    ModelState.AddModelError("UserName", "That username is already in use");
                }
                if (ModelState.IsValid)
                {
                    dbUser.UserName = user.UserName;
                    dbUser.EmailAddress = user.EmailAddress;
                    database.SaveChanges();
                    //TODO: Add a status update
                    FormsAuthentication.SetAuthCookie(user.UserName, true);
                    if (!string.IsNullOrEmpty(ReturnUrl))
                    {
                        return Redirect(ReturnUrl);
                    }
                    return RedirectToAction("Index");
                }
                else
                {
                    return View(user);
                }
            }
            else
            {
                //TODO: better to redirect to failed login page?
                return RedirectToAction("Index");
            }
        }
Пример #5
0
        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);
        }
Пример #6
0
        public void GlobalNotificationToggle_SendPublicTweet_True_Correct()
        {
            var db = new TestTweetHarborDbContext();
            var user = new User()
            {
                EmailAddress = "*****@*****.**",
                OAuthToken = "<FakeOauthToken>",
                OAuthTokenSecret = "<FakeOauthTokenSecret>",
                UniqueId = "db7a3a64156d0b33beae93fe99ca599e",
                SendPrivateTweet = false,
                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);
            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.TwitterUserName == ident.Name).SendPublicTweet);
        }
Пример #7
0
        private void MergeUsers(User destinationUser, User fromUser, TwitterUser user)
        {
            var authAccount = fromUser.AuthenticationAccounts.FirstOrDefault(t => t.AccountProvider == "twitter" && t.UserName == user.ScreenName);
            destinationUser.AuthenticationAccounts.Add(authAccount);
            fromUser.AuthenticationAccounts.Remove(authAccount);

            foreach (var p in fromUser.Projects)
            {
                destinationUser.Projects.Add(p);

            }
            fromUser.Projects.Clear();
        }
Пример #8
0
        private User TwitterCreateOrUpdateAccountIfNeeded(OAuthAccessToken accessToken, TwitterUser user, User returnUser)
        {
            // If not passed a user, let's query to find out if
            // we already have a master user for this twitter user
            if (null == returnUser)
            {
                returnUser = (from u in database.Users
                              where u.AuthenticationAccounts.FirstOrDefault(ac => ac.AccountProvider == "twitter" && ac.UserName == user.ScreenName) != null
                              select u).FirstOrDefault();
            }
            else
            {
                var otherUser = (from u in database.Users
                                 where u.AuthenticationAccounts.FirstOrDefault(ac => ac.AccountProvider == "twitter" && ac.UserName == user.ScreenName) != null
                                 && u.UserId != returnUser.UserId
                                 select u).FirstOrDefault();

                if (null != otherUser)
                {
                    // This twitter account is owned by another user
                    // we need to merge the data
                    MergeUsers(returnUser, otherUser, user);
                }
            }

            // If we're still short a user account, we will create one here
            if (null == returnUser) // CREATE
            {
                returnUser = new User();
                returnUser.UserName = user.ScreenName;
                returnUser.EmailAddress = "";
                returnUser.UpdateUniqueId();
                database.Users.Add(returnUser);
            }

            // Now we will pull our actual twitter account, it if exists
            // If it doesn't, we will create it
            UserAuthenticationAccount twitterAccount = returnUser.
                AuthenticationAccounts.FirstOrDefault(a => a.AccountProvider == "twitter" && a.UserName == user.ScreenName);
            if (twitterAccount == null)
            {
                twitterAccount = new UserAuthenticationAccount();
                twitterAccount.AccountProvider = "twitter";

                twitterAccount.UserName = user.ScreenName;
                twitterAccount.ProfilePicUrl = user.ProfileImageUrl;
                if (null == returnUser.AuthenticationAccounts)
                    returnUser.AuthenticationAccounts = new Collection<UserAuthenticationAccount>();
                returnUser.AuthenticationAccounts.Add(twitterAccount);

            }

            // We'll update some information here
            if (string.IsNullOrEmpty(returnUser.UserProfilePicUrl))
                returnUser.UserProfilePicUrl = user.ProfileImageUrl;
            twitterAccount.OAuthToken = accessToken.Token;
            twitterAccount.OAuthTokenSecret = accessToken.TokenSecret;
            twitterAccount.ProfilePicUrl = user.ProfileImageUrl;

            try
            {
                database.SaveChanges();
            }
            catch (Exception e)
            {
                Trace.WriteLine("Exception: " + e.Message);
                throw e;
            }
            return returnUser;
        }
Пример #9
0
        private User CreateOrUpdateAccountIfNeeded(OAuthAccessToken accessToken, TwitterUser user)
        {
            var returnUser = database.Users.FirstOrDefault(usr => usr.TwitterUserName == user.ScreenName);
            if (null == returnUser) // CREATE
            {
                returnUser = new User();
                returnUser.TwitterUserName = user.ScreenName;
                returnUser.UniqueId = returnUser.TwitterUserName.MD5Hash(accessToken.Token);
                database.Users.Add(returnUser);
            }

            returnUser.UserProfilePicUrl = user.ProfileImageUrl;
            returnUser.OAuthToken = accessToken.Token;
            returnUser.OAuthTokenSecret = accessToken.TokenSecret;
            try
            {
                database.SaveChanges();
            }
            catch (Exception e)
            {

                throw e;
            }
            return returnUser;
        }