Changeset 3955
- Timestamp:
- 06/26/10 10:13:12 (15 years ago)
- 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 11 11 /// <returns>database connection (could be null)</returns> 12 12 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"); 21 14 } 22 15 -
branches/HeuristicLab.Services.Authentication Prototype/Persistence/Persistence.csproj
r3941 r3955 50 50 </ItemGroup> 51 51 <ItemGroup> 52 <Compile Include="HeuristicLabUserRole.cs" /> 52 53 <Compile Include="DatabaseUtil.cs" /> 53 54 <Compile Include="DataClasses.designer.cs"> -
branches/HeuristicLab.Services.Authentication Prototype/Service/Provider/HeuristicLabRoleProvider.cs
r3954 r3955 8 8 namespace Service.Provider { 9 9 class HeuristicLabRoleProvider : RoleProvider { 10 protected string applicationName; 11 10 12 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 } 12 29 } 13 30 14 31 public override string ApplicationName { 15 32 get { 16 throw new NotImplementedException();33 return this.applicationName; 17 34 } 18 35 set { 19 th row new NotImplementedException();36 this.applicationName = value; 20 37 } 21 38 } … … 27 44 28 45 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 } 35 54 } 36 55 … … 44 63 if (roleName != null && db.HeuristicLabRole.Count(r => r.RoleName == roleName) == 1) { 45 64 HeuristicLabRole role = db.HeuristicLabRole.Single(r => r.RoleName == roleName); 46 foreach(HeuristicLabUserRole userRole in role.HeuristicLabUserRole) 47 { 65 foreach (HeuristicLabUserRole userRole in role.HeuristicLabUserRole) { 48 66 if (userRole.HeuristicLabUser.UserName.Contains(usernameToMatch)) { 49 67 returnValue.Add(userRole.HeuristicLabUser.UserName); 50 68 } 51 69 } 52 70 } 53 71 db.Dispose(); 54 72 return returnValue.ToArray(); … … 59 77 60 78 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) { 63 80 roleList.Add(r.RoleName); 64 81 } … … 71 88 DataClassesDataContext context = DatabaseUtil.createDataClassesDataContext(); 72 89 if (username != null && context.HeuristicLabUsers.Count(u => u.UserName == username) > 0) { 73 90 74 91 Persistence.HeuristicLabUser user = context.HeuristicLabUsers.Single(u => u.UserName == username); 75 92 foreach (Persistence.HeuristicLabUserRole userRole in user.HeuristicLabUserRole) { -
branches/HeuristicLab.Services.Authentication Prototype/UnitTests/HeuristicLabRoleProviderTest.cs
r3953 r3955 2 2 using Microsoft.VisualStudio.TestTools.UnitTesting; 3 3 using System.Linq; 4 using System.Collections.Generic; 4 5 namespace UnitTests { 5 6 /// <summary> … … 60 61 [TestMethod()] 61 62 public void ApplicationNameTest() { 62 HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value63 string expected = string.Empty; // TODO: Initialize to an appropriate value63 HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); 64 string expected = "JavaIsEvenCooler"; 64 65 string actual; 65 66 target.ApplicationName = expected; 66 67 actual = target.ApplicationName; 67 68 Assert.AreEqual(expected, actual); 68 Assert.Inconclusive("Verify the correctness of this test method.");69 69 } 70 70 … … 143 143 [TestMethod()] 144 144 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); 151 161 } 152 162 … … 195 205 } 196 206 207 protected int getUserRolesCount() { 208 return db.HeuristicLabUserRole.Count(); 209 210 211 } 212 213 197 214 /// <summary> 198 215 ///A test for AddUsersToRoles … … 201 218 public void AddUsersToRolesTest() { 202 219 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 207 245 } 208 246
Note: See TracChangeset
for help on using the changeset viewer.