File: MembershipPasswordAttributeTest.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (138 lines) | stat: -rw-r--r-- 6,786 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Security;
using NUnit.Framework;

namespace MonoTests.System.Web.Security {

	[TestFixture]
	public class MembershipPasswordAttributeTest
	{
		private ValidationContext _validationContext;

		public MembershipPasswordAttributeTest ()
		{
			_validationContext = new ValidationContext (new object ())
				{
					DisplayName = "testDisplay",
					MemberName = "testMember"
				};
		}

		[Test]
		public void IsValid ()
		{
			var passwordAttribute = new MembershipPasswordAttributeTestClass ();
			Assert.IsTrue (passwordAttribute.IsValid (""), "sending an empty password password should should be treated as valid");
		}

		[Test]
		public void IsValid_with_ValidationContext ()
		{
			var passwordAttribute = new MembershipPasswordAttributeTestClass ();
			var result = passwordAttribute.TestValidation ("", _validationContext);
			Assert.IsNull (result, "sending an empty password password should return a null response");

			result = passwordAttribute.TestValidation ("a!12345", _validationContext);
			Assert.AreEqual (ValidationResult.Success, result, "Should suceed with a 7 character length and a single nonalphanumeric");

			// test error priority
			passwordAttribute.MinRequiredPasswordLength = 4;
			passwordAttribute.MinRequiredNonAlphanumericCharacters = 2;
			result = passwordAttribute.TestValidation ("aaa", _validationContext);
			Assert.AreEqual ("The 'testDisplay' field is an invalid password. Password must have 4 or more characters.", result.ErrorMessage);
			
		}

		[Test]
		public void MinRequiredPasswordLength ()
		{
			var passwordAttribute = new MembershipPasswordAttributeTestClass ();
			var result = passwordAttribute.TestValidation ("a!1234", _validationContext);
			Assert.AreEqual ("The 'testDisplay' field is an invalid password. Password must have 7 or more characters.", result.ErrorMessage, "Error message not correct for lower Min characters");
			Assert.AreEqual (_validationContext.MemberName, result.MemberNames.FirstOrDefault (), "Member name not correct");

			passwordAttribute.MinRequiredPasswordLength = 6;
			result = passwordAttribute.TestValidation ("a!1234", _validationContext);
			Assert.AreEqual (ValidationResult.Success, result, "Should suceed with a 6 character length after it's reset");

			result = passwordAttribute.TestValidation ("a!123", _validationContext);
			Assert.AreEqual("The 'testDisplay' field is an invalid password. Password must have 6 or more characters.", result.ErrorMessage, "Error message not correct for Min characters of 6");


			passwordAttribute.MinRequiredPasswordLength = 1;
			result = passwordAttribute.TestValidation ("!", _validationContext);
			Assert.AreEqual(ValidationResult.Success, result, "Should suceed with a 6 character length after it's reset");

			// Note there is no test for empty password here as it returns null and is therefore in the generic test

			// Error Message changes
			passwordAttribute.MinRequiredPasswordLength = 5;
			passwordAttribute.MinPasswordLengthError = "There was an error";
			result = passwordAttribute.TestValidation ("a!13", _validationContext);
			Assert.AreEqual("There was an error", result.ErrorMessage, "Error Message wasn't correct without parameters.");

			passwordAttribute.MinPasswordLengthError = "There was an error parameter1: {0}";
			result = passwordAttribute.TestValidation ("a!13", _validationContext);
			Assert.AreEqual("There was an error parameter1: testDisplay", result.ErrorMessage, "Error Message wasn't correct with 1 parameter.");

			passwordAttribute.MinPasswordLengthError = "There was an error parameter1: {0} parameter2: {1}";
			result = passwordAttribute.TestValidation ("a!13", _validationContext);
			Assert.AreEqual ("There was an error parameter1: testDisplay parameter2: 5", result.ErrorMessage, "Error Message wasn't correct with 2 parameters.");

		}

		[Test]
		public void MinRequiredNonAlphanumericCharacters ()
		{
			var passwordAttribute = new MembershipPasswordAttributeTestClass ();
			var result = passwordAttribute.TestValidation ("a!12345", _validationContext);
			Assert.AreEqual (ValidationResult.Success, result, "Should succeed with the default 1 non alpha numeric");

			result = passwordAttribute.TestValidation ("a123456", _validationContext);
			Assert.AreEqual ("The 'testDisplay' field is an invalid password. Password must have 1 or more non-alphanumeric characters.", result.ErrorMessage, "Expected validation to fail without non-alphanumerics");


			passwordAttribute.MinRequiredNonAlphanumericCharacters = 3;
			result = passwordAttribute.TestValidation ("a!&12345", _validationContext);
			Assert.AreEqual ("The 'testDisplay' field is an invalid password. Password must have 3 or more non-alphanumeric characters.", result.ErrorMessage, "Expected validation to fail without 3 non-alphanumerics");

			result = passwordAttribute.TestValidation ("a!?&132154", _validationContext);
			Assert.AreEqual (ValidationResult.Success, result, "Should succeed with 3 non alpha numerics");

			passwordAttribute.MinRequiredNonAlphanumericCharacters = 0;
			result = passwordAttribute.TestValidation ("a123456", _validationContext);
			Assert.AreEqual (ValidationResult.Success, result, "Should succeed with 0 non alpha numerics");

			// Error Message changes
			passwordAttribute.MinRequiredNonAlphanumericCharacters = 1;
			passwordAttribute.MinNonAlphanumericCharactersError = "There was an error";
			result = passwordAttribute.TestValidation ("a123456", _validationContext);
			Assert.AreEqual ("There was an error", result.ErrorMessage, "Error Message wasn't correct without parameters.");

			passwordAttribute.MinNonAlphanumericCharactersError = "There was an error parameter1: {0}";
			result = passwordAttribute.TestValidation ("a123456", _validationContext);
			Assert.AreEqual("There was an error parameter1: testDisplay", result.ErrorMessage, "Error Message wasn't correct with 1 parameter.");

			passwordAttribute.MinNonAlphanumericCharactersError = "There was an error parameter1: {0} parameter2: {1}";
			result = passwordAttribute.TestValidation ("a123456", _validationContext);
			Assert.AreEqual ("There was an error parameter1: testDisplay parameter2: 1", result.ErrorMessage, "Error Message wasn't correct with 2 parameters.");
		}

		[Test]
		public void FormatErrorMessage ()
		{
			var passwordAttribute = new MembershipPasswordAttribute ();
			Assert.AreEqual ("The field testDisplay2 is invalid.", passwordAttribute.FormatErrorMessage ("testDisplay2"));
		}

		internal class MembershipPasswordAttributeTestClass : MembershipPasswordAttribute
		{
			public ValidationResult TestValidation (object val, ValidationContext context)
			{
				return IsValid (val, context);
			}
		}
	}
}