Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3977 was 3977, checked in by dkahn, 14 years ago

#1061 GetAllUsers with unit test. Under construction.

File size: 21.6 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      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
70      bool actual;
71      actual = target.RequiresUniqueEmail;
72      Assert.Inconclusive("Verify the correctness of this test method.");
73    }
74
75    /// <summary>
76    ///A test for RequiresQuestionAndAnswer
77    ///</summary>
78    [TestMethod()]
79    public void RequiresQuestionAndAnswerTest() {
80      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
81      bool actual;
82      actual = target.RequiresQuestionAndAnswer;
83      Assert.Inconclusive("Verify the correctness of this test method.");
84    }
85
86    /// <summary>
87    ///A test for PasswordStrengthRegularExpression
88    ///</summary>
89    [TestMethod()]
90    public void PasswordStrengthRegularExpressionTest() {
91      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
92      string actual;
93      actual = target.PasswordStrengthRegularExpression;
94      Assert.Inconclusive("Verify the correctness of this test method.");
95    }
96
97    /// <summary>
98    ///A test for PasswordFormat
99    ///</summary>
100    [TestMethod()]
101    public void PasswordFormatTest() {
102      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
103      MembershipPasswordFormat actual;
104      actual = target.PasswordFormat;
105      Assert.Inconclusive("Verify the correctness of this test method.");
106    }
107
108    /// <summary>
109    ///A test for PasswordAttemptWindow
110    ///</summary>
111    [TestMethod()]
112    public void PasswordAttemptWindowTest() {
113      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
114      int actual;
115      actual = target.PasswordAttemptWindow;
116      Assert.Inconclusive("Verify the correctness of this test method.");
117    }
118
119    /// <summary>
120    ///A test for MinRequiredPasswordLength
121    ///</summary>
122    [TestMethod()]
123    public void MinRequiredPasswordLengthTest() {
124      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
125      int actual;
126      actual = target.MinRequiredPasswordLength;
127      Assert.Inconclusive("Verify the correctness of this test method.");
128    }
129
130    /// <summary>
131    ///A test for MinRequiredNonAlphanumericCharacters
132    ///</summary>
133    [TestMethod()]
134    public void MinRequiredNonAlphanumericCharactersTest() {
135      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
136      int actual;
137      actual = target.MinRequiredNonAlphanumericCharacters;
138      Assert.Inconclusive("Verify the correctness of this test method.");
139    }
140
141    /// <summary>
142    ///A test for MaxInvalidPasswordAttempts
143    ///</summary>
144    [TestMethod()]
145    public void MaxInvalidPasswordAttemptsTest() {
146      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
147      int actual;
148      actual = target.MaxInvalidPasswordAttempts;
149      Assert.Inconclusive("Verify the correctness of this test method.");
150    }
151
152    /// <summary>
153    ///A test for EnablePasswordRetrieval
154    ///</summary>
155    [TestMethod()]
156    public void EnablePasswordRetrievalTest() {
157      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
158      bool actual;
159      actual = target.EnablePasswordRetrieval;
160      Assert.Inconclusive("Verify the correctness of this test method.");
161    }
162
163    /// <summary>
164    ///A test for EnablePasswordReset
165    ///</summary>
166    [TestMethod()]
167    public void EnablePasswordResetTest() {
168      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
169      bool actual;
170      actual = target.EnablePasswordReset;
171      Assert.Inconclusive("Verify the correctness of this test method.");
172    }
173
174    /// <summary>
175    ///A test for ApplicationName
176    ///</summary>
177    [TestMethod()]
178    public void ApplicationNameTest() {
179      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
180      string expected = string.Empty; // TODO: Initialize to an appropriate value
181      string actual;
182      target.ApplicationName = expected;
183      actual = target.ApplicationName;
184      Assert.AreEqual(expected, actual);
185      Assert.Inconclusive("Verify the correctness of this test method.");
186    }
187
188    /// <summary>
189    ///A test for ValidateUser
190    ///</summary>
191    [TestMethod()]
192    public void ValidateUserTest() {
193      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider();
194      MembershipCreateStatus status;
195      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
196      Assert.IsTrue(target.ValidateUser("testname", "newPassword"));
197    }
198
199    /// <summary>
200    ///A test for ValidateUser
201    ///</summary>
202    [TestMethod()]
203    public void ValidateUserTestEncrypted() {
204      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider();
205      NameValueCollection config = new NameValueCollection();
206      config.Add("passwordFormat", "Encrypted");
207      target.Initialize("", config);
208      MembershipCreateStatus status;
209      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
210      Assert.IsTrue(target.ValidateUser("testname", "newPassword"));
211    }
212
213    /// <summary>
214    ///A test for ValidateUser
215    ///</summary>
216    [TestMethod()]
217    public void ValidateUserTestHashed() {
218      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider();
219      NameValueCollection config = new NameValueCollection();
220      config.Add("passwordFormat", "Hashed");
221      target.Initialize("", config);
222      MembershipCreateStatus status;
223      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
224      Assert.IsTrue(target.ValidateUser("testname", "newPassword"));
225    }
226
227    /// <summary>
228    ///A test for UpdateUser
229    ///</summary>
230    [TestMethod()]
231    public void UpdateUserTest() {
232      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
233      MembershipUser user = null; // TODO: Initialize to an appropriate value
234      target.UpdateUser(user);
235      Assert.Inconclusive("A method that does not return a value cannot be verified.");
236    }
237
238    /// <summary>
239    ///A test for UnlockUser
240    ///</summary>
241    [TestMethod()]
242    public void UnlockUserTest() {
243      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
244      string userName = string.Empty; // TODO: Initialize to an appropriate value
245      bool expected = false; // TODO: Initialize to an appropriate value
246      bool actual;
247      actual = target.UnlockUser(userName);
248      Assert.AreEqual(expected, actual);
249      Assert.Inconclusive("Verify the correctness of this test method.");
250    }
251
252    /// <summary>
253    ///A test for ResetPassword
254    ///</summary>
255    [TestMethod()]
256    public void ResetPasswordTest() {
257      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
258      string username = string.Empty; // TODO: Initialize to an appropriate value
259      string answer = string.Empty; // TODO: Initialize to an appropriate value
260      string expected = string.Empty; // TODO: Initialize to an appropriate value
261      string actual;
262      actual = target.ResetPassword(username, answer);
263      Assert.AreEqual(expected, actual);
264      Assert.Inconclusive("Verify the correctness of this test method.");
265    }
266
267    /// <summary>
268    ///A test for GetUserNameByEmail
269    ///</summary>
270    [TestMethod()]
271    public void GetUserNameByEmailTest() {
272      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
273      string email = string.Empty; // TODO: Initialize to an appropriate value
274      string expected = string.Empty; // TODO: Initialize to an appropriate value
275      string actual;
276      actual = target.GetUserNameByEmail(email);
277      Assert.AreEqual(expected, actual);
278      Assert.Inconclusive("Verify the correctness of this test method.");
279    }
280
281    /// <summary>
282    ///A test for GetUser
283    ///</summary>
284    [TestMethod()]
285    public void GetUserTest1() {
286      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
287      object providerUserKey = null; // TODO: Initialize to an appropriate value
288      bool userIsOnline = false; // TODO: Initialize to an appropriate value
289      MembershipUser expected = null; // TODO: Initialize to an appropriate value
290      MembershipUser actual;
291      actual = target.GetUser(providerUserKey, userIsOnline);
292      Assert.AreEqual(expected, actual);
293      Assert.Inconclusive("Verify the correctness of this test method.");
294    }
295
296    /// <summary>
297    ///A test for GetUser
298    ///</summary>
299    [TestMethod()]
300    public void GetUserTest() {
301      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
302      string username = string.Empty; // TODO: Initialize to an appropriate value
303      bool userIsOnline = false; // TODO: Initialize to an appropriate value
304      MembershipUser expected = null; // TODO: Initialize to an appropriate value
305      MembershipUser actual;
306      actual = target.GetUser(username, userIsOnline);
307      Assert.AreEqual(expected, actual);
308      Assert.Inconclusive("Verify the correctness of this test method.");
309    }
310
311    /// <summary>
312    ///A test for GetPassword
313    ///</summary>
314    [TestMethod()]
315    public void GetPasswordTest() {
316      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
317      string username = string.Empty; // TODO: Initialize to an appropriate value
318      string answer = string.Empty; // TODO: Initialize to an appropriate value
319      string expected = string.Empty; // TODO: Initialize to an appropriate value
320      string actual;
321      actual = target.GetPassword(username, answer);
322      Assert.AreEqual(expected, actual);
323      Assert.Inconclusive("Verify the correctness of this test method.");
324    }
325
326    /// <summary>
327    ///A test for GetNumberOfUsersOnline
328    ///</summary>
329    [TestMethod()]
330    public void GetNumberOfUsersOnlineTest() {
331      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
332      int expected = 0; // TODO: Initialize to an appropriate value
333      int actual;
334      actual = target.GetNumberOfUsersOnline();
335      Assert.AreEqual(expected, actual);
336      Assert.Inconclusive("Verify the correctness of this test method.");
337    }
338
339    /// <summary>
340    ///A test for GetAllUsers
341    ///
342    /// work in progress
343    ///</summary>
344    [TestMethod()]
345    public void GetAllUsersTest() {
346      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider();
347      MembershipCreateStatus status;
348      int totalRecords;
349
350      // Create some users
351      for (int i = 0; i < 50; i++)
352      {
353          target.CreateUser("User " + i, "newPassword " + i, "testemail " + i, "testquestion " + i, "testanswer " + i, true, null, out status);
354      }
355
356      MembershipUserCollection users = target.GetAllUsers(0, 5, out totalRecords);
357
358      int j = 0;
359      foreach (HeuristicLabUser user in users) {
360        Assert.Equals("User " + j, user.UserName);
361        j++;
362      }
363
364
365    }
366
367    /// <summary>
368    ///A test for FindUsersByName
369    ///</summary>
370    [TestMethod()]
371    public void FindUsersByNameTest() {
372      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
373      string usernameToMatch = string.Empty; // TODO: Initialize to an appropriate value
374      int pageIndex = 0; // TODO: Initialize to an appropriate value
375      int pageSize = 0; // TODO: Initialize to an appropriate value
376      int totalRecords = 0; // TODO: Initialize to an appropriate value
377      int totalRecordsExpected = 0; // TODO: Initialize to an appropriate value
378      MembershipUserCollection expected = null; // TODO: Initialize to an appropriate value
379      MembershipUserCollection actual;
380      actual = target.FindUsersByName(usernameToMatch, pageIndex, pageSize, out totalRecords);
381      Assert.AreEqual(totalRecordsExpected, totalRecords);
382      Assert.AreEqual(expected, actual);
383      Assert.Inconclusive("Verify the correctness of this test method.");
384    }
385
386    /// <summary>
387    ///A test for FindUsersByEmail
388    ///</summary>
389    [TestMethod()]
390    public void FindUsersByEmailTest() {
391      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider(); // TODO: Initialize to an appropriate value
392      string emailToMatch = string.Empty; // TODO: Initialize to an appropriate value
393      int pageIndex = 0; // TODO: Initialize to an appropriate value
394      int pageSize = 0; // TODO: Initialize to an appropriate value
395      int totalRecords = 0; // TODO: Initialize to an appropriate value
396      int totalRecordsExpected = 0; // TODO: Initialize to an appropriate value
397      MembershipUserCollection expected = null; // TODO: Initialize to an appropriate value
398      MembershipUserCollection actual;
399      actual = target.FindUsersByEmail(emailToMatch, pageIndex, pageSize, out totalRecords);
400      Assert.AreEqual(totalRecordsExpected, totalRecords);
401      Assert.AreEqual(expected, actual);
402      Assert.Inconclusive("Verify the correctness of this test method.");
403    }
404
405    /// <summary>
406    ///A test for DeleteUser
407    ///</summary>
408    [TestMethod()]
409    public void DeleteUserTest() {
410      // insert new user
411      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider();
412      MembershipCreateStatus status;
413      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
414      Assert.AreEqual(MembershipCreateStatus.Success, status);
415
416      // delete user
417      Assert.IsTrue(target.DeleteUser("testname", true));
418      Assert.AreEqual(0, db.HeuristicLabUsers.Count(x => x.UserName == "testname"));
419    }
420
421    /// <summary>
422    ///A test for CreateUser
423    ///</summary>
424    [TestMethod()]
425    public void CreateUserTest() {
426      // create user
427      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider();
428      MembershipCreateStatus status;
429      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
430      Assert.AreEqual(MembershipCreateStatus.Success, status);
431
432      // check if user is OK
433      HeuristicLabUser u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
434      Assert.AreEqual<string>("testname", u.UserName);
435      Assert.AreEqual<string>("testemail", u.Email);
436      Assert.AreEqual<string>("newPassword", u.Password);
437      Assert.AreEqual<string>("testquestion", u.PasswordQuestion);
438      Assert.AreEqual<string>("testanswer", u.PasswordAnswer);
439      Assert.AreEqual<string>("", u.Comment);
440
441      // check for duplicate errors
442      target.CreateUser("testname", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
443      Assert.AreEqual(MembershipCreateStatus.DuplicateUserName, status);
444      target.CreateUser("testname2", "newPassword", "testemail", "testquestion", "testanswer", true, null, out status);
445      Assert.AreEqual(MembershipCreateStatus.DuplicateEmail, status);
446    }
447
448    /// <summary>
449    ///A test for ChangePasswordQuestionAndAnswer
450    ///</summary>
451    [TestMethod()]
452    public void ChangePasswordQuestionAndAnswerTest() {
453      // create user
454      HeuristicLabUser u = new HeuristicLabUser("testname", "testemail", "testquestion", "testcomment");
455      db.HeuristicLabUsers.InsertOnSubmit(u);
456      db.SubmitChanges();
457
458      // check if user is stored
459      u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
460      Assert.AreEqual<String>("testquestion", u.PasswordQuestion);
461      Assert.AreEqual<String>("", u.PasswordAnswer);
462
463      // change data and check again
464      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider();
465      // gibt zwar true zurück, was schon mal gut ist
466      Assert.IsTrue(target.ChangePasswordQuestionAndAnswer("testname", "INIT", "newquestion", "newanswer"));
467      // aber hier ist die änderung noch nicht da!! es ist immer noch die alte frage + alte antwort
468      u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
469      Assert.AreEqual<String>("newquestion", u.PasswordQuestion);
470      Assert.AreEqual<String>("newanswer", u.PasswordAnswer);
471    }
472
473    /// <summary>
474    ///A test for ChangePassword
475    ///</summary>
476    [TestMethod()]
477    public void ChangePasswordTest() {
478      // create user
479      HeuristicLabUser u = new HeuristicLabUser("testname", "testemail", "testquestion", "testcomment");
480      db.HeuristicLabUsers.InsertOnSubmit(u);
481      db.SubmitChanges();
482
483      // check if user is stored
484      u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
485      Assert.AreEqual<String>("INIT", u.Password);
486
487      // change data and check again
488      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider();
489      Assert.IsTrue(target.ChangePassword("testname", "INIT", "newPassword"));
490      // hat nix gemacht!! :(
491      u = db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == "testname");
492      Assert.AreEqual<String>("newPassword", u.Password);
493    }
494
495    /// <summary>
496    ///A test for HeuristicLabMembershipProvider Constructor
497    ///</summary>
498    [TestMethod()]
499    public void HeuristicLabMembershipProviderConstructorTest() {
500      HeuristicLabMembershipProvider target = new HeuristicLabMembershipProvider();
501      Assert.Inconclusive("TODO: Implement code to verify target");
502    }
503
504    [TestMethod()]
505    public void NoDuplicateUserNameTest() {
506      try {
507        Persistence.HeuristicLabUser user = new Persistence.HeuristicLabUser("duplicateName", "mail", "myQuestion", "myComment");
508        db.HeuristicLabUsers.InsertOnSubmit(user);
509        Persistence.HeuristicLabUser user2 = new Persistence.HeuristicLabUser("duplicateName", "mail2", "myQuestion2", "myComment2");
510        db.HeuristicLabUsers.InsertOnSubmit(user2);
511        db.SubmitChanges();
512        Assert.Fail();
513      }
514      catch (SqlException) {
515        //swallowing Exception because it is expected that a SQL Exception is thrown       
516      }
517    }
518    [TestMethod()]
519    public void NoDuplicateEmailTest() {
520      try {
521        Persistence.HeuristicLabUser user = new Persistence.HeuristicLabUser("duplicateName", "mail", "myQuestion", "myComment");
522        db.HeuristicLabUsers.InsertOnSubmit(user);
523        Persistence.HeuristicLabUser user2 = new Persistence.HeuristicLabUser("duplicateName2", "mail", "myQuestion2", "myComment2");
524        db.HeuristicLabUsers.InsertOnSubmit(user2);
525        db.SubmitChanges();
526        Assert.Fail();
527      }
528      catch (SqlException) {
529        //swallowing Exception because it is expected that a SQL Exception is thrown       
530      }
531    }
532  }
533}
Note: See TracBrowser for help on using the repository browser.