Free cookie consent management tool by TermsFeed Policy Generator

Changeset 3955


Ignore:
Timestamp:
06/26/10 10:13:12 (14 years ago)
Author:
bfarka
Message:

created impl and test of AddUsersToRoles (#1046)

Location:
branches/HeuristicLab.Services.Authentication Prototype
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Services.Authentication Prototype/Persistence/DatabaseUtil.cs

    r3950 r3955  
    1111    /// <returns>database connection (could be null)</returns>
    1212    public static DataClassesDataContext createDataClassesDataContext() {
    13       DataClassesDataContext db;
    14       try {
    15         db = new Persistence.DataClassesDataContext(@"C:\TEMP\HeuristicLabTest.mdf");
    16       }
    17       catch (Exception) {
    18         return null;
    19       }
    20       return db;
     13        return  new Persistence.DataClassesDataContext(@"C:\TEMP\HeuristicLabTest.mdf");
    2114    }
    2215
  • branches/HeuristicLab.Services.Authentication Prototype/Persistence/Persistence.csproj

    r3941 r3955  
    5050  </ItemGroup>
    5151  <ItemGroup>
     52    <Compile Include="HeuristicLabUserRole.cs" />
    5253    <Compile Include="DatabaseUtil.cs" />
    5354    <Compile Include="DataClasses.designer.cs">
  • branches/HeuristicLab.Services.Authentication Prototype/Service/Provider/HeuristicLabRoleProvider.cs

    r3954 r3955  
    88namespace Service.Provider {
    99  class HeuristicLabRoleProvider : RoleProvider {
     10    protected string applicationName;
     11
    1012    public override void AddUsersToRoles(string[] usernames, string[] roleNames) {
    11       throw new NotImplementedException();
     13      if (usernames != null && roleNames != null) {
     14        Persistence.DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext();
     15        List<HeuristicLabUser> users = db.HeuristicLabUsers.Where(u => usernames.Contains(u.UserName)).ToList<HeuristicLabUser>();
     16        List<HeuristicLabRole> roles = db.HeuristicLabRole.Where(r => roleNames.Contains(r.RoleName)).ToList<HeuristicLabRole>();
     17        foreach (HeuristicLabUser user in users) {
     18          foreach (HeuristicLabRole role in roles) {
     19            HeuristicLabUserRole userRole = new HeuristicLabUserRole();
     20            userRole.HeuristicLabRole = role;
     21            userRole.HeuristicLabUser = user;
     22
     23            db.HeuristicLabUserRole.InsertOnSubmit(userRole);
     24
     25          }
     26        }
     27        db.SubmitChanges();
     28      }
    1229    }
    1330
    1431    public override string ApplicationName {
    1532      get {
    16         throw new NotImplementedException();
     33        return this.applicationName;
    1734      }
    1835      set {
    19         throw new NotImplementedException();
     36        this.applicationName = value;
    2037      }
    2138    }
     
    2744
    2845    public virtual void CreateRole(String roleName, bool isPermission) {
    29       Persistence.DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext();
    30       HeuristicLabRole role = new HeuristicLabRole();
    31       role.RoleName = roleName;
    32       role.IsPermission = isPermission;
    33       db.HeuristicLabRole.InsertOnSubmit(role);
    34       db.SubmitChanges();
     46      if (roleName != null) {
     47        Persistence.DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext();
     48        HeuristicLabRole role = new HeuristicLabRole();
     49        role.RoleName = roleName;
     50        role.IsPermission = isPermission;
     51        db.HeuristicLabRole.InsertOnSubmit(role);
     52        db.SubmitChanges();
     53      }
    3554    }
    3655
     
    4463      if (roleName != null && db.HeuristicLabRole.Count(r => r.RoleName == roleName) == 1) {
    4564        HeuristicLabRole role = db.HeuristicLabRole.Single(r => r.RoleName == roleName);
    46         foreach(HeuristicLabUserRole userRole in role.HeuristicLabUserRole)
    47         {
     65        foreach (HeuristicLabUserRole userRole in role.HeuristicLabUserRole) {
    4866          if (userRole.HeuristicLabUser.UserName.Contains(usernameToMatch)) {
    4967            returnValue.Add(userRole.HeuristicLabUser.UserName);
    5068          }
    5169        }
    52        }
     70      }
    5371      db.Dispose();
    5472      return returnValue.ToArray();
     
    5977
    6078      List<HeuristicLabRole> roles = new List<HeuristicLabRole>(DatabaseUtil.createDataClassesDataContext().HeuristicLabRole.Select(r => r));
    61       foreach(HeuristicLabRole r in roles)
    62       {
     79      foreach (HeuristicLabRole r in roles) {
    6380        roleList.Add(r.RoleName);
    6481      }
     
    7188      DataClassesDataContext context = DatabaseUtil.createDataClassesDataContext();
    7289      if (username != null && context.HeuristicLabUsers.Count(u => u.UserName == username) > 0) {
    73        
     90
    7491        Persistence.HeuristicLabUser user = context.HeuristicLabUsers.Single(u => u.UserName == username);
    7592        foreach (Persistence.HeuristicLabUserRole userRole in user.HeuristicLabUserRole) {
  • branches/HeuristicLab.Services.Authentication Prototype/UnitTests/HeuristicLabRoleProviderTest.cs

    r3953 r3955  
    22using Microsoft.VisualStudio.TestTools.UnitTesting;
    33using System.Linq;
     4using System.Collections.Generic;
    45namespace UnitTests {
    56  /// <summary>
     
    6061    [TestMethod()]
    6162    public void ApplicationNameTest() {
    62       HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
    63       string expected = string.Empty; // TODO: Initialize to an appropriate value
     63      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
     64      string expected = "JavaIsEvenCooler";
    6465      string actual;
    6566      target.ApplicationName = expected;
    6667      actual = target.ApplicationName;
    6768      Assert.AreEqual(expected, actual);
    68       Assert.Inconclusive("Verify the correctness of this test method.");
    6969    }
    7070
     
    143143    [TestMethod()]
    144144    public void GetAllRolesTest() {
    145       HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
    146       string[] expected = null; // TODO: Initialize to an appropriate value
    147       string[] actual;
    148       actual = target.GetAllRoles();
    149       Assert.AreEqual(expected, actual);
    150       Assert.Inconclusive("Verify the correctness of this test method.");
     145      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
     146      List<string> roleNames = new List<string>();
     147      roleNames.Add("Pascal");
     148      roleNames.Add("Java");
     149      roleNames.Add("Pascal");
     150      roleNames.Add("VisalBasic");
     151      foreach (string s in roleNames) {
     152        target.CreateRole(s);
     153      }
     154      target.CreateRole(null);
     155      string[] roles = target.GetAllRoles();
     156      foreach(string role in roles)
     157      {
     158        Assert.IsTrue(roleNames.Remove(role));
     159      }
     160      Assert.IsTrue(roleNames.Count == 0);
    151161    }
    152162
     
    195205    }
    196206
     207    protected int getUserRolesCount() {
     208      return db.HeuristicLabUserRole.Count();
     209           
     210     
     211    }
     212
     213
    197214    /// <summary>
    198215    ///A test for AddUsersToRoles
     
    201218    public void AddUsersToRolesTest() {
    202219      HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
    203       string[] usernames = null; // TODO: Initialize to an appropriate value
    204       string[] roleNames = null; // TODO: Initialize to an appropriate value
    205       target.AddUsersToRoles(usernames, roleNames);
    206       Assert.Inconclusive("A method that does not return a value cannot be verified.");
     220      Dictionary<string, Persistence.HeuristicLabUser> users = new Dictionary<string, Persistence.HeuristicLabUser>();
     221      List<string> roles = new List<string>();
     222      users.Add("dkhan", new Persistence.HeuristicLabUser("dkhan", "foo", "password", "comment"));
     223      users.Add("hmayr", new Persistence.HeuristicLabUser("hmayr", "foo", "password", "comment"));
     224      users.Add("bfarka", new Persistence.HeuristicLabUser("bfarka", "foo", "password", "comment"));
     225
     226      roles.Add("admin");
     227      roles.Add("users");
     228      //testing to create roles with users that doesn't exists
     229      target.AddUsersToRoles(users.Keys.ToArray(), roles.ToArray());
     230      Assert.IsTrue(getUserRolesCount() == 0);
     231
     232      foreach (string role in roles)
     233      {
     234        target.CreateRole(role);
     235      }
     236      target.AddUsersToRoles(users.Keys.ToArray(), roles.ToArray());
     237      Assert.IsTrue(getUserRolesCount() == 0);
     238      foreach (Persistence.HeuristicLabUser user in users.Values) {
     239        db.HeuristicLabUsers.InsertOnSubmit(user);
     240      }
     241      db.SubmitChanges();
     242      target.AddUsersToRoles(users.Keys.ToArray(), roles.ToArray());
     243      Assert.IsTrue(getUserRolesCount() == (roles.Count + users.Count));
     244
    207245    }
    208246
Note: See TracChangeset for help on using the changeset viewer.