RoleGroupTest.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // RoleGroupTest.cs - Unit tests for System.Web.UI.WebControls.RoleGroup
  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. [TestFixture]
  38. public class RoleGroupTest : ITemplate {
  39. public void InstantiateIn (Control container)
  40. {
  41. // ITemplate
  42. }
  43. private IPrincipal GetPrincipal (string name)
  44. {
  45. return new GenericPrincipal (new GenericIdentity (name), null);
  46. }
  47. private IPrincipal GetPrincipal (string name, string role)
  48. {
  49. return new GenericPrincipal (new GenericIdentity (name), new string[1] { role });
  50. }
  51. private IPrincipal GetUnauthenticatedPrincipal (string name, string role)
  52. {
  53. return new GenericPrincipal (new UnauthenticatedIdentity (name), new string[1] { role });
  54. }
  55. [Test]
  56. public void DefaultValues ()
  57. {
  58. RoleGroup rg = new RoleGroup ();
  59. Assert.IsNull (rg.ContentTemplate, "ContentTemplate");
  60. Assert.AreEqual (0, rg.Roles.Length, "Roles");
  61. Assert.AreEqual (String.Empty, rg.ToString ());
  62. }
  63. [Test]
  64. public void ContentTemplate ()
  65. {
  66. RoleGroup rg = new RoleGroup ();
  67. rg.ContentTemplate = this;
  68. Assert.IsNotNull (rg.ContentTemplate, "ContentTemplate");
  69. }
  70. [Test]
  71. public void Roles_Null ()
  72. {
  73. RoleGroup rg = new RoleGroup ();
  74. rg.Roles = null;
  75. Assert.AreEqual (String.Empty, rg.ToString ());
  76. Assert.AreEqual (0, rg.Roles.Length, "Roles");
  77. }
  78. [Test]
  79. public void Roles_One ()
  80. {
  81. RoleGroup rg = new RoleGroup ();
  82. rg.Roles = new string[1] { "mono" };
  83. Assert.AreEqual ("mono", rg.ToString ());
  84. Assert.AreEqual (1, rg.Roles.Length, "Roles");
  85. }
  86. [Test]
  87. public void Roles_Two ()
  88. {
  89. RoleGroup rg = new RoleGroup ();
  90. rg.Roles = new string[2] { "mono", "hackers" };
  91. Assert.AreEqual ("mono,hackers", rg.ToString ());
  92. Assert.AreEqual (2, rg.Roles.Length, "Roles");
  93. }
  94. [Test]
  95. [ExpectedException (typeof (ArgumentNullException))]
  96. public void ContainsUser_Null ()
  97. {
  98. RoleGroup rg = new RoleGroup ();
  99. Assert.IsFalse (rg.ContainsUser (null), "null");
  100. }
  101. [Test]
  102. public void ContainsUser_NoRoles ()
  103. {
  104. RoleGroup rg = new RoleGroup ();
  105. Assert.IsFalse (rg.ContainsUser (GetPrincipal ("me")), "me");
  106. }
  107. [Test]
  108. public void ContainsUser_In ()
  109. {
  110. RoleGroup rg = new RoleGroup ();
  111. rg.Roles = new string[2] { "mono", "hackers" };
  112. Assert.IsTrue (rg.ContainsUser (GetPrincipal ("me1", "mono")), "me+mono");
  113. Assert.IsTrue (rg.ContainsUser (GetPrincipal ("me2", "hackers")), "me+hackers");
  114. // works for unauthenticated principals too
  115. Assert.IsTrue (rg.ContainsUser (GetUnauthenticatedPrincipal ("me3", "mono")), "unauthenticated+me+mono");
  116. Assert.IsTrue (rg.ContainsUser (GetUnauthenticatedPrincipal ("me4", "hackers")), "unauthenticated+me+hackers");
  117. // case insensitive
  118. Assert.IsTrue (rg.ContainsUser (GetPrincipal ("me5", "MoNo")), "case+me+mono");
  119. Assert.IsTrue (rg.ContainsUser (GetPrincipal ("me6", "hAcKeRs")), "case+me+hackers");
  120. }
  121. [Test]
  122. public void ContainsUser_Out ()
  123. {
  124. RoleGroup rg = new RoleGroup ();
  125. rg.Roles = new string[2] { "mono", "hackers" };
  126. Assert.IsFalse (rg.ContainsUser (GetPrincipal ("me", "m0n0")), "m0n0");
  127. Assert.IsFalse (rg.ContainsUser (GetPrincipal ("me", "h4ck")), "h4ck");
  128. }
  129. }
  130. }
  131. #endif