示例#1
0
        public void LoginAndAssociateSuccess()
        {
            const string email    = "*****@*****.**";
            const string password = "******";
            var          user     = new User(12, DateTime.MaxValue)
            {
                Email = email
            };
            const bool persist       = true;
            var        controller    = GetController();
            var        contextHelper = new HttpContextHelper();

            contextHelper.MockRequest.Setup(x => x.UserHostAddress).Returns(String.Empty);
            controller.ControllerContext = new ControllerContext(contextHelper.MockContext.Object, new RouteData(), controller);
            _userService.Setup(u => u.Login(email, password, persist, contextHelper.MockContext.Object)).Returns(true);
            _userService.Setup(x => x.GetUserByEmail(email)).Returns(user);
            var authManager = new Mock <IAuthenticationManager>();

            _owinContext.Setup(x => x.Authentication).Returns(authManager.Object);
            var externalAuthResult = new ExternalAuthenticationResult();
            var authResult         = Task.FromResult(externalAuthResult);

            _externalAuth.Setup(x => x.GetAuthenticationResult(authManager.Object)).Returns(authResult);

            var result = controller.LoginAndAssociate(email, password, persist).Result;

            _userAssociation.Verify(x => x.Associate(user, authResult.Result, It.IsAny <string>()));
            _userService.Verify(u => u.Login(email, password, persist, contextHelper.MockContext.Object), Times.Once());
            Assert.IsInstanceOf <JsonResult>(result);
            var resultObject = (BasicJsonMessage)result.Data;

            Assert.IsTrue(resultObject.Result);
        }
        public async Task <IActionResult> ExternalLoginCallback(string returnUrl = null)
        {
            var ip   = HttpContext.Connection.RemoteIpAddress.ToString();
            var info = await GetExternalLoginInfoAsync(HttpContext);

            if (info == null)
            {
                return(RedirectToAction("Login", "Account", new { error = Resources.ExpiredLogin }));
            }
            var email = info.ExternalPrincipal.HasClaim(x => x.Type == ClaimTypes.Email) ? info.ExternalPrincipal.FindFirst(ClaimTypes.Email).Value : null;
            var name  = info.ExternalPrincipal.HasClaim(x => x.Type == ClaimTypes.Name) ? info.ExternalPrincipal.FindFirst(ClaimTypes.Name).Value : null;
            var externalAuthResult = new ExternalAuthenticationResult
            {
                Issuer      = info.LoginProvider,
                Email       = email,
                Name        = name,
                ProviderKey = info.ProviderKey
            };
            var matchResult = _externalUserAssociationManager.ExternalUserAssociationCheck(externalAuthResult, ip);

            if (matchResult.Successful)
            {
                _userService.Login(matchResult.User, true, ip);
                await PerformSignInAsync(true, matchResult.User, HttpContext);

                return(Redirect(returnUrl));
            }
            ViewBag.Referrer = returnUrl;
            return(View());
        }
        public void CreateValidCallExternalAuthAssociateWithAuthResult()
        {
            var controller = GetController();

            MockUpUrl(controller);
            _userService.Setup(u => u.IsEmailInUse(It.IsAny <string>())).Returns(false);
            _userService.Setup(u => u.IsNameInUse(It.IsAny <string>())).Returns(false);
            var user   = UserServiceTests.GetDummyUser("Diana", "*****@*****.**");
            var signUp = new SignupData {
                Email = "*****@*****.**", IsCoppa = true, IsDaylightSaving = true, IsSubscribed = true, IsTos = true, Name = "Diana", Password = "******", PasswordRetype = "passwerd", TimeZone = -5
            };

            _userService.Setup(u => u.CreateUser(signUp, It.IsAny <string>())).Returns(user);
            _newAccountMailer.Setup(n => n.Send(It.IsAny <User>(), It.IsAny <string>())).Returns(System.Net.Mail.SmtpStatusCode.CommandNotImplemented);
            var settings = new Settings {
                IsNewUserApproved = true
            };

            _settingsManager.Setup(s => s.Current).Returns(settings);
            var authManager = new Mock <IAuthenticationManager>();

            _owinContext.Setup(x => x.Authentication).Returns(authManager.Object);
            var externalAuthResult = new ExternalAuthenticationResult();
            var authResult         = Task.FromResult(externalAuthResult);

            _externalAuth.Setup(x => x.GetAuthenticationResult(authManager.Object)).Returns(authResult);

            var result = controller.Create(signUp).Result;

            _userAssociationManager.Verify(x => x.Associate(user, externalAuthResult, It.IsAny <string>()), Times.Once());
        }
