Пример #1
0
        public void RetrieveLoginSession_ShouldReturnSessionWhenSecretAndClientIpMatch()
        {
            //Arrange
            var clientIp = "100.1.123.0";
            var context  = new ControllerContextBuilder().WithClientIp(clientIp).Build();

            _controller.ControllerContext = context;

            var existingSession = new LoginSession
            {
                PublicIdentifier = Guid.NewGuid().ToString(),
                IpAddress        = clientIp,
                SessionToken     = Guid.NewGuid().ToString()
            };

            _loginSessionServiceMock.Setup(service => service.GetSessionAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(existingSession);

            //Act
            var result = _controller.RetrieveLoginSession(existingSession.PublicIdentifier, existingSession.SessionToken).Result as ObjectResult;

            //Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(result.Value, Is.SameAs(existingSession));
            _loginSessionServiceMock.Verify(service => service.GetSessionAsync(existingSession.PublicIdentifier, clientIp, existingSession.SessionToken), Times.Once);
        }
Пример #2
0
        public void Setup()
        {
            var userStoreMock = new Mock <IUserStore <User> >();

            _passwordHasherMock = new Mock <IPasswordHasher <User> >();
            var lookupNormalizerMock = new Mock <ILookupNormalizer>();
            var errorsMock           = new Mock <IdentityErrorDescriber>();
            var loggerMock           = new Mock <ILogger <UserManager <User> > >();

            _userManagerMock = new Mock <UserManager <User> >(
                userStoreMock.Object,
                null,
                _passwordHasherMock.Object,
                null,
                null,
                lookupNormalizerMock.Object,
                errorsMock.Object,
                null,
                loggerMock.Object);

            _userManagerMock.Setup(manager => manager.CreateAsync(It.IsAny <User>(), It.IsAny <string>()))
            .ReturnsAsync(new IdentityResult());

            var expectedCaptchaResult = new CaptchaVerificationResult {
                Success = true
            };

            _captchaValidatorMock = new Mock <ICaptchaValidator>();
            _captchaValidatorMock.Setup(validator => validator.Validate(It.IsAny <string>(), It.IsAny <IPAddress>()))
            .ReturnsAsync(expectedCaptchaResult);

            _mailSenderMock = new Mock <IMailSender>();

            _accessPassFactoryMock = new Mock <ITokenAccessPassFactory>();

            _loginSessionServiceMock = new Mock <ILoginSessionService>();

            _controller = new AuthController(_userManagerMock.Object,
                                             _passwordHasherMock.Object,
                                             _captchaValidatorMock.Object,
                                             _mailSenderMock.Object,
                                             _accessPassFactoryMock.Object,
                                             _loginSessionServiceMock.Object);

            var context = new ControllerContextBuilder().WithClientIp().Build();

            _controller.ControllerContext = context;
        }
Пример #3
0
        public void CancelLoginSession_ShouldCancelSessionIfFound()
        {
            //Arrange
            var clientIp = "200.1.123.0";
            var context  = new ControllerContextBuilder().WithClientIp(clientIp).Build();

            _controller.ControllerContext = context;

            var sessionPublicIdentifier = Guid.NewGuid().ToString();

            //Act
            var result = _controller.CancelLoginSession(sessionPublicIdentifier).Result as OkResult;

            //Assert
            Assert.That(result, Is.Not.Null);
            _loginSessionServiceMock.Verify(service => service.CancelSessionAsync(sessionPublicIdentifier, clientIp), Times.Once);
        }
Пример #4
0
        public void CancelLoginSession_ShouldReturnBadRequestWhenSessionCannotBeFound()
        {
            //Arrange
            var clientIp = "100.1.123.0";
            var context  = new ControllerContextBuilder().WithClientIp(clientIp).Build();

            _controller.ControllerContext = context;

            var somePublicIdentifier = Guid.NewGuid().ToString();

            _loginSessionServiceMock.Setup(service => service.CancelSessionAsync(It.IsAny <string>(), It.IsAny <string>())).Throws <DataNotFoundException>();

            //Act
            var result = _controller.CancelLoginSession(somePublicIdentifier).Result as BadRequestResult;

            //Assert
            Assert.That(result, Is.Not.Null);
            _loginSessionServiceMock.Verify(service => service.CancelSessionAsync(somePublicIdentifier, clientIp), Times.Once);
        }
Пример #5
0
        public void CreateLoginSession_ShouldCreateASessionUsingRemoteIpOfClient()
        {
            //Arrange
            var createdSession = new LoginSession();

            _loginSessionServiceMock.Setup(service => service.CreateSessionAsync(It.IsAny <string>())).ReturnsAsync(createdSession);

            var clientIp = "123.1.123.0";
            var context  = new ControllerContextBuilder().WithClientIp(clientIp).Build();

            _controller.ControllerContext = context;

            //Act
            var result = _controller.CreateLoginSession().Result as OkObjectResult;

            //Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(result.Value, Is.SameAs(createdSession));
            _loginSessionServiceMock.Verify(service => service.CreateSessionAsync(clientIp), Times.Once);
        }