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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
//
// PasswordRecoveryTest.cs - Unit tests for System.Web.UI.WebControls.PasswordRecovery
//
// Author:
// Vladimir Krasnov <vladimirk@mainsoft.com>
//
// (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_2_0
using System;
using System.Drawing;
using System.Threading;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Text.RegularExpressions;
using MonoTests.SystemWeb.Framework;
using MonoTests.stand_alone.WebHarness;
using NUnit.Framework;
namespace MonoTests.System.Web.UI.WebControls
{
public class TestPasswordRecovery : PasswordRecovery
{
public string Tag
{
get { return base.TagName; }
}
public StateBag StateBag
{
get { return base.ViewState; }
}
public string Render ()
{
StringWriter sw = new StringWriter ();
sw.NewLine = "\n";
HtmlTextWriter writer = new HtmlTextWriter (sw);
base.Render (writer);
return writer.InnerWriter.ToString ();
}
public Style GetStyle ()
{
return base.CreateControlStyle ();
}
public void TrackState ()
{
TrackViewState ();
}
public void LoadState (object state)
{
LoadViewState (state);
}
public object SaveState ()
{
return SaveViewState ();
}
public void SetDesignMode (IDictionary dic)
{
base.SetDesignModeState (dic);
}
private bool onBubble;
public bool OnBubbleEventCalled
{
get { return onBubble; }
set { onBubble = value; }
}
protected override bool OnBubbleEvent (object source, EventArgs e)
{
onBubble = true;
return base.OnBubbleEvent (source, e);
}
public bool DoBubbleEvent (object source, EventArgs e)
{
return base.OnBubbleEvent (source, e);
}
public void DoEnsureChildControls ()
{
base.EnsureChildControls ();
}
public bool DoOnBubbleEvent (EventArgs e)
{
return base.OnBubbleEvent (this, e);
}
}
[Serializable]
[TestFixture]
public class PasswordRecoveryTest
{
// Merged required Web.config settings into NunitWeb/Resources/Web.config
[SetUp]
public void TestSetup ()
{
Thread.Sleep (150);
}
[Test]
public void DefaultProperties ()
{
TestPasswordRecovery w = new TestPasswordRecovery ();
Assert.AreEqual (0, w.Attributes.Count, "Attributes.Count");
Assert.AreEqual (0, w.StateBag.Count, "ViewState.Count");
Assert.AreEqual ("Answer:", w.AnswerLabelText, "AnswerLabelText");
Assert.AreEqual ("Answer is required.", w.AnswerRequiredErrorMessage, "AnswerRequiredErrorMessage");
Assert.AreEqual ("Your attempt to retrieve your password was not successful. Please try again.", w.GeneralFailureText, "CompleteSuccessText");
Assert.AreEqual (string.Empty, w.HelpPageIconUrl, "HelpPageIconUrl");
Assert.AreEqual (string.Empty, w.HelpPageText, "HelpPageText");
Assert.AreEqual (string.Empty, w.HelpPageUrl, "HelpPageUrl");
Assert.AreEqual ("Your answer could not be verified. Please try again.", w.QuestionFailureText, "InstructionText");
Assert.AreEqual ("Answer the following question to receive your password.", w.QuestionInstructionText, "InstructionText");
Assert.AreEqual ("Question:", w.QuestionLabelText, "ConfirmPasswordCompareErrorMessage");
Assert.AreEqual ("Identity Confirmation", w.QuestionTitleText, "ConfirmPasswordLabelText");
Assert.AreEqual ("Submit", w.SubmitButtonText, "ConfirmPasswordRequiredErrorMessage");
Assert.AreEqual (ButtonType.Button, w.SubmitButtonType, "ContinueButtonImageUrl");
Assert.AreEqual (string.Empty, w.SuccessPageUrl, "ContinueButtonText");
Assert.AreEqual ("Your password has been sent to you.", w.SuccessText, "ContinueButtonType");
Assert.AreEqual ("We were unable to access your information. Please try again.", w.UserNameFailureText, "ContinueDestinationPageUrl");
Assert.AreEqual ("Enter your User Name to receive your password.", w.UserNameInstructionText, "CreateUserButtonImageUrl");
Assert.AreEqual ("User Name:", w.UserNameLabelText, "CreateUserButtonText");
Assert.AreEqual ("User Name is required.", w.UserNameRequiredErrorMessage, "CreateUserButtonType");
Assert.AreEqual ("Forgot Your Password?", w.UserNameTitleText, "DuplicateEmailErrorMessage");
}
[Test]
public void AssignToDefaultProperties ()
{
TestPasswordRecovery w = new TestPasswordRecovery ();
Assert.AreEqual (0, w.Attributes.Count, "Attributes.Count");
Assert.AreEqual (0, w.StateBag.Count, "ViewState.Count");
int count = 0;
w.AnswerLabelText = "text";
Assert.AreEqual ("text", w.AnswerLabelText, "Assign AnswerLabelText,");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate AnswerLabelText,");
w.AnswerRequiredErrorMessage = "text";
Assert.AreEqual ("text", w.AnswerRequiredErrorMessage, "Assign AnswerRequiredErrorMessage");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate AnswerRequiredErrorMessage");
w.GeneralFailureText = "text";
Assert.AreEqual ("text", w.GeneralFailureText, "Assign GeneralFailureText");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate GeneralFailureText");
w.HelpPageIconUrl = "text";
Assert.AreEqual ("text", w.HelpPageIconUrl, "Assign HelpPageIconUrl");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate HelpPageIconUrl");
w.HelpPageText = "text";
Assert.AreEqual ("text", w.HelpPageText, "Assign HelpPageText");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate HelpPageText");
w.HelpPageUrl = "text";
Assert.AreEqual ("text", w.HelpPageUrl, "Assign HelpPageUrl");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate HelpPageUrl");
w.MembershipProvider = "text";
Assert.AreEqual ("text", w.MembershipProvider, "Assign MembershipProvider");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate MembershipProvider");
w.QuestionFailureText = "msg";
Assert.AreEqual ("msg", w.QuestionFailureText, "Assign QuestionFailureText");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate QuestionFailureText");
w.QuestionInstructionText = "msg";
Assert.AreEqual ("msg", w.QuestionInstructionText, "Assign QuestionInstructionText");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate QuestionInstructionText");
w.QuestionLabelText = "msg";
Assert.AreEqual ("msg", w.QuestionLabelText, "Assign QuestionLabelText");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate QuestionLabelText");
w.QuestionTitleText = "msg";
Assert.AreEqual ("msg", w.QuestionTitleText, "Assign QuestionTitleText");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate QuestionTitleText");
w.SubmitButtonImageUrl = "msg";
Assert.AreEqual ("msg", w.SubmitButtonImageUrl, "Assign SubmitButtonImageUrl");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate SubmitButtonImageUrl");
w.SuccessPageUrl = "msg";
Assert.AreEqual ("msg", w.SuccessPageUrl, "Assign SuccessPageUrl");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate SuccessPageUrl");
w.SuccessText = "msg";
Assert.AreEqual ("msg", w.SuccessText, "Assign SuccessText");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate SuccessText");
w.UserName = "msg";
Assert.AreEqual ("msg", w.UserName, "Assign UserName");
Assert.AreEqual (count, w.StateBag.Count, "Viewstate UserName");
w.UserNameInstructionText = "msg";
Assert.AreEqual ("msg", w.UserNameInstructionText, "Assign UserNameInstructionText");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate UserNameInstructionText");
w.UserNameLabelText = "msg";
Assert.AreEqual ("msg", w.UserNameLabelText, "Assign UserNameLabelText");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate UserNameLabelText");
w.UserNameRequiredErrorMessage = "msg";
Assert.AreEqual ("msg", w.UserNameRequiredErrorMessage, "Assign UserNameRequiredErrorMessage");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate UserNameRequiredErrorMessage");
w.UserNameTitleText = "msg";
Assert.AreEqual ("msg", w.UserNameTitleText, "Assign UserNameTitleText");
Assert.AreEqual (++count, w.StateBag.Count, "Viewstate UserNameTitleText");
}
public static void BasicRenderTestInit (Page p)
{
CreateTestControl (p);
}
public static PasswordRecovery CreateTestControl (Page p)
{
LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
PasswordRecovery w = new PasswordRecovery ();
w.ID = "PasswordRecovery1";
p.Form.Controls.Add (lcb);
p.Form.Controls.Add (w);
p.Form.Controls.Add (lce);
return w;
}
[Test]
[Category ("NunitWeb")]
public void BasicRenderTest ()
{
string html = new WebTest (PageInvoker.CreateOnLoad (
new PageDelegate (BasicRenderTestInit))).Run ();
int st = 0;
Assert.IsTrue ((st = html.IndexOf ("<table", st)) > 0, "base render test 1");
Assert.IsTrue ((st = html.IndexOf ("PasswordRecovery1", st)) > 0, "base render test 2");
Assert.IsTrue ((st = html.IndexOf ("border-collapse:collapse", st)) > 0, "base render test 3");
Assert.IsTrue ((st = html.IndexOf ("<table", st)) > 0, "base render test 4");
Assert.IsTrue ((st = html.IndexOf ("Forgot Your Password?", st)) > 0, "base render test 6");
Assert.IsTrue ((st = html.IndexOf ("Enter your User Name to receive your password.", st)) > 0, "base render test 7");
Assert.IsTrue ((st = html.IndexOf ("User Name:", st)) > 0, "base render test 8");
Assert.IsTrue ((st = html.IndexOf ("Submit", st)) > 0, "base render test 9");
}
[Test]
[Category ("NunitWeb")]
public void TitlesRenderTest ()
{
string html = new WebTest (PageInvoker.CreateOnLoad (
new PageDelegate (TitlesRenderTestInit))).Run ();
Assert.IsTrue (html.IndexOf ("userid") > 0, "UserNameLabelText");
Assert.IsTrue (html.IndexOf ("forgot") > 0, "UserNameTitleText");
Assert.IsTrue (html.IndexOf ("UserNameInstructionText") > 0, "UserNameInstructionText");
Assert.IsTrue (html.IndexOf ("zzxcmnmncx") > 0, "SubmitButtonText");
}
public static void TitlesRenderTestInit (Page p)
{
PasswordRecovery w = CreateTestControl (p);
w.UserNameLabelText = "userid";
w.UserNameTitleText = "forgot";
w.UserNameInstructionText = "UserNameInstructionText";
w.SubmitButtonText = "zzxcmnmncx";
}
[Test]
[Category ("NunitWeb")]
public void ExtraTitlesRenderTest ()
{
string html = new WebTest (PageInvoker.CreateOnLoad (
new PageDelegate (ExtraTitlesRenderTestInit))).Run ();
Assert.IsTrue (html.IndexOf ("http://www.HelpPageUrl.com") > 0, "HelpPageUrl");
Assert.IsTrue (html.IndexOf ("HelpPageText") > 0, "HelpPageText");
Assert.IsTrue (html.IndexOf ("http://www.HelpPageIconUrl.com") > 0, "HelpPageIconUrl");
}
public static void ExtraTitlesRenderTestInit (Page p)
{
PasswordRecovery w = CreateTestControl (p);
w.HelpPageUrl = "http://www.HelpPageUrl.com";
w.HelpPageText = "HelpPageText";
w.HelpPageIconUrl = "http://www.HelpPageIconUrl.com";
}
[Test]
[Category ("NunitWeb")]
public void StylesRenderTest ()
{
string html = new WebTest (PageInvoker.CreateOnLoad (
new PageDelegate (StylesRenderTestInit))).Run ();
Assert.IsTrue (html.IndexOf ("LightGoldenrodYellow;") > 0, "TextBoxStyle");
Assert.IsTrue (html.IndexOf ("732px") > 0, "TitleTextStyle");
Assert.IsTrue (html.IndexOf ("LightSkyBlue;") > 0, "HyperLinkStyle");
Assert.IsTrue (html.IndexOf ("MediumSeaGreen;") > 0, "InstructionTextStyle");
Assert.IsTrue (html.IndexOf ("MediumSpringGreen;") > 0, "LabelStyle");
}
private string GetDecoratedId (string html, string id)
{
Regex reg = new Regex ("name=\".*[\\$\\:]" + id + "\"");
Match match = reg.Match (html);
string fixedId = match.Value;
if (fixedId.Length > 0)
fixedId = fixedId.Substring (fixedId.IndexOf ("\""), fixedId.Length - fixedId.IndexOf ("\"")).Trim ('\"');
return fixedId;
}
private static string GetEventTarget (string html, string id)
{
Regex reg = new Regex ("__doPostBack.*\\(.*'.*" + id + "'");
Match match = reg.Match (html);
string fixedId = match.Value;
if (fixedId.Length > 0)
fixedId = fixedId.Substring (fixedId.IndexOf ("'"), fixedId.Length - fixedId.IndexOf ("'")).Trim ('\'');
return fixedId;
}
[Test]
[Category ("NunitWeb")]
public void BasicPostbackTest ()
{
PageInvoker pi = PageInvoker.CreateOnLoad (new PageDelegate (StylesRenderTestInit));
WebTest test = new WebTest (pi);
string html = test.Run ();
test.Invoker = pi;
FormRequest fr = new FormRequest (test.Response, "form1");
fr.Controls.Add (new BaseControl (GetDecoratedId (html, "UserName"), "username"));
PageDelegates pd = new PageDelegates ();
pd.PreRender = new PageDelegate (BasicPostTestPreRender);
pd.Load = new PageDelegate (StylesRenderTestInit);
pi.Delegates = pd;
test.Request = fr;
html = test.Run ();
Assert.IsTrue (html.IndexOf ("username") > 0, "rendered user name");
Assert.IsTrue (html.IndexOf ("LightGoldenrodYellow;") > 0, "TextBoxStyle");
Assert.IsTrue (html.IndexOf ("732px") > 0, "TitleTextStyle");
Assert.IsTrue (html.IndexOf ("LightSkyBlue;") > 0, "HyperLinkStyle");
Assert.IsTrue (html.IndexOf ("MediumSeaGreen;") > 0, "InstructionTextStyle");
Assert.IsTrue (html.IndexOf ("MediumSpringGreen;") > 0, "LabelStyle");
}
public static void BasicPostTestPreRender (Page p)
{
PasswordRecovery w = (PasswordRecovery) p.FindControl ("PasswordRecovery1");
if (w == null)
Assert.Fail ("postback1");
Assert.AreEqual ("username", w.UserName, "posted user name");
}
public static void StylesRenderTestInit (Page p)
{
PasswordRecovery w = CreateTestControl (p);
w.MembershipProvider = "FakeProvider";
w.SendingMail += new MailMessageEventHandler(w_SendingMail);
w.SendMailError += new SendMailErrorEventHandler(w_SendMailError);
if (!p.IsPostBack) {
w.TextBoxStyle.BackColor = Color.LightGoldenrodYellow;
w.TitleTextStyle.Height = Unit.Pixel (732);
w.LabelStyle.BackColor = Color.MediumSpringGreen;
w.HelpPageUrl = "http://www.HelpPageUrl.com";
w.HelpPageText = "hhh";
w.HyperLinkStyle.BackColor = Color.LightSkyBlue;
w.UserNameInstructionText = "text";
w.InstructionTextStyle.BackColor = Color.MediumSeaGreen;
}
}
public static void w_SendingMail (object sender, MailMessageEventArgs e)
{
if (e.Message.Body.IndexOf ("123") > 0)
WebTest.CurrentTest.UserData = "w_SendingMail";
}
public static void w_SendMailError (object sender, SendMailErrorEventArgs e)
{
e.Handled = true;
}
// TODO: ValidatorTextStyle, ErrorMessageStyle
[Test]
[Category ("NotDotNet")] // MS does not call GetPassword on the FakeProvider
[Category ("NunitWeb")]
public void GetPasswordTest ()
{
PageInvoker pi = PageInvoker.CreateOnLoad (new PageDelegate (StylesRenderTestInit));
WebTest test = new WebTest (pi);
string html = test.Run ();
test.Invoker = pi;
FormRequest fr = new FormRequest (test.Response, "form1");
fr.Controls.Add (new BaseControl (GetDecoratedId (html, "UserName"), "heh"));
string button = GetDecoratedId (html, "SubmitButton");
if (button.Length > 0)
fr.Controls.Add (new BaseControl (GetDecoratedId (html, "SubmitButton"), "SubmitButton"));
else
fr.Controls.Add (new BaseControl ("__EVENTTARGET", GetEventTarget (html, "SubmitButton")));
test.Request = fr;
html = test.Run ();
FormRequest fr2 = new FormRequest (test.Response, "form1");
fr2.Controls.Add (new BaseControl (GetDecoratedId (html, "Answer"), "heh"));
button = GetDecoratedId (html, "SubmitButton");
if (button.Length > 0)
fr2.Controls.Add (new BaseControl (GetDecoratedId (html, "SubmitButton"), "SubmitButton"));
else
fr2.Controls.Add (new BaseControl ("__EVENTTARGET", GetEventTarget (html, "SubmitButton")));
test.Request = fr2;
html = test.Run ();
Assert.IsTrue (html.IndexOf ("sent to you") > 0, "GetPassword");
Assert.AreEqual ("w_SendingMail", (string)test.UserData, "Mailsent");
}
}
}
#endif
|