PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/1.31/src/Tests/Platform/MembershipApiTests.cs

#
C# | 439 lines | 340 code | 70 blank | 29 comment | 5 complexity | 2fd26ea29663f6a487a509b30827fc1b MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. /****************************************************************************************************
  2. Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
  3. Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ****************************************************************************************************/
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Data.Linq;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Linq.Expressions;
  21. using System.Text;
  22. using System.Transactions;
  23. using System.Web.UI;
  24. using System.Web.UI.WebControls;
  25. using System.Xml;
  26. using System.Xml.Schema;
  27. using BaoJianSoft.Common;
  28. using BaoJianSoft.Common.Caching;
  29. using BaoJianSoft.Common.Data;
  30. using BaoJianSoft.Common.Validation;
  31. using BaoJianSoft.Platform;
  32. using BaoJianSoft.Platform.Initialization;
  33. using BaoJianSoft.Platform.Linq;
  34. using BaoJianSoft.RapidWeb;
  35. using BaoJianSoft.RapidWeb.Controls;
  36. using BaoJianSoft.RapidWeb.DynamicPages;
  37. using NUnit.Framework;
  38. using Rhino.Mocks;
  39. using Rhino.Mocks.Interfaces;
  40. namespace BaoJianSoft.Tests.Platform
  41. {
  42. [TestFixture]
  43. public class MembershipApiTests
  44. {
  45. private List<Guid> createdObjectIds = new List<Guid>();
  46. [TearDown]
  47. public void TearDown()
  48. {
  49. using (MembershipDataContext ctx = DataContextFactory.Create<MembershipDataContext>())
  50. {
  51. foreach (Guid createdObjectId in createdObjectIds)
  52. {
  53. ctx.Memberships.Delete(m => m.UserId == createdObjectId);
  54. ctx.Users.Delete(u => u.UserId == createdObjectId);
  55. ctx.SubmitChanges();
  56. }
  57. }
  58. createdObjectIds.Clear();
  59. }
  60. [Test, Description("Basic membership CRUD test")]
  61. public void BasicTest()
  62. {
  63. IMembershipApi membershipApi = SpringContext.Current.GetObject<IMembershipApi>();
  64. Guid userId = CreateUserWithExtensionProperties(membershipApi);
  65. createdObjectIds.Add(userId);
  66. Assert.IsNotNull(membershipApi.Get("Eunge"));
  67. UserObject resolvedUserObject = membershipApi.Get(userId);
  68. Assert.AreEqual("Eunge", resolvedUserObject.UserName);
  69. Assert.AreEqual("eunge.liu@gmail.com", resolvedUserObject.Email);
  70. Assert.AreEqual("Eunge Liu", resolvedUserObject.DisplayName);
  71. Assert.AreEqual("IT specialist", resolvedUserObject.Comment);
  72. Assert.IsTrue(resolvedUserObject.IsApproved);
  73. Assert.AreEqual(new DateTime(1982, 2, 7), resolvedUserObject["Birthday"]);
  74. Assert.AreEqual("Male", resolvedUserObject["Sex"]);
  75. Assert.AreEqual("51010419820207XXXX", resolvedUserObject["IdentityNo"]);
  76. Assert.AreEqual("200708200002", resolvedUserObject["EmployeeNo"]);
  77. Assert.AreEqual("Simulation", resolvedUserObject["Department"]);
  78. Assert.AreEqual("Team Lead", resolvedUserObject["Position"]);
  79. Assert.AreEqual("021-647660XX", resolvedUserObject["PhoneNo"]);
  80. Assert.AreEqual("ShangHai", resolvedUserObject["City"]);
  81. Assert.AreEqual("MeiLong 2nd Cun, MingHang District", resolvedUserObject["Address"]);
  82. Assert.AreEqual("210000", resolvedUserObject["ZipCode"]);
  83. resolvedUserObject = membershipApi.Get("Eunge");
  84. Assert.AreEqual(userId, resolvedUserObject.UserId);
  85. resolvedUserObject["PhoneNo"] = "1376418AAAA";
  86. membershipApi.Save(resolvedUserObject, null, null);
  87. resolvedUserObject = membershipApi.Get(userId);
  88. Assert.AreEqual("Eunge", resolvedUserObject.UserName);
  89. Assert.AreEqual("eunge.liu@gmail.com", resolvedUserObject.Email);
  90. Assert.AreEqual("1376418AAAA", resolvedUserObject["PhoneNo"]);
  91. Assert.AreEqual(LoginResults.Successful, membershipApi.Login("eunge", "password1"));
  92. membershipApi.ChangePassword(userId, "password1", "password2");
  93. Assert.AreEqual(LoginResults.Successful, membershipApi.Login("eunge", "password2"));
  94. }
  95. [Test, Description("Update body of a user from nonnull to null test")]
  96. public void SaveUserWithOutExtensionProperties()
  97. {
  98. MockRepository mockRepository = new MockRepository();
  99. IOrganizationApi organizationApi = mockRepository.StrictMock<IOrganizationApi>();
  100. SetupResult.For(organizationApi.GetOrganization(Guid.Empty)).IgnoreArguments().Return(new OrganizationObject());
  101. mockRepository.ReplayAll();
  102. UserObject userObject = new UserObject
  103. {
  104. Comment = "IT specialist",
  105. DisplayName = "Eunge Liu",
  106. Email = "eunge.liu@gmail.com",
  107. IsApproved = true,
  108. MobilePin = "137641855XX",
  109. UserName = "Eunge"
  110. };
  111. IMembershipApi membershipApi = new MembershipApi(SpringContext.Current.GetObject<IAuthenticationContext>(), organizationApi);
  112. membershipApi.Save(userObject, "password1", null);
  113. createdObjectIds.Add(userObject.UserId);
  114. userObject = membershipApi.Get(userObject.UserId);
  115. userObject.DisplayName = "Eunge";
  116. membershipApi.Save(userObject, null, null);
  117. userObject = membershipApi.Get(userObject.UserId);
  118. Assert.AreEqual("Eunge", userObject.DisplayName);
  119. }
  120. [Test, Description("Save a user with existed display name test - duplicate display name is allowed.")]
  121. [ExpectedException(typeof(ValidationException))]
  122. public void SaveUserWithExistedDisplayNameTest()
  123. {
  124. MockRepository mockRepository = new MockRepository();
  125. IOrganizationApi organizationApi = mockRepository.StrictMock<IOrganizationApi>();
  126. SetupResult.For(organizationApi.GetOrganization(Guid.Empty)).IgnoreArguments().Return(new OrganizationObject());
  127. mockRepository.ReplayAll();
  128. UserObject userObject = new UserObject
  129. {
  130. Comment = "IT specialist",
  131. DisplayName = "Eunge Liu",
  132. Email = "eunge.liu@gmail.com",
  133. IsApproved = true,
  134. MobilePin = "137641855XX",
  135. UserName = "Eunge"
  136. };
  137. IMembershipApi membershipApi = new MembershipApi(SpringContext.Current.GetObject<IAuthenticationContext>(), organizationApi);
  138. membershipApi.Save(userObject, "password1", null);
  139. createdObjectIds.Add(userObject.UserId);
  140. Assert.IsNotNull(membershipApi.Get(userObject.UserName));
  141. UserObject duplicateUserObject = new UserObject
  142. {
  143. Comment = "IT specialist",
  144. DisplayName = "Eunge Liu",
  145. Email = "lucy.liu@gmail.com",
  146. IsApproved = true,
  147. MobilePin = "137641855XX",
  148. UserName = "Lucy"
  149. };
  150. membershipApi.Save(duplicateUserObject, "password1", null);
  151. }
  152. [Test, Description("Add multiple users first, then validate the query against them.")]
  153. public void FindUserTest()
  154. {
  155. IMembershipApi membershipApi = SpringContext.Current.GetObject<IMembershipApi>();
  156. this.CreateUser(membershipApi);
  157. this.CreateUser(membershipApi);
  158. this.CreateUser(membershipApi);
  159. this.CreateUser(membershipApi);
  160. int recordCount;
  161. Expression<Func<User, bool>> predicate = user => user.UserName != "Admin" && user.UserName != "Anonymous";
  162. IEnumerable<UserObject> userObjects = membershipApi.FindUsers(predicate, null, "UserName", 0, 10, out recordCount);
  163. Assert.AreEqual(4, recordCount);
  164. Assert.AreEqual(4, userObjects.Count());
  165. userObjects = membershipApi.FindUsers(predicate, null, "DisplayName", 0, 3, out recordCount);
  166. Assert.AreEqual(4, recordCount);
  167. Assert.AreEqual(3, userObjects.Count());
  168. }
  169. [Test, Description("Test ICache usage while adding an user by membership APIs.")]
  170. public void CacheCallingWhenAddUserTest()
  171. {
  172. IAuthenticationContext authenticationContext = SpringContext.Current.GetObject<IAuthenticationContext>();
  173. MockRepository mockRepository = new MockRepository();
  174. IOrganizationApi organizationApi = mockRepository.StrictMock<IOrganizationApi>();
  175. ICache cacheInstance = mockRepository.StrictMock<ICache>();
  176. MembershipApi membershipApi = new MembershipApi(authenticationContext, organizationApi) { Cache = cacheInstance };
  177. using (mockRepository.Record())
  178. using (mockRepository.Ordered())
  179. {
  180. organizationApi.Expect(api => api.GetOrganization(Guid.Empty)).IgnoreArguments().Return(new OrganizationObject());
  181. // add the pair (id, user)
  182. cacheInstance.Add(null, null, TimeSpan.MinValue, CachePriorityTypes.Default);
  183. LastCall.IgnoreArguments();
  184. // add the pair (name, user)
  185. cacheInstance.Add(null, null, TimeSpan.MinValue, CachePriorityTypes.Default);
  186. LastCall.IgnoreArguments();
  187. }
  188. using (mockRepository.Playback())
  189. {
  190. this.CreateUser(membershipApi);
  191. }
  192. }
  193. [Test, Description("Test ICache usage while updating an existed user by membership APIs.")]
  194. public void CacheCallingWhenUpdateUserTest()
  195. {
  196. IAuthenticationContext authenticationContext = SpringContext.Current.GetObject<IAuthenticationContext>();
  197. MockRepository mockRepository = new MockRepository();
  198. IOrganizationApi organizationApi = mockRepository.StrictMock<IOrganizationApi>();
  199. SetupResult.For(organizationApi.GetOrganization(Guid.Empty)).IgnoreArguments().Return(new OrganizationObject());
  200. mockRepository.ReplayAll();
  201. MembershipApi membershipApi = new MembershipApi(authenticationContext, organizationApi);
  202. Guid userId = this.CreateUser(membershipApi);
  203. UserObject userObject = membershipApi.Get(userId);
  204. organizationApi = mockRepository.StrictMock<IOrganizationApi>();
  205. ICache cacheInstance = mockRepository.StrictMock<ICache>();
  206. membershipApi = new MembershipApi(authenticationContext, organizationApi);
  207. membershipApi.Cache = cacheInstance;
  208. using (mockRepository.Record())
  209. using (mockRepository.Ordered())
  210. {
  211. // get UserObject by UserId, then removes cache for original user name
  212. cacheInstance.Expect(cache => cache.Get(null)).IgnoreArguments().Return(userObject);
  213. // remove the cache for original user name
  214. cacheInstance.Expect(cache => cache.Remove(null)).IgnoreArguments();
  215. organizationApi.Expect(api => api.GetOrganization(Guid.Empty)).IgnoreArguments().Return(new OrganizationObject());
  216. Action<MethodInvocation> methodInvocationCallback = methodInvocation =>
  217. {
  218. Assert.AreEqual(1, methodInvocation.Arguments.Length);
  219. Assert.AreEqual(typeof(string), methodInvocation.Arguments[0].GetType());
  220. };
  221. // Add pair of (UserId, UserObject) to cache
  222. cacheInstance.Expect(cache => cache.Add(null, null, TimeSpan.Zero, CachePriorityTypes.Normal)).IgnoreArguments();
  223. // Add pair of (UserName, UserObject) to cache
  224. cacheInstance.Expect(cache => cache.Add(null, null, TimeSpan.Zero, CachePriorityTypes.Normal)).IgnoreArguments();
  225. }
  226. using (mockRepository.Playback())
  227. {
  228. userObject.UserName = "Liue";
  229. membershipApi.Save(userObject, "password1", null);
  230. }
  231. }
  232. [Test, Description("Test ICache usage while getting an existed user by id through membership APIs.")]
  233. public void CacheCallingWhenGetUserByIdTest()
  234. {
  235. IAuthenticationContext authenticationContext = SpringContext.Current.GetObject<IAuthenticationContext>();
  236. MockRepository mockRepository = new MockRepository();
  237. ICache cacheInstance = mockRepository.StrictMock<ICache>();
  238. IOrganizationApi organizationApi = mockRepository.DynamicMock<IOrganizationApi>();
  239. SetupResult.For(organizationApi.GetOrganization(Guid.Empty)).IgnoreArguments().Return(new OrganizationObject());
  240. mockRepository.ReplayAll();
  241. MembershipApi membershipApi = new MembershipApi(authenticationContext, organizationApi);
  242. Guid userId = this.CreateUser(membershipApi);
  243. membershipApi.Cache = cacheInstance;
  244. using (mockRepository.Record())
  245. using (mockRepository.Ordered())
  246. {
  247. Action<MethodInvocation> methodInvocationCallback = methodInvocation =>
  248. {
  249. Assert.AreEqual(1, methodInvocation.Arguments.Length);
  250. Assert.AreEqual(typeof(string), methodInvocation.Arguments[0].GetType());
  251. };
  252. // get UserObject by UserId, returns null from the cache
  253. cacheInstance.Expect(cache => cache.Get(null)).IgnoreArguments().WhenCalled(methodInvocationCallback).Return(null);
  254. methodInvocationCallback = methodInvocation =>
  255. {
  256. Assert.AreEqual(4, methodInvocation.Arguments.Length);
  257. Assert.AreEqual(typeof(string), methodInvocation.Arguments[0].GetType());
  258. Assert.AreEqual(typeof(UserObject), methodInvocation.Arguments[1].GetType());
  259. };
  260. // Add pair of (UserId, UserObject) to cache
  261. cacheInstance.Expect(cache => cache.Add(null, null, TimeSpan.Zero, CachePriorityTypes.Normal)).IgnoreArguments().WhenCalled(methodInvocationCallback);
  262. methodInvocationCallback = methodInvocation =>
  263. {
  264. Assert.AreEqual(4, methodInvocation.Arguments.Length);
  265. Assert.AreEqual(typeof(string), methodInvocation.Arguments[0].GetType());
  266. Assert.AreEqual(typeof(UserObject), methodInvocation.Arguments[1].GetType());
  267. };
  268. // Add pair of (UserName, UserObject) to cache
  269. cacheInstance.Expect(cache => cache.Add(null, null, TimeSpan.Zero, CachePriorityTypes.Normal)).IgnoreArguments().WhenCalled(methodInvocationCallback);
  270. }
  271. using (mockRepository.Playback())
  272. {
  273. membershipApi.Get(userId);
  274. }
  275. }
  276. [Test, Description("Test ICache usage while getting an existed user by name through membership APIs.")]
  277. public void CacheCallingWhenGetUserByNameTest()
  278. {
  279. IAuthenticationContext authenticationContext = SpringContext.Current.GetObject<IAuthenticationContext>();
  280. MockRepository mockRepository = new MockRepository();
  281. ICache cacheInstance = mockRepository.StrictMock<ICache>();
  282. IOrganizationApi organizationApi = mockRepository.DynamicMock<IOrganizationApi>();
  283. SetupResult.For(organizationApi.GetOrganization(Guid.Empty)).IgnoreArguments().Return(new OrganizationObject());
  284. mockRepository.ReplayAll();
  285. MembershipApi membershipApi = new MembershipApi(authenticationContext, organizationApi);
  286. Guid userId = this.CreateUser(membershipApi);
  287. UserObject userObject = membershipApi.Get(userId);
  288. string userName = userObject.UserName;
  289. membershipApi.Cache = cacheInstance;
  290. using (mockRepository.Record())
  291. using (mockRepository.Ordered())
  292. {
  293. Action<MethodInvocation> methodInvocationCallback = methodInvocation =>
  294. {
  295. Assert.AreEqual(1, methodInvocation.Arguments.Length);
  296. Assert.AreEqual(typeof(string), methodInvocation.Arguments[0].GetType());
  297. };
  298. // get UserObject by user name, returns null from the cache
  299. cacheInstance.Expect(cache => cache.Get(null)).IgnoreArguments().WhenCalled(methodInvocationCallback).Return(null);
  300. methodInvocationCallback = methodInvocation =>
  301. {
  302. Assert.AreEqual(4, methodInvocation.Arguments.Length);
  303. Assert.AreEqual(typeof(string), methodInvocation.Arguments[0].GetType());
  304. Assert.AreEqual(typeof(UserObject), methodInvocation.Arguments[1].GetType());
  305. };
  306. // Add pair of (UserId, UserObject) to cache
  307. cacheInstance.Expect(cache => cache.Add(null, null, TimeSpan.Zero, CachePriorityTypes.Normal)).IgnoreArguments().WhenCalled(methodInvocationCallback);
  308. methodInvocationCallback = methodInvocation =>
  309. {
  310. Assert.AreEqual(4, methodInvocation.Arguments.Length);
  311. Assert.AreEqual(typeof(string), methodInvocation.Arguments[0].GetType());
  312. Assert.AreEqual(typeof(UserObject), methodInvocation.Arguments[1].GetType());
  313. };
  314. // Add pair of (UserName, UserObject) to cache
  315. cacheInstance.Expect(cache => cache.Add(null, null, TimeSpan.Zero, CachePriorityTypes.Normal)).IgnoreArguments().WhenCalled(methodInvocationCallback);
  316. }
  317. using (mockRepository.Playback())
  318. {
  319. membershipApi.Get(userName);
  320. }
  321. }
  322. private static Guid CreateUserWithExtensionProperties(IMembershipApi membershipApi)
  323. {
  324. IPlatformConfiguration platformConfiguration = SpringContext.Current.GetObject<IPlatformConfiguration>();
  325. UserObject userObject = new UserObject
  326. {
  327. OrganizationId = platformConfiguration.Organization.OrganizationId,
  328. Comment = "IT specialist",
  329. DisplayName = "Eunge Liu",
  330. Email = "eunge.liu@gmail.com",
  331. IsApproved = true,
  332. MobilePin = "137641855XX",
  333. UserName = "Eunge"
  334. };
  335. userObject["Birthday"] = new DateTime(1982, 2, 7);
  336. userObject["Sex"] = "Male";
  337. userObject["IdentityNo"] = "51010419820207XXXX";
  338. userObject["EmployeeNo"] = "200708200002";
  339. userObject["Department"] = "Simulation";
  340. userObject["Position"] = "Team Lead";
  341. userObject["PhoneNo"] = "021-647660XX";
  342. userObject["City"] = "ShangHai";
  343. userObject["Address"] = "MeiLong 2nd Cun, MingHang District";
  344. userObject["ZipCode"] = "210000";
  345. membershipApi.Save(userObject, "password1", null);
  346. return userObject.UserId;
  347. }
  348. private Guid CreateUser(IMembershipApi membershipApi)
  349. {
  350. IPlatformConfiguration platformConfiguration = SpringContext.Current.GetObject<IPlatformConfiguration>();
  351. UserObject userObject = new UserObject
  352. {
  353. OrganizationId = platformConfiguration.Organization.OrganizationId,
  354. Comment = "IT specialist",
  355. DisplayName = string.Format("DisplayName {0}", Guid.NewGuid()),
  356. Email = "eunge.liu@gmail.com",
  357. IsApproved = true,
  358. MobilePin = "137641855XX",
  359. UserName = string.Format("UserName {0}", Guid.NewGuid())
  360. };
  361. membershipApi.Save(userObject, "password1", null);
  362. createdObjectIds.Add(userObject.UserId);
  363. return userObject.UserId;
  364. }
  365. }
  366. }