-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: rename CreateBranchResponseDto.cs to BranchResponseDto.cs * chore: move BranchResponseDto.cs to Common directory * chore: implement get branch list api * chore: write unit tests * chore: implement create pull request api; except its response dto * chore: write unit tests * chore: add pull request facade to gitea client * chore: write integration test for get branch list api * chore: add response dto * chore: add integration test * chore: add auto-init field to create repo * chore: remove class fixtures * chore: resolve warning * fix: integration tests * chore: add cancellation token * fix: use unique names for each integration tests
- Loading branch information
Showing
21 changed files
with
538 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...iteaClient.IntegrationTests/Gitea/PullRequest/CreatePullRequest/CreatePullRequestTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Net; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Mohaymen.GiteaClient.Gitea.Client.Abstractions; | ||
using Mohaymen.GiteaClient.Gitea.PullRequest.CreatePullRequest.Dtos; | ||
using Mohaymen.GiteaClient.IntegrationTests.Common.Collections.Gitea; | ||
using Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions; | ||
using Mohaymen.GiteaClient.IntegrationTests.Common.Models; | ||
|
||
namespace Mohaymen.GiteaClient.IntegrationTests.Gitea.PullRequest.CreatePullRequest; | ||
|
||
[Collection("GiteaIntegrationTests")] | ||
public class CreatePullRequestTests | ||
{ | ||
private readonly IGiteaClient _sut; | ||
private readonly ITestRepositoryCreator _repositoryCreator; | ||
private readonly ITestBranchCreator _branchCreator; | ||
private readonly GiteaCollectionFixture _giteaCollectionFixture; | ||
|
||
public CreatePullRequestTests(GiteaCollectionFixture giteaCollectionFixture) | ||
{ | ||
_giteaCollectionFixture = giteaCollectionFixture ?? throw new ArgumentNullException(nameof(giteaCollectionFixture)); | ||
_repositoryCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestRepositoryCreator>(); | ||
_branchCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestBranchCreator>(); | ||
_sut = giteaCollectionFixture.ServiceProvider.GetRequiredService<IGiteaClient>(); | ||
} | ||
|
||
[Fact] | ||
public async Task CreatePullRequest_ShouldGetBranchListOfRepo_WhenInputsAreProvidedProperly() | ||
{ | ||
// Arrange | ||
const string repositoryName = "create_pull_request_repo"; | ||
const string branchName = "create_pull_request_branch"; | ||
var cancellationToken = _giteaCollectionFixture.CancellationToken; | ||
|
||
await _repositoryCreator.CreateRepositoryAsync(repositoryName, cancellationToken); | ||
await _branchCreator.CreateBranchAsync(repositoryName, branchName, cancellationToken); | ||
|
||
const string title = "title"; | ||
var createPullRequestCommandDto = new CreatePullRequestCommandDto | ||
{ | ||
RepositoryName = repositoryName, | ||
Title = title, | ||
HeadBranch = branchName, | ||
BaseBranch = GiteaTestConstants.DefaultBranch | ||
}; | ||
|
||
// Act | ||
var actual = await _sut.PullRequestClient.CreatePullRequestAsync(createPullRequestCommandDto, cancellationToken); | ||
|
||
// Assert | ||
actual.StatusCode.Should().Be(HttpStatusCode.Created); | ||
actual.Content!.Title.Should().Be(title); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
Mohaymen.GiteaClient.Tests/Gitea/PullRequest/Common/Facade/PullRequestFacadeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using MediatR; | ||
using Mohaymen.GiteaClient.Gitea.PullRequest.Common.Facade; | ||
using Mohaymen.GiteaClient.Gitea.PullRequest.Common.Facade.Abstractions; | ||
using Mohaymen.GiteaClient.Gitea.PullRequest.CreatePullRequest.Commands; | ||
using Mohaymen.GiteaClient.Gitea.PullRequest.CreatePullRequest.Dtos; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace Mohaymen.GiteaClient.Tests.Gitea.PullRequest.Common.Facade; | ||
|
||
public class PullRequestFacadeTests | ||
{ | ||
private readonly IMediator _mediator; | ||
private readonly IPullRequestFacade _sut; | ||
|
||
public PullRequestFacadeTests() | ||
{ | ||
_mediator = Substitute.For<IMediator>(); | ||
_sut = new PullRequestFacade(_mediator); | ||
} | ||
|
||
[Fact] | ||
public async Task CreatePullRequestAsync_ShouldCallSend_WhenAllFieldsAreProvided() | ||
{ | ||
// Arrange | ||
const string repositoryName = "repo"; | ||
const string headBranch = "head"; | ||
const string baseBranch = "base"; | ||
const string body = "body"; | ||
const string title = "title"; | ||
const string assignee = "assignee"; | ||
var assignees = new List<string> | ||
{ | ||
"assignee1", | ||
"assignee2" | ||
}; | ||
var commandDto = new CreatePullRequestCommandDto | ||
{ | ||
RepositoryName = repositoryName, | ||
HeadBranch = headBranch, | ||
BaseBranch = baseBranch, | ||
Body = body, | ||
Title = title, | ||
Assignee = assignee, | ||
Assignees = assignees | ||
}; | ||
|
||
// Act | ||
await _sut.CreatePullRequestAsync(commandDto, default); | ||
|
||
// Assert | ||
await _mediator.Received(1).Send(Arg.Is<CreatePullRequestCommand>(x => x.RepositoryName == repositoryName | ||
&& x.HeadBranch == headBranch | ||
&& x.BaseBranch == baseBranch | ||
&& x.Body == body | ||
&& x.Title == title | ||
&& x.Assignee == assignee | ||
&& x.Assignees!.SequenceEqual(assignees))); | ||
} | ||
|
||
[Fact] | ||
public async Task CreatePullRequestAsync_ShouldCallSend_WhenOptionalFieldsAreNotProvided() | ||
{ | ||
// Arrange | ||
const string repositoryName = "repo"; | ||
var commandDto = new CreatePullRequestCommandDto | ||
{ | ||
RepositoryName = repositoryName | ||
}; | ||
|
||
// Act | ||
await _sut.CreatePullRequestAsync(commandDto, default); | ||
|
||
// Assert | ||
await _mediator.Received(1).Send(Arg.Is<CreatePullRequestCommand>(x => x.RepositoryName == repositoryName)); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...ests/Gitea/PullRequest/CreatePullRequest/Commands/CreatePullRequestCommandHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using FluentAssertions; | ||
using FluentValidation; | ||
using MediatR; | ||
using Microsoft.Extensions.Options; | ||
using Mohaymen.GiteaClient.Core.Configs; | ||
using Mohaymen.GiteaClient.Gitea.PullRequest.Common.ApiCall.Abstractions; | ||
using Mohaymen.GiteaClient.Gitea.PullRequest.CreatePullRequest.Commands; | ||
using Mohaymen.GiteaClient.Gitea.PullRequest.CreatePullRequest.Context; | ||
using Mohaymen.GiteaClient.Gitea.PullRequest.CreatePullRequest.Dtos; | ||
using NSubstitute; | ||
using Refit; | ||
using Xunit; | ||
|
||
namespace Mohaymen.GiteaClient.Tests.Gitea.PullRequest.CreatePullRequest.Commands; | ||
|
||
public class CreatePullRequestCommandHandlerTests | ||
{ | ||
private readonly IPullRequestRestClient _pullRequestRestClient; | ||
private readonly IOptions<GiteaApiConfiguration> _options; | ||
private readonly InlineValidator<CreatePullRequestCommand> _validator; | ||
private readonly IRequestHandler<CreatePullRequestCommand, ApiResponse<CreatePullRequestResponseDto>> _sut; | ||
|
||
public CreatePullRequestCommandHandlerTests() | ||
{ | ||
_pullRequestRestClient = Substitute.For<IPullRequestRestClient>(); | ||
_options = Substitute.For<IOptions<GiteaApiConfiguration>>(); | ||
_validator = new InlineValidator<CreatePullRequestCommand>(); | ||
_sut = new CreatePullRequestCommandHandler(_pullRequestRestClient, _options, _validator); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_ShouldThrowsValidationException_WhenInputIsInvalid() | ||
{ | ||
// Arrange | ||
_validator.RuleFor(x => x).Must(_ => false); | ||
var command = new CreatePullRequestCommand | ||
{ | ||
RepositoryName = "repo" | ||
}; | ||
|
||
// Act | ||
var actual = async () => await _sut.Handle(command, default); | ||
|
||
// Assert | ||
await actual.Should().ThrowAsync<ValidationException>(); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_ShouldCallCreatePullRequestAsync_AndInputsAreValid() | ||
{ | ||
// Arrange | ||
_validator.RuleFor(x => x).Must(_ => true); | ||
const string owner = "owner"; | ||
const string repositoryName = "repo"; | ||
const string headBranch = "head"; | ||
const string baseBranch = "base"; | ||
const string body = "body"; | ||
const string title = "title"; | ||
const string assignee = "assignee"; | ||
var assignees = new List<string> | ||
{ | ||
"assignee1", | ||
"assignee2" | ||
}; | ||
var command = new CreatePullRequestCommand | ||
{ | ||
RepositoryName = repositoryName, | ||
HeadBranch = headBranch, | ||
BaseBranch = baseBranch, | ||
Body = body, | ||
Title = title, | ||
Assignee = assignee, | ||
Assignees = assignees | ||
}; | ||
_options.Value.Returns(new GiteaApiConfiguration | ||
{ | ||
BaseUrl = "url", | ||
PersonalAccessToken = "token", | ||
RepositoriesOwner = owner | ||
}); | ||
|
||
// Act | ||
await _sut.Handle(command, default); | ||
|
||
// Assert | ||
await _pullRequestRestClient.Received(1).CreatePullRequestAsync(owner, | ||
repositoryName, | ||
Arg.Is<CreatePullRequestRequest>(x => x.HeadBranch == headBranch | ||
&& x.BaseBranch == baseBranch | ||
&& x.Body == body | ||
&& x.Title == title | ||
&& x.Assignee == assignee | ||
&& x.Assignees!.SequenceEqual(assignees)), | ||
default); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
.../Gitea/PullRequest/CreatePullRequest/Validators/CreatePullRequestCommandValidatorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using FluentAssertions; | ||
using FluentValidation; | ||
using Mohaymen.GiteaClient.Gitea.PullRequest.CreatePullRequest.Commands; | ||
using Mohaymen.GiteaClient.Gitea.PullRequest.CreatePullRequest.Validators; | ||
using Xunit; | ||
|
||
namespace Mohaymen.GiteaClient.Tests.Gitea.PullRequest.CreatePullRequest.Validators; | ||
|
||
public class CreatePullRequestCommandValidatorTests | ||
{ | ||
private readonly IValidator<CreatePullRequestCommand> _sut; | ||
|
||
public CreatePullRequestCommandValidatorTests() | ||
{ | ||
_sut = new CreatePullRequestCommandValidator(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
public void Validate_ShouldReturnEmptyRepositoryNameErrorCode_WhenRepositoryNameIsNullOrEmpty(string repositoryName) | ||
{ | ||
// Arrange | ||
var command = new CreatePullRequestCommand | ||
{ | ||
RepositoryName = repositoryName | ||
}; | ||
|
||
// Act | ||
var actual = _sut.Validate(command); | ||
|
||
// Assert | ||
actual.Errors.Select(x => x.ErrorCode) | ||
.Should().Contain(CreatePullRequestErrorCodes.EmptyRepositoryNameErrorCode); | ||
} | ||
|
||
[Fact] | ||
public void Validate_ShouldReturnValidResult_WhenEveryThingIsProvidedProperly() | ||
{ | ||
// Arrange | ||
var command = new CreatePullRequestCommand | ||
{ | ||
RepositoryName = "repo" | ||
}; | ||
|
||
// Act | ||
var actual = _sut.Validate(command); | ||
|
||
// Assert | ||
actual.IsValid.Should().BeTrue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
Mohaymen.GiteaClient/Gitea/PullRequest/Common/ApiCall/Abstractions/IPullRequestRestClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Mohaymen.GiteaClient.Core.ApiCall.Abstractions; | ||
using Mohaymen.GiteaClient.Gitea.PullRequest.CreatePullRequest.Context; | ||
using Mohaymen.GiteaClient.Gitea.PullRequest.CreatePullRequest.Dtos; | ||
using Refit; | ||
|
||
namespace Mohaymen.GiteaClient.Gitea.PullRequest.Common.ApiCall.Abstractions; | ||
|
||
internal interface IPullRequestRestClient : IRefitClientInterface | ||
{ | ||
[Post("/repos/{owner}/{repo}/pulls")] | ||
Task<ApiResponse<CreatePullRequestResponseDto>> CreatePullRequestAsync( | ||
[AliasAs("owner")] string owner, | ||
[AliasAs("repo")] string repositoryName, | ||
[Body] CreatePullRequestRequest createPullRequestRequest, | ||
CancellationToken cancellationToken); | ||
} |
Oops, something went wrong.