ProjectMembershipTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.Generic;
  14. using System.Collections.Specialized;
  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", "ProjectMemberships")]
  23. #if !(NET20 || NET40)
  24. [Collection("RedmineCollection")]
  25. #endif
  26. public class ProjectMembershipTests
  27. {
  28. public ProjectMembershipTests(RedmineFixture fixture)
  29. {
  30. this.fixture = fixture;
  31. }
  32. private readonly RedmineFixture fixture;
  33. private const string PROJECT_IDENTIFIER = "redmine-net-testq";
  34. [Fact, Order(1)]
  35. public void Should_Add_Project_Membership()
  36. {
  37. const int NEW_PROJECT_MEMBERSHIP_USER_ID = 2;
  38. const int NEW_PROJECT_MEMBERSHIP_ROLE_ID = 5;
  39. var pm = new ProjectMembership
  40. {
  41. User = IdentifiableName.Create<IdentifiableName>(NEW_PROJECT_MEMBERSHIP_USER_ID),
  42. Roles = new List<MembershipRole> { (MembershipRole)IdentifiableName.Create<MembershipRole>(NEW_PROJECT_MEMBERSHIP_ROLE_ID)}
  43. };
  44. var createdPm = fixture.RedmineManager.CreateObject(pm, PROJECT_IDENTIFIER);
  45. Assert.NotNull(createdPm);
  46. Assert.True(createdPm.User.Id == NEW_PROJECT_MEMBERSHIP_USER_ID, "User is invalid.");
  47. Assert.NotNull(createdPm.Roles);
  48. //Assert.True(createdPm.Roles.Exists(r => r.Id == NEW_PROJECT_MEMBERSHIP_ROLE_ID),
  49. // string.Format("Role id {0} does not exist.", NEW_PROJECT_MEMBERSHIP_ROLE_ID));
  50. }
  51. [Fact,Order(99)]
  52. public void Should_Delete_Project_Membership()
  53. {
  54. const string DELETED_PROJECT_MEMBERSHIP_ID = "142";
  55. var exception =
  56. (RedmineException)
  57. Record.Exception(
  58. () =>
  59. fixture.RedmineManager.DeleteObject<ProjectMembership>(DELETED_PROJECT_MEMBERSHIP_ID));
  60. Assert.Null(exception);
  61. Assert.Throws<NotFoundException>(
  62. () => fixture.RedmineManager.GetObject<ProjectMembership>(DELETED_PROJECT_MEMBERSHIP_ID, null));
  63. }
  64. [Fact, Order(2)]
  65. public void Should_Get_Memberships_By_Project_Identifier()
  66. {
  67. const int NUMBER_OF_PROJECT_MEMBERSHIPS = 3;
  68. var projectMemberships =
  69. fixture.RedmineManager.GetObjects<ProjectMembership>(new NameValueCollection
  70. {
  71. {RedmineKeys.PROJECT_ID, PROJECT_IDENTIFIER}
  72. });
  73. Assert.NotNull(projectMemberships);
  74. Assert.True(projectMemberships.Count == NUMBER_OF_PROJECT_MEMBERSHIPS,
  75. "Project memberships count ( "+ projectMemberships.Count +" ) != " + NUMBER_OF_PROJECT_MEMBERSHIPS);
  76. }
  77. [Fact, Order(3)]
  78. public void Should_Get_Project_Membership_By_Id()
  79. {
  80. const string PROJECT_MEMBERSHIP_ID = "143";
  81. var projectMembership = fixture.RedmineManager.GetObject<ProjectMembership>(PROJECT_MEMBERSHIP_ID, null);
  82. Assert.NotNull(projectMembership);
  83. Assert.NotNull(projectMembership.Project);
  84. Assert.True(projectMembership.User != null || projectMembership.Group != null,
  85. "User and group are both null.");
  86. Assert.NotNull(projectMembership.Roles);
  87. }
  88. [Fact, Order(4)]
  89. public void Should_Update_Project_Membership()
  90. {
  91. const string UPDATED_PROJECT_MEMBERSHIP_ID = "143";
  92. const int UPDATED_PROJECT_MEMBERSHIP_ROLE_ID = 4;
  93. var pm = fixture.RedmineManager.GetObject<ProjectMembership>(UPDATED_PROJECT_MEMBERSHIP_ID, null);
  94. pm.Roles.Add((MembershipRole)IdentifiableName.Create<MembershipRole>(UPDATED_PROJECT_MEMBERSHIP_ROLE_ID));
  95. fixture.RedmineManager.UpdateObject(UPDATED_PROJECT_MEMBERSHIP_ID, pm);
  96. var updatedPm = fixture.RedmineManager.GetObject<ProjectMembership>(UPDATED_PROJECT_MEMBERSHIP_ID, null);
  97. Assert.NotNull(updatedPm);
  98. Assert.NotNull(updatedPm.Roles);
  99. //Assert.True(updatedPm.Roles.Find(r => r.Id == UPDATED_PROJECT_MEMBERSHIP_ROLE_ID) != null,
  100. // string.Format("Role with id {0} was not found in roles list.", UPDATED_PROJECT_MEMBERSHIP_ROLE_ID));
  101. }
  102. }
  103. }