ChangePasswordTest.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. //
  2. // ChangePasswordTest.cs - Unit tests for System.Web.UI.WebControls.ChangePassword
  3. //
  4. // Author:
  5. // Vladimir Krasnov <[email protected]>
  6. //
  7. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Drawing;
  30. using System.Threading;
  31. using System.Collections;
  32. using System.Globalization;
  33. using System.IO;
  34. using System.Web;
  35. using System.Web.UI;
  36. using System.Web.UI.WebControls;
  37. using System.Web.Security;
  38. using System.Text.RegularExpressions;
  39. using MonoTests.SystemWeb.Framework;
  40. using MonoTests.stand_alone.WebHarness;
  41. using MonoTests.Common;
  42. using NUnit.Framework;
  43. using System.Collections.Specialized;
  44. using System.Web.Configuration;
  45. namespace MonoTests.System.Web.UI.WebControls
  46. {
  47. public class TestChangePassword : ChangePassword
  48. {
  49. public string Tag
  50. {
  51. get { return base.TagName; }
  52. }
  53. public StateBag StateBag
  54. {
  55. get { return base.ViewState; }
  56. }
  57. public string Render ()
  58. {
  59. StringWriter sw = new StringWriter ();
  60. sw.NewLine = "\n";
  61. HtmlTextWriter writer = new HtmlTextWriter (sw);
  62. base.Render (writer);
  63. return writer.InnerWriter.ToString ();
  64. }
  65. public Style GetStyle ()
  66. {
  67. return base.CreateControlStyle ();
  68. }
  69. public void TrackState ()
  70. {
  71. TrackViewState ();
  72. }
  73. public void LoadState (object state)
  74. {
  75. LoadViewState (state);
  76. }
  77. public object SaveState ()
  78. {
  79. return SaveViewState ();
  80. }
  81. public void SetDesignMode (IDictionary dic)
  82. {
  83. base.SetDesignModeState (dic);
  84. }
  85. private bool onBubble;
  86. public bool OnBubbleEventCalled
  87. {
  88. get { return onBubble; }
  89. set { onBubble = value; }
  90. }
  91. protected override bool OnBubbleEvent (object source, EventArgs e)
  92. {
  93. onBubble = true;
  94. return base.OnBubbleEvent (source, e);
  95. }
  96. public bool DoBubbleEvent (object source, EventArgs e)
  97. {
  98. return base.OnBubbleEvent (source, e);
  99. }
  100. public void DoEnsureChildControls ()
  101. {
  102. base.EnsureChildControls ();
  103. }
  104. public bool DoOnBubbleEvent (EventArgs e)
  105. {
  106. return base.OnBubbleEvent (this, e);
  107. }
  108. }
  109. [Serializable]
  110. [TestFixture]
  111. public class ChangePasswordTest
  112. {
  113. [TestFixtureSetUp]
  114. public void CopyTestResources ()
  115. {
  116. WebTest.CopyResource (GetType (), "ChangePasswordContainer_FindControl.aspx", "ChangePasswordContainer_FindControl.aspx");
  117. }
  118. [Test]
  119. public void DefaultProperties ()
  120. {
  121. CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
  122. CultureInfo currentUICulture = Thread.CurrentThread.CurrentUICulture;
  123. try {
  124. CultureInfo ci = CultureInfo.GetCultureInfo ("en-US");
  125. Thread.CurrentThread.CurrentCulture = ci;
  126. Thread.CurrentThread.CurrentUICulture = ci;
  127. RunDefaultPropertiesTests ();
  128. } finally {
  129. Thread.CurrentThread.CurrentCulture = currentCulture;
  130. Thread.CurrentThread.CurrentUICulture = currentUICulture;
  131. }
  132. }
  133. void RunDefaultPropertiesTests ()
  134. {
  135. TestChangePassword w = new TestChangePassword ();
  136. Assert.AreEqual (0, w.Attributes.Count, "Attributes.Count");
  137. Assert.AreEqual (0, w.StateBag.Count, "ViewState.Count");
  138. Assert.AreEqual (string.Empty, w.CancelButtonImageUrl, "CancelButtonImageUrl");
  139. Assert.AreEqual ("Cancel", w.CancelButtonText, "CancelButtonText");
  140. Assert.AreEqual (ButtonType.Button, w.CancelButtonType, "CancelButtonType");
  141. Assert.AreEqual (string.Empty, w.CancelDestinationPageUrl, "CancelDestinationPageUrl");
  142. Assert.AreEqual (string.Empty, w.ChangePasswordButtonImageUrl, "ChangePasswordButtonImageUrl");
  143. Assert.AreEqual ("Change Password", w.ChangePasswordButtonText, "ChangePasswordButtonText");
  144. Assert.AreEqual (ButtonType.Button, w.ChangePasswordButtonType, "ChangePasswordButtonType");
  145. Assert.AreEqual ("Change Your Password", w.ChangePasswordTitleText, "ChangePasswordTitleText");
  146. Assert.AreEqual (string.Empty, w.ConfirmNewPassword, "CompleteSuccessText");
  147. Assert.AreEqual (string.Empty, w.HelpPageIconUrl, "HelpPageIconUrl");
  148. Assert.AreEqual (string.Empty, w.HelpPageText, "HelpPageText");
  149. Assert.AreEqual (string.Empty, w.HelpPageUrl, "HelpPageUrl");
  150. Assert.AreEqual (string.Empty, w.CreateUserIconUrl, "CreateUserIconUrl");
  151. Assert.AreEqual (string.Empty, w.CreateUserText, "CreateUserText");
  152. Assert.AreEqual (string.Empty, w.CreateUserUrl, "CreateUserUrl");
  153. Assert.AreEqual (string.Empty, w.EditProfileIconUrl, "EditProfileIconUrl");
  154. Assert.AreEqual (string.Empty, w.EditProfileText, "EditProfileText");
  155. Assert.AreEqual (string.Empty, w.EditProfileUrl, "EditProfileUrl");
  156. Assert.AreEqual (string.Empty, w.PasswordRecoveryIconUrl, "PasswordRecoveryIconUrl");
  157. Assert.AreEqual (string.Empty, w.PasswordRecoveryText, "PasswordRecoveryText");
  158. Assert.AreEqual (string.Empty, w.PasswordRecoveryUrl, "PasswordRecoveryUrl");
  159. Assert.AreEqual ("Confirm New Password:", w.ConfirmNewPasswordLabelText, "ConfirmNewPasswordLabelText");
  160. Assert.AreEqual ("The Confirm New Password must match the New Password entry.", w.ConfirmPasswordCompareErrorMessage, "ConfirmPasswordCompareErrorMessage");
  161. Assert.AreEqual ("Confirm New Password is required.", w.ConfirmPasswordRequiredErrorMessage, "ConfirmPasswordRequiredErrorMessage");
  162. Assert.AreEqual (string.Empty, w.ContinueButtonImageUrl, "ContinueButtonImageUrl");
  163. Assert.AreEqual ("Continue", w.ContinueButtonText, "ContinueButtonText");
  164. Assert.AreEqual (ButtonType.Button, w.ContinueButtonType, "ContinueButtonType");
  165. Assert.AreEqual (string.Empty, w.ContinueDestinationPageUrl, "ContinueDestinationPageUrl");
  166. Assert.AreEqual (false, w.DisplayUserName, "DisplayUserName");
  167. Assert.AreEqual (string.Empty, w.InstructionText, "InstructionText");
  168. Assert.AreEqual ("New Password:", w.NewPasswordLabelText, "NewPasswordLabelText");
  169. Assert.AreEqual (string.Empty, w.PasswordHintText, "PasswordHintText");
  170. Assert.AreEqual ("Password:", w.PasswordLabelText, "PasswordLabelText");
  171. Assert.AreEqual (string.Empty, w.SuccessPageUrl, "SuccessPageUrl");
  172. Assert.AreEqual ("Your password has been changed!", w.SuccessText, "SuccessText");
  173. Assert.AreEqual ("Change Password Complete", w.SuccessTitleText, "SuccessTitleText");
  174. Assert.AreEqual ("User Name:", w.UserNameLabelText, "UserNameLabelText");
  175. }
  176. [Test]
  177. public void AssignToDefaultProperties ()
  178. {
  179. TestChangePassword w = new TestChangePassword ();
  180. Assert.AreEqual (0, w.Attributes.Count, "Attributes.Count");
  181. Assert.AreEqual (0, w.StateBag.Count, "ViewState.Count");
  182. int count = 0;
  183. w.CancelButtonImageUrl = "text";
  184. Assert.AreEqual ("text", w.CancelButtonImageUrl, "Assign CancelButtonImageUrl,");
  185. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate CancelButtonImageUrl,");
  186. w.CancelButtonText = "text";
  187. Assert.AreEqual ("text", w.CancelButtonText, "Assign CancelButtonText");
  188. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate CancelButtonText");
  189. w.CancelDestinationPageUrl = "text";
  190. Assert.AreEqual ("text", w.CancelDestinationPageUrl, "Assign CancelDestinationPageUrl");
  191. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate CancelDestinationPageUrl");
  192. w.ChangePasswordButtonImageUrl = "text";
  193. Assert.AreEqual ("text", w.ChangePasswordButtonImageUrl, "Assign ChangePasswordButtonImageUrl");
  194. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate ChangePasswordButtonImageUrl");
  195. w.HelpPageText = "text";
  196. Assert.AreEqual ("text", w.HelpPageText, "Assign HelpPageText");
  197. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate HelpPageText");
  198. w.ChangePasswordButtonText = "text";
  199. Assert.AreEqual ("text", w.ChangePasswordButtonText, "Assign ChangePasswordButtonText");
  200. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate ChangePasswordButtonText");
  201. w.ChangePasswordFailureText = "text";
  202. Assert.AreEqual ("text", w.ChangePasswordFailureText, "Assign ChangePasswordFailureText");
  203. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate ChangePasswordFailureText");
  204. w.ChangePasswordTitleText = "msg";
  205. Assert.AreEqual ("msg", w.ChangePasswordTitleText, "Assign ChangePasswordTitleText");
  206. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate ChangePasswordTitleText");
  207. w.ConfirmNewPasswordLabelText = "msg";
  208. Assert.AreEqual ("msg", w.ConfirmNewPasswordLabelText, "Assign ConfirmNewPasswordLabelText");
  209. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate ConfirmNewPasswordLabelText");
  210. w.ContinueButtonImageUrl = "msg";
  211. Assert.AreEqual ("msg", w.ContinueButtonImageUrl, "Assign ContinueButtonImageUrl");
  212. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate ContinueButtonImageUrl");
  213. w.ContinueButtonText = "msg";
  214. Assert.AreEqual ("msg", w.ContinueButtonText, "Assign ContinueButtonText");
  215. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate ContinueButtonText");
  216. w.ContinueDestinationPageUrl = "msg";
  217. Assert.AreEqual ("msg", w.ContinueDestinationPageUrl, "Assign ContinueDestinationPageUrl");
  218. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate ContinueDestinationPageUrl");
  219. w.CreateUserIconUrl = "msg";
  220. Assert.AreEqual ("msg", w.CreateUserIconUrl, "Assign CreateUserIconUrl");
  221. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate CreateUserIconUrl");
  222. w.CreateUserText = "msg";
  223. Assert.AreEqual ("msg", w.CreateUserText, "Assign CreateUserText");
  224. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate CreateUserText");
  225. w.CreateUserUrl = "msg";
  226. Assert.AreEqual ("msg", w.CreateUserUrl, "Assign CreateUserUrl");
  227. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate CreateUserUrl");
  228. w.DisplayUserName = true;
  229. Assert.AreEqual (true, w.DisplayUserName, "Assign DisplayUserName");
  230. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate DisplayUserName");
  231. w.EditProfileIconUrl = "msg";
  232. Assert.AreEqual ("msg", w.EditProfileIconUrl, "Assign EditProfileIconUrl");
  233. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate EditProfileIconUrl");
  234. w.EditProfileText = "msg";
  235. Assert.AreEqual ("msg", w.EditProfileText, "Assign EditProfileText");
  236. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate EditProfileText");
  237. w.HelpPageIconUrl = "msg";
  238. Assert.AreEqual ("msg", w.HelpPageIconUrl, "Assign HelpPageIconUrl");
  239. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate HelpPageIconUrl");
  240. w.HelpPageUrl = "msg";
  241. Assert.AreEqual ("msg", w.HelpPageUrl, "Assign HelpPageUrl");
  242. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate HelpPageUrl");
  243. w.InstructionText = "msg";
  244. Assert.AreEqual ("msg", w.InstructionText, "Assign InstructionText");
  245. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate InstructionText");
  246. w.MembershipProvider = "msg";
  247. Assert.AreEqual ("msg", w.MembershipProvider, "Assign MembershipProvider");
  248. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate MembershipProvider");
  249. w.NewPasswordLabelText = "msg";
  250. Assert.AreEqual ("msg", w.NewPasswordLabelText, "Assign NewPasswordLabelText");
  251. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate NewPasswordLabelText");
  252. w.PasswordHintText = "msg";
  253. Assert.AreEqual ("msg", w.PasswordHintText, "Assign PasswordHintText");
  254. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate PasswordHintText");
  255. w.PasswordLabelText = "msg";
  256. Assert.AreEqual ("msg", w.PasswordLabelText, "Assign PasswordLabelText");
  257. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate PasswordLabelText");
  258. w.PasswordRecoveryIconUrl = "msg";
  259. Assert.AreEqual ("msg", w.PasswordRecoveryIconUrl, "Assign PasswordRecoveryIconUrl");
  260. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate PasswordRecoveryIconUrl");
  261. w.PasswordRecoveryText = "msg";
  262. Assert.AreEqual ("msg", w.PasswordRecoveryText, "Assign PasswordRecoveryText");
  263. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate PasswordRecoveryText");
  264. w.PasswordRecoveryUrl = "msg";
  265. Assert.AreEqual ("msg", w.PasswordRecoveryUrl, "Assign PasswordRecoveryUrl");
  266. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate PasswordRecoveryUrl");
  267. w.PasswordRequiredErrorMessage = "msg";
  268. Assert.AreEqual ("msg", w.PasswordRequiredErrorMessage, "Assign PasswordRequiredErrorMessage");
  269. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate PasswordRequiredErrorMessage");
  270. w.SuccessPageUrl = "msg";
  271. Assert.AreEqual ("msg", w.SuccessPageUrl, "Assign SuccessPageUrl");
  272. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate SuccessPageUrl");
  273. w.SuccessText = "msg";
  274. Assert.AreEqual ("msg", w.SuccessText, "Assign SuccessText");
  275. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate SuccessText");
  276. w.SuccessTitleText = "msg";
  277. Assert.AreEqual ("msg", w.SuccessTitleText, "Assign SuccessTitleText");
  278. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate SuccessTitleText");
  279. w.UserNameLabelText = "msg";
  280. Assert.AreEqual ("msg", w.UserNameLabelText, "Assign UserNameLabelText");
  281. Assert.AreEqual (++count, w.StateBag.Count, "Viewstate UserNameLabelText");
  282. }
  283. public static void BasicRenderTestInit (Page p)
  284. {
  285. CreateTestControl (p);
  286. }
  287. public static ChangePassword CreateTestControl (Page p)
  288. {
  289. LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
  290. LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
  291. ChangePassword w = new ChangePassword ();
  292. w.ID = "ChangePassword1";
  293. w.DisplayUserName = true;
  294. w.MembershipProvider = "FakeProvider";
  295. p.Form.Controls.Add (lcb);
  296. p.Form.Controls.Add (w);
  297. p.Form.Controls.Add (lce);
  298. return w;
  299. }
  300. [Test]
  301. [Category ("NunitWeb")]
  302. public void BasicRenderTest ()
  303. {
  304. string html = new WebTest (PageInvoker.CreateOnLoad (
  305. new PageDelegate (BasicRenderTestInit))).Run ();
  306. int st = 0;
  307. Assert.IsTrue ((st = html.IndexOf ("<table", st)) > 0, "base render test 1");
  308. Assert.IsTrue ((st = html.IndexOf ("ChangePassword1", st)) > 0, "base render test 2");
  309. Assert.IsTrue ((st = html.IndexOf ("border-collapse:collapse", st)) > 0, "base render test 3");
  310. Assert.IsTrue ((st = html.IndexOf ("<table", st)) > 0, "base render test 4");
  311. Assert.IsTrue ((st = html.IndexOf ("Change Your Password", st)) > 0, "base render test 5");
  312. Assert.IsTrue ((st = html.IndexOf ("User Name:", st)) > 0, "base render test 6");
  313. Assert.IsTrue ((st = html.IndexOf ("Password:", st)) > 0, "base render test 7");
  314. Assert.IsTrue ((st = html.IndexOf ("New Password:", st)) > 0, "base render test 8");
  315. Assert.IsTrue ((st = html.IndexOf ("Change Password", st)) > 0, "base render test 9");
  316. }
  317. [Test]
  318. [Category ("NunitWeb")]
  319. public void TitlesRenderTest ()
  320. {
  321. string html = new WebTest (PageInvoker.CreateOnLoad (
  322. new PageDelegate (TitlesRenderTestInit))).Run ();
  323. Assert.IsTrue (html.IndexOf ("userid") > 0, "UserNameLabelText");
  324. Assert.IsTrue (html.IndexOf ("PasswordLabelText") > 0, "PasswordLabelText");
  325. Assert.IsTrue (html.IndexOf ("NewPasswordLabelText") > 0, "NewPasswordLabelText");
  326. Assert.IsTrue (html.IndexOf ("ConfirmNewPasswordLabelText") > 0, "ConfirmNewPasswordLabelText");
  327. Assert.IsTrue (html.IndexOf ("InstructionText") > 0, "InstructionText");
  328. Assert.IsTrue (html.IndexOf ("PasswordHintText") > 0, "PasswordHintText");
  329. Assert.IsTrue (html.IndexOf ("zzxcmnmncx") > 0, "zzxcmnmncx");
  330. }
  331. public static void TitlesRenderTestInit (Page p)
  332. {
  333. ChangePassword w = CreateTestControl (p);
  334. w.UserNameLabelText = "userid";
  335. w.PasswordLabelText = "PasswordLabelText";
  336. w.NewPasswordLabelText = "NewPasswordLabelText";
  337. w.ConfirmNewPasswordLabelText = "ConfirmNewPasswordLabelText";
  338. w.InstructionText = "InstructionText";
  339. w.PasswordHintText = "PasswordHintText";
  340. w.ChangePasswordButtonText = "zzxcmnmncx";
  341. }
  342. [Test]
  343. [Category ("NunitWeb")]
  344. public void ExtraTitlesRenderTest ()
  345. {
  346. string html = new WebTest (PageInvoker.CreateOnLoad (
  347. new PageDelegate (ExtraTitlesRenderTestInit))).Run ();
  348. Assert.IsTrue (html.IndexOf ("PasswordHintText") > 0, "PasswordHintText");
  349. Assert.IsTrue (html.IndexOf ("http://www.HelpPageUrl.com") > 0, "HelpPageUrl");
  350. Assert.IsTrue (html.IndexOf ("HelpPageText") > 0, "HelpPageText");
  351. Assert.IsTrue (html.IndexOf ("http://www.HelpPageIconUrl.com") > 0, "HelpPageIconUrl");
  352. Assert.IsTrue (html.IndexOf ("CreateUserIconUrl") > 0, "CreateUserIconUrl");
  353. Assert.IsTrue (html.IndexOf ("CreateUserText") > 0, "CreateUserText");
  354. Assert.IsTrue (html.IndexOf ("CreateUserUrl") > 0, "CreateUserUrl");
  355. }
  356. public static void ExtraTitlesRenderTestInit (Page p)
  357. {
  358. ChangePassword w = CreateTestControl (p);
  359. w.HelpPageUrl = "http://www.HelpPageUrl.com";
  360. w.HelpPageText = "HelpPageText";
  361. w.HelpPageIconUrl = "http://www.HelpPageIconUrl.com";
  362. w.CreateUserIconUrl = "CreateUserIconUrl";
  363. w.CreateUserText = "CreateUserText";
  364. w.CreateUserUrl = "CreateUserUrl";
  365. w.PasswordHintText = "PasswordHintText";
  366. }
  367. [Test]
  368. [Category ("NunitWeb")]
  369. public void StylesRenderTest ()
  370. {
  371. string html = new WebTest (PageInvoker.CreateOnLoad (
  372. new PageDelegate (StylesRenderTestInit))).Run ();
  373. Assert.IsTrue (html.IndexOf ("LightGoldenrodYellow;") > 0, "TextBoxStyle");
  374. Assert.IsTrue (html.IndexOf ("732px") > 0, "TitleTextStyle");
  375. Assert.IsTrue (html.IndexOf ("LightSkyBlue;") > 0, "HyperLinkStyle");
  376. Assert.IsTrue (html.IndexOf ("MediumSeaGreen;") > 0, "InstructionTextStyle");
  377. Assert.IsTrue (html.IndexOf ("MediumSpringGreen;") > 0, "LabelStyle");
  378. }
  379. private string GetDecoratedId (string html, string id)
  380. {
  381. Regex reg = new Regex ("name=\".*[\\$\\:]" + id + "\"");
  382. Match match = reg.Match (html);
  383. string fixedId = match.Value;
  384. if (fixedId.Length > 0)
  385. fixedId = fixedId.Substring (fixedId.IndexOf ("\""), fixedId.Length - fixedId.IndexOf ("\"")).Trim ('\"');
  386. return fixedId;
  387. }
  388. private static string GetEventTarget (string html, string id)
  389. {
  390. Regex reg = new Regex ("__doPostBack.*\\(.*'.*" + id + "'");
  391. Match match = reg.Match (html);
  392. string fixedId = match.Value;
  393. if (fixedId.Length > 0)
  394. fixedId = fixedId.Substring (fixedId.IndexOf ("'"), fixedId.Length - fixedId.IndexOf ("'")).Trim ('\'');
  395. return fixedId;
  396. }
  397. [Test]
  398. [Category ("NunitWeb")]
  399. public void BasicPostbackTest ()
  400. {
  401. PageInvoker pi = PageInvoker.CreateOnLoad (new PageDelegate (StylesRenderTestInit));
  402. WebTest test = new WebTest (pi);
  403. string html = test.Run ();
  404. test.Invoker = pi;
  405. FormRequest fr = new FormRequest (test.Response, "form1");
  406. fr.Controls.Add (new BaseControl (GetDecoratedId (html, "UserName"), "username"));
  407. PageDelegates pd = new PageDelegates ();
  408. pd.PreRender = new PageDelegate (BasicPostTestPreRender);
  409. pd.Load = new PageDelegate (StylesRenderTestInit);
  410. pi.Delegates = pd;
  411. test.Request = fr;
  412. html = test.Run ();
  413. Assert.IsTrue (html.IndexOf ("username") > 0, "rendered user name");
  414. Assert.IsTrue (html.IndexOf ("LightGoldenrodYellow;") > 0, "TextBoxStyle");
  415. Assert.IsTrue (html.IndexOf ("732px") > 0, "TitleTextStyle");
  416. Assert.IsTrue (html.IndexOf ("LightSkyBlue;") > 0, "HyperLinkStyle");
  417. Assert.IsTrue (html.IndexOf ("MediumSeaGreen;") > 0, "InstructionTextStyle");
  418. Assert.IsTrue (html.IndexOf ("MediumSpringGreen;") > 0, "LabelStyle");
  419. Assert.IsTrue (html.IndexOf ("PeachPuff;") > 0, "LabelStyle");
  420. }
  421. public static void BasicPostTestPreRender (Page p)
  422. {
  423. ChangePassword w = (ChangePassword) p.FindControl ("ChangePassword1");
  424. if (w == null)
  425. Assert.Fail ("postback1");
  426. Assert.AreEqual ("username", w.UserName, "posted user name");
  427. }
  428. public static void StylesRenderTestInit (Page p)
  429. {
  430. ChangePassword w = CreateTestControl (p);
  431. w.MembershipProvider = "FakeProvider";
  432. w.SendingMail += new MailMessageEventHandler(w_SendingMail);
  433. w.SendMailError += new SendMailErrorEventHandler(w_SendMailError);
  434. w.DisplayUserName = true;
  435. if (!p.IsPostBack) {
  436. w.TextBoxStyle.BackColor = Color.LightGoldenrodYellow;
  437. w.TitleTextStyle.Height = Unit.Pixel (732);
  438. w.LabelStyle.BackColor = Color.MediumSpringGreen;
  439. w.HelpPageUrl = "http://www.HelpPageUrl.com";
  440. w.HelpPageText = "hhh";
  441. w.HyperLinkStyle.BackColor = Color.LightSkyBlue;
  442. w.InstructionText = "text";
  443. w.InstructionTextStyle.BackColor = Color.MediumSeaGreen;
  444. w.PasswordHintText = "PasswordHintText";
  445. w.PasswordHintStyle.BackColor = Color.PeachPuff;
  446. }
  447. }
  448. public static void w_SendingMail (object sender, MailMessageEventArgs e)
  449. {
  450. if (e.Message.Body.IndexOf ("123") > 0)
  451. WebTest.CurrentTest.UserData = "w_SendingMail";
  452. }
  453. public static void w_SendMailError (object sender, SendMailErrorEventArgs e)
  454. {
  455. e.Handled = true;
  456. }
  457. // TODO:
  458. // ValidatorTextStyle
  459. // ErrorMessageStyle
  460. [Test]
  461. [Category ("NotDotNet")]
  462. [Category ("NunitWeb")]
  463. public void ChngPasswordTest ()
  464. {
  465. PageInvoker pi = PageInvoker.CreateOnLoad (new PageDelegate (StylesRenderTestInit));
  466. WebTest test = new WebTest (pi);
  467. string html = test.Run ();
  468. test.Invoker = pi;
  469. FormRequest fr = new FormRequest (test.Response, "form1");
  470. fr.Controls.Add (new BaseControl (GetDecoratedId (html, "UserName"), "heh"));
  471. fr.Controls.Add (new BaseControl (GetDecoratedId (html, "CurrentPassword"), "heh"));
  472. fr.Controls.Add (new BaseControl (GetDecoratedId (html, "NewPassword"), "hehe"));
  473. fr.Controls.Add (new BaseControl (GetDecoratedId (html, "ConfirmNewPassword"), "hehe"));
  474. string button = GetDecoratedId (html, "ChangePasswordPushButton");
  475. if (button.Length > 0)
  476. fr.Controls.Add (new BaseControl (GetDecoratedId (html, "ChangePasswordPushButton"), "ChangePasswordPushButton"));
  477. else
  478. fr.Controls.Add (new BaseControl ("__EVENTTARGET", GetEventTarget (html, "ChangePassword")));
  479. test.Request = fr;
  480. html = test.Run ();
  481. Assert.IsTrue (html.IndexOf ("has been changed") > 0, "GetPassword");
  482. }
  483. [Test]
  484. [Category ("NunitWeb")]
  485. public void PostBackEventCancel ()
  486. {
  487. WebTest t = new WebTest ();
  488. t.Invoker = PageInvoker.CreateOnInit (new PageDelegate (_PostBackEventCancel));
  489. string html = t.Run ();
  490. if (html.IndexOf ("Change Your Password") < 0)
  491. Assert.Fail ("ChangePassword not created");
  492. FormRequest fr = new FormRequest (t.Response, "form1");
  493. fr.Controls.Add ("__EVENTTARGET");
  494. fr.Controls.Add ("__EVENTARGUMENT");
  495. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$CurrentPassword");
  496. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$NewPassword");
  497. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$ConfirmNewPassword");
  498. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$CancelPushButton");
  499. fr.Controls["ChangePassword1$ChangePasswordContainerID$CancelPushButton"].Value = "Cancel";
  500. t.Request = fr;
  501. html = t.Run ();
  502. if (t.UserData == null || t.UserData.ToString () != "CancelButtonClick")
  503. Assert.Fail ("CancelButtonClick event not fired");
  504. }
  505. public static void _PostBackEventCancel (Page p)
  506. {
  507. ChangePassword w = CreateTestControl (p);
  508. w.CancelButtonClick += new EventHandler (w_CancelButtonClick);
  509. }
  510. static void w_CancelButtonClick (object sender, EventArgs e)
  511. {
  512. WebTest.CurrentTest.UserData = "CancelButtonClick";
  513. }
  514. [Test]
  515. [Category ("NunitWeb")]
  516. public void PostBackEventChanging ()
  517. {
  518. WebTest t = new WebTest ();
  519. t.Invoker = PageInvoker.CreateOnInit (new PageDelegate (_PostBackEventChanging));
  520. string html = t.Run ();
  521. if (html.IndexOf ("Change Your Password") < 0)
  522. Assert.Fail ("ChangePassword not created");
  523. FormRequest fr = new FormRequest (t.Response, "form1");
  524. fr.Controls.Add ("__EVENTTARGET");
  525. fr.Controls.Add ("__EVENTARGUMENT");
  526. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$CurrentPassword");
  527. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$NewPassword");
  528. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$ConfirmNewPassword");
  529. fr.Controls["ChangePassword1$ChangePasswordContainerID$CurrentPassword"].Value = "a";
  530. fr.Controls["ChangePassword1$ChangePasswordContainerID$NewPassword"].Value = "1";
  531. fr.Controls["ChangePassword1$ChangePasswordContainerID$ConfirmNewPassword"].Value = "1";
  532. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$ChangePasswordPushButton");
  533. fr.Controls["ChangePassword1$ChangePasswordContainerID$ChangePasswordPushButton"].Value = "Change+Password";
  534. t.Request = fr;
  535. html = t.Run ();
  536. if (t.UserData == null || t.UserData.ToString () != "ChangingPassword")
  537. Assert.Fail ("ChangingPassword event not fired");
  538. }
  539. public static void _PostBackEventChanging (Page p)
  540. {
  541. ChangePassword w = CreateTestControl (p);
  542. w.DisplayUserName = false;
  543. w.ChangingPassword +=new LoginCancelEventHandler(w_ChangingPassword);
  544. }
  545. public static void w_ChangingPassword (object sender, LoginCancelEventArgs e)
  546. {
  547. WebTest.CurrentTest.UserData = "ChangingPassword";
  548. e.Cancel = true;
  549. }
  550. [Test]
  551. [Category ("NunitWeb")]
  552. public void PostBackEventError ()
  553. {
  554. WebTest t = new WebTest ();
  555. t.Invoker = PageInvoker.CreateOnInit (new PageDelegate (_PostBackEventError));
  556. string html = t.Run ();
  557. if (html.IndexOf ("Change Your Password") < 0)
  558. Assert.Fail ("ChangePassword not created");
  559. FormRequest fr = new FormRequest (t.Response, "form1");
  560. fr.Controls.Add ("__EVENTTARGET");
  561. fr.Controls.Add ("__EVENTARGUMENT");
  562. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$UserName");
  563. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$CurrentPassword");
  564. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$NewPassword");
  565. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$ConfirmNewPassword");
  566. fr.Controls["ChangePassword1$ChangePasswordContainerID$UserName"].Value = "WrongUser";
  567. fr.Controls["ChangePassword1$ChangePasswordContainerID$CurrentPassword"].Value = "a";
  568. fr.Controls["ChangePassword1$ChangePasswordContainerID$NewPassword"].Value = "1";
  569. fr.Controls["ChangePassword1$ChangePasswordContainerID$ConfirmNewPassword"].Value = "1";
  570. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$ChangePasswordPushButton");
  571. fr.Controls["ChangePassword1$ChangePasswordContainerID$ChangePasswordPushButton"].Value = "Change+Password";
  572. t.Request = fr;
  573. html = t.Run ();
  574. if (t.UserData == null || t.UserData.ToString () != "ChangePasswordError")
  575. Assert.Fail ("ChangePasswordError event not fired");
  576. }
  577. public static void _PostBackEventError (Page p)
  578. {
  579. ChangePassword w = CreateTestControl (p);
  580. w.ChangePasswordError += new EventHandler (w_ChangePasswordError);
  581. }
  582. public static void w_ChangePasswordError (object sender, EventArgs e)
  583. {
  584. WebTest.CurrentTest.UserData = "ChangePasswordError";
  585. }
  586. [Test]
  587. [Category ("NunitWeb")]
  588. public void PostBackEventChangedPassword ()
  589. {
  590. WebTest t = new WebTest ();
  591. t.Invoker = PageInvoker.CreateOnInit (new PageDelegate (_PostBackEventChangedPassword));
  592. string html = t.Run ();
  593. if (html.IndexOf ("Change Your Password") < 0)
  594. Assert.Fail ("ChangePassword not created");
  595. FormRequest fr = new FormRequest (t.Response, "form1");
  596. fr.Controls.Add ("__EVENTTARGET");
  597. fr.Controls.Add ("__EVENTARGUMENT");
  598. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$CurrentPassword");
  599. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$NewPassword");
  600. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$ConfirmNewPassword");
  601. fr.Controls["ChangePassword1$ChangePasswordContainerID$CurrentPassword"].Value = "p@ssword";
  602. fr.Controls["ChangePassword1$ChangePasswordContainerID$NewPassword"].Value = "123456?";
  603. fr.Controls["ChangePassword1$ChangePasswordContainerID$ConfirmNewPassword"].Value = "123456?";
  604. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$ChangePasswordPushButton");
  605. fr.Controls["ChangePassword1$ChangePasswordContainerID$ChangePasswordPushButton"].Value = "Change+Password";
  606. t.Request = fr;
  607. html = t.Run ();
  608. if (t.UserData == null || t.UserData.ToString () != "ChangedPassword")
  609. Assert.Fail ("ChangedPassword event not fired");
  610. }
  611. public static void _PostBackEventChangedPassword (Page p)
  612. {
  613. ChangePassword w = CreateTestControl (p);
  614. w.DisplayUserName = false;
  615. w.ChangedPassword += new EventHandler (w_ChangedPassword);
  616. }
  617. public static void w_ChangedPassword (object sender, EventArgs e)
  618. {
  619. WebTest.CurrentTest.UserData = "ChangedPassword";
  620. }
  621. [Test]
  622. [Category ("NunitWeb")]
  623. public void PostBackEventContinue ()
  624. {
  625. WebTest t = new WebTest ();
  626. t.Invoker = PageInvoker.CreateOnInit (new PageDelegate (_PostBackEventContinue));
  627. string html = t.Run ();
  628. if (html.IndexOf ("Change Your Password") < 0)
  629. Assert.Fail ("ChangePassword not created");
  630. FormRequest fr = new FormRequest (t.Response, "form1");
  631. fr.Controls.Add ("__EVENTTARGET");
  632. fr.Controls.Add ("__EVENTARGUMENT");
  633. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$CurrentPassword");
  634. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$NewPassword");
  635. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$ConfirmNewPassword");
  636. fr.Controls["ChangePassword1$ChangePasswordContainerID$CurrentPassword"].Value = "p@ssword";
  637. fr.Controls["ChangePassword1$ChangePasswordContainerID$NewPassword"].Value = "123456?";
  638. fr.Controls["ChangePassword1$ChangePasswordContainerID$ConfirmNewPassword"].Value = "123456?";
  639. fr.Controls.Add ("ChangePassword1$ChangePasswordContainerID$ChangePasswordPushButton");
  640. fr.Controls["ChangePassword1$ChangePasswordContainerID$ChangePasswordPushButton"].Value = "Change+Password";
  641. t.Request = fr;
  642. html = t.Run ();
  643. if (html.IndexOf ("Change Password Complete") < 0)
  644. Assert.Fail ("Password has not been changed!");
  645. fr = new FormRequest (t.Response, "form1");
  646. fr.Controls.Add ("__EVENTTARGET");
  647. fr.Controls.Add ("__EVENTARGUMENT");
  648. fr.Controls.Add ("ChangePassword1$SuccessContainerID$ContinuePushButton");
  649. fr.Controls["ChangePassword1$SuccessContainerID$ContinuePushButton"].Value = "Continue";
  650. t.Request = fr;
  651. html = t.Run ();
  652. if (t.UserData == null || t.UserData.ToString () != "ContinueButtonClick")
  653. Assert.Fail ("ContinueButtonClick event not fired");
  654. }
  655. public static void _PostBackEventContinue (Page p)
  656. {
  657. ChangePassword w = CreateTestControl (p);
  658. w.DisplayUserName = false;
  659. w.ContinueButtonClick += new EventHandler (w_ContinueButtonClick);
  660. }
  661. public static void w_ContinueButtonClick (object sender, EventArgs e)
  662. {
  663. WebTest.CurrentTest.UserData = "ContinueButtonClick";
  664. }
  665. [Test]
  666. [Category ("NunitWeb")]
  667. public void DefaultProvider ()
  668. {
  669. WebTest t = new WebTest ();
  670. t.Invoker = PageInvoker.CreateOnInit (new PageDelegate (_DefaultProvider));
  671. string html = t.Run ();
  672. }
  673. public static void _DefaultProvider (Page p)
  674. {
  675. MembershipSection section = (MembershipSection) WebConfigurationManager.GetSection ("system.web/membership");
  676. Assert.AreEqual (section.DefaultProvider, "FakeProvider", "section.DefaultProvider");
  677. Assert.AreEqual (Membership.Provider.GetType (), typeof (FakeMembershipProvider), "Membership.Provider.GetType ()");
  678. }
  679. [Test]
  680. public void ChangePasswordContainer_FindControl ()
  681. {
  682. WebTest t = new WebTest ("ChangePasswordContainer_FindControl.aspx");
  683. t.Invoker = PageInvoker.CreateOnLoad (new PageDelegate (ChangePasswordContainer_FindControl_OnLoad));
  684. t.Run ();
  685. }
  686. public static void ChangePasswordContainer_FindControl_OnLoad (Page p)
  687. {
  688. ChangePassword cp = p.FindControl ("ChangePassword1") as ChangePassword;
  689. Assert.IsNotNull (cp, "#A1");
  690. RequiredFieldValidator rfv = cp.ChangePasswordTemplateContainer.FindControl ("text1required") as RequiredFieldValidator;
  691. Assert.IsNotNull (rfv, "#A2");
  692. }
  693. [Test]
  694. public void RenderOuterTableForbiddenStyles ()
  695. {
  696. var cp = new ChangePassword ();
  697. cp.RenderOuterTable = false;
  698. cp.BackColor = Color.Red;
  699. TestRenderFailure (cp, "BackColor");
  700. cp = new ChangePassword ();
  701. cp.RenderOuterTable = false;
  702. cp.BorderColor = Color.Red;
  703. TestRenderFailure (cp, "BorderColor");
  704. cp = new ChangePassword ();
  705. cp.RenderOuterTable = false;
  706. cp.BorderStyle = BorderStyle.Dashed;
  707. TestRenderFailure (cp, "BorderStyle");
  708. cp = new ChangePassword ();
  709. cp.RenderOuterTable = false;
  710. cp.BorderWidth = new Unit (10, UnitType.Pixel);
  711. TestRenderFailure (cp, "BorderWidth");
  712. cp = new ChangePassword ();
  713. cp.RenderOuterTable = false;
  714. cp.CssClass = "MyClass";
  715. TestRenderFailure (cp, "CssClass");
  716. cp = new ChangePassword ();
  717. cp.RenderOuterTable = false;
  718. cp.Font.Bold = true;
  719. TestRenderFailure (cp, "Font", false);
  720. cp = new ChangePassword ();
  721. cp.RenderOuterTable = false;
  722. cp.ForeColor = Color.Red;
  723. TestRenderFailure (cp, "ForeColor");
  724. cp = new ChangePassword ();
  725. cp.RenderOuterTable = false;
  726. cp.Height = new Unit (20, UnitType.Pixel);
  727. TestRenderFailure (cp, "Height");
  728. cp = new ChangePassword ();
  729. cp.RenderOuterTable = false;
  730. cp.Width = new Unit (20, UnitType.Pixel);
  731. TestRenderFailure (cp, "Width");
  732. }
  733. void TestRenderFailure (ChangePassword cp, string message, bool shouldFail = true)
  734. {
  735. using (var sw = new StringWriter ()) {
  736. using (var w = new HtmlTextWriter (sw)) {
  737. if (shouldFail)
  738. AssertExtensions.Throws<InvalidOperationException> (() => {
  739. cp.RenderControl (w);
  740. }, message);
  741. else {
  742. cp.RenderControl (w);
  743. Assert.IsTrue (sw.ToString ().Length > 0, message);
  744. }
  745. }
  746. }
  747. }
  748. [TestFixtureTearDown]
  749. public void TearDown ()
  750. {
  751. WebTest.Unload ();
  752. }
  753. }
  754. }