Skip to content

Commit 327b039

Browse files
authored
Merge pull request #1 from bobf/login_example
Adjust test, fix memory leak in hashPassword, deinit repo on test app…
2 parents 79574ed + 2cb1b4e commit 327b039

File tree

4 files changed

+15
-20
lines changed

4 files changed

+15
-20
lines changed

.github/workflows/CI.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
JETQUERY_DATABASE: 'test'
5858
# Assume a small amount of connections are allowed
5959
# into postgres
60-
JETQUERY_POOL_SIZE: 1
60+
JETQUERY_POOL_SIZE: 2
6161

6262
- name: Build artifacts
6363
if: ${{ matrix.os == 'ubuntu-latest' }}

demo/src/app/views/login.zig

+7-16
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,25 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
3737
return request.fail(.forbidden);
3838
}
3939

40-
test "setup" {
40+
test "post" {
4141
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
4242
defer app.deinit();
43-
const hashed_pass = try auth.hashPassword(app.allocator, "test");
44-
defer app.allocator.free(hashed_pass);
43+
44+
const hashed_pass = try auth.hashPassword(std.testing.allocator, "test");
45+
defer std.testing.allocator.free(hashed_pass);
46+
47+
try jetzig.database.Query(.User).deleteAll().execute(app.repo);
4548
try app.repo.insert(.User, .{
4649
.id = 1,
4750
.email = "[email protected]",
4851
.password_hash = hashed_pass,
4952
});
50-
}
5153

52-
test "post" {
53-
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
54-
defer app.deinit();
5554
const response = try app.request(.POST, "/login", .{
56-
.params = .{
55+
.json = .{
5756
.email = "[email protected]",
5857
.password = "test",
5958
},
6059
});
6160
try response.expectStatus(.found);
6261
}
63-
64-
test "teardown" {
65-
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
66-
defer app.deinit();
67-
const q = jetzig.database.Query(.User).find(1);
68-
const user = try app.repo.execute(q) orelse @panic("not found");
69-
try app.repo.delete(user);
70-
}

src/jetzig/auth.zig

+6-3
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ pub fn verifyPassword(
4242
}
4343

4444
pub fn hashPassword(allocator: std.mem.Allocator, password: []const u8) ![]const u8 {
45-
const buf = try allocator.alloc(u8, 128);
46-
return try std.crypto.pwhash.argon2.strHash(
45+
var buf: [128]u8 = undefined;
46+
const hash = try std.crypto.pwhash.argon2.strHash(
4747
password,
4848
.{
4949
.allocator = allocator,
5050
.params = .{ .t = 3, .m = 32, .p = 4 },
5151
},
52-
buf,
52+
&buf,
5353
);
54+
const result = try allocator.alloc(u8, hash.len);
55+
@memcpy(result, hash);
56+
return result;
5457
}

src/jetzig/testing/App.zig

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub fn init(allocator: std.mem.Allocator, routes_module: type) !App {
7878

7979
/// Free allocated resources for test app.
8080
pub fn deinit(self: *App) void {
81+
self.repo.deinit();
8182
self.arena.deinit();
8283
self.allocator.destroy(self.arena);
8384
if (self.logger.test_logger.file) |file| file.close();

0 commit comments

Comments
 (0)