LoginStatusTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // LoginStatusTest.cs - Unit tests for System.Web.UI.WebControls.LoginStatus
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.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. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Security.Principal;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.Web.UI.HtmlControls;
  36. using System.Web.UI.WebControls;
  37. using NUnit.Framework;
  38. namespace MonoTests.System.Web.UI.WebControls {
  39. public class TestLoginStatus : LoginStatus {
  40. private bool cancel;
  41. private bool onLoggedOut;
  42. private bool onLoggingOut;
  43. private bool onPreRender;
  44. public string Tag {
  45. get { return base.TagName; }
  46. }
  47. public StateBag StateBag {
  48. get { return base.ViewState; }
  49. }
  50. private HtmlTextWriter GetWriter ()
  51. {
  52. StringWriter sw = new StringWriter ();
  53. sw.NewLine = "\n";
  54. return new HtmlTextWriter (sw);
  55. }
  56. public string Render ()
  57. {
  58. HtmlTextWriter writer = GetWriter ();
  59. Render (writer);
  60. return writer.InnerWriter.ToString ();
  61. }
  62. public string RenderContents ()
  63. {
  64. HtmlTextWriter writer = GetWriter ();
  65. base.RenderContents (writer);
  66. return writer.InnerWriter.ToString ();
  67. }
  68. public void SetDesignMode (IDictionary dic)
  69. {
  70. base.SetDesignModeState (dic);
  71. }
  72. public bool Cancel {
  73. get { return cancel; }
  74. set { cancel = value; }
  75. }
  76. public bool OnLoggedOutCalled {
  77. get { return onLoggedOut; }
  78. set { onLoggedOut = value; }
  79. }
  80. protected override void OnLoggedOut (EventArgs e)
  81. {
  82. onLoggedOut = true;
  83. base.OnLoggedOut (e);
  84. }
  85. public void DoLoggedOut (EventArgs e)
  86. {
  87. base.OnLoggedOut (e);
  88. }
  89. public bool OnLoggingOutCalled {
  90. get { return onLoggingOut; }
  91. set { onLoggingOut = value; }
  92. }
  93. protected override void OnLoggingOut (LoginCancelEventArgs e)
  94. {
  95. onLoggingOut = true;
  96. e.Cancel = cancel;
  97. base.OnLoggingOut (e);
  98. }
  99. public void DoLoggingIn (LoginCancelEventArgs e)
  100. {
  101. base.OnLoggingOut (e);
  102. }
  103. public bool OnPreRenderCalled {
  104. get { return onPreRender; }
  105. set { onPreRender = value; }
  106. }
  107. protected override void OnPreRender (EventArgs e)
  108. {
  109. onPreRender = true;
  110. base.OnPreRender (e);
  111. }
  112. }
  113. [TestFixture]
  114. public class LoginStatusTest {
  115. private IPrincipal GetPrincipal (string name)
  116. {
  117. return new GenericPrincipal (new GenericIdentity (name), null);
  118. }
  119. private IPrincipal GetUnauthenticatedPrincipal (string name)
  120. {
  121. return new GenericPrincipal (new UnauthenticatedIdentity (name), null);
  122. }
  123. [Test]
  124. public void DefaultProperties ()
  125. {
  126. TestLoginStatus ls = new TestLoginStatus ();
  127. Assert.AreEqual (0, ls.Attributes.Count, "Attributes.Count");
  128. Assert.AreEqual (0, ls.StateBag.Count, "ViewState.Count");
  129. Assert.AreEqual (String.Empty, ls.LoginImageUrl, "LoginImageUrl");
  130. Assert.AreEqual ("Login", ls.LoginText, "LoginText");
  131. Assert.AreEqual (LogoutAction.Refresh, ls.LogoutAction, "LogoutAction");
  132. Assert.AreEqual (String.Empty, ls.LogoutImageUrl, "LogoutImageUrl");
  133. Assert.AreEqual (String.Empty, ls.LogoutPageUrl, "LogoutPageUrl");
  134. Assert.AreEqual ("Logout", ls.LogoutText, "LogoutText");
  135. Assert.AreEqual ("a", ls.Tag, "TagName");
  136. Assert.AreEqual (0, ls.Attributes.Count, "Attributes.Count-2");
  137. Assert.AreEqual (0, ls.StateBag.Count, "ViewState.Count-2");
  138. }
  139. [Test]
  140. public void SetOriginalProperties ()
  141. {
  142. TestLoginStatus ls = new TestLoginStatus ();
  143. ls.LoginImageUrl = String.Empty;
  144. Assert.AreEqual (String.Empty, ls.LoginImageUrl, "LoginImageUrl");
  145. ls.LoginText = "Login";
  146. Assert.AreEqual ("Login", ls.LoginText, "LoginText");
  147. ls.LogoutAction = LogoutAction.Refresh;
  148. Assert.AreEqual (LogoutAction.Refresh, ls.LogoutAction, "LogoutAction");
  149. ls.LogoutImageUrl = String.Empty;
  150. Assert.AreEqual (String.Empty, ls.LogoutImageUrl, "LogoutImageUrl");
  151. ls.LogoutPageUrl = String.Empty;
  152. Assert.AreEqual (String.Empty, ls.LogoutPageUrl, "LogoutPageUrl");
  153. ls.LogoutText = "Logout";
  154. Assert.AreEqual ("Logout", ls.LogoutText, "LogoutText");
  155. Assert.AreEqual ("a", ls.Tag, "TagName");
  156. Assert.AreEqual (6, ls.StateBag.Count, "ViewState.Count-1");
  157. }
  158. [Test]
  159. public void CleanProperties ()
  160. {
  161. TestLoginStatus ls = new TestLoginStatus ();
  162. ls.LoginImageUrl = String.Empty;
  163. ls.LoginText = "Login";
  164. ls.LogoutAction = LogoutAction.Refresh;
  165. ls.LogoutImageUrl = String.Empty;
  166. ls.LogoutPageUrl = String.Empty;
  167. ls.LogoutText = "Logout";
  168. Assert.AreEqual (6, ls.StateBag.Count, "ViewState.Count-1");
  169. ls.LoginImageUrl = null;
  170. Assert.AreEqual (String.Empty, ls.LoginImageUrl, "LoginImageUrl");
  171. ls.LoginText = null;
  172. Assert.AreEqual ("Login", ls.LoginText, "LoginText");
  173. ls.LogoutImageUrl = null;
  174. Assert.AreEqual (String.Empty, ls.LogoutImageUrl, "LogoutImageUrl");
  175. ls.LogoutPageUrl = null;
  176. Assert.AreEqual (String.Empty, ls.LogoutPageUrl, "LogoutPageUrl");
  177. ls.LogoutText = null;
  178. Assert.AreEqual ("Logout", ls.LogoutText, "LogoutText");
  179. Assert.AreEqual ("a", ls.Tag, "TagName");
  180. Assert.AreEqual (1, ls.StateBag.Count, "ViewState.Count-2");
  181. }
  182. [Test]
  183. public void Tag ()
  184. {
  185. TestLoginStatus ls = new TestLoginStatus ();
  186. Assert.AreEqual ("a", ls.Tag, "TagName");
  187. ls.LoginImageUrl = "http://www.go-mono.com";
  188. ls.LogoutImageUrl = "http://www.mono-project.com";
  189. Assert.AreEqual ("a", ls.Tag, "TagName");
  190. }
  191. [Test]
  192. public void Controls ()
  193. {
  194. TestLoginStatus ls = new TestLoginStatus ();
  195. Assert.AreEqual (4, ls.Controls.Count, "Count");
  196. Assert.IsTrue (ls.Controls[0] is LinkButton, "Type-0");
  197. Assert.IsTrue (ls.Controls[1] is ImageButton, "Type-1");
  198. Assert.IsTrue (ls.Controls[2] is LinkButton, "Type-2");
  199. Assert.IsTrue (ls.Controls[3] is ImageButton, "Type-3");
  200. Assert.IsTrue (ls.Controls[0].Visible, "Visible-0");
  201. Assert.IsTrue (ls.Controls[1].Visible, "Visible-1");
  202. Assert.IsTrue (ls.Controls[2].Visible, "Visible-2");
  203. Assert.IsTrue (ls.Controls[3].Visible, "Visible-3");
  204. Assert.IsNotNull (ls.Controls[0].UniqueID, "UniqueID-0");
  205. Assert.IsNotNull (ls.Controls[1].UniqueID, "UniqueID-1");
  206. Assert.IsNotNull (ls.Controls[2].UniqueID, "UniqueID-2");
  207. Assert.IsNotNull (ls.Controls[3].UniqueID, "UniqueID-3");
  208. Assert.IsNull (ls.Controls[0].ID, "ID-0");
  209. Assert.IsNull (ls.Controls[1].ID, "ID-1");
  210. Assert.IsNull (ls.Controls[2].ID, "ID-2");
  211. Assert.IsNull (ls.Controls[3].ID, "ID-3");
  212. ls.LoginText = "LoginText";
  213. ls.LoginImageUrl = "LoginImageUrl";
  214. ls.LogoutText = "LogoutText";
  215. ls.LogoutImageUrl = "LogoutImageUrl";
  216. Assert.AreEqual (String.Empty, (ls.Controls[0] as LinkButton).Text, "Text-0");
  217. Assert.AreEqual (String.Empty, (ls.Controls[1] as ImageButton).ImageUrl, "ImageUrl-1");
  218. Assert.AreEqual (String.Empty, (ls.Controls[2] as LinkButton).Text, "Text-2");
  219. Assert.AreEqual (String.Empty, (ls.Controls[3] as ImageButton).ImageUrl, "ImageUrl-3");
  220. ls.Render ();
  221. Assert.IsFalse (ls.OnPreRenderCalled, "!OnPreRender");
  222. Assert.IsFalse (ls.Controls[0].Visible, "Render-Visible-0");
  223. Assert.IsFalse (ls.Controls[1].Visible, "Render-Visible-1");
  224. Assert.IsFalse (ls.Controls[2].Visible, "Render-Visible-2");
  225. Assert.IsTrue (ls.Controls[3].Visible, "Render-Visible-3");
  226. Assert.AreEqual (String.Empty, (ls.Controls[0] as LinkButton).Text, "Render-Text-0");
  227. Assert.AreEqual (String.Empty, (ls.Controls[1] as ImageButton).ImageUrl, "Render-ImageUrl-1");
  228. Assert.AreEqual (String.Empty, (ls.Controls[2] as LinkButton).Text, "Render-Text-2");
  229. Assert.AreEqual ("LoginImageUrl", (ls.Controls[3] as ImageButton).ImageUrl, "Render-ImageUrl-3");
  230. }
  231. [Test]
  232. public void Render_NoPage ()
  233. {
  234. TestLoginStatus ls = new TestLoginStatus ();
  235. Assert.AreEqual ("<a>Login</a>", ls.Render (), "Render");
  236. Assert.AreEqual ("<a>Login</a>", ls.RenderContents (), "RenderContents");
  237. ls.LoginText = "Log In";
  238. Assert.AreEqual ("<a>Log In</a>", ls.Render (), "Render-2");
  239. Assert.AreEqual ("<a>Log In</a>", ls.RenderContents (), "RenderContents-2");
  240. ls.LoginImageUrl = "http://www.go-mono-com";
  241. string s = ls.Render ();
  242. Assert.IsTrue (s.StartsWith ("<input "), "Render-3a");
  243. Assert.IsTrue (s.IndexOf (" type=\"image\" ") > 0, "Render-3b");
  244. Assert.IsTrue (s.IndexOf (" name=\"" + ls.Controls [3].UniqueID + "\" ") > 0, "Render-3c");
  245. Assert.IsTrue (s.IndexOf (" src=\"http://www.go-mono-com\" ") > 0, "Render-3d");
  246. Assert.IsTrue (s.IndexOf (" alt=\"Log In\" ") > 0, "Render-3e");
  247. Assert.IsTrue (s.EndsWith (" />"), "Render-3z");
  248. Assert.AreEqual (s, ls.RenderContents (), "RenderContents-3");
  249. // rendering <input> but we're still report an A as tag
  250. Assert.AreEqual ("a", ls.Tag, "TagName");
  251. }
  252. [Test]
  253. [ExpectedException (typeof (HttpException))]
  254. public void Render_User_WithoutForm ()
  255. {
  256. ContextPage page = new ContextPage (GetPrincipal ("me"));
  257. TestLoginStatus ls = new TestLoginStatus ();
  258. ls.Page = page;
  259. ls.Render ();
  260. // must be in a server form
  261. }
  262. // all other rendering fails because I can't setup a proper environment
  263. // mostly because overriding Context doesn't make Page.Request to work :-(
  264. }
  265. }
  266. #endif