测试密码格式

时间:2013-05-28 21:16:57

标签: c# unit-testing passwords membership-provider

好的我正在为会员密码更改编写测试。下面的代码是我到目前为止的代码。我需要一些帮助来检查密码格式。最小字符长度为7,最大长度为8。

此外,我可以测试密码格式是否清晰,但如何测试加密格式?

有人可以帮忙吗?

    [TestMethod]
    public void TestChangePassword()
    {
        try      
        {
            AsaMembershipProvider prov = this.GetMembershipProvider();
            MembershipCreateStatus status;

            //creates user
            MembershipUser user = prov.CreateUser("testUserX", "12345", "test.UserX@abc.com", "", "", true, null, out status);

            //gets user
            user = prov.GetUser("testUserX", false); 
            Assert.AreEqual(user.UserName, "testUserX");

            //Authenticates username and password
            var isAuthenticated = prov.ValidateUser(user.UserName, "12345");
            Assert.IsTrue(isAuthenticated);


            //changes password
            prov.ChangePassword("testUserX",  "12345", "ABCDE");

            //Validates password has been changed
            prov.ValidateUser(user.UserName, "ABCDE");
            Assert.IsTrue(isAuthenticated);

            // Change password back
            prov.ChangePassword("testUserX", "ABCDE", "12345");

            //Validates password has been changed back
            prov.ValidateUser(user.UserName, "12345");


            //Deletes User
            prov.DeleteUser("testUserX", true);
            //tries to get user again
            user = prov.GetUser("testUserX", false);
            //test that no user is returned
            Assert.AreEqual(null, user);

        }
        catch (Exception ex)
        {
            LogMessage(ex);
            Assert.Fail(ex.Message);
        }

    }

1 个答案:

答案 0 :(得分:1)

一种选择是编写一个针对AsaMembershipProvider的扩展方法,该方法将验证密码,然后从其中调用ChangePassword来进行实际更改

该方法的缺点是您将有更多代码来维护

public static class CryptoExtensions {

    public static void ChangePasswordEx(this AsaMembershipProvider mp, string username, string oldPassword, string newPassword){
        // validate format of the password
        if (true /*validation code*/ )
        {
            throw new Exception("Invalid password format");
        }

        // rest of the code to encrypt and store the password
        mp.ChangePassword(username, oldPassword, newPassword);
    }

}

您的测试代码现在应该使用 prov.ChangePasswordEx

调用 prov.ChangePassword