-
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.
Merge branch 'master' into feature/get_pull_request_list
# Conflicts: # Mohaymen.GiteaClient.IntegrationTests/Common/DependencyInjection/GiteaIntegrationTestDependencyInjection.cs
- Loading branch information
Showing
59 changed files
with
1,785 additions
and
18 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
Mohaymen.GiteaClient.IntegrationTests/Common/Assertions/Abstractions/ITestCommitChecker.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,9 @@ | ||
namespace Mohaymen.GiteaClient.IntegrationTests.Common.Assertions.Abstractions; | ||
|
||
internal interface ITestCommitChecker | ||
{ | ||
Task<bool> ContainsCommitWithShaAsync(string repositoryName, | ||
string branchName, | ||
string commitSha, | ||
CancellationToken cancellationToken); | ||
} |
7 changes: 7 additions & 0 deletions
7
Mohaymen.GiteaClient.IntegrationTests/Common/Assertions/Abstractions/ITestFileChecker.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,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); | ||
} |
34 changes: 34 additions & 0 deletions
34
Mohaymen.GiteaClient.IntegrationTests/Common/Assertions/TestCommitChecker.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,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); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
Mohaymen.GiteaClient.IntegrationTests/Common/Assertions/TestFileChecker.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,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; | ||
} | ||
} |
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
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
15 changes: 15 additions & 0 deletions
15
Mohaymen.GiteaClient.IntegrationTests/Common/Models/Responses/FileMetadataResponse.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,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; } | ||
} |
174 changes: 174 additions & 0 deletions
174
Mohaymen.GiteaClient.IntegrationTests/Gitea/Commit/CreateCommit/CreateCommitTests.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,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(); | ||
} | ||
} |
Oops, something went wrong.