Skip to content

Commit

Permalink
Merge branch 'master' into feature/add_commit_api
Browse files Browse the repository at this point in the history
  • Loading branch information
AlirezaEijiProgrammer authored Jul 30, 2024
2 parents c70c0bf + d888a95 commit 1280c42
Show file tree
Hide file tree
Showing 44 changed files with 1,237 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public CreateBranchTests(GiteaCollectionFixture giteaCollectionFixture)
public async Task CreateBranch_ShouldCreateBranchWithCreatedStatusCode_WhenInputsAreProvidedProperly()
{
// Arrange
const string repositoryName = GiteaTestConstants.RepositoryName;
const string repositoryName = "create_branch_repo";
await _repositoryCreator.CreateRepositoryAsync(repositoryName, _giteaCollectionFixture.CancellationToken);

const string newBranchName = "feature/test_new_branch";
const string newBranchName = "create_branch_branch";
var createBranchCommandDto = new CreateBranchCommandDto
{
RepositoryName = repositoryName,
NewBranchName = newBranchName,
OldReferenceName = "main"
OldReferenceName = GiteaTestConstants.DefaultBranch
};

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public GetBranchListTests(GiteaCollectionFixture giteaCollectionFixture)
public async Task GetBranchList_ShouldGetBranchListOfRepo_WhenInputsAreProvidedProperly()
{
// Arrange
const string repositoryName = GiteaTestConstants.RepositoryName;
const string branch1 = "branch1";
const string branch2 = "branch2";
const string branch3 = "branch3";
const string repositoryName = "get_branch_list_repo";
const string branch1 = "get_branch_list_repo_branch_1";
const string branch2 = "get_branch_list_repo_branch_2";
const string branch3 = "get_branch_list_repo_branch_3";
var cancellationToken = _giteaCollectionFixture.CancellationToken;

await _repositoryCreator.CreateRepositoryAsync(repositoryName, cancellationToken);
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Mohaymen.GiteaClient.Gitea.Repository.CreateRepository.Dtos;
using Mohaymen.GiteaClient.IntegrationTests.Common.Assertions.Abstractions;
using Mohaymen.GiteaClient.IntegrationTests.Common.Collections.Gitea;
using Mohaymen.GiteaClient.IntegrationTests.Common.Models;

namespace Mohaymen.GiteaClient.IntegrationTests.Gitea.Repository.CreateRepository;

Expand All @@ -31,7 +32,7 @@ public async Task CreateRepository_ShouldCreateRepositoryWithCreatedStatusCode_W
var createRepositoryCommandDto = new CreateRepositoryCommandDto
{
Name = repositoryName,
DefaultBranch = "main",
DefaultBranch = GiteaTestConstants.DefaultBranch,
IsPrivateBranch = true
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Net;
using FluentAssertions;
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using Mohaymen.GiteaClient.Gitea.Client.Abstractions;
using Mohaymen.GiteaClient.Gitea.Repository.DeleteRepository.Dto;
using Mohaymen.GiteaClient.IntegrationTests.Common.Assertions.Abstractions;
using Mohaymen.GiteaClient.IntegrationTests.Common.Collections.Gitea;

namespace Mohaymen.GiteaClient.IntegrationTests.Gitea.Repository.DeleteRepository;

[Collection("GiteaIntegrationTests")]
public class DeleteRepositoryTests : IClassFixture<RepositoryClassFixture>
{
private readonly IGiteaClient _sut;
private readonly ITestRepositoryChecker _testRepositoryChecker;
private readonly GiteaCollectionFixture _giteaCollectionFixture;

public DeleteRepositoryTests(GiteaCollectionFixture giteaCollectionFixture)
{
_giteaCollectionFixture = giteaCollectionFixture ?? throw new ArgumentNullException(nameof(giteaCollectionFixture));
_testRepositoryChecker = _giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestRepositoryChecker>();
_sut = _giteaCollectionFixture.ServiceProvider.GetRequiredService<IGiteaClient>();
}

[Fact]
public async Task DeleteRepositoryAsync_ShouldReturnNotContentSuccessResponse_WhenRepositoryExistsAndDeleted()
{
// Arrange
var deletedRepositoryCommandDto = new DeleteRepositoryCommandDto
{
RepositoryName = RepositoryClassFixture.DeleteRepositoryName
};

// Act
var actual = await _sut.RepositoryClient.DeleteRepositoryAsync(deletedRepositoryCommandDto, _giteaCollectionFixture.CancellationToken);

// Assert
actual.StatusCode.Should().Be(HttpStatusCode.NoContent);
var isRepositoryExists = await _testRepositoryChecker.ContainsRepositoryAsync(RepositoryClassFixture.DeleteRepositoryName, _giteaCollectionFixture.CancellationToken);
isRepositoryExists.Should().BeFalse();
}

[Fact]
public async Task DeleteRepositoryAsync_ShouldReturnNotFound_WhenRepositoryDoesNotExist()
{
// Arrange
const string repositoryName = "sampleFakeRepo";
var deletedRepositoryCommandDto = new DeleteRepositoryCommandDto
{
RepositoryName = repositoryName
};

// Act
var actual = await _sut.RepositoryClient.DeleteRepositoryAsync(deletedRepositoryCommandDto, _giteaCollectionFixture.CancellationToken);

// Assert
actual.StatusCode.Should().Be(HttpStatusCode.NotFound);
var isRepositoryExists = await _testRepositoryChecker.ContainsRepositoryAsync(repositoryName, _giteaCollectionFixture.CancellationToken);
isRepositoryExists.Should().BeFalse();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.Extensions.DependencyInjection;
using Mohaymen.GiteaClient.IntegrationTests.Common.Collections.Gitea;
using Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions;

namespace Mohaymen.GiteaClient.IntegrationTests.Gitea.Repository;

internal sealed class RepositoryClassFixture : IAsyncLifetime
{
public const string SearchRepositoryName = "SearchUseCaseRepository";
public const string DeleteRepositoryName = "DeleteUseCaseRepository";

private readonly ITestRepositoryCreator _testRepositoryCreator;
private readonly GiteaCollectionFixture _giteaCollectionFixture;

public RepositoryClassFixture(GiteaCollectionFixture giteaCollectionFixture)
{
_giteaCollectionFixture = giteaCollectionFixture ?? throw new ArgumentNullException(nameof(giteaCollectionFixture));
_testRepositoryCreator = giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestRepositoryCreator>();
}

public async Task InitializeAsync()
{
await _testRepositoryCreator.CreateRepositoryAsync(SearchRepositoryName, _giteaCollectionFixture.CancellationToken);
await _testRepositoryCreator.CreateRepositoryAsync(DeleteRepositoryName, _giteaCollectionFixture.CancellationToken);
}

public Task DisposeAsync()
{
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Net;
using FluentAssertions;
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using Mohaymen.GiteaClient.Gitea.Client.Abstractions;
using Mohaymen.GiteaClient.Gitea.Repository.SearchRepository.Dtos;
using Mohaymen.GiteaClient.IntegrationTests.Common.Assertions.Abstractions;
using Mohaymen.GiteaClient.IntegrationTests.Common.Collections.Gitea;

namespace Mohaymen.GiteaClient.IntegrationTests.Gitea.Repository.SearchRepository;

[Collection("GiteaIntegrationTests")]
public class SearchRepositoryTests : IClassFixture<RepositoryClassFixture>
{
private readonly IGiteaClient _sut;
private readonly GiteaCollectionFixture _giteaCollectionFixture;

public SearchRepositoryTests(GiteaCollectionFixture giteaCollectionFixture)
{
_giteaCollectionFixture = giteaCollectionFixture ?? throw new ArgumentNullException(nameof(giteaCollectionFixture));
_sut = giteaCollectionFixture.ServiceProvider.GetRequiredService<IGiteaClient>();

}

[Fact]
public async Task SearchRepositoryAsync_ShouldReturnListOfMatchedRepositories_WhenSearchQueryIsProvidedAndHasMathc()
{
// Arrange
var searchRepositoryQuery = new SearchRepositoryQueryDto
{
Query = "Repo",
Limit = 10,
Page = 1
};

// Act
var actual = await _sut.RepositoryClient.SearchRepositoryAsync(searchRepositoryQuery, _giteaCollectionFixture.CancellationToken);

// Assert
actual.StatusCode.Should().Be(HttpStatusCode.OK);
actual.Content!.SearchResult.Select(x => x.RepositoryName).Should()
.Contain(RepositoryClassFixture.SearchRepositoryName);
}

[Fact]
public async Task SearchRepositoryAsync_ShouldReturnEmptyList_WhenSearchQueryIsProvidedAndNotMatch()
{
// Arrange
var searchRepositoryQuery = new SearchRepositoryQueryDto
{
Query = "ahmad",
Page = 1,
Limit = 5
};

// Act
var actual = await _sut.RepositoryClient.SearchRepositoryAsync(searchRepositoryQuery, _giteaCollectionFixture.CancellationToken);

// Assert
actual.StatusCode.Should().Be(HttpStatusCode.OK);
actual.Content!.SearchResult.Should().NotBeNull();
actual.Content!.SearchResult.Should().BeEmpty();
}
}
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));
}
}
Loading

0 comments on commit 1280c42

Please sign in to comment.