Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4024 was 4024, checked in by bfarka, 14 years ago

last bugfixes for project #1046

File size: 15.6 KB
Line 
1using Service.Provider;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3using System.Linq;
4using System.Collections.Generic;
5using System.Data.SqlClient;
6namespace UnitTests {
7  /// <summary>
8  ///This is a test class for HeuristicLabRoleProviderTest and is intended
9  ///to contain all HeuristicLabRoleProviderTest Unit Tests
10  ///</summary>
11  [TestClass()]
12  public class HeuristicLabRoleProviderTest : AbstractHeuristicLabTest {
13    private TestContext testContextInstance;
14    private const string TEST_ROLE_NAME = "testRole";
15    private const string TEST_USER_NAME = "testUser";
16    /// <summary>
17    ///Gets or sets the test context which provides
18    ///information about and functionality for the current test run.
19    ///</summary>
20    public TestContext TestContext {
21      get {
22        return testContextInstance;
23      }
24      set {
25        testContextInstance = value;
26      }
27    }
28
29    #region Additional test attributes
30    //
31    //You can use the following additional attributes as you write your tests:
32    //
33    //Use ClassInitialize to run code before running the first test in the class
34    //[ClassInitialize()]
35    //public static void MyClassInitialize(TestContext testContext)
36    //{
37    //}
38    //
39    //Use ClassCleanup to run code after all tests in a class have run
40    //[ClassCleanup()]
41    //public static void MyClassCleanup()
42    //{
43    //}
44    //
45    //Use TestInitialize to run code before running each test
46    //[TestInitialize()]
47    //public void MyTestInitialize()
48    //{
49    //}
50    //
51    //Use TestCleanup to run code after each test has run
52    //[TestCleanup()]
53    //public void MyTestCleanup()
54    //{
55    //}
56    //
57    #endregion
58
59
60    /// <summary>
61    ///A test for ApplicationName
62    ///</summary>
63    [TestMethod()]
64    public void ApplicationNameTest() {
65      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
66      string expected = "JavaIsEvenCooler";
67      string actual;
68      target.ApplicationName = expected;
69      actual = target.ApplicationName;
70      Assert.AreEqual(expected, actual);
71    }
72
73    /// <summary>
74    ///tests if the RoleExits method works --> test is done in a positiv and negativ way
75    ///</summary>
76    [TestMethod()]
77    public void RoleExistsTest() {
78      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
79      Persistence.HeuristicLabRole role = new Persistence.HeuristicLabRole();
80      role.RoleName = TEST_ROLE_NAME;
81      db.HeuristicLabRole.InsertOnSubmit((Persistence.HeuristicLabRole)role);
82      db.SubmitChanges();
83      Assert.IsTrue(target.RoleExists(TEST_ROLE_NAME));
84      Assert.IsFalse(target.RoleExists(TEST_ROLE_NAME + TEST_ROLE_NAME));
85    }
86
87    /// <summary>
88    ///A test for RemoveUsersFromRoles
89    ///</summary>
90    [TestMethod()]
91    public void RemoveUsersFromRolesTest() {
92
93      //creates users dkhan,hmayer,bfarka (with AddUsersToRolesTest())
94      //and add all these users to groups admin and users
95      AddUsersToRolesTest();
96      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
97      string[] users = new string[2];
98      string[] roles = new string[1];
99
100      //remove hmayer from role admin
101      users[0] = "hmayr";
102      roles[0] = "admin";
103
104
105      //before removing, check if all three users exits correctly in admin group
106      Assert.IsTrue((target.GetRolesForUser("hmayr").ToList()).Contains(roles[0]));
107      Assert.IsTrue((target.GetRolesForUser("dkhan").ToList()).Contains(roles[0]));
108      Assert.IsTrue((target.GetRolesForUser("bfarka").ToList()).Contains(roles[0]));
109
110      //remmove hmayr from  admin group
111      target.RemoveUsersFromRoles(users, roles);
112
113      //final check
114      Assert.IsTrue((target.GetRolesForUser("dkhan").ToList()).Contains(roles[0]));
115      Assert.IsTrue((target.GetRolesForUser("bfarka").ToList()).Contains(roles[0]));
116      Assert.IsFalse((target.GetRolesForUser("hmayr").ToList()).Contains(roles[0]));
117
118    }
119
120    /// <summary>
121    /// test if user is in Role (positive and negative Assertion)
122    ///</summary>
123    [TestMethod()]
124    public void IsUserInRoleTest() {
125      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
126      Dictionary<string, Persistence.HeuristicLabUser> users = new Dictionary<string, Persistence.HeuristicLabUser>();
127      List<string> roles = new List<string>();
128      users.Add("mholper", new Persistence.HeuristicLabUser("mholper", "foo", "password", "comment"));
129
130      roles.Add("admin");
131      roles.Add("users");
132      foreach (string role in roles) {
133        target.CreateRole(role);
134      }
135      foreach (Persistence.HeuristicLabUser user in users.Values) {
136        db.HeuristicLabUsers.InsertOnSubmit(user);
137      }
138      db.SubmitChanges();
139      string[] rolesToTest = new string[1];
140      rolesToTest[0] = "admin";
141      target.AddUsersToRoles(users.Keys.ToArray(), rolesToTest); // roles.ToArray());
142      Assert.IsTrue(target.IsUserInRole("mholper", "admin"));
143      Assert.IsFalse(target.IsUserInRole("mholper", "user"));
144
145      target.AddChildRole("admin", "users");
146      Assert.IsTrue(target.IsUserInRole("mholper", "users"));
147    }
148
149    /// <summary>
150    ///A test for GetUsersInRole
151    ///</summary>
152    [TestMethod()]
153    public void GetUsersInRoleTest() {
154      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
155      string dummyRole = "dummyRole";
156      Assert.IsTrue(target.GetUsersInRole(null).Length == 0);
157      Assert.IsTrue(target.GetUsersInRole(dummyRole).Length == 0);
158      Persistence.HeuristicLabUser user = new Persistence.HeuristicLabUser("dummyUser", "foo1", "foo", "foo");
159      Persistence.HeuristicLabUser user2 = new Persistence.HeuristicLabUser("dummyUser2", "foo2", "foo", "foo");
160      db.HeuristicLabUsers.InsertOnSubmit(user);
161      db.HeuristicLabUsers.InsertOnSubmit(user2);
162      db.SubmitChanges();
163      target.CreateRole("testRole1");
164      target.CreateRole("testRole2");
165      List<string> users = new List<string>();
166      List<string> roles = new List<string>();
167      roles.Add("testRole1");
168      roles.Add("testRole2");
169      users.Add("dummyUser");
170
171      //--> dummyUser get Role testRole1+testRole2
172      target.AddUsersToRoles(users.ToArray(), roles.ToArray());
173      string[] usersForRole = target.GetUsersInRole("testRole2");
174      Assert.IsTrue(usersForRole.Length == 1);
175      Assert.IsTrue(usersForRole.Contains("dummyUser"));
176
177      //--> dummyUser2 get onl Role testRole1
178      roles.Remove("testRole2");
179      users.Remove("dummyUser");
180      users.Add("dummyUser2");
181      target.AddUsersToRoles(users.ToArray(), roles.ToArray());
182
183      usersForRole = target.GetUsersInRole("testRole1");
184      Assert.IsTrue(usersForRole.Length == 2);
185      Assert.IsTrue(usersForRole.Contains("dummyUser"));
186      Assert.IsTrue(usersForRole.Contains("dummyUser2"));
187
188    }
189
190    /// <summary>
191    ///A test for GetRolesForUser
192    ///</summary>
193    [TestMethod()]
194    public void GetRolesForUserTest() {
195      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
196      string dummyUser = "dummyUser";
197      Assert.IsTrue(target.GetRolesForUser(null).Length == 0);
198      Assert.IsTrue(target.GetRolesForUser(dummyUser).Length == 0);
199      Persistence.HeuristicLabUser user = new Persistence.HeuristicLabUser("dummyUser", "foo", "foo", "foo");
200      Persistence.HeuristicLabUser user2 = new Persistence.HeuristicLabUser("dummyUser2", "foo2", "foo", "foo");
201      db.HeuristicLabUsers.InsertOnSubmit(user);
202      db.HeuristicLabUsers.InsertOnSubmit(user2);
203      db.SubmitChanges();
204      target.CreateRole("testRole1");
205      target.CreateRole("testRole2");
206      List<string> users = new List<string>();
207      List<string> roles = new List<string>();
208      users.Add("dummyUser");
209      users.Add("dummyUser2");
210      roles.Add("testRole1");
211      target.AddUsersToRoles(users.ToArray(), roles.ToArray());
212      users.Remove("dummyUser2");
213      roles.Add("testRole2");
214      roles.Remove("testRole1");
215      target.AddUsersToRoles(users.ToArray(), roles.ToArray());
216      string[] rolesForUser = target.GetRolesForUser("dummyUser");
217      Assert.IsTrue(rolesForUser.Length == 2);
218      Assert.IsTrue(rolesForUser.Contains("testRole1"));
219      Assert.IsTrue(rolesForUser.Contains("testRole2"));
220
221      rolesForUser = target.GetRolesForUser("dummyUser2");
222      Assert.IsTrue(rolesForUser.Length == 1);
223      Assert.IsTrue(rolesForUser.Contains("testRole1"));
224
225
226
227    }
228
229    /// <summary>
230    ///A test for GetAllRoles
231    ///</summary>
232    [TestMethod()]
233    public void GetAllRolesTest() {
234      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
235      List<string> roleNames = new List<string>();
236      roleNames.Add("Pascal");
237      roleNames.Add("Java");
238      roleNames.Add("c++");
239      roleNames.Add("VisalBasic");
240      foreach (string s in roleNames) {
241        target.CreateRole(s);
242      }
243      target.CreateRole(null);
244      string[] roles = target.GetAllRoles();
245      foreach (string role in roles) {
246        Assert.IsTrue(roleNames.Remove(role));
247      }
248      Assert.IsTrue(roleNames.Count == 0);
249    }
250
251    /// <summary>
252    ///A test for FindUsersInRole
253    ///</summary>
254    [TestMethod()]
255    public void FindUsersInRoleTest() {
256      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
257      Assert.IsTrue(target.FindUsersInRole(null, null).Length == 0);
258      Assert.IsTrue(target.FindUsersInRole("dummyRole", null).Length == 0);
259      Assert.IsTrue(target.FindUsersInRole(null, "dummyUser").Length == 0);
260      Persistence.HeuristicLabUser user = new Persistence.HeuristicLabUser("dummyUser", "foo", "foo", "foo");
261      Persistence.HeuristicLabUser user2 = new Persistence.HeuristicLabUser("dummyUser2", "foo2", "foo", "foo");
262      db.HeuristicLabUsers.InsertOnSubmit(user);
263      db.HeuristicLabUsers.InsertOnSubmit(user2);
264      db.SubmitChanges();
265      target.CreateRole("testRole");
266      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser").Length == 0);
267      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser2").Length == 0);
268      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser3").Length == 0);
269
270      target.AddUsersToRoles(new string[] { "dummyUser", "dummyUser2" }, new string[] { "testRole" });
271      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser").Length == 2);
272      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser2").Length == 1);
273      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser3").Length == 0);
274
275
276    }
277
278    /// <summary>
279    ///A test for DeleteRole
280    ///</summary>
281    [TestMethod()]
282    public void DeleteRoleTest() {
283
284      //creates users dkhan,hmayer,bfarka (with AddUsersToRolesTest())
285      //and add all these users to groups admin and users
286      AddUsersToRolesTest();
287      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
288      string[] users = new string[2];
289      string[] roles = new string[1];
290
291      //remove hmayer from role admin
292      users[0] = "hmayr";
293      roles[0] = "admin";
294
295      //before removing, check if all three users exits correctly in admin group
296      Assert.IsTrue((target.GetRolesForUser("hmayr").ToList()).Contains(roles[0]));
297      Assert.IsTrue((target.GetRolesForUser("dkhan").ToList()).Contains(roles[0]));
298      Assert.IsTrue((target.GetRolesForUser("bfarka").ToList()).Contains(roles[0]));
299
300      //try to delete Role admin (with user hmayr from  admin group exists)
301      Assert.IsFalse(target.DeleteRole(roles[0], true));
302
303      Assert.IsTrue(target.IsUserInRole(users[0], roles[0]));
304
305      //final populateRole if false
306      //try to delete Role admin (with user hmayr from  admin group exists)
307      Assert.IsTrue(target.DeleteRole(roles[0], false));
308      Assert.IsFalse(target.IsUserInRole(users[0], roles[0]));
309      Assert.IsFalse((target.GetRolesForUser("hmayr").ToList()).Contains(roles[0]));
310    }
311
312    /// <summary>
313    ///A test for CreateRole
314    ///</summary>
315    [TestMethod()]
316    public void CreateRoleTest() {
317      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
318      target.CreateRole("role1");
319      target.CreateRole("role2");
320      target.CreateRole("role3", true);
321      Assert.IsTrue(db.HeuristicLabRole.Count(r => r.RoleName == "role1" && r.IsPermission == false) == 1);
322      Assert.IsTrue(db.HeuristicLabRole.Count(r => r.RoleName == "role2" && r.IsPermission == false) == 1);
323      Assert.IsTrue(db.HeuristicLabRole.Count(r => r.RoleName == "role3" && r.IsPermission == true) == 1);
324    }
325
326    protected int getUserRolesCount() {
327      return db.HeuristicLabUserRole.Count();
328    }
329
330
331    /// <summary>
332    ///A test for AddUsersToRoles
333    ///</summary>
334    [TestMethod()]
335    public void AddUsersToRolesTest() {
336      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
337      Dictionary<string, Persistence.HeuristicLabUser> users = new Dictionary<string, Persistence.HeuristicLabUser>();
338      List<string> roles = new List<string>();
339      users.Add("dkhan", new Persistence.HeuristicLabUser("dkhan", "foo1", "password", "comment"));
340      users.Add("hmayr", new Persistence.HeuristicLabUser("hmayr", "foo2", "password", "comment"));
341      users.Add("bfarka", new Persistence.HeuristicLabUser("bfarka", "foo3", "password", "comment"));
342
343      roles.Add("admin");
344      roles.Add("users");
345      //testing to create roles with users that doesn't exists
346      target.AddUsersToRoles(users.Keys.ToArray(), roles.ToArray());
347      Assert.IsTrue(getUserRolesCount() == 0);
348
349      foreach (string role in roles) {
350        target.CreateRole(role);
351      }
352      target.AddUsersToRoles(users.Keys.ToArray(), roles.ToArray());
353      Assert.IsTrue(getUserRolesCount() == 0);
354      foreach (Persistence.HeuristicLabUser user in users.Values) {
355        db.HeuristicLabUsers.InsertOnSubmit(user);
356      }
357      db.SubmitChanges();
358      target.AddUsersToRoles(users.Keys.ToArray(), roles.ToArray());
359      Assert.IsTrue(getUserRolesCount() == (roles.Count * users.Count));
360
361    }
362
363    /// <summary>
364    ///A test for HeuristicLabRoleProvider Constructor
365    ///</summary>
366    [TestMethod()]
367    public void HeuristicLabRoleProviderConstructorTest() {
368      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
369      Assert.IsNotNull(target);
370    }
371    [TestMethod()]
372    public void NoDuplicateRoleTest() {
373      try {
374        Persistence.HeuristicLabRole role = new Persistence.HeuristicLabRole();
375        role.RoleName = "role1";
376        Persistence.HeuristicLabRole role1 = new Persistence.HeuristicLabRole();
377        role1.RoleName = "role1";
378        db.HeuristicLabRole.InsertOnSubmit(role);
379        db.HeuristicLabRole.InsertOnSubmit(role1);
380        db.SubmitChanges();
381        Assert.Fail();
382      }
383      catch (SqlException) {
384        //swallowing Exception because it is expected that a SQL Exception is thrown       
385      }
386    }
387
388    [TestMethod()]
389    public void AddChildRoleTest() {
390      HeuristicLabRoleProvider roleProvider = new HeuristicLabRoleProvider();
391      roleProvider.CreateRole("parent");
392      roleProvider.CreateRole("child");
393      roleProvider.AddChildRole("parent", "child");
394      Persistence.HeuristicLabRole parent = db.HeuristicLabRole.Single(x => x.RoleName == "parent");
395      Persistence.HeuristicLabRole child = parent.HeuristicLabRoleChilds.ToList().ElementAt(0);
396      Assert.AreEqual(child.RoleName, "child");
397
398    }
399
400  }
401}
402
Note: See TracBrowser for help on using the repository browser.