コード例 #1
0
        public void AuthenticateShouldFailWithBadCredentials()
        {
            var nancyModule     = new Mock <INancyModule>();
            var userMapper      = new FormsUserMapper();
            var configuration   = new Mock <IConfiguration>();
            var userRepository  = new Mock <IUserRepository>();
            var userCredentials = new UserCredentials {
                User = "******", Password = "******"
            };
            var viewRenderer         = new Mock <IViewRenderer>();
            var moduleStaticWrappers = new Mock <IModuleStaticWrappers>();

            nancyModule
            .Setup(nm => nm.Context)
            .Returns(new NancyContext())
            .Verifiable();

            viewRenderer
            .Setup(vr => vr.RenderView(nancyModule.Object.Context, AuthenticationRedirectUrl.Url, null))
            .Verifiable();

            userMapper.Authenticate(
                nancyModule.Object,
                userMapper,
                configuration.Object,
                userRepository.Object,
                userCredentials,
                viewRenderer.Object,
                moduleStaticWrappers.Object);

            nancyModule.Verify();
            userRepository.Verify();
        }
コード例 #2
0
        public void AuthenticateShouldPassWithValidCredentials()
        {
            var nancyModule     = new Mock <INancyModule>();
            var userMapper      = new FormsUserMapper();
            var configuration   = new Mock <IConfiguration>();
            var userRepository  = new Mock <IUserRepository>();
            var userCredentials = new UserCredentials {
                User = "******", Password = "******"
            };
            var viewRenderer         = new Mock <IViewRenderer>();
            var moduleStaticWrappers = new Mock <IModuleStaticWrappers>();

            var user = new UserIdentity("name", "xxx", new[] { "admin" }, "first", "last");

            userRepository.Setup(ur => ur.UpdateUser(user)).Verifiable();
            userRepository.Setup(ur => ur.Authenticate(userCredentials.User, userCredentials.Password)).Returns(user).Verifiable();

            moduleStaticWrappers
            .Setup(msw => msw.LoginAndRedirect(nancyModule.Object, It.IsAny <Guid>(), null, "~/home"))
            .Returns(new Response());

            userMapper.Authenticate(
                nancyModule.Object,
                userMapper,
                configuration.Object,
                userRepository.Object,
                userCredentials,
                viewRenderer.Object,
                moduleStaticWrappers.Object)
            .Should().BeOfType <Response>();

            nancyModule.Verify();
            userRepository.Verify();
        }
コード例 #3
0
        public void AddedUserShouldBeRetrievable()
        {
            var userMapper = new FormsUserMapper();
            var guid       = userMapper.AddUser("user", "first", "last", new string[0]);
            var user       = (UserIdentity)userMapper.GetUserFromIdentifier(guid, null);

            user.UserName.Should().Be("user");
            user.FirstName.Should().Be("first");
            user.LastName.Should().Be("last");
            user.Claims.Should().BeEmpty();
        }
コード例 #4
0
ファイル: FormsModule.cs プロジェクト: xiaopohou/NancyDemo
        public FormsModule()
            : base("/forms")
        {
            Get["/"] = r =>
            {
                var os = System.Environment.OSVersion;
                if (Context.CurrentUser == null)
                {
                    return("no login");
                }
                var user = Context.CurrentUser.UserName;
                return("Nancy Forms <br/> System:" + os.VersionString + "<br/>" + user + "<br/><a href='/forms/logout'>logout</a>");
            };

            Get["/login"] = r =>
            {
                var error = this.Request.Query.error.HasValue;
                return(View["login", new { Errored = error }]);
            };

            Post["/login"] = r =>
            {
                var userGuid = FormsUserMapper.ValidateUser((string)this.Request.Form.Username, (string)this.Request.Form.Password);

                if (userGuid == null)
                {
                    return(this.Context.GetRedirect("~/forms/login?error=true&username="******"/logout"] = r =>
            {
                return(this.Logout("~/forms"));
            };
        }
コード例 #5
0
        public void AddUserShouldReturnGuid()
        {
            var userMapper = new FormsUserMapper();

            userMapper.AddUser("user", "first", "last", new string[0]).Should().NotBeEmpty();
        }