UserAsyncTests.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #if !(NET20 || NET40)
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Redmine.Net.Api;
  9. using Redmine.Net.Api.Async;
  10. using Redmine.Net.Api.Exceptions;
  11. using Redmine.Net.Api.Types;
  12. using Xunit;
  13. namespace Padi.RedmineApi.Tests.Tests.Async
  14. {
  15. [Collection("RedmineCollection")]
  16. public class UserAsyncTests
  17. {
  18. private const string USER_ID = "8";
  19. private const string LIMIT = "2";
  20. private const string OFFSET = "1";
  21. private const int GROUP_ID = 9;
  22. private readonly RedmineFixture fixture;
  23. public UserAsyncTests(RedmineFixture fixture)
  24. {
  25. this.fixture = fixture;
  26. }
  27. [Fact]
  28. public async Task Should_Get_CurrentUser()
  29. {
  30. var currentUser = await fixture.RedmineManager.GetCurrentUserAsync();
  31. Assert.NotNull(currentUser);
  32. }
  33. [Fact]
  34. public async Task Should_Get_User_By_Id()
  35. {
  36. var user = await fixture.RedmineManager.GetObjectAsync<User>(USER_ID, null);
  37. Assert.NotNull(user);
  38. }
  39. [Fact]
  40. public async Task Should_Get_User_By_Id_Including_Groups_And_Memberships()
  41. {
  42. var user = await fixture.RedmineManager.GetObjectAsync<User>(USER_ID, new NameValueCollection() { { RedmineKeys.INCLUDE, "groups,memberships" } });
  43. Assert.NotNull(user);
  44. Assert.NotNull(user.Groups);
  45. Assert.True(user.Groups.Count == 1, "Group count != 1");
  46. Assert.NotNull(user.Memberships);
  47. Assert.True(user.Memberships.Count == 3, "Membership count != 3");
  48. }
  49. [Fact]
  50. public async Task Should_Get_X_Users_From_Offset_Y()
  51. {
  52. var result = await fixture.RedmineManager.GetPaginatedObjectsAsync<User>(new NameValueCollection() {
  53. { RedmineKeys.INCLUDE, "groups, memberships" },
  54. {RedmineKeys.LIMIT,LIMIT },
  55. {RedmineKeys.OFFSET,OFFSET }
  56. });
  57. Assert.NotNull(result);
  58. Assert.All(result.Items, u => Assert.IsType<User>(u));
  59. }
  60. [Fact]
  61. public async Task Should_Get_All_Users_With_Groups_And_Memberships()
  62. {
  63. var users = await fixture.RedmineManager.GetObjectsAsync<User>(new NameValueCollection { { RedmineKeys.INCLUDE, "groups, memberships" } });
  64. Assert.NotNull(users);
  65. Assert.All(users, u => Assert.IsType<User>(u));
  66. }
  67. [Fact]
  68. public async Task Should_Get_Active_Users()
  69. {
  70. var users = await fixture.RedmineManager.GetObjectsAsync<User>(new NameValueCollection()
  71. {
  72. { RedmineKeys.STATUS, ((int)UserStatus.StatusActive).ToString(CultureInfo.InvariantCulture) }
  73. });
  74. Assert.NotNull(users);
  75. Assert.True(users.Count == 6);
  76. Assert.All(users, u => Assert.IsType<User>(u));
  77. }
  78. [Fact]
  79. public async Task Should_Get_Anonymous_Users()
  80. {
  81. var users = await fixture.RedmineManager.GetObjectsAsync<User>(new NameValueCollection()
  82. {
  83. { RedmineKeys.STATUS, ((int)UserStatus.StatusAnonymous).ToString(CultureInfo.InvariantCulture) }
  84. });
  85. Assert.NotNull(users);
  86. Assert.True(users.Count == 0);
  87. Assert.All(users, u => Assert.IsType<User>(u));
  88. }
  89. [Fact]
  90. public async Task Should_Get_Locked_Users()
  91. {
  92. var users = await fixture.RedmineManager.GetObjectsAsync<User>(new NameValueCollection()
  93. {
  94. { RedmineKeys.STATUS, ((int)UserStatus.StatusLocked).ToString(CultureInfo.InvariantCulture) }
  95. });
  96. Assert.NotNull(users);
  97. Assert.True(users.Count == 1);
  98. Assert.All(users, u => Assert.IsType<User>(u));
  99. }
  100. [Fact]
  101. public async Task Should_Get_Registered_Users()
  102. {
  103. var users = await fixture.RedmineManager.GetObjectsAsync<User>(new NameValueCollection()
  104. {
  105. { RedmineKeys.STATUS, ((int)UserStatus.StatusRegistered).ToString(CultureInfo.InvariantCulture) }
  106. });
  107. Assert.NotNull(users);
  108. Assert.True(users.Count == 1);
  109. Assert.All(users, u => Assert.IsType<User>(u));
  110. }
  111. [Fact]
  112. public async Task Should_Get_Users_By_Group()
  113. {
  114. var users = await fixture.RedmineManager.GetObjectsAsync<User>(new NameValueCollection()
  115. {
  116. {RedmineKeys.GROUP_ID, GROUP_ID.ToString(CultureInfo.InvariantCulture)}
  117. });
  118. Assert.NotNull(users);
  119. Assert.True(users.Count == 3);
  120. Assert.All(users, u => Assert.IsType<User>(u));
  121. }
  122. [Fact]
  123. public async Task Should_Add_User_To_Group()
  124. {
  125. await fixture.RedmineManager.AddUserToGroupAsync(GROUP_ID, int.Parse(USER_ID));
  126. var user = fixture.RedmineManager.GetObject<User>(USER_ID.ToString(CultureInfo.InvariantCulture), new NameValueCollection { { RedmineKeys.INCLUDE, RedmineKeys.GROUPS } });
  127. Assert.NotNull(user.Groups);
  128. Assert.True(user.Groups.FirstOrDefault(g => g.Id == GROUP_ID) != null);
  129. }
  130. [Fact]
  131. public async Task Should_Remove_User_From_Group()
  132. {
  133. await fixture.RedmineManager.RemoveUserFromGroupAsync(GROUP_ID, int.Parse(USER_ID));
  134. var user = await fixture.RedmineManager.GetObjectAsync<User>(USER_ID.ToString(CultureInfo.InvariantCulture), new NameValueCollection { { RedmineKeys.INCLUDE, RedmineKeys.GROUPS } });
  135. Assert.True(user.Groups == null || user.Groups.FirstOrDefault(g => g.Id == GROUP_ID) == null);
  136. }
  137. [Fact]
  138. public async Task Should_Create_User()
  139. {
  140. var user = new User
  141. {
  142. Login = "userTestLogin4",
  143. FirstName = "userTestFirstName",
  144. LastName = "userTestLastName",
  145. Email = "testTest4@redmineapi.com",
  146. Password = "123456",
  147. AuthenticationModeId = 1,
  148. MustChangePassword = false
  149. };
  150. var icf = (IssueCustomField)IdentifiableName.Create<IssueCustomField>(4);
  151. icf.Values = new List<CustomFieldValue> { new CustomFieldValue { Info = "userTestCustomField:" + DateTime.UtcNow } };
  152. user.CustomFields = new List<IssueCustomField>();
  153. user.CustomFields.Add(icf);
  154. var createdUser = await fixture.RedmineManager.CreateObjectAsync(user);
  155. Assert.Equal(user.Login, createdUser.Login);
  156. Assert.Equal(user.Email, createdUser.Email);
  157. }
  158. [Fact]
  159. public async Task Should_Update_User()
  160. {
  161. var userId = 59.ToString();
  162. var user = fixture.RedmineManager.GetObject<User>(userId, null);
  163. user.FirstName = "modified first name";
  164. await fixture.RedmineManager.UpdateObjectAsync(userId, user);
  165. var updatedUser = await fixture.RedmineManager.GetObjectAsync<User>(userId, null);
  166. Assert.Equal(user.FirstName, updatedUser.FirstName);
  167. }
  168. [Fact]
  169. public async Task Should_Delete_User()
  170. {
  171. var userId = 62.ToString();
  172. await fixture.RedmineManager.DeleteObjectAsync<User>(userId);
  173. await Assert.ThrowsAsync<NotFoundException>(async () => await fixture.RedmineManager.GetObjectAsync<User>(userId, null));
  174. }
  175. }
  176. }
  177. #endif