LoginNameTest.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // LoginNameTest.cs - Unit tests for System.Web.UI.WebControls.LoginName
  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.IO;
  31. using System.Security.Principal;
  32. using System.Web;
  33. using System.Web.UI;
  34. using System.Web.UI.WebControls;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Web.UI.WebControls {
  37. public class TestLoginName : LoginName {
  38. public HttpContext HttpContext {
  39. get { return base.Context; }
  40. }
  41. public string Tag {
  42. get { return base.TagName; }
  43. }
  44. public StateBag StateBag {
  45. get { return base.ViewState; }
  46. }
  47. private HtmlTextWriter GetWriter ()
  48. {
  49. StringWriter sw = new StringWriter ();
  50. sw.NewLine = "\n";
  51. return new HtmlTextWriter (sw);
  52. }
  53. public string Render ()
  54. {
  55. HtmlTextWriter writer = GetWriter ();
  56. base.Render (writer);
  57. return writer.InnerWriter.ToString ();
  58. }
  59. public string RenderContents ()
  60. {
  61. HtmlTextWriter writer = GetWriter ();
  62. base.RenderContents (writer);
  63. return writer.InnerWriter.ToString ();
  64. }
  65. public string RenderBeginTag ()
  66. {
  67. HtmlTextWriter writer = GetWriter ();
  68. base.RenderBeginTag (writer);
  69. return writer.InnerWriter.ToString ();
  70. }
  71. public string RenderEndTag (bool includeBeginTag)
  72. {
  73. HtmlTextWriter writer = GetWriter ();
  74. if (includeBeginTag) {
  75. // required before calling RenderEndTag
  76. base.RenderBeginTag (writer);
  77. // unless we're not really calling our base...
  78. }
  79. base.RenderEndTag (writer);
  80. return writer.InnerWriter.ToString ();
  81. }
  82. }
  83. public class ContextPage : Page {
  84. HttpContext ctx;
  85. public ContextPage ()
  86. {
  87. }
  88. public ContextPage (IPrincipal principal)
  89. {
  90. Context.User = principal;
  91. }
  92. protected override HttpContext Context {
  93. get {
  94. if (ctx == null) {
  95. ctx = new HttpContext (
  96. new HttpRequest (String.Empty, "http://www.mono-project.com", String.Empty),
  97. new HttpResponse (new StringWriter ())
  98. );
  99. }
  100. return ctx;
  101. }
  102. }
  103. }
  104. public class UnauthenticatedIdentity : GenericIdentity {
  105. public UnauthenticatedIdentity (string name)
  106. : base (name)
  107. {
  108. }
  109. public override bool IsAuthenticated {
  110. get { return false; }
  111. }
  112. }
  113. [TestFixture]
  114. public class LoginNameTest {
  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. TestLoginName ln = new TestLoginName ();
  127. Assert.AreEqual (0, ln.Attributes.Count, "Attributes.Count");
  128. Assert.AreEqual (0, ln.StateBag.Count, "ViewState.Count");
  129. Assert.AreEqual ("{0}", ln.FormatString, "FormatString");
  130. Assert.AreEqual ("span", ln.Tag, "span");
  131. Assert.AreEqual (0, ln.Attributes.Count, "Attributes.Count-1");
  132. Assert.AreEqual (0, ln.StateBag.Count, "ViewState.Count-1");
  133. }
  134. [Test]
  135. public void SetOriginalProperties ()
  136. {
  137. TestLoginName ln = new TestLoginName ();
  138. ln.FormatString = "{0}";
  139. Assert.AreEqual (1, ln.StateBag.Count, "ViewState.Count-1");
  140. }
  141. [Test]
  142. public void CleanProperties ()
  143. {
  144. TestLoginName ln = new TestLoginName ();
  145. ln.FormatString = "Hola {0}!";
  146. Assert.AreEqual ("Hola {0}!", ln.FormatString, "FormatString-1");
  147. Assert.AreEqual (1, ln.StateBag.Count, "ViewState.Count-1");
  148. ln.FormatString = "{0}";
  149. Assert.AreEqual ("{0}", ln.FormatString, "FormatString-2");
  150. Assert.AreEqual (1, ln.StateBag.Count, "ViewState.Count-2");
  151. ln.FormatString = String.Empty;
  152. Assert.AreEqual (String.Empty, ln.FormatString, "FormatString-3");
  153. Assert.AreEqual (1, ln.StateBag.Count, "ViewState.Count-3");
  154. ln.FormatString = null;
  155. Assert.AreEqual ("{0}", ln.FormatString, "FormatString-4");
  156. Assert.AreEqual (0, ln.StateBag.Count, "ViewState.Count-4");
  157. }
  158. [Test]
  159. public void CacheIdentity ()
  160. {
  161. TestLoginName ln = new TestLoginName ();
  162. Assert.AreEqual (String.Empty, ln.RenderContents (), "Anonymous");
  163. ln.Page = new ContextPage (GetPrincipal ("me"));
  164. Assert.AreEqual ("me", ln.RenderContents (), "me");
  165. ln.Page = new ContextPage (GetPrincipal ("you"));
  166. Assert.AreEqual ("you", ln.RenderContents (), "you");
  167. }
  168. [Test]
  169. public void Render_NoPage ()
  170. {
  171. TestLoginName ln = new TestLoginName ();
  172. Assert.AreEqual (String.Empty, ln.Render (), "Render");
  173. Assert.AreEqual (String.Empty, ln.RenderContents (), "RenderContents");
  174. Assert.AreEqual (String.Empty, ln.RenderBeginTag (), "RenderBeginTag");
  175. Assert.AreEqual (String.Empty, ln.RenderEndTag (false), "RenderEndTag");
  176. }
  177. [Test]
  178. public void Render_Anonymous_NoPrincipal ()
  179. {
  180. ContextPage page = new ContextPage ();
  181. TestLoginName ln = new TestLoginName ();
  182. ln.Page = page;
  183. Assert.AreEqual (String.Empty, ln.Render (), "Render");
  184. Assert.AreEqual (String.Empty, ln.RenderContents (), "RenderContents");
  185. Assert.AreEqual (String.Empty, ln.RenderBeginTag (), "RenderBeginTag");
  186. Assert.AreEqual (String.Empty, ln.RenderEndTag (false), "RenderEndTag");
  187. }
  188. [Test]
  189. public void Render_Anonymous_IPrincipal ()
  190. {
  191. ContextPage page = new ContextPage (GetPrincipal (String.Empty));
  192. TestLoginName ln = new TestLoginName ();
  193. ln.Page = page;
  194. Assert.AreEqual (String.Empty, ln.Render (), "Render");
  195. Assert.AreEqual (String.Empty, ln.RenderContents (), "RenderContents");
  196. Assert.AreEqual (String.Empty, ln.RenderBeginTag (), "RenderBeginTag");
  197. Assert.AreEqual (String.Empty, ln.RenderEndTag (false), "RenderEndTag");
  198. }
  199. [Test]
  200. public void Render_User ()
  201. {
  202. ContextPage page = new ContextPage (GetPrincipal ("me"));
  203. TestLoginName ln = new TestLoginName ();
  204. ln.Page = page;
  205. Assert.IsTrue (page.User.Identity.IsAuthenticated, "IsAuthenticated");
  206. Assert.AreEqual ("<span>me</span>", ln.Render (), "Render");
  207. Assert.AreEqual ("me", ln.RenderContents (), "RenderContents");
  208. Assert.AreEqual ("<span>", ln.RenderBeginTag (), "RenderBeginTag");
  209. Assert.AreEqual ("<span></span>", ln.RenderEndTag (true), "RenderEndTag");
  210. }
  211. [Test]
  212. public void Render_UnauthenticatedUser ()
  213. {
  214. ContextPage page = new ContextPage (GetUnauthenticatedPrincipal ("me"));
  215. TestLoginName ln = new TestLoginName ();
  216. ln.Page = page;
  217. Assert.IsFalse (page.User.Identity.IsAuthenticated, "IsAuthenticated");
  218. Assert.AreEqual ("<span>me</span>", ln.Render (), "Render");
  219. Assert.AreEqual ("me", ln.RenderContents (), "RenderContents");
  220. Assert.AreEqual ("<span>", ln.RenderBeginTag (), "RenderBeginTag");
  221. Assert.AreEqual ("<span></span>", ln.RenderEndTag (true), "RenderEndTag");
  222. }
  223. [Test]
  224. public void Render_StringFormat ()
  225. {
  226. ContextPage page = new ContextPage (GetPrincipal ("me"));
  227. TestLoginName ln = new TestLoginName ();
  228. ln.Page = page;
  229. ln.FormatString = "Hola {0}!";
  230. Assert.AreEqual ("Hola me!", ln.RenderContents (), "RenderContents");
  231. }
  232. [Test]
  233. public void Render_StringFormat_Empty ()
  234. {
  235. ContextPage page = new ContextPage (GetPrincipal ("me"));
  236. TestLoginName ln = new TestLoginName ();
  237. ln.Page = page;
  238. ln.FormatString = String.Empty;
  239. Assert.AreEqual ("me", ln.RenderContents (), "RenderContents");
  240. }
  241. [Test]
  242. public void Render_StringFormat_NoVar ()
  243. {
  244. ContextPage page = new ContextPage (GetPrincipal ("me"));
  245. TestLoginName ln = new TestLoginName ();
  246. ln.Page = page;
  247. ln.FormatString = "Hola!";
  248. Assert.AreEqual ("Hola!", ln.RenderContents (), "RenderContents");
  249. }
  250. [Test]
  251. [ExpectedException (typeof (FormatException))]
  252. public void Render_StringFormat_TwoVars ()
  253. {
  254. ContextPage page = new ContextPage (GetPrincipal ("me"));
  255. TestLoginName ln = new TestLoginName ();
  256. ln.Page = page;
  257. ln.FormatString = "Hola {0} {1}!";
  258. Assert.AreEqual ("Hola me!", ln.RenderContents (), "RenderContents");
  259. }
  260. }
  261. }
  262. #endif