Skip to content

Commit

Permalink
add test case for #173, currently unable to reproduce.
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed Jul 19, 2023
1 parent c84187e commit c126b62
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
18 changes: 18 additions & 0 deletions backend/LexBoxApi/Controllers/ProjectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,22 @@ public async Task<ActionResult<int>> AddLexboxSuffix()
return await _lexBoxDbContext.Projects.Where(p => !p.Code.EndsWith("-lexbox"))
.ExecuteUpdateAsync(_ => _.SetProperty(p => p.Code, p => p.Code + "-lexbox"));
}

[HttpDelete("project/{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesDefaultResponseType]
[AdminRequired]
public async Task<ActionResult<Project>> DeleteProject(Guid id)
{
//this project is only for testing purposes. It will delete projects permanently from the database.
var project = await _lexBoxDbContext.Projects.FindAsync(id);
if (project is null) return NotFound();
if (project.RetentionPolicy != RetentionPolicy.Dev) return Forbid();
_lexBoxDbContext.Projects.Remove(project);
var hgService = HttpContext.RequestServices.GetRequiredService<IHgService>();
await hgService.DeleteRepo(project.Code);
await _lexBoxDbContext.SaveChangesAsync();
return project;
}
}
5 changes: 5 additions & 0 deletions backend/LexBoxApi/Services/HgService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ await Task.Run(() => CopyFilesRecursively(
));
}

public async Task DeleteRepo(string code)
{
await Task.Run(() => Directory.Delete(Path.Combine(_options.Value.RepoPath, code), true));
}

private void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
{
foreach (DirectoryInfo dir in source.GetDirectories())
Expand Down
1 change: 1 addition & 0 deletions backend/LexCore/ServiceInterfaces/IHgService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public interface IHgService
Task InitRepo(string code);
Task<DateTimeOffset?> GetLastCommitTimeFromHg(string projectCode);
Task<Changeset[]> GetChangesets(string projectCode);
Task DeleteRepo(string code);
}
40 changes: 40 additions & 0 deletions backend/Testing/ApiTests/NewProjectRaceCondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Text.Json.Nodes;
using Shouldly;

namespace Testing.ApiTests;

// issue: https://github.com/sillsdev/languageforge-lexbox/issues/173
[Trait("Category", "Integration")]
public class NewProjectRaceCondition : ApiTestBase
{
readonly Guid _projectId = Guid.Parse("3e81814d-ce7e-438f-b8e8-beac1cd7596b");
[Fact]
public async Task CanCreateProjectAndQueryItRightAway()
{

await LoginAs("admin", "pass");
await HttpClient.DeleteAsync($"http://{Host}/api/project/project/{_projectId}");
await Task.Delay(TimeSpan.FromSeconds(1));
var response = await ExecuteGql($$"""
mutation {
createProject(input: {
name: "Test race condition",
type: FL_EX,
id: "{{_projectId}}",
code: "test-race-flex",
description: "this is just a testing project for testing a race condition",
retentionPolicy: DEV
}) {
project {
name
changesets {
desc
}
}
}
}
""");
var project = response["data"]!["createProject"]!["project"].ShouldBeOfType<JsonObject>();
project["name"]!.GetValue<string>().ShouldBe("test");
}
}
2 changes: 1 addition & 1 deletion backend/Testing/Services/TestingEnvironmentVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testing.Services;

public static class TestingEnvironmentVariables
{
public static string ServerHostname = Environment.GetEnvironmentVariable("TEST_SERVER_HOSTNAME") ?? "localhost:5158";
public static string ServerHostname = Environment.GetEnvironmentVariable("TEST_SERVER_HOSTNAME") ?? "localhost";
public static string StandardHgHostname = Environment.GetEnvironmentVariable("TEST_STANDARD_HG_HOSTNAME") ?? "hg.localhost";
public static string ResumableHgHostname = Environment.GetEnvironmentVariable("TEST_RESUMABLE_HG_HOSTNAME") ?? "resumable.localhost";

Expand Down

0 comments on commit c126b62

Please sign in to comment.