Free cookie consent management tool by TermsFeed Policy Generator

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

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

implemented Method HeuristicLabRoleProvider.GetUsersInRoleTest and UnitTest (#1046)

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