Adjust test, fix memory leak in hashPassword, deinit repo on test app deinit

This commit is contained in:
Bob Farrell 2025-03-23 13:28:09 +00:00
parent 79574ed861
commit 2cb1b4e959
4 changed files with 15 additions and 20 deletions

View File

@ -57,7 +57,7 @@ jobs:
JETQUERY_DATABASE: 'test'
# Assume a small amount of connections are allowed
# into postgres
JETQUERY_POOL_SIZE: 1
JETQUERY_POOL_SIZE: 2
- name: Build artifacts
if: ${{ matrix.os == 'ubuntu-latest' }}

View File

@ -37,34 +37,25 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
return request.fail(.forbidden);
}
test "setup" {
test "post" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const hashed_pass = try auth.hashPassword(app.allocator, "test");
defer app.allocator.free(hashed_pass);
const hashed_pass = try auth.hashPassword(std.testing.allocator, "test");
defer std.testing.allocator.free(hashed_pass);
try jetzig.database.Query(.User).deleteAll().execute(app.repo);
try app.repo.insert(.User, .{
.id = 1,
.email = "test@test.com",
.password_hash = hashed_pass,
});
}
test "post" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const response = try app.request(.POST, "/login", .{
.params = .{
.json = .{
.email = "test@test.com",
.password = "test",
},
});
try response.expectStatus(.found);
}
test "teardown" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const q = jetzig.database.Query(.User).find(1);
const user = try app.repo.execute(q) orelse @panic("not found");
try app.repo.delete(user);
}

View File

@ -42,13 +42,16 @@ pub fn verifyPassword(
}
pub fn hashPassword(allocator: std.mem.Allocator, password: []const u8) ![]const u8 {
const buf = try allocator.alloc(u8, 128);
return try std.crypto.pwhash.argon2.strHash(
var buf: [128]u8 = undefined;
const hash = try std.crypto.pwhash.argon2.strHash(
password,
.{
.allocator = allocator,
.params = .{ .t = 3, .m = 32, .p = 4 },
},
buf,
&buf,
);
const result = try allocator.alloc(u8, hash.len);
@memcpy(result, hash);
return result;
}

View File

@ -78,6 +78,7 @@ pub fn init(allocator: std.mem.Allocator, routes_module: type) !App {
/// Free allocated resources for test app.
pub fn deinit(self: *App) void {
self.repo.deinit();
self.arena.deinit();
self.allocator.destroy(self.arena);
if (self.logger.test_logger.file) |file| file.close();