示例#4
0
        public void AssociateMapsObjectsToRepoCall()
        {
            var manager            = GetManager();
            var user               = new User(123, DateTime.MinValue);
            var externalAuthResult = new ExternalAuthenticationResult {
                Issuer = "weihf", ProviderKey = "weoihf", Name = "woehf"
            };

            manager.Associate(user, externalAuthResult, String.Empty);

            _externalUserAssociationRepo.Verify(x => x.Save(user.UserID, externalAuthResult.Issuer, externalAuthResult.ProviderKey, externalAuthResult.Name), Times.Once());
        }
示例#5
0
        public void AssociateSuccessCallsSecurityLog()
        {
            var manager            = GetManager();
            var user               = new User(123, DateTime.MinValue);
            var externalAuthResult = new ExternalAuthenticationResult {
                Issuer = "weihf", ProviderKey = "weoihf", Name = "woehf"
            };
            const string ip = "1.1.1.1";

            manager.Associate(user, externalAuthResult, ip);

            _securityLogService.Verify(x => x.CreateLogEntry(user, user, ip, It.IsAny <string>(), SecurityLogType.ExternalAssociationSet), Times.Once());
        }
        public void ExternalUserAssociationCheckResultFalseNoMatchCallsSecurityLog()
        {
            var manager = GetManager();

            _externalUserAssociationRepo.Setup(x => x.Get(It.IsAny <string>(), It.IsAny <string>())).Returns((ExternalUserAssociation)null);
            const string ip         = "1.1.1.1";
            var          authResult = new ExternalAuthenticationResult {
                Issuer = "Google", ProviderKey = "abc"
            };

            manager.ExternalUserAssociationCheck(authResult, ip);

            _securityLogService.Verify(x => x.CreateLogEntry((int?)null, null, ip, It.IsAny <string>(), SecurityLogType.ExternalAssociationCheckFailed), Times.Once());
        }
        public void ExternalUserAssociationCheckResultTrueCallsSecurityLog()
        {
            var manager     = GetManager();
            var association = new ExternalUserAssociation {
                Issuer = "Google", UserID = 123, ProviderKey = "abc"
            };
            var user = new User(association.UserID, DateTime.MinValue);

            _externalUserAssociationRepo.Setup(x => x.Get(association.Issuer, association.ProviderKey)).Returns(association);
            _userRepo.Setup(x => x.GetUser(association.UserID)).Returns(user);
            const string ip         = "1.1.1.1";
            var          authResult = new ExternalAuthenticationResult {
                Issuer = "Google", ProviderKey = "abc"
            };

            manager.ExternalUserAssociationCheck(authResult, ip);

            _securityLogService.Verify(x => x.CreateLogEntry(user, user, ip, It.IsAny <string>(), SecurityLogType.ExternalAssociationCheckSuccessful));
        }
        public void ExternalUserAssociationCheckResultTrueWithHydratedResultIfMatchingAssociationAndUser()
        {
            var manager     = GetManager();
            var association = new ExternalUserAssociation {
                Issuer = "Google", UserID = 123, ProviderKey = "abc"
            };
            var user = new User(association.UserID, DateTime.MinValue);

            _externalUserAssociationRepo.Setup(x => x.Get(association.Issuer, association.ProviderKey)).Returns(association);
            _userRepo.Setup(x => x.GetUser(association.UserID)).Returns(user);
            var authResult = new ExternalAuthenticationResult {
                Issuer = "Google", ProviderKey = "abc"
            };

            var result = manager.ExternalUserAssociationCheck(authResult, "");

            Assert.True(result.Successful);
            Assert.Same(user, result.User);
            Assert.Same(association, result.ExternalUserAssociation);
        }