Skip to content

Commit

Permalink
Implements validation where you can't add a user to a default team. R…
Browse files Browse the repository at this point in the history
…efactor test data. DynamicsValue/fake-xrm-easy#108
  • Loading branch information
jordimontana82 committed Mar 3, 2024
1 parent a41d164 commit 2db8d1f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 50 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Changed

- Introduces validation error when trying to add a user to a default team - https://github.com/DynamicsValue/fake-xrm-easy/issues/108
- Fixes responsible type in CloseIncidentRequestExecutor - https://github.com/DynamicsValue/fake-xrm-easy/issues/116
- Increase code coverage for NavigateToNextEntityOrganizationRequest
- Remove unnecessary checks in NavigateToNextEntityOrganizationRequestExecutor, thanks Temmy
Expand Down
2 changes: 2 additions & 0 deletions FakeXrmEasy.Messages.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=isdefault/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public OrganizationResponse Execute(OrganizationRequest request, IXrmFakedContex
throw FakeOrganizationServiceFaultFactory.New(ErrorCodes.ObjectDoesNotExist, string.Format("Team with Id {0} wasn't found", req.TeamId.ToString()));
}

if (team.GetAttributeValue<bool?>("isdefault") == true)
{
throw FakeOrganizationServiceFaultFactory.New(ErrorCodes.CannotAddMembersToDefaultTeam, "You cannot join one or more of the teams selected. The membership of default teams cannot be modified.");
}

foreach (var memberId in req.MemberIds)
{
var user = ctx.CreateQuery("systemuser").FirstOrDefault(e => e.Id == memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,51 @@
using Microsoft.Crm.Sdk.Messages;
using Crm;
using System.ServiceModel;
using FakeXrmEasy.Abstractions;

namespace FakeXrmEasy.Messages.Tests.FakeMessageExecutors.AddMembersTeamRequestTests
{
public class Tests : FakeXrmEasyTestsBase
public class AddMembersTeamRequestTests : FakeXrmEasyTestsBase
{
private readonly SystemUser _systemUser;
private readonly BusinessUnit _businessUnit;
private readonly Team _team;

public AddMembersTeamRequestTests(): base()
{
_businessUnit = new BusinessUnit() { Id = Guid.NewGuid() };
_systemUser = new SystemUser() { Id = Guid.NewGuid() };
_team = new Team()
{
Id = Guid.NewGuid(),
Name = "Some team"
};
}

[Fact]
public void When_a_member_is_added_to_a_non_existing_team_exception_is_thrown()
{
AddMembersTeamRequest addMembersTeamRequest = new AddMembersTeamRequest
{
MemberIds = new[]
{
Guid.NewGuid()
_systemUser.Id
},
TeamId = Guid.NewGuid()
TeamId = _team.Id
};

// Execute the request.
Assert.Throws<FaultException<OrganizationServiceFault>>(() => _service.Execute(addMembersTeamRequest));
}

[Fact]
public void When_a_request_is_called_with_an_empty_teamid_parameter_exception_is_thrown()
public void When_a_request_is_called_with_an_empty_teamId_parameter_exception_is_thrown()
{



AddMembersTeamRequest addMembersTeamRequest = new AddMembersTeamRequest
{
MemberIds = new[]
{
Guid.NewGuid()
_systemUser.Id
},
TeamId = Guid.Empty
};
Expand All @@ -47,34 +60,28 @@ public void When_a_request_is_called_with_an_empty_teamid_parameter_exception_is
}

[Fact]
public void When_a_request_is_called_with_a_null_memberid_parameter_exception_is_thrown()
public void When_a_request_is_called_with_a_null_memberId_parameter_exception_is_thrown()
{



AddMembersTeamRequest addMembersTeamRequest = new AddMembersTeamRequest
{
MemberIds = null,
TeamId = Guid.NewGuid()
TeamId = _team.Id
};

// Execute the request.
Assert.Throws<FaultException<OrganizationServiceFault>>(() => _service.Execute(addMembersTeamRequest));
}

[Fact]
public void When_a_request_is_called_with_an_empty_memberid_parameter_exception_is_thrown()
public void When_a_request_is_called_with_an_empty_memberId_parameter_exception_is_thrown()
{



AddMembersTeamRequest addMembersTeamRequest = new AddMembersTeamRequest
{
MemberIds = new[]
{
Guid.Empty
},
TeamId = Guid.NewGuid()
TeamId = _team.Id
};

// Execute the request.
Expand All @@ -84,18 +91,9 @@ public void When_a_request_is_called_with_an_empty_memberid_parameter_exception_
[Fact]
public void When_a_non_existing_member_is_added_to_an_existing_list_exception_is_thrown()
{



var team = new Team
{
Id = Guid.NewGuid(),
Name = "Some team"
};

_context.Initialize(new List<Entity>
{
team
_team
});

AddMembersTeamRequest addMembersTeamRequest = new AddMembersTeamRequest
Expand All @@ -104,7 +102,7 @@ public void When_a_non_existing_member_is_added_to_an_existing_list_exception_is
{
Guid.NewGuid()
},
TeamId = team.ToEntityReference().Id
TeamId = _team.Id
};

Assert.Throws<FaultException<OrganizationServiceFault>>(() => _service.Execute(addMembersTeamRequest));
Expand All @@ -113,43 +111,51 @@ public void When_a_non_existing_member_is_added_to_an_existing_list_exception_is
[Fact]
public void When_a_member_is_added_to_an_existing_list_member_is_added_successfully()
{



var team = new Team
_context.Initialize(new List<Entity>
{
Id = Guid.NewGuid(),
Name = "Some team"
};
_team,
_systemUser
});

var systemuser = new SystemUser
AddMembersTeamRequest addMembersTeamRequest = new AddMembersTeamRequest
{
Id = Guid.NewGuid()
MemberIds = new[]
{
_systemUser.Id
},
TeamId = _team.Id
};

_service.Execute(addMembersTeamRequest);

var member = _context.CreateQuery<TeamMembership>()
.FirstOrDefault(tm => tm.TeamId == _team.Id && tm.SystemUserId == _systemUser.Id);

Assert.NotNull(member);
}

[Fact]
public void Should_raise_exception_when_a_user_is_added_to_a_default_team()
{
_team["isdefault"] = true;

_context.Initialize(new List<Entity>
{
team,
systemuser
_team,
_systemUser
});

AddMembersTeamRequest addMembersTeamRequest = new AddMembersTeamRequest
{
MemberIds = new[]
{
systemuser.Id
_systemUser.Id
},
TeamId = team.ToEntityReference().Id
TeamId = _team.Id
};

_service.Execute(addMembersTeamRequest);

using (var context = new XrmServiceContext(_service))
{
var member = context.CreateQuery<TeamMembership>().FirstOrDefault(tm => tm.TeamId == team.Id && tm.SystemUserId == systemuser.Id);

Assert.NotNull(member);
}
var exception = Assert.Throws<FaultException<OrganizationServiceFault>>(() => _service.Execute(addMembersTeamRequest));
Assert.Equal((int) ErrorCodes.CannotAddMembersToDefaultTeam, exception.Detail.ErrorCode);
}
}
}

0 comments on commit 2db8d1f

Please sign in to comment.