Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Services.Authentication Prototype/UnitTests/HeuristicLabRoleProviderTest.cs @ 3964

Last change on this file since 3964 was 3964, checked in by mholper, 14 years ago

formated with vssettings, added missing .Dispose() to following HeuristicLabRoleProvider Methods: GetAllRolesTest, IsUserInRole, CreateRole,GetAllRoles (#1046)

File size: 11.8 KB
Line 
1using Service.Provider;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3using System.Linq;
4using System.Collections.Generic;
5namespace UnitTests {
6  /// <summary>
7  ///This is a test class for HeuristicLabRoleProviderTest and is intended
8  ///to contain all HeuristicLabRoleProviderTest Unit Tests
9  ///</summary>
10  [TestClass()]
11  public class HeuristicLabRoleProviderTest : AbstractHeuristicLabTest {
12    private TestContext testContextInstance;
13    private const string TEST_ROLE_NAME = "testRole";
14    private const string TEST_USER_NAME = "testUser";
15    /// <summary>
16    ///Gets or sets the test context which provides
17    ///information about and functionality for the current test run.
18    ///</summary>
19    public TestContext TestContext {
20      get {
21        return testContextInstance;
22      }
23      set {
24        testContextInstance = value;
25      }
26    }
27
28    #region Additional test attributes
29    //
30    //You can use the following additional attributes as you write your tests:
31    //
32    //Use ClassInitialize to run code before running the first test in the class
33    //[ClassInitialize()]
34    //public static void MyClassInitialize(TestContext testContext)
35    //{
36    //}
37    //
38    //Use ClassCleanup to run code after all tests in a class have run
39    //[ClassCleanup()]
40    //public static void MyClassCleanup()
41    //{
42    //}
43    //
44    //Use TestInitialize to run code before running each test
45    //[TestInitialize()]
46    //public void MyTestInitialize()
47    //{
48    //}
49    //
50    //Use TestCleanup to run code after each test has run
51    //[TestCleanup()]
52    //public void MyTestCleanup()
53    //{
54    //}
55    //
56    #endregion
57
58
59    /// <summary>
60    ///A test for ApplicationName
61    ///</summary>
62    [TestMethod()]
63    public void ApplicationNameTest() {
64      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
65      string expected = "JavaIsEvenCooler";
66      string actual;
67      target.ApplicationName = expected;
68      actual = target.ApplicationName;
69      Assert.AreEqual(expected, actual);
70    }
71
72    /// <summary>
73    ///tests if the RoleExits method works --> test is done in a positiv and negativ way
74    ///</summary>
75    [TestMethod()]
76    public void RoleExistsTest() {
77      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
78      Persistence.HeuristicLabRole role = new Persistence.HeuristicLabRole();
79      role.RoleName = TEST_ROLE_NAME;
80      db.HeuristicLabRole.InsertOnSubmit((Persistence.HeuristicLabRole)role);
81      db.SubmitChanges();
82      Assert.IsTrue(target.RoleExists(TEST_ROLE_NAME));
83      Assert.IsFalse(target.RoleExists(TEST_ROLE_NAME + TEST_ROLE_NAME));
84    }
85
86    /// <summary>
87    ///A test for RemoveUsersFromRoles
88    ///</summary>
89    [TestMethod()]
90    public void RemoveUsersFromRolesTest() {
91      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
92      string[] usernames = null; // TODO: Initialize to an appropriate value
93      string[] roleNames = null; // TODO: Initialize to an appropriate value
94      target.RemoveUsersFromRoles(usernames, roleNames);
95      Assert.Inconclusive("A method that does not return a value cannot be verified.");
96    }
97
98    /// <summary>
99    /// test if user is in Role (positive and negative Assertion)
100    ///</summary>
101    [TestMethod()]
102    public void IsUserInRoleTest() {
103      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
104      Dictionary<string, Persistence.HeuristicLabUser> users = new Dictionary<string, Persistence.HeuristicLabUser>();
105      List<string> roles = new List<string>();
106      users.Add("mholper", new Persistence.HeuristicLabUser("mholper", "foo", "password", "comment"));
107
108      roles.Add("admin");
109      roles.Add("users");
110      foreach (string role in roles) {
111        target.CreateRole(role);
112      }
113      foreach (Persistence.HeuristicLabUser user in users.Values) {
114        db.HeuristicLabUsers.InsertOnSubmit(user);
115      }
116      db.SubmitChanges();
117      string[] rolesToTest = new string[1];
118      rolesToTest[0] = "admin";
119      target.AddUsersToRoles(users.Keys.ToArray(), rolesToTest); // roles.ToArray());
120      Assert.IsTrue(target.IsUserInRole("mholper", "admin"));
121      Assert.IsFalse(target.IsUserInRole("mholper", "user"));
122    }
123
124    /// <summary>
125    ///A test for GetUsersInRole
126    ///</summary>
127    [TestMethod()]
128    public void GetUsersInRoleTest() {
129      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
130      string roleName = string.Empty; // TODO: Initialize to an appropriate value
131      string[] expected = null; // TODO: Initialize to an appropriate value
132      string[] actual;
133      actual = target.GetUsersInRole(roleName);
134      Assert.AreEqual(expected, actual);
135      Assert.Inconclusive("Verify the correctness of this test method.");
136    }
137
138    /// <summary>
139    ///A test for GetRolesForUser
140    ///</summary>
141    [TestMethod()]
142    public void GetRolesForUserTest() {
143      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
144      string dummyUser = "dummyUser";
145      Assert.IsTrue(target.GetRolesForUser(null).Length == 0);
146      Assert.IsTrue(target.GetRolesForUser(dummyUser).Length == 0);
147      Persistence.HeuristicLabUser user = new Persistence.HeuristicLabUser("dummyUser", "foo", "foo", "foo");
148      Persistence.HeuristicLabUser user2 = new Persistence.HeuristicLabUser("dummyUser2", "foo", "foo", "foo");
149      db.HeuristicLabUsers.InsertOnSubmit(user);
150      db.HeuristicLabUsers.InsertOnSubmit(user2);
151      db.SubmitChanges();
152      target.CreateRole("testRole1");
153      target.CreateRole("testRole2");
154      List<string> users = new List<string>();
155      List<string> roles = new List<string>();
156      users.Add("dummyUser");
157      users.Add("dummyUser2");
158      roles.Add("testRole1");
159      target.AddUsersToRoles(users.ToArray(), roles.ToArray());
160      users.Remove("dummyUser2");
161      roles.Add("testRole2");
162      roles.Remove("testRole1");
163      target.AddUsersToRoles(users.ToArray(), roles.ToArray());
164      string[] rolesForUser = target.GetRolesForUser("dummyUser");
165      Assert.IsTrue(rolesForUser.Length == 2);
166      Assert.IsTrue(rolesForUser.Contains("testRole1"));
167      Assert.IsTrue(rolesForUser.Contains("testRole2"));
168
169      rolesForUser = target.GetRolesForUser("dummyUser2");
170      Assert.IsTrue(rolesForUser.Length == 1);
171      Assert.IsTrue(rolesForUser.Contains("testRole1"));
172
173
174
175    }
176
177    /// <summary>
178    ///A test for GetAllRoles
179    ///</summary>
180    [TestMethod()]
181    public void GetAllRolesTest() {
182      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
183      List<string> roleNames = new List<string>();
184      roleNames.Add("Pascal");
185      roleNames.Add("Java");
186      roleNames.Add("Pascal");
187      roleNames.Add("VisalBasic");
188      foreach (string s in roleNames) {
189        target.CreateRole(s);
190      }
191      target.CreateRole(null);
192      string[] roles = target.GetAllRoles();
193      foreach (string role in roles) {
194        Assert.IsTrue(roleNames.Remove(role));
195      }
196      Assert.IsTrue(roleNames.Count == 0);
197    }
198
199    /// <summary>
200    ///A test for FindUsersInRole
201    ///</summary>
202    [TestMethod()]
203    public void FindUsersInRoleTest() {
204      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
205      Assert.IsTrue(target.FindUsersInRole(null, null).Length == 0);
206      Assert.IsTrue(target.FindUsersInRole("dummyRole", null).Length == 0);
207      Assert.IsTrue(target.FindUsersInRole(null, "dummyUser").Length == 0);
208      Persistence.HeuristicLabUser user = new Persistence.HeuristicLabUser("dummyUser", "foo", "foo", "foo");
209      Persistence.HeuristicLabUser user2 = new Persistence.HeuristicLabUser("dummyUser2", "foo", "foo", "foo");
210      db.HeuristicLabUsers.InsertOnSubmit(user);
211      db.HeuristicLabUsers.InsertOnSubmit(user2);
212      db.SubmitChanges();
213      target.CreateRole("testRole");
214      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser").Length == 0);
215      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser2").Length == 0);
216      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser3").Length == 0);
217
218      target.AddUsersToRoles(new string[] { "dummyUser", "dummyUser2" }, new string[] { "testRole" });
219      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser").Length == 2);
220      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser2").Length == 1);
221      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser3").Length == 0);
222
223
224    }
225
226    /// <summary>
227    ///A test for DeleteRole
228    ///</summary>
229    [TestMethod()]
230    public void DeleteRoleTest() {
231      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
232      string roleName = string.Empty; // TODO: Initialize to an appropriate value
233      bool throwOnPopulatedRole = false; // TODO: Initialize to an appropriate value
234      bool expected = false; // TODO: Initialize to an appropriate value
235      bool actual;
236      actual = target.DeleteRole(roleName, throwOnPopulatedRole);
237      Assert.AreEqual(expected, actual);
238      Assert.Inconclusive("Verify the correctness of this test method.");
239    }
240
241    /// <summary>
242    ///A test for CreateRole
243    ///</summary>
244    [TestMethod()]
245    public void CreateRoleTest() {
246      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
247      target.CreateRole("role1");
248      target.CreateRole("role2");
249      target.CreateRole("role3", true);
250      Assert.IsTrue(db.HeuristicLabRole.Count(r => r.RoleName == "role1" && r.IsPermission == false) == 1);
251      Assert.IsTrue(db.HeuristicLabRole.Count(r => r.RoleName == "role2" && r.IsPermission == false) == 1);
252      Assert.IsTrue(db.HeuristicLabRole.Count(r => r.RoleName == "role3" && r.IsPermission == true) == 1);
253    }
254
255    protected int getUserRolesCount() {
256      return db.HeuristicLabUserRole.Count();
257    }
258
259
260    /// <summary>
261    ///A test for AddUsersToRoles
262    ///</summary>
263    [TestMethod()]
264    public void AddUsersToRolesTest() {
265      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
266      Dictionary<string, Persistence.HeuristicLabUser> users = new Dictionary<string, Persistence.HeuristicLabUser>();
267      List<string> roles = new List<string>();
268      users.Add("dkhan", new Persistence.HeuristicLabUser("dkhan", "foo", "password", "comment"));
269      users.Add("hmayr", new Persistence.HeuristicLabUser("hmayr", "foo", "password", "comment"));
270      users.Add("bfarka", new Persistence.HeuristicLabUser("bfarka", "foo", "password", "comment"));
271
272      roles.Add("admin");
273      roles.Add("users");
274      //testing to create roles with users that doesn't exists
275      target.AddUsersToRoles(users.Keys.ToArray(), roles.ToArray());
276      Assert.IsTrue(getUserRolesCount() == 0);
277
278      foreach (string role in roles) {
279        target.CreateRole(role);
280      }
281      target.AddUsersToRoles(users.Keys.ToArray(), roles.ToArray());
282      Assert.IsTrue(getUserRolesCount() == 0);
283      foreach (Persistence.HeuristicLabUser user in users.Values) {
284        db.HeuristicLabUsers.InsertOnSubmit(user);
285      }
286      db.SubmitChanges();
287      target.AddUsersToRoles(users.Keys.ToArray(), roles.ToArray());
288      Assert.IsTrue(getUserRolesCount() == (roles.Count + users.Count));
289
290    }
291
292    /// <summary>
293    ///A test for HeuristicLabRoleProvider Constructor
294    ///</summary>
295    [TestMethod()]
296    public void HeuristicLabRoleProviderConstructorTest() {
297      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
298      Assert.IsNotNull(target);
299    }
300  }
301
302
303
304}
Note: See TracBrowser for help on using the repository browser.