Merge pull request #1 from bobf/login_example

Adjust test, fix memory leak in hashPassword, deinit repo on test app…
This commit is contained in:
Sean M. Collins 2025-03-23 09:36:56 -04:00 committed by GitHub
commit 327b0390de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 15 additions and 20 deletions

View File

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

View File

@ -37,34 +37,25 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
return request.fail(.forbidden); return request.fail(.forbidden);
} }
test "setup" { test "post" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes")); var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit(); 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, .{ try app.repo.insert(.User, .{
.id = 1, .id = 1,
.email = "test@test.com", .email = "test@test.com",
.password_hash = hashed_pass, .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", .{ const response = try app.request(.POST, "/login", .{
.params = .{ .json = .{
.email = "test@test.com", .email = "test@test.com",
.password = "test", .password = "test",
}, },
}); });
try response.expectStatus(.found); 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 { pub fn hashPassword(allocator: std.mem.Allocator, password: []const u8) ![]const u8 {
const buf = try allocator.alloc(u8, 128); var buf: [128]u8 = undefined;
return try std.crypto.pwhash.argon2.strHash( const hash = try std.crypto.pwhash.argon2.strHash(
password, password,
.{ .{
.allocator = allocator, .allocator = allocator,
.params = .{ .t = 3, .m = 32, .p = 4 }, .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. /// Free allocated resources for test app.
pub fn deinit(self: *App) void { pub fn deinit(self: *App) void {
self.repo.deinit();
self.arena.deinit(); self.arena.deinit();
self.allocator.destroy(self.arena); self.allocator.destroy(self.arena);
if (self.logger.test_logger.file) |file| file.close(); if (self.logger.test_logger.file) |file| file.close();