Changeset 3964


Ignore:
Timestamp:
06/26/10 13:11:44 (9 years ago)
Author:
mholper
Message:

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

Location:
branches/HeuristicLab.Services.Authentication Prototype
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Services.Authentication Prototype/Service/Provider/HeuristicLabRoleProvider.cs

    r3963 r3964  
    2424        }
    2525        db.SubmitChanges();
     26        db.Dispose();
    2627      }
    2728    }
     
    4950        db.HeuristicLabRole.InsertOnSubmit(role);
    5051        db.SubmitChanges();
     52        db.Dispose();
    5153      }
    5254    }
     
    7476      List<string> roleList = new List<string>();
    7577
    76       List<HeuristicLabRole> roles = new List<HeuristicLabRole>(DatabaseUtil.createDataClassesDataContext().HeuristicLabRole.Select(r => r));
     78      Persistence.DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext();
     79      List<HeuristicLabRole> roles = new List<HeuristicLabRole>(db.HeuristicLabRole.Select(r => r));
    7780      foreach (HeuristicLabRole r in roles) {
    7881        roleList.Add(r.RoleName);
    7982      }
     83      db.Dispose();
    8084
    8185      return roleList.ToArray();
     
    100104    }
    101105
    102 
    103     /// <summary>
    104     /// true if given username has exists in given Role, else false
    105     /// </summary>
    106     /// <param name="username"></param>
    107     /// <param name="roleName"></param>
    108     /// <returns>bool</returns>
    109106    public override bool IsUserInRole(string username, string roleName) {
    110         bool returnValue = false;
    111         DataClassesDataContext context = DatabaseUtil.createDataClassesDataContext();
    112         if (username != null && roleName != null)
    113         {
    114             returnValue = (context.HeuristicLabUserRole.Count(ur => ur.HeuristicLabRole.RoleName == roleName && ur.HeuristicLabUser.UserName == username) > 0);
    115         }
    116         return returnValue;
     107      bool returnValue = false;
     108      DataClassesDataContext context = DatabaseUtil.createDataClassesDataContext();
     109      if (username != null && roleName != null) {
     110        returnValue = (context.HeuristicLabUserRole.Count(ur => ur.HeuristicLabRole.RoleName == roleName && ur.HeuristicLabUser.UserName == username) > 0);
     111      }
     112      context.Dispose();
     113      return returnValue;
    117114    }
    118115
  • branches/HeuristicLab.Services.Authentication Prototype/UnitTests/HeuristicLabRoleProviderTest.cs

    r3963 r3964  
    101101    [TestMethod()]
    102102    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         {
    112             target.CreateRole(role);
    113         }
    114         foreach (Persistence.HeuristicLabUser user in users.Values)
    115         {
    116             db.HeuristicLabUsers.InsertOnSubmit(user);
    117         }
    118         db.SubmitChanges();
    119         string[] rolesToTest = new string[1];
    120         rolesToTest[0] = "admin";
    121         target.AddUsersToRoles(users.Keys.ToArray(), rolesToTest); // roles.ToArray());
    122         Assert.IsTrue(target.IsUserInRole("mholper", "admin"));
    123         Assert.IsFalse(target.IsUserInRole("mholper", "user"));
     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"));
    124122    }
    125123
     
    173171      Assert.IsTrue(rolesForUser.Contains("testRole1"));
    174172
    175      
    176      
     173
     174
    177175    }
    178176
     
    182180    [TestMethod()]
    183181    public void GetAllRolesTest() {
    184       HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); 
     182      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
    185183      List<string> roleNames = new List<string>();
    186184      roleNames.Add("Pascal");
     
    193191      target.CreateRole(null);
    194192      string[] roles = target.GetAllRoles();
    195       foreach(string role in roles)
    196       {
     193      foreach (string role in roles) {
    197194        Assert.IsTrue(roleNames.Remove(role));
    198195      }
     
    219216      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser3").Length == 0);
    220217
    221       target.AddUsersToRoles(new string[] { "dummyUser", "dummyUser2" }, new string[] {"testRole" });
     218      target.AddUsersToRoles(new string[] { "dummyUser", "dummyUser2" }, new string[] { "testRole" });
    222219      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser").Length == 2);
    223220      Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser2").Length == 1);
     
    247244    [TestMethod()]
    248245    public void CreateRoleTest() {
    249       HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); 
     246      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
    250247      target.CreateRole("role1");
    251248      target.CreateRole("role2");
     
    257254
    258255    protected int getUserRolesCount() {
    259       return db.HeuristicLabUserRole.Count();     
     256      return db.HeuristicLabUserRole.Count();
    260257    }
    261258
     
    279276      Assert.IsTrue(getUserRolesCount() == 0);
    280277
    281       foreach (string role in roles)
    282       {
     278      foreach (string role in roles) {
    283279        target.CreateRole(role);
    284280      }
Note: See TracChangeset for help on using the changeset viewer.