mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 14:06:08 +00:00
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:
commit
327b0390de
2
.github/workflows/CI.yml
vendored
2
.github/workflows/CI.yml
vendored
@ -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' }}
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user