UserTests.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Copyright 2011 - 2022 Adrian Popescu
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. using System.Collections.Specialized;
  14. using System.Globalization;
  15. using Padi.RedmineApi.Tests.Infrastructure;
  16. using Redmine.Net.Api;
  17. using Redmine.Net.Api.Exceptions;
  18. using Redmine.Net.Api.Types;
  19. using Xunit;
  20. namespace Padi.RedmineApi.Tests.Tests.Sync
  21. {
  22. [Trait("Redmine-Net-Api", "Users")]
  23. #if !(NET20 || NET40)
  24. [Collection("RedmineCollection")]
  25. #endif
  26. [Order(2)]
  27. public class UserTests
  28. {
  29. private readonly RedmineFixture fixture;
  30. public UserTests(RedmineFixture fixture)
  31. {
  32. this.fixture = fixture;
  33. }
  34. private const string USER_LOGIN = "testUser";
  35. private const string USER_FIRST_NAME = "User";
  36. private const string USER_LAST_NAME = "One";
  37. private const string USER_EMAIL = "testUser@mail.com";
  38. private static string createdUserId;
  39. private static string createdUserWithAllPropId;
  40. private static User CreateTestUserWithRequiredPropertiesSet()
  41. {
  42. var user = new User()
  43. {
  44. Login = USER_LOGIN,
  45. FirstName = USER_FIRST_NAME,
  46. LastName = USER_LAST_NAME,
  47. Email = USER_EMAIL,
  48. };
  49. return user;
  50. }
  51. [Fact, Order(1)]
  52. public void Should_Create_User_With_Required_Properties()
  53. {
  54. var savedUser = fixture.RedmineManager.CreateObject(CreateTestUserWithRequiredPropertiesSet());
  55. Assert.NotNull(savedUser);
  56. Assert.NotEqual(0, savedUser.Id);
  57. createdUserId = savedUser.Id.ToString();
  58. Assert.True(savedUser.Login.Equals(USER_LOGIN), "User login is invalid.");
  59. Assert.True(savedUser.FirstName.Equals(USER_FIRST_NAME), "User first name is invalid.");
  60. Assert.True(savedUser.LastName.Equals(USER_LAST_NAME), "User last name is invalid.");
  61. Assert.True(savedUser.Email.Equals(USER_EMAIL), "User email is invalid.");
  62. }
  63. [Fact, Order(2)]
  64. public void Should_Throw_Exception_When_Create_Empty_User()
  65. {
  66. Assert.Throws<RedmineException>(() => fixture.RedmineManager.CreateObject(new User()));
  67. }
  68. [Fact, Order(3)]
  69. public void Should_Create_User_With_All_Properties_Set()
  70. {
  71. var login = "testUserAllProp";
  72. var firstName = "firstName";
  73. var lastName = "lastName";
  74. var email = "email@a.com";
  75. var password = "pass123456";
  76. var mailNotification = "only_assigned";
  77. var savedUser = fixture.RedmineManager.CreateObject(new User()
  78. {
  79. Login = login,
  80. FirstName = firstName,
  81. LastName = lastName,
  82. Email = email,
  83. Password = password,
  84. MustChangePassword = true,
  85. MailNotification = mailNotification
  86. });
  87. Assert.NotNull(savedUser);
  88. Assert.NotEqual(0, savedUser.Id);
  89. createdUserWithAllPropId = savedUser.Id.ToString();
  90. Assert.True(savedUser.Login.Equals(login), "User login is invalid.");
  91. Assert.True(savedUser.FirstName.Equals(firstName), "User first name is invalid.");
  92. Assert.True(savedUser.LastName.Equals(lastName), "User last name is invalid.");
  93. Assert.True(savedUser.Email.Equals(email), "User email is invalid.");
  94. }
  95. [Fact, Order(4)]
  96. public void Should_Get_Created_User_With_Required_Fields()
  97. {
  98. var user = fixture.RedmineManager.GetObject<User>(createdUserId, null);
  99. Assert.NotNull(user);
  100. Assert.IsType<User>(user);
  101. Assert.True(user.Login.Equals(USER_LOGIN), "User login is invalid.");
  102. Assert.True(user.FirstName.Equals(USER_FIRST_NAME), "User first name is invalid.");
  103. Assert.True(user.LastName.Equals(USER_LAST_NAME), "User last name is invalid.");
  104. Assert.True(user.Email.Equals(USER_EMAIL), "User email is invalid.");
  105. }
  106. [Fact, Order(5)]
  107. public void Should_Update_User()
  108. {
  109. const string UPDATED_USER_FIRST_NAME = "UpdatedFirstName";
  110. const string UPDATED_USER_LAST_NAME = "UpdatedLastName";
  111. const string UPDATED_USER_EMAIL = "updatedEmail@mail.com";
  112. var user = fixture.RedmineManager.GetObject<User>("8", null);
  113. user.FirstName = UPDATED_USER_FIRST_NAME;
  114. user.LastName = UPDATED_USER_LAST_NAME;
  115. user.Email = UPDATED_USER_EMAIL;
  116. var exception =
  117. (RedmineException)
  118. Record.Exception(() => fixture.RedmineManager.UpdateObject("8", user));
  119. Assert.Null(exception);
  120. var updatedUser = fixture.RedmineManager.GetObject<User>("8", null);
  121. Assert.True(updatedUser.FirstName.Equals(UPDATED_USER_FIRST_NAME), "User first name was not updated.");
  122. Assert.True(updatedUser.LastName.Equals(UPDATED_USER_LAST_NAME), "User last name was not updated.");
  123. Assert.True(updatedUser.Email.Equals(UPDATED_USER_EMAIL), "User email was not updated.");
  124. // curl -v --user zapadi:1qaz2wsx -H 'Content-Type: application/json' -X PUT -d '{"user":{"login":"testuser","firstname":"UpdatedFirstName","lastname":"UpdatedLastName","mail":"updatedEmail@mail.com","must_change_passwd":"false","status":"1"}}' http://192.168.1.53:8089/users/8.json
  125. }
  126. [Fact, Order(6)]
  127. public void Should_Not_Update_User_With_Invalid_Properties()
  128. {
  129. var user = fixture.RedmineManager.GetObject<User>(createdUserId, null);
  130. user.FirstName = "";
  131. Assert.Throws<RedmineException>(() => fixture.RedmineManager.UpdateObject(createdUserId, user));
  132. }
  133. [Fact, Order(7)]
  134. public void Should_Delete_User()
  135. {
  136. var exception =
  137. (RedmineException)
  138. Record.Exception(() => fixture.RedmineManager.DeleteObject<User>(createdUserId));
  139. Assert.Null(exception);
  140. Assert.Throws<NotFoundException>(() => fixture.RedmineManager.GetObject<User>(createdUserId, null));
  141. }
  142. [Fact, Order(8)]
  143. public void Should_Delete_User_Created_With_All_Properties_Set()
  144. {
  145. var exception =
  146. (RedmineException)
  147. Record.Exception(() => fixture.RedmineManager.DeleteObject<User>(createdUserWithAllPropId));
  148. Assert.Null(exception);
  149. Assert.Throws<NotFoundException>(() => fixture.RedmineManager.GetObject<User>(createdUserWithAllPropId, null));
  150. }
  151. [Fact, Order(9)]
  152. public void Should_Get_Current_User()
  153. {
  154. var currentUser = fixture.RedmineManager.GetCurrentUser();
  155. Assert.NotNull(currentUser);
  156. Assert.Equal(currentUser.ApiKey, fixture.Credentials.ApiKey);
  157. }
  158. [Fact, Order(10)]
  159. public void Should_Get_X_Users_From_Offset_Y()
  160. {
  161. var result = fixture.RedmineManager.GetPaginatedObjects<User>(new NameValueCollection()
  162. {
  163. {RedmineKeys.INCLUDE, RedmineKeys.GROUPS + "," + RedmineKeys.MEMBERSHIPS},
  164. {RedmineKeys.LIMIT, "2"},
  165. {RedmineKeys.OFFSET, "1"}
  166. });
  167. Assert.NotNull(result);
  168. }
  169. [Fact, Order(11)]
  170. public void Should_Get_Users_By_State()
  171. {
  172. var users = fixture.RedmineManager.GetObjects<User>(new NameValueCollection()
  173. {
  174. {RedmineKeys.STATUS, ((int) UserStatus.StatusActive).ToString(CultureInfo.InvariantCulture)}
  175. });
  176. Assert.NotNull(users);
  177. }
  178. }
  179. }