Skip to content

Commit c510166

Browse files
committed
Finalize tests
1 parent 494448e commit c510166

File tree

6 files changed

+34
-14
lines changed

6 files changed

+34
-14
lines changed

samples/terraform-local-exec/main.tf

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
terraform {
22
backend "http" {
3-
address = "http://localhost:5293/state/demo_devpro"
4-
lock_address = "http://localhost:5293/state/demo_devpro/lock"
5-
unlock_address = "http://localhost:5293/state/demo_devpro/lock"
3+
address = "http://localhost:5293/state/demo_localexec"
4+
lock_address = "http://localhost:5293/state/demo_localexec/lock"
5+
unlock_address = "http://localhost:5293/state/demo_localexec/lock"
66
lock_method = "POST"
77
unlock_method = "DELETE"
88
username = "admin"
@@ -16,3 +16,12 @@ resource "null_resource" "test_backend" {
1616
command = "echo 'Testing HTTP backend state management' > test.txt"
1717
}
1818
}
19+
20+
resource "local_file" "test" {
21+
content = "Test HTTP backend"
22+
filename = "${path.module}/temp.txt"
23+
}
24+
25+
resource "random_string" "test" {
26+
length = 16
27+
}

samples/terraform-local-exec/test.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Infrastructure.MongoDb/Repositories/StateLockRepository.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public async Task<List<StateLockModel>> FindAllAsync()
4040

4141
public async Task<StateLockModel> CreateAsync(StateLockModel input)
4242
{
43-
input.Id = ObjectId.GenerateNewId().ToString();
44-
input.Created = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fff+00:00");
4543
await _modelCollection.InsertOneAsync(input);
4644
return input;
4745
}

src/WebApi/Controllers/StateController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public async Task<IActionResult> Unlock(string name, [FromBody] StateLockModel i
114114
var existingLock = await stateLockRepository.FindOneAsync(name);
115115
if (existingLock != null)
116116
{
117-
if (!string.IsNullOrEmpty(lockId))
117+
if (string.IsNullOrEmpty(lockId))
118118
{
119119
return StatusCode(423, new { Message = "The state is locked." });
120120
}

test/WebApi.IntegrationTests/IntegrationTestBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System.Net.Http.Headers;
22
using Devpro.TerraformBackend.Domain.Models;
3-
using Microsoft.AspNetCore.Hosting;
4-
using Microsoft.Extensions.Configuration;
53

64
namespace Devpro.TerraformBackend.WebApi.IntegrationTests;
75

@@ -15,7 +13,9 @@ public abstract class IntegrationTestBase(WebApplicationFactory<Program> factory
1513

1614
protected Faker<StateModel> StateFaker { get; } = new Faker<StateModel>("en");
1715

18-
protected Faker<StateLockModel> StateLockFaker { get; } = new Faker<StateLockModel>("en");
16+
protected Faker<StateLockModel> StateLockFaker { get; } = new Faker<StateLockModel>("en")
17+
.RuleFor(u => u.Id, f => Guid.NewGuid().ToString())
18+
.RuleFor(o => o.Created, f => DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fff+00:00"));
1919

2020
protected HttpClient CreateClient(bool isAuthorizationNeeded = false)
2121
{

test/WebApi.IntegrationTests/Resources/StateControllerResourceTest.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Devpro.TerraformBackend.WebApi.IntegrationTests.Http;
1+
using System.Net;
2+
using Devpro.TerraformBackend.WebApi.IntegrationTests.Http;
23

34
namespace Devpro.TerraformBackend.WebApi.IntegrationTests.Resources;
45

@@ -18,7 +19,7 @@ public async Task StateResource_GetNotExisting_ReturnsNoContent()
1819
var response = await client.GetAsync($"/state/{name}");
1920

2021
// Assert
21-
await response.CheckResponseAndGetContent(System.Net.HttpStatusCode.NoContent, null);
22+
await response.CheckResponseAndGetContent(HttpStatusCode.NoContent, null);
2223
}
2324

2425
[Fact]
@@ -35,7 +36,7 @@ public async Task StateResource_CreateNew_ReturnsCreated()
3536

3637
// Assert
3738
//TODO: test resource URL in response
38-
await response.CheckResponseAndGetContent(System.Net.HttpStatusCode.OK, null, string.Empty);
39+
await response.CheckResponseAndGetContent(HttpStatusCode.OK, null, string.Empty);
3940
}
4041

4142
[Fact]
@@ -45,9 +46,22 @@ public async Task StateResource_LockLifeCycle_IsSuccess()
4546
var client = CreateClient(true);
4647
var name = Faker.Random.Word();
4748
var stateLock = StateLockFaker.Generate();
49+
var payload = GeneratePayload();
4850

4951
// Act & Assert
5052
var createLockResponse = await client.PostAsync($"/state/{name}/lock", Serialize(stateLock));
51-
await createLockResponse.CheckResponseAndGetContent(System.Net.HttpStatusCode.OK, "application/json; charset=utf-8", null);
53+
await createLockResponse.CheckResponseAndGetContent(HttpStatusCode.OK, "application/json; charset=utf-8", null);
54+
var deleteLockRequest = new HttpRequestMessage(HttpMethod.Delete, $"/state/{name}/lock")
55+
{
56+
Content = Serialize(stateLock)
57+
};
58+
var missingLockIdUpdateResponse = await client.PostAsync($"/state/{name}", payload);
59+
await missingLockIdUpdateResponse.CheckResponseAndGetContent(HttpStatusCode.Locked, "application/json; charset=utf-8", "{\"message\":\"The state is locked.\"}");
60+
var wrongLockIdUpdateResponse = await client.PostAsync($"/state/{name}?ID=1234", payload);
61+
await wrongLockIdUpdateResponse.CheckResponseAndGetContent(HttpStatusCode.Conflict, "text/plain; charset=utf-8", "LockId doesn't match with the existing lock");
62+
var updateResponse = await client.PostAsync($"/state/{name}?ID={stateLock.Id}", payload);
63+
await updateResponse.CheckResponseAndGetContent(HttpStatusCode.OK, null, string.Empty);
64+
var deleteLockResponse = await client.SendAsync(deleteLockRequest);
65+
await deleteLockResponse.CheckResponseAndGetContent(HttpStatusCode.OK, null);
5266
}
5367
}

0 commit comments

Comments
 (0)