Changeset 3956


Ignore:
Timestamp:
06/26/10 10:18:57 (9 years ago)
Author:
hmayr
Message:

following changes (#1046):

  • implemented some methods of HeuristicLabMembershipProvider and unit tests
  • tests for ChangePassword and ChangePasswordQuestionAndAnswer do not work at the moment!
Location:
branches/HeuristicLab.Services.Authentication Prototype
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Services.Authentication Prototype/HeuristicLab.Services.Authentication Prototyp.sln

    r3953 r3956  
    1111  ProjectSection(SolutionItems) = preProject
    1212    HeuristicLab.Services.Authentication Prototyp.vsmdi = HeuristicLab.Services.Authentication Prototyp.vsmdi
    13     HeuristicLab.Services.Authentication Prototyp1.vsmdi = HeuristicLab.Services.Authentication Prototyp1.vsmdi
     13    HeuristicLab.Services.Authentication Prototyp2.vsmdi = HeuristicLab.Services.Authentication Prototyp2.vsmdi
    1414    LocalTestRun.testrunconfig = LocalTestRun.testrunconfig
    1515  EndProjectSection
     
    1717Global
    1818  GlobalSection(TestCaseManagementSettings) = postSolution
    19     CategoryFile = HeuristicLab.Services.Authentication Prototyp1.vsmdi
     19    CategoryFile = HeuristicLab.Services.Authentication Prototyp2.vsmdi
    2020  EndGlobalSection
    2121  GlobalSection(SolutionConfigurationPlatforms) = preSolution
  • branches/HeuristicLab.Services.Authentication Prototype/Service/Provider/HeuristicLabMembershipProvider.cs

    r3948 r3956  
    1616
    1717    public override bool ChangePassword(string username, string oldPassword, string newPassword) {
    18       DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext();
    19       if (db == null) {
    20         return false;
    21       }
    22       try {
    23         HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
    24         if (u.ChangePassword(oldPassword, newPassword)) {
     18      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
     19        // check database connection
     20        if (db == null) {
     21          return false;
     22        }
     23        try {
     24          // try to get user
     25          HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
     26          if (u.ChangePassword(oldPassword, newPassword)) {
     27            // save user to database only if needed
     28            db.SubmitChanges();
     29            return true;
     30          } else {
     31            return false;
     32          }
     33        }
     34        catch (Exception) {
     35          return false;
     36        }
     37      }
     38    }
     39
     40    public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) {
     41      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
     42        // check database connection
     43        if (db == null) {
     44          return false;
     45        }
     46        try {
     47          // try to get user
     48          HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
     49          if (u.ChangePasswordQuestionAndAnswer(password, newPasswordQuestion, newPasswordAnswer)) {
     50            // save user to database only if needed
     51            db.SubmitChanges();
     52            return true;
     53          } else {
     54            return false;
     55          }
     56        }
     57        catch (Exception) {
     58          return false;
     59        }
     60      }
     61    }
     62
     63    public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) {
     64      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
     65        // check database connection
     66        if (db == null) {
     67          status = MembershipCreateStatus.ProviderError;
     68          return null;
     69        }
     70        try {
     71          // check for duplicate entries
     72          if (db.HeuristicLabUsers.Count(x => x.UserName == username) > 0) {
     73            status = MembershipCreateStatus.DuplicateUserName;
     74            return null;
     75          }
     76          if (db.HeuristicLabUsers.Count(x => x.Email == email) > 0) {
     77            status = MembershipCreateStatus.DuplicateEmail;
     78            return null;
     79          }
     80
     81          // create new user
     82          HeuristicLabUser u = new HeuristicLabUser(username, email, passwordQuestion, "");
     83          u.ChangePassword("INIT", password);
     84          u.ChangePasswordQuestionAndAnswer(password, passwordQuestion, passwordAnswer);
     85          // save user into database
     86          db.HeuristicLabUsers.InsertOnSubmit(u);
     87          db.SubmitChanges();
     88
     89          // success
     90          status = MembershipCreateStatus.Success;
     91          return u;
     92        }
     93        catch (Exception) {
     94          // error
     95          status = MembershipCreateStatus.ProviderError;
     96          return null;
     97        }
     98      }
     99    }
     100
     101    public override bool DeleteUser(string username, bool deleteAllRelatedData) {
     102      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
     103        // check database connection
     104        if (db == null) {
     105          return false;
     106        }
     107        try {
     108          // try to get user
     109          HeuristicLabUser u =
     110            db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == username);
     111
     112          // optionally delete related data
     113          if (deleteAllRelatedData) {
     114            db.HeuristicLabUserRole.DeleteAllOnSubmit<HeuristicLabUserRole>(u.HeuristicLabUserRole);
     115          }
     116         
     117          // delete user
     118          db.HeuristicLabUsers.DeleteOnSubmit(u);
    25119          db.SubmitChanges();
    26120          return true;
    27         } else {
    28           return false;
    29         }
    30       }
    31       catch (Exception) {
    32         return false;
    33       }
    34     }
    35 
    36     public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) {
    37       DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext();
    38       if (db == null) {
    39         return false;
    40       }
    41       try {
    42         HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
    43         if (u.ChangePasswordQuestionAndAnswer(password, newPasswordQuestion, newPasswordAnswer)) {
    44           db.SubmitChanges();
    45           return true;
    46         } else {
    47           return false;
    48         }
    49       }
    50       catch (Exception) {
    51         return false;
    52       }
    53     }
    54 
    55     public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) {
    56       throw new NotImplementedException();
    57     }
    58 
    59     public override bool DeleteUser(string username, bool deleteAllRelatedData) {
    60       throw new NotImplementedException();
     121        }
     122        catch (Exception) {
     123          return false;
     124        }
     125      }
    61126    }
    62127
     
    86151
    87152    public override string GetPassword(string username, string answer) {
     153
    88154      throw new NotImplementedException();
    89155    }
  • branches/HeuristicLab.Services.Authentication Prototype/UnitTests/HeuristicLabMembershipProviderTest.cs

    r3948 r3956  
    1 using Service.Provider;
     1using System;
     2using Service.Provider;
    23using Microsoft.VisualStudio.TestTools.UnitTesting;
    34using System.Web.Security;
     5using Persistence;
     6using System.Linq;
     7using System.Collections.Generic;
    48
    59namespace UnitTests {
     
    368372    [TestMethod()]
    369373    public void DeleteUserTest() {
    370       HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
    371       string username = string.Empty; // TODO: Initialize to an appropriate value
    372       bool deleteAllRelatedData = false; // TODO: Initialize to an appropriate value
    373       bool expected = false; // TODO: Initialize to an appropriate value
    374       bool actual;
    375       actual = target.DeleteUser(username, deleteAllRelatedData);
    376       Assert.AreEqual(expected, actual);
    377       Assert.Inconclusive("Verify the correctness of this test method.");
     374      // insert new user
     375      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider();
     376      MembershipCreateStatus status;
     377      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
     378      Assert.AreEqual(MembershipCreateStatus.Success, status);
     379
     380      // delete user
     381      Assert.IsTrue(target.DeleteUser("testname", true));
     382      Assert.AreEqual(0, db.HeuristicLabUsers.Count(x => x.UserName == "testname"));
    378383    }
    379384
     
    383388    [TestMethod()]
    384389    public void CreateUserTest() {
    385       HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
    386       string username = string.Empty; // TODO: Initialize to an appropriate value
    387       string password = string.Empty; // TODO: Initialize to an appropriate value
    388       string email = string.Empty; // TODO: Initialize to an appropriate value
    389       string passwordQuestion = string.Empty; // TODO: Initialize to an appropriate value
    390       string passwordAnswer = string.Empty; // TODO: Initialize to an appropriate value
    391       bool isApproved = false; // TODO: Initialize to an appropriate value
    392       object providerUserKey = null; // TODO: Initialize to an appropriate value
    393       MembershipCreateStatus status = new MembershipCreateStatus(); // TODO: Initialize to an appropriate value
    394       MembershipCreateStatus statusExpected = new MembershipCreateStatus(); // TODO: Initialize to an appropriate value
    395       MembershipUser expected = null; // TODO: Initialize to an appropriate value
    396       MembershipUser actual;
    397       actual = target.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
    398       Assert.AreEqual(statusExpected, status);
    399       Assert.AreEqual(expected, actual);
    400       Assert.Inconclusive("Verify the correctness of this test method.");
     390      // create user
     391      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider();
     392      MembershipCreateStatus status;
     393      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
     394      Assert.AreEqual(MembershipCreateStatus.Success, status);
     395
     396      // check if user is OK
     397      HeuristicLabUser u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
     398      Assert.AreEqual<string>("testname", u.UserName);
     399      Assert.AreEqual<string>("testemail", u.Email);
     400      Assert.AreEqual<string>("newPassword", u.Password);
     401      Assert.AreEqual<string>("testquestion", u.PasswordQuestion);
     402      Assert.AreEqual<string>("testanswer", u.PasswordAnswer);
     403      Assert.AreEqual<string>("", u.Comment);
     404
     405      // check for duplicate errors
     406      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
     407      Assert.AreEqual(MembershipCreateStatus.DuplicateUserName, status);
     408      target.CreateUser("testname2", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
     409      Assert.AreEqual(MembershipCreateStatus.DuplicateEmail, status);
    401410    }
    402411
     
    406415    [TestMethod()]
    407416    public void ChangePasswordQuestionAndAnswerTest() {
    408       HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
    409       string username = string.Empty; // TODO: Initialize to an appropriate value
    410       string password = string.Empty; // TODO: Initialize to an appropriate value
    411       string newPasswordQuestion = string.Empty; // TODO: Initialize to an appropriate value
    412       string newPasswordAnswer = string.Empty; // TODO: Initialize to an appropriate value
    413       bool expected = false; // TODO: Initialize to an appropriate value
    414       bool actual;
    415       actual = target.ChangePasswordQuestionAndAnswer(username, password, newPasswordQuestion, newPasswordAnswer);
    416       Assert.AreEqual(expected, actual);
    417       Assert.Inconclusive("Verify the correctness of this test method.");
     417      // create user
     418      HeuristicLabUser u = new HeuristicLabUser("testname", "testemail", "testquestion", "testcomment");
     419      db.HeuristicLabUsers.InsertOnSubmit(u);
     420      db.SubmitChanges();
     421
     422      // check if user is stored
     423      u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
     424      Assert.AreEqual<String>("testquestion", u.PasswordQuestion);
     425      Assert.AreEqual<String>("", u.PasswordAnswer);
     426
     427      // change data and check again
     428      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider();
     429      // gibt zwar true zurück, was schon mal gut ist
     430      Assert.IsTrue(target.ChangePasswordQuestionAndAnswer("testname", "INIT", "newquestion", "newanswer"));
     431      // aber hier ist die änderung noch nicht da!! es ist immer noch die alte frage + alte antwort
     432      u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
     433      Assert.AreEqual<String>("newquestion", u.PasswordQuestion);
     434      Assert.AreEqual<String>("newanswer", u.PasswordAnswer);
    418435    }
    419436
     
    423440    [TestMethod()]
    424441    public void ChangePasswordTest() {
    425       HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
    426       string username = string.Empty; // TODO: Initialize to an appropriate value
    427       string oldPassword = string.Empty; // TODO: Initialize to an appropriate value
    428       string newPassword = string.Empty; // TODO: Initialize to an appropriate value
    429       bool expected = false; // TODO: Initialize to an appropriate value
    430       bool actual;
    431       actual = target.ChangePassword(username, oldPassword, newPassword);
    432       Assert.AreEqual(expected, actual);
    433       Assert.Inconclusive("Verify the correctness of this test method.");
     442      // create user
     443      HeuristicLabUser u = new HeuristicLabUser("testname", "testemail", "testquestion", "testcomment");
     444      db.HeuristicLabUsers.InsertOnSubmit(u);
     445      db.SubmitChanges();
     446
     447      // check if user is stored
     448      u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
     449      Assert.AreEqual<String>("INIT", u.Password);
     450
     451      // change data and check again
     452      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider();
     453      Assert.IsTrue(target.ChangePassword("testname", "INIT", "newPassword"));
     454      // hat nix gemacht!! :(
     455      u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
     456      Assert.AreEqual<String>("newPassword", u.Password);
    434457    }
    435458
  • branches/HeuristicLab.Services.Authentication Prototype/UnitTests/HeuristicLabUserTest.cs

    r3949 r3956  
    2020    public void modifyUserTest() {
    2121      // insert new user
    22       Persistence.HeuristicLabUser user = new HeuristicLabUser("testname", "testemail", "testquestion", "testcomment");
     22      HeuristicLabUser user = new HeuristicLabUser("testname", "testemail", "testquestion", "testcomment");
    2323      db.HeuristicLabUsers.InsertOnSubmit(user);
    2424      db.SubmitChanges();
    25       Persistence.HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == "testname");
     25      HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == "testname");
    2626      Assert.IsNotNull(u);
    2727      Assert.AreEqual<string>("testname", u.UserName);
Note: See TracChangeset for help on using the changeset viewer.