Skip to content

Commit

Permalink
Merge branch 'master' into feature/get_pull_request_list
Browse files Browse the repository at this point in the history
# Conflicts:
#	Mohaymen.GiteaClient.IntegrationTests/Common/DependencyInjection/GiteaIntegrationTestDependencyInjection.cs
  • Loading branch information
HamedSY committed Jul 30, 2024
2 parents 04d40f2 + 21f115d commit 013ddc2
Show file tree
Hide file tree
Showing 59 changed files with 1,785 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Mohaymen.GiteaClient.IntegrationTests.Common.Assertions.Abstractions;

internal interface ITestCommitChecker
{
Task<bool> ContainsCommitWithShaAsync(string repositoryName,
string branchName,
string commitSha,
CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Mohaymen.GiteaClient.IntegrationTests.Common.Assertions.Abstractions;

internal interface ITestFileChecker
{
Task<bool> ContainsFileAsync(string repositoryName, string fileName, CancellationToken cancellationToken);
Task<bool> HasFileContent(string repositoryName, string filePath, string content, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Net.Http.Headers;
using Microsoft.Extensions.Options;
using Mohaymen.GiteaClient.Core.Configs;
using Mohaymen.GiteaClient.Gitea.Commit.GetBranchCommits.Dtos;
using Mohaymen.GiteaClient.IntegrationTests.Common.Assertions.Abstractions;
using Mohaymen.GiteaClient.IntegrationTests.Common.Models;
using Newtonsoft.Json;

namespace Mohaymen.GiteaClient.IntegrationTests.Common.Assertions;

public class TestCommitChecker : ITestCommitChecker
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly IOptions<GiteaApiConfiguration> _options;

public TestCommitChecker(IHttpClientFactory httpClientFactory, IOptions<GiteaApiConfiguration> options)
{
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
_options = options ?? throw new ArgumentNullException(nameof(options));
}

public async Task<bool> ContainsCommitWithShaAsync(string repositoryName,
string branchName,
string commitSha,
CancellationToken cancellationToken)
{
var httpClient = _httpClientFactory.CreateClient(GiteaTestConstants.ApiClientName);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("token", $"{_options.Value.PersonalAccessToken}");
var httpResponse = await httpClient.GetAsync($"repos/{_options.Value.RepositoriesOwner}/{repositoryName}/commits?sha={branchName}", cancellationToken);
var serializedResponse = await httpResponse.Content.ReadAsStringAsync(cancellationToken);
var commitsList = JsonConvert.DeserializeObject<List<LoadBranchCommitsResponseDto>>(serializedResponse);
return commitsList!.Select(x => x.CommitSha).Contains(commitSha);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Net.Http.Headers;
using Microsoft.Extensions.Options;
using Mohaymen.GiteaClient.Core.Configs;
using Mohaymen.GiteaClient.IntegrationTests.Common.Assertions.Abstractions;
using Mohaymen.GiteaClient.IntegrationTests.Common.Models;
using Mohaymen.GiteaClient.IntegrationTests.Common.Models.Responses;
using Newtonsoft.Json;

namespace Mohaymen.GiteaClient.IntegrationTests.Common.Assertions;

internal class TestFileChecker : ITestFileChecker
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly IOptions<GiteaApiConfiguration> _options;

public TestFileChecker(IHttpClientFactory httpClientFactory, IOptions<GiteaApiConfiguration> options)
{
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
_options = options ?? throw new ArgumentNullException(nameof(options));
}

public async Task<bool> ContainsFileAsync(string repositoryName, string fileName, CancellationToken cancellationToken)
{
var httpClient = _httpClientFactory.CreateClient(GiteaTestConstants.ApiClientName);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("token", $"{_options.Value.PersonalAccessToken}");
var httpResponse = await httpClient.GetAsync($"repos/{_options.Value.RepositoriesOwner}/{repositoryName}/contents", cancellationToken);
var serializedResponse = await httpResponse.Content.ReadAsStringAsync(cancellationToken);
var filesList = JsonConvert.DeserializeObject<List<FileMetadataResponse>>(serializedResponse);
return filesList!.Select(x => x.FileName).Contains(fileName);
}

public async Task<bool> HasFileContent(string repositoryName, string filePath, string content, CancellationToken cancellationToken)
{
var httpClient = _httpClientFactory.CreateClient(GiteaTestConstants.ApiClientName);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("token", $"{_options.Value.PersonalAccessToken}");
var httpResponse = await httpClient.GetAsync($"repos/{_options.Value.RepositoriesOwner}/{repositoryName}/contents/{filePath}", cancellationToken);
var serializedResponse = await httpResponse.Content.ReadAsStringAsync(cancellationToken);
var file = JsonConvert.DeserializeObject<FileMetadataResponse>(serializedResponse);
return file!.Content == content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static async Task<IContainer> StartContainerAsync()
var container = new ContainerBuilder()
.WithImage(GiteaTestConstants.ImageName)
.WithPortBinding(GiteaTestConstants.PortNumber, true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(r => r.ForPort(3000)))
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(r => r.ForPort(GiteaTestConstants.PortNumber)))
.WithEnvironment(new Dictionary<string, string>
{
{ "USER_UID", "1000" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public static IServiceCollection AddGiteaIntegrationTestsServices(this IServiceC
serviceCollection.AddSingleton<ITestRepositoryChecker, TestRepositoryChecker>();
serviceCollection.AddSingleton<ITestBranchChecker, TestBranchChecker>();
serviceCollection.AddSingleton<ITestBranchCreator, TestBranchCreator>();
serviceCollection.AddSingleton<ITestCommitChecker, TestCommitChecker>();
serviceCollection.AddSingleton<ITestFileChecker, TestFileChecker>();
serviceCollection.AddSingleton<ITestCommiter, TestCommiter>();
return serviceCollection;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Net.Http.Headers;
using Microsoft.Extensions.Options;
using Mohaymen.GiteaClient.Core.Configs;
using Mohaymen.GiteaClient.Gitea.Commit.Common.Facades.Abstractions;
using Mohaymen.GiteaClient.Gitea.Commit.CreateCommit.Dtos.Request;
using Mohaymen.GiteaClient.Gitea.Repository.CreateRepository.Dtos;
using Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions;
using Mohaymen.GiteaClient.IntegrationTests.Common.Models;
using Mohaymen.GiteaClient.IntegrationTests.Common.Models.Requests;
Expand All @@ -10,12 +13,12 @@ namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData;

internal class TestCommiter : ITestCommiter
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly ICommitFacade _commitFacade;
private readonly IOptions<GiteaApiConfiguration> _giteaOptions;

public TestCommiter(IHttpClientFactory httpClientFactory, IOptions<GiteaApiConfiguration> giteaOptions)
public TestCommiter(IOptions<GiteaApiConfiguration> giteaOptions, ICommitFacade commitFacade)
{
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
_commitFacade = commitFacade ?? throw new ArgumentNullException(nameof(commitFacade));
_giteaOptions = giteaOptions ?? throw new ArgumentNullException(nameof(giteaOptions));
}

Expand All @@ -25,17 +28,22 @@ public async Task CreateFileAsync(string repositoryName,
string commitMessage,
CancellationToken cancellationToken)
{
var httpClient = _httpClientFactory.CreateClient(GiteaTestConstants.ApiClientName);
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("token", $"{_giteaOptions.Value.PersonalAccessToken}");
var createFileRequest = new CreateFileRequest
var createCommitDto = new CreateCommitCommandDto
{
Content = Convert.ToBase64String("sample test content"u8.ToArray()),
RepositoryName = repositoryName,
BranchName = branchName,
CommitMessage = commitMessage,
BranchName = branchName
FileDtos =
[
new FileCommitDto
{
Path = filePath,
Content = Convert.ToBase64String("sample test content"u8.ToArray()),
CommitActionDto = CommitActionDto.Create
}
]
};
var jsonContent = new StringContent(JsonConvert.SerializeObject(createFileRequest));
jsonContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var resykt = await httpClient.PostAsync($"repos/{_giteaOptions.Value.RepositoriesOwner}/{repositoryName}/contents/{filePath}", jsonContent, cancellationToken);

await _commitFacade.CreateCommitAsync(createCommitDto, cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Newtonsoft.Json;

namespace Mohaymen.GiteaClient.IntegrationTests.Common.Models.Responses;

public class FileMetadataResponse
{
[JsonProperty("name")]
public required string FileName { get; init; }

[JsonProperty("path")]
public required string FilePath { get; init; }

[JsonProperty("content")]
public required string Content { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
using System.Net;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Mohaymen.GiteaClient.Gitea.Client.Abstractions;
using Mohaymen.GiteaClient.Gitea.Commit.CreateCommit.Dtos.Request;
using Mohaymen.GiteaClient.IntegrationTests.Common.Assertions.Abstractions;
using Mohaymen.GiteaClient.IntegrationTests.Common.Collections.Gitea;
using Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions;

namespace Mohaymen.GiteaClient.IntegrationTests.Gitea.Commit.CreateCommit;

[Collection("GiteaIntegrationTests")]
public class CreateCommitTests
{
private const string RepositoryName = "CreateCommitRepository";
private readonly IGiteaClient _sut;
private readonly ITestCommitChecker _testCommitChecker;
private readonly ITestFileChecker _testFileChecker;
private readonly ITestRepositoryCreator _testRepositoryCreator;
private readonly ITestCommiter _testCommiter;
private readonly GiteaCollectionFixture _giteaCollectionFixture;

public CreateCommitTests(GiteaCollectionFixture giteaCollectionFixture)
{
_giteaCollectionFixture = giteaCollectionFixture ?? throw new ArgumentNullException(nameof(giteaCollectionFixture));
_sut = _giteaCollectionFixture.ServiceProvider.GetRequiredService<IGiteaClient>();
_testFileChecker = giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestFileChecker>();
_testCommitChecker = giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestCommitChecker>();
_testRepositoryCreator = giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestRepositoryCreator>();
_testCommiter = giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestCommiter>();
}

[Fact]
public async Task CreateCommitAsync_ShouldCreateCommitAndAddFileWithItsContent_WhenCommitIsForFileCreating()
{
// Arrange
const string filePath = "sample.txt";
const string content = "alireza eiji is the man of focus!";
const string expectedContent = "YWxpcmV6YSBlaWppIGlzIHRoZSBtYW4gb2YgZm9jdXMh";
const string repoName = $"CreateFile_{RepositoryName}";
var createCommitCommandDto = new CreateCommitCommandDto
{
RepositoryName = repoName,
BranchName = "main",
CommitMessage = "some trivial commit",
FileDtos =
[
new FileCommitDto
{
Path = filePath,
Content = content,
CommitActionDto = CommitActionDto.Create
}
]
};

await _testRepositoryCreator.CreateRepositoryAsync(repoName, _giteaCollectionFixture.CancellationToken);

// Act
var actual = await _sut.CommitClient.CreateCommitAsync(createCommitCommandDto, _giteaCollectionFixture.CancellationToken);

// Assert
actual.StatusCode.Should().Be(HttpStatusCode.Created);
var commitSha = actual.Content!.CommitResponseDto.Sha;
var isCommitCreated= await _testCommitChecker.ContainsCommitWithShaAsync(repoName,
"main",
commitSha,
_giteaCollectionFixture.CancellationToken);
isCommitCreated.Should().BeTrue();
var isFileExists = await _testFileChecker.ContainsFileAsync(repoName,
filePath,
_giteaCollectionFixture.CancellationToken);
isFileExists.Should().BeTrue();
var doesFileHaveContent = await _testFileChecker.HasFileContent(repoName,
filePath,
expectedContent,
_giteaCollectionFixture.CancellationToken);
doesFileHaveContent.Should().BeTrue();
}

[Fact]
public async Task CreateCommitAsync_ShouldCreateCommitAndEditFileWithItsContent_WhenCommitIsForFileEditing()
{
// Arrange
const string filePath = "README.md";
const string content = "alireza eiji is the man of focus!";
const string expectedContent = "YWxpcmV6YSBlaWppIGlzIHRoZSBtYW4gb2YgZm9jdXMh";
const string repoName = $"EditFile_{RepositoryName}";
var createCommitCommandDto = new CreateCommitCommandDto
{
RepositoryName = repoName,
BranchName = "main",
CommitMessage = "some trivial commit",
FileDtos =
[
new FileCommitDto
{
Path = filePath,
Content = content,
CommitActionDto = CommitActionDto.Update
}
]
};

await _testRepositoryCreator.CreateRepositoryAsync($"EditFile_{RepositoryName}", _giteaCollectionFixture.CancellationToken);

// Act
var actual = await _sut.CommitClient.CreateCommitAsync(createCommitCommandDto, _giteaCollectionFixture.CancellationToken);

// Assert
actual.StatusCode.Should().Be(HttpStatusCode.Created);
var commitSha = actual.Content!.CommitResponseDto.Sha;
var isCommitCreated= await _testCommitChecker.ContainsCommitWithShaAsync(repoName,
"main",
commitSha,
_giteaCollectionFixture.CancellationToken);
isCommitCreated.Should().BeTrue();
var isFileExists = await _testFileChecker.ContainsFileAsync(repoName,
filePath,
_giteaCollectionFixture.CancellationToken);
isFileExists.Should().BeTrue();
var doesFileHaveContent = await _testFileChecker.HasFileContent(repoName,
filePath,
expectedContent,
_giteaCollectionFixture.CancellationToken);
doesFileHaveContent.Should().BeTrue();
}

[Fact]
public async Task CreateCommitAsync_ShouldCreateCommitAndDeleteFileWithItsContent_WhenCommitIsForFileDeleting()
{
// Arrange
const string fileDelete = "SampleDeleteFile.txt";
const string repoName = $"DeleteFile_{RepositoryName}";
var createCommitCommandDto = new CreateCommitCommandDto
{
RepositoryName = repoName,
BranchName = "main",
CommitMessage = "some trivial commit",
FileDtos =
[
new FileCommitDto
{
Path = fileDelete,
Content = "",
CommitActionDto = CommitActionDto.Delete
}
]
};

await _testRepositoryCreator.CreateRepositoryAsync(repoName, _giteaCollectionFixture.CancellationToken);
await _testCommiter.CreateFileAsync(repoName,
"main",
fileDelete,
"ccc",
_giteaCollectionFixture.CancellationToken);

// Act
var actual = await _sut.CommitClient.CreateCommitAsync(createCommitCommandDto, _giteaCollectionFixture.CancellationToken);

// Assert
actual.StatusCode.Should().Be(HttpStatusCode.Created);
var commitSha = actual.Content!.CommitResponseDto.Sha;
var isCommitCreated= await _testCommitChecker.ContainsCommitWithShaAsync(repoName,
"main",
commitSha,
_giteaCollectionFixture.CancellationToken);
isCommitCreated.Should().BeTrue();
var isFileExists = await _testFileChecker.ContainsFileAsync(repoName,
fileDelete,
_giteaCollectionFixture.CancellationToken);
isFileExists.Should().BeFalse();
}
}
Loading

0 comments on commit 013ddc2

Please sign in to comment.