Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Services.Authentication Prototype/UnitTests/HeuristicLabMembershipProviderTest.cs @ 4021

Last change on this file since 4021 was 4018, checked in by bfarka, 14 years ago

all needed methods of membershipprovier are implemented and all testes are passing (#1046)

File size: 21.3 KB
Line 
1using System;
2using System.Collections.Specialized;
3using Service.Provider;
4using Microsoft.VisualStudio.TestTools.UnitTesting;
5using System.Web.Security;
6using Persistence;
7using System.Linq;
8using System.Collections.Generic;
9using System.Data.SqlClient;
10
11namespace UnitTests {
12  /// <summary>
13  ///This is a test class for HeuristicLabMembershipProviderTest and is intended
14  ///to contain all HeuristicLabMembershipProviderTest Unit Tests
15  ///</summary>
16  [TestClass()]
17  public class HeuristicLabMembershipProviderTest : AbstractHeuristicLabTest {
18    private TestContext testContextInstance;
19
20    /// <summary>
21    ///Gets or sets the test context which provides
22    ///information about and functionality for the current test run.
23    ///</summary>
24    public TestContext TestContext {
25      get {
26        return testContextInstance;
27      }
28      set {
29        testContextInstance = value;
30      }
31    }
32
33    #region Additional test attributes
34    //
35    //You can use the following additional attributes as you write your tests:
36    //
37    //Use ClassInitialize to run code before running the first test in the class
38    //[ClassInitialize()]
39    //public static void MyClassInitialize(TestContext testContext)
40    //{
41    //}
42    //
43    //Use ClassCleanup to run code after all tests in a class have run
44    //[ClassCleanup()]
45    //public static void MyClassCleanup()
46    //{
47    //}
48    //
49    //Use TestInitialize to run code before running each test
50    //[TestInitialize()]
51    //public void MyTestInitialize()
52    //{
53    //}
54    //
55    //Use TestCleanup to run code after each test has run
56    //[TestCleanup()]
57    //public void MyTestCleanup()
58    //{
59    //}
60    //
61    #endregion
62
63
64    /// <summary>
65    ///A test for RequiresUniqueEmail
66    ///</summary>
67    [TestMethod()]
68    public void RequiresUniqueEmailTest() {
69      MembershipProvider target = Membership.Provider;  // TODO: Initialize to an appropriate value
70      bool actual;
71      actual = target.RequiresUniqueEmail;
72      Assert.IsTrue(actual);
73    }
74
75    /// <summary>
76    ///A test for RequiresQuestionAndAnswer
77    ///</summary>
78    [TestMethod()]
79    public void RequiresQuestionAndAnswerTest() {
80      MembershipProvider target = Membership.Provider;  // TODO: Initialize to an appropriate value
81      bool actual;
82      actual = target.RequiresQuestionAndAnswer;
83      Assert.IsFalse(actual);
84    }
85
86    /// <summary>
87    ///A test for PasswordStrengthRegularExpression
88    ///</summary>
89    [TestMethod()]
90    public void PasswordStrengthRegularExpressionTest() {
91      MembershipProvider target = Membership.Provider;
92      string actual;
93      actual = target.PasswordStrengthRegularExpression;
94      Assert.AreEqual(actual, String.Empty);
95    }
96
97
98
99 
100
101    /// <summary>
102    ///A test for MinRequiredPasswordLength
103    ///</summary>
104    [TestMethod()]
105    public void MinRequiredPasswordLengthTest() {
106      MembershipProvider target = Membership.Provider;  // TODO: Initialize to an appropriate value
107
108      MembershipCreateStatus status;
109      target.CreateUser("username", "", "mail", "question", "answer", true, null, out status);
110      Assert.AreEqual(status, MembershipCreateStatus.InvalidPassword);
111      target.CreateUser("username", "testusername", "mail", "question", "answer", true, null, out status);
112      Assert.AreEqual(status, MembershipCreateStatus.Success);
113      Assert.IsFalse(target.ChangePassword("username", "testusername", ""));
114      Assert.IsTrue(target.ChangePassword("username", "testusername", "foofoofoo"));
115    }
116
117    /// <summary>
118    ///A test for MinRequiredNonAlphanumericCharacters
119    ///</summary>
120    [TestMethod()]
121    public void MinRequiredNonAlphanumericCharactersTest() {
122      MembershipProvider target = Membership.Provider;  // TODO: Initialize to an appropriate value
123      int actual;
124      actual = target.MinRequiredNonAlphanumericCharacters;
125      Assert.AreEqual(actual, 0);
126    }
127
128    /// <summary>
129    ///A test for MaxInvalidPasswordAttempts
130    ///</summary>
131    [TestMethod()]
132    public void MaxInvalidPasswordAttemptsTest() {
133      MembershipProvider target = Membership.Provider;
134      MembershipCreateStatus status;
135      target.CreateUser("testUser", "MySuperPassword", "mail", "question", "answer", true, null, out status);
136      Assert.AreEqual(MembershipCreateStatus.Success, status);
137
138      target.ValidateUser("testUser", "MySuperPassword");
139      Assert.IsFalse(target.GetUser("testUser", true).IsLockedOut);
140
141      for (int i = 0; i < target.MaxInvalidPasswordAttempts - 1; i++) {
142        target.ValidateUser("testUser", "foo");
143        Assert.IsFalse(target.GetUser("testUser", true).IsLockedOut);
144      }
145
146      target.ValidateUser("testUser", "MySuperPassword");
147      Assert.IsFalse(target.GetUser("testUser", true).IsLockedOut);
148
149
150      for (int i = 0; i < target.MaxInvalidPasswordAttempts; i++) {
151        target.ValidateUser("testUser", "foo");
152        Assert.IsFalse(target.GetUser("testUser", true).IsLockedOut);
153      }
154     
155      target.ValidateUser("testUser", "foo");
156      Assert.IsTrue(target.GetUser("testUser", true).IsLockedOut);
157
158      target.ValidateUser("testUser", "MySuperPassword");
159      Assert.IsTrue(target.GetUser("testUser", true).IsLockedOut);
160
161    }
162
163    /// <summary>
164    ///A test for EnablePasswordRetrieval
165    ///</summary>
166    [TestMethod()]
167    public void EnablePasswordRetrievalTest() {
168      MembershipProvider target = Membership.Provider;
169      bool actual;
170      actual = target.EnablePasswordRetrieval;
171      Assert.IsFalse(actual);
172    }
173
174    /// <summary>
175    ///A test for EnablePasswordReset
176    ///</summary>
177    [TestMethod()]
178    public void EnablePasswordResetTest() {
179      MembershipProvider target = Membership.Provider;  // TODO: Initialize to an appropriate value
180      bool actual;
181      actual = target.EnablePasswordReset;
182      Assert.IsFalse(actual);
183    }
184
185    /// <summary>
186    ///A test for ValidateUser
187    ///</summary>
188    [TestMethod()]
189    public void ValidateUserTest() {
190      MembershipProvider target = Membership.Provider;
191      MembershipCreateStatus status;
192      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
193      Assert.IsTrue(target.ValidateUser("testname", "newPassword"));
194    }
195
196    /// <summary>
197    ///A test for ValidateUser
198    ///</summary>
199    [TestMethod()]
200    public void ValidateUserTestEncrypted() {
201      MembershipProvider target = Membership.Provider;
202      NameValueCollection config = new NameValueCollection();
203      config.Add("passwordFormat", "Encrypted");
204      MembershipCreateStatus status;
205      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
206      Assert.IsTrue(target.ValidateUser("testname", "newPassword"));
207    }
208
209    /// <summary>
210    ///A test for ValidateUser
211    ///</summary>
212    [TestMethod()]
213    public void ValidateUserTestHashed() {
214      MembershipProvider target = Membership.Provider;
215      MembershipCreateStatus status;
216      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
217      Assert.IsTrue(target.ValidateUser("testname", "newPassword"));
218    }
219
220    /// <summary>
221    ///A test for UpdateUser
222    ///</summary>
223    [TestMethod()]
224    public void UpdateUserTest() {
225      MembershipProvider target = Membership.Provider;
226      MembershipCreateStatus status;
227      MembershipUser user = null;
228      target.CreateUser("testUser", "myPassword", "mail", "question", "answer", true, null, out status);
229      Assert.AreEqual(MembershipCreateStatus.Success, status);
230      user =target.GetUser("testUser", true);
231      Assert.IsNotNull(user);
232      user.Email = "newMail";
233      target.UpdateUser(user);
234      user = target.GetUser("testUser", true);
235      Assert.AreEqual(user.Email, "newMail");
236    }
237
238    /// <summary>
239    ///A test for LockUser
240    ///</summary>
241    [TestMethod()]
242    public void LockUserTest() {
243      MembershipProvider target = Membership.Provider;
244      MembershipCreateStatus status;
245      Persistence.HeuristicLabMembershipUser user = (Persistence.HeuristicLabMembershipUser)target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
246      user.UnlockUser();
247      HeuristicLabUser dbUser = db.HeuristicLabUsers.Single(u => u.UserName == "testname");
248      Assert.IsFalse(dbUser.Locked);
249
250    }
251
252
253
254    /// <summary>
255    ///A test for ResetPassword
256    ///</summary>
257    [TestMethod()]
258    public void ResetPasswordTest() {
259      MembershipProvider target = Membership.Provider;
260      string username = string.Empty;
261      string answer = string.Empty;
262      string expected = string.Empty;
263      string actual;
264      try {
265        target.ResetPassword("foo", "foo");
266      }
267      catch (NotSupportedException) {
268      //should be thrown
269      }
270     
271    }
272
273    /// <summary>
274    ///A test for GetUserNameByEmail
275    ///</summary>
276    [TestMethod()]
277    public void GetUserNameByEmailTest() {
278      MembershipProvider target = Membership.Provider; // TODO: Initialize to an appropriate value
279      string email = string.Empty; // TODO: Initialize to an appropriate value
280      string expected = string.Empty; // TODO: Initialize to an appropriate value
281      string actual;
282      actual = target.GetUserNameByEmail(email);
283      Assert.IsNull(actual);
284      MembershipCreateStatus status;
285      target.CreateUser("testuser", "mySecret", "testmail", "question", "answer", true, null, out status);
286      Assert.AreEqual(status, MembershipCreateStatus.Success);
287      Assert.AreEqual(target.GetUserNameByEmail("testmail"), "testuser");
288    }
289
290
291    /// <summary>
292    ///A test for GetUser
293    ///</summary>
294    [TestMethod()]
295    public void GetUserTest() {
296      MembershipProvider target = Membership.Provider; // TODO: Initialize to an appropriate value
297      MembershipCreateStatus status;
298      target.CreateUser("testUser","myPersonalSecret", "root@nowhere.com","myQuestion", "myAnswer", true, null, out status);
299      Assert.AreEqual(MembershipCreateStatus.Success, status);
300      MembershipUser user = target.GetUser("testUser", true);
301      Assert.AreEqual(user.UserName, "testUser");
302      Assert.AreEqual(user.Email, "root@nowhere.com");
303      Assert.AreEqual(user.PasswordQuestion, "myQuestion");
304      Assert.IsTrue(target.ValidateUser("testUser", "myPersonalSecret"));
305     
306    }
307
308    /// <summary>
309    ///A test for GetPassword
310    ///</summary>
311    [TestMethod()]
312    public void GetPasswordTest() {
313      MembershipProvider target = Membership.Provider; // TODO: Initialize to an appropriate value
314      string username = string.Empty; // TODO: Initialize to an appropriate value
315      string answer = string.Empty; // TODO: Initialize to an appropriate value
316      string expected = string.Empty; // TODO: Initialize to an appropriate value
317      string actual;
318      try {
319        actual = target.GetPassword(username, answer);
320        Assert.Fail();
321      }
322      catch (NotSupportedException) {
323        //swallow because expected
324      }
325     
326    }
327
328    /// <summary>
329    ///A test for GetNumberOfUsersOnline
330    ///</summary>
331    [TestMethod()]
332    public void GetNumberOfUsersOnlineTest() {
333      MembershipProvider target = Membership.Provider; // TODO: Initialize to an appropriate value
334      int expected = 0; // TODO: Initialize to an appropriate value
335      int actual;
336      actual = target.GetNumberOfUsersOnline();
337      Assert.AreEqual(expected, actual);
338    }
339
340    /// <summary>
341    ///A test for GetAllUsers
342    ///
343    /// work in progress
344    ///</summary>
345    [TestMethod()]
346    public void GetAllUsersTest() {
347      MembershipProvider target = Membership.Provider;
348      MembershipCreateStatus status;
349      MembershipUserCollection users;
350      int totalRecords;
351      int numberOfUsers = 13;
352      int pageSize = 5;
353
354      // make sure that we get an empty result if there are no users
355      Assert.AreEqual(0, target.GetAllUsers(1, pageSize, out totalRecords).Count);
356
357      // Create some users
358      for (int i = 0; i < numberOfUsers; i++) {
359        target.CreateUser("User " + i, "newPassword " + i, "testemail " + i, "testquestion " + i, "testanswer " + i, true, null, out status);
360      }
361
362      // basic proof as in http://msdn.microsoft.com/en-us/library/system.web.security.membershipprovider.getallusers.aspx
363      users = target.GetAllUsers(1, pageSize, out totalRecords);
364      HeuristicLabMembershipUser firstUser = null;
365      HeuristicLabMembershipUser lastUser = null;
366      foreach (HeuristicLabMembershipUser user in users) {
367        if (firstUser == null) {
368          firstUser = user;
369        }
370        lastUser = user;
371      }
372      Assert.AreEqual("User 5", firstUser.UserName);
373      Assert.AreEqual("User 9", lastUser.UserName);
374
375      // simulate paging and make sure that a valid empty collection is returned in the end
376      int pageIndex = 0;
377      while ((users = target.GetAllUsers(pageIndex, pageSize, out totalRecords)).Count != 0) {
378
379        // check that totalRecords constantly equals the number of created users
380        Assert.AreEqual(numberOfUsers, totalRecords);
381
382        int j = pageIndex * pageSize;
383        foreach (HeuristicLabMembershipUser user in users) {
384          Assert.AreEqual("User " + j, user.UserName);
385          j++;
386        }
387        pageIndex++;
388      }
389
390      // Make sure that we ended up at the last page
391      Assert.AreEqual(3, pageIndex);
392
393      // Make sure that we get an empty result if the page index is out of bounds
394      Assert.AreEqual(0, target.GetAllUsers(42, pageSize, out totalRecords).Count);
395    }
396
397    /// <summary>
398    ///A test for FindUsersByName
399    ///</summary>
400    [TestMethod()]
401    public void FindUsersByNameTest() {
402      MembershipProvider target = Membership.Provider;
403      MembershipCreateStatus status;
404      int totalRecords;
405      int pageSize = 5;
406
407      target.CreateUser("User 1", "newPassword ", "foo@bar.com", "testquestion ", "testanswer ", true, null, out status);
408      target.CreateUser("User 2", "newPassword ", "foo@baz.com", "testquestion ", "testanswer ", true, null, out status);
409      MembershipUserCollection users = target.FindUsersByName("User 2", 0, pageSize, out totalRecords);
410      HeuristicLabMembershipUser firstUser = null;
411      HeuristicLabMembershipUser lastUser = null;
412      foreach (HeuristicLabMembershipUser user in users) {
413        if (firstUser == null) {
414          firstUser = user;
415        }
416        lastUser = user;
417      }
418      Assert.AreEqual("User 2", firstUser.UserName);
419      Assert.AreEqual("User 2", lastUser.UserName);
420      Assert.AreEqual(1, totalRecords);
421    }
422
423    /// <summary>
424    ///A test for FindUsersByEmail
425    ///</summary>
426    [TestMethod()]
427    public void FindUsersByEmailTest() {
428      MembershipProvider target = Membership.Provider;
429      MembershipCreateStatus status;
430      int totalRecords;
431      int pageSize = 5;
432
433      target.CreateUser("User 1", "newPassword ", "foo@bar.com", "testquestion ", "testanswer ", true, null, out status);
434      target.CreateUser("User 2", "newPassword ", "foo@baz.com", "testquestion ", "testanswer ", true, null, out status);
435      MembershipUserCollection users = target.FindUsersByEmail("foo@bar.com", 0, pageSize, out totalRecords);
436      HeuristicLabMembershipUser firstUser = null;
437      HeuristicLabMembershipUser lastUser = null;
438      foreach (HeuristicLabMembershipUser user in users) {
439        if (firstUser == null) {
440          firstUser = user;
441        }
442        lastUser = user;
443      }
444      Assert.AreEqual("User 1", firstUser.UserName);
445      Assert.AreEqual("User 1", lastUser.UserName);
446      Assert.AreEqual(1, totalRecords);
447    }
448
449    /// <summary>
450    ///A test for DeleteUser
451    ///</summary>
452    [TestMethod()]
453    public void DeleteUserTest() {
454      // insert new user
455      MembershipProvider target = Membership.Provider;
456      MembershipCreateStatus status;
457      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
458      Assert.AreEqual(MembershipCreateStatus.Success, status);
459
460      // delete user
461      Assert.IsTrue(target.DeleteUser("testname", true));
462      Assert.AreEqual(0, db.HeuristicLabUsers.Count(x => x.UserName == "testname"));
463    }
464
465    /// <summary>
466    ///A test for CreateUser
467    ///</summary>
468    [TestMethod()]
469    public void CreateUserTest() {
470      // create user
471      MembershipProvider target = Membership.Provider;
472      MembershipCreateStatus status;
473      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
474      Assert.AreEqual(MembershipCreateStatus.Success, status);
475
476      // check if user is OK
477      HeuristicLabUser u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
478      Assert.AreEqual<string>("testname", u.UserName);
479      Assert.AreEqual<string>("testemail", u.Email);
480      Assert.AreEqual<string>(((HeuristicLabMembershipProvider)target).EncodePassword("newPassword"), u.Password);
481      Assert.AreEqual<string>("testquestion", u.PasswordQuestion);
482      Assert.AreEqual<string>("testanswer", u.PasswordAnswer);
483      Assert.AreEqual<string>("", u.Comment);
484
485      // check for duplicate errors
486      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
487      Assert.AreEqual(MembershipCreateStatus.DuplicateUserName, status);
488      target.CreateUser("testname2", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
489      Assert.AreEqual(MembershipCreateStatus.DuplicateEmail, status);
490    }
491
492    /// <summary>
493    ///A test for ChangePasswordQuestionAndAnswer
494    ///</summary>
495    [TestMethod()]
496    public void ChangePasswordQuestionAndAnswerTest() {
497      // create user
498      HeuristicLabUser u = new HeuristicLabUser("testname", "testemail", "testquestion", "testcomment");
499      db.HeuristicLabUsers.InsertOnSubmit(u);
500      db.SubmitChanges();
501
502      // check if user is stored
503      u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
504      Assert.AreEqual<String>("testquestion", u.PasswordQuestion);
505      Assert.AreEqual<String>("", u.PasswordAnswer);
506
507      // change data and check again
508      MembershipProvider target = Membership.Provider;
509      // gibt zwar true zurück, was schon mal gut ist
510      Assert.IsTrue(target.ChangePasswordQuestionAndAnswer("testname", "INIT", "newquestion", "newanswer"));
511      db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, u);
512      u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
513      Assert.AreEqual<String>("newquestion", u.PasswordQuestion);
514      Assert.AreEqual<String>("newanswer", u.PasswordAnswer);
515    }
516
517    /// <summary>
518    ///A test for ChangePassword
519    ///</summary>
520    [TestMethod()]
521    public void ChangePasswordTest() {
522      MembershipProvider target = Membership.Provider;
523     
524      // create user
525      HeuristicLabUser u = new HeuristicLabUser("testname", "testemail", "testquestion", "testcomment");
526      u.Password = ((HeuristicLabMembershipProvider)target).EncodePassword("INIT");
527      db.HeuristicLabUsers.InsertOnSubmit(u);
528      db.SubmitChanges();
529
530      // check if user is stored
531      u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
532      Assert.AreEqual<String>(((HeuristicLabMembershipProvider)target).EncodePassword("INIT"), u.Password);
533
534      // change data and check again
535      Assert.IsTrue(target.ChangePassword("testname", "INIT", "newPassword"));
536      db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, u);
537      u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
538      Assert.AreEqual<String>(((HeuristicLabMembershipProvider)target).EncodePassword("newPassword"), u.Password);
539    }
540
541
542    [TestMethod()]
543    public void NoDuplicateUserNameTest() {
544      try {
545        Persistence.HeuristicLabUser user = new Persistence.HeuristicLabUser("duplicateName", "mail", "myQuestion", "myComment");
546        db.HeuristicLabUsers.InsertOnSubmit(user);
547        Persistence.HeuristicLabUser user2 = new Persistence.HeuristicLabUser("duplicateName", "mail2", "myQuestion2", "myComment2");
548        db.HeuristicLabUsers.InsertOnSubmit(user2);
549        db.SubmitChanges();
550        Assert.Fail();
551      }
552      catch (SqlException) {
553        //swallowing Exception because it is expected that a SQL Exception is thrown       
554      }
555    }
556    [TestMethod()]
557    public void NoDuplicateEmailTest() {
558      try {
559        Persistence.HeuristicLabUser user = new Persistence.HeuristicLabUser("duplicateName", "mail", "myQuestion", "myComment");
560        db.HeuristicLabUsers.InsertOnSubmit(user);
561        Persistence.HeuristicLabUser user2 = new Persistence.HeuristicLabUser("duplicateName2", "mail", "myQuestion2", "myComment2");
562        db.HeuristicLabUsers.InsertOnSubmit(user2);
563        db.SubmitChanges();
564        Assert.Fail();
565      }
566      catch (SqlException) {
567        //swallowing Exception because it is expected that a SQL Exception is thrown       
568      }
569    }
570  }
571}
Note: See TracBrowser for help on using the repository browser.