fediglam/tests/api_integration/lib.zig
2022-10-03 19:41:59 -07:00

46 lines
1.2 KiB
Zig

const std = @import("std");
const main = @import("main");
const sql = @import("sql");
const test_config = .{
.db = .{
.sqlite = .{
.db_file = ":memory:",
},
},
};
const ApiSource = main.api.ApiSource;
const root_user = "root";
const root_password = "password1234";
const admin_host = "example.com";
const admin_origin = "https://" ++ admin_host;
const random_seed = 1234;
fn makeDb(alloc: std.mem.Allocator) sql.Db {
var db = try sql.Db.open(test_config.db);
try main.migrations.up(&db);
try main.api.setupAdmin(&db, admin_origin, root_user, root_password, alloc);
}
fn makeApi(alloc: std.mem.Allocator, db: *sql.Db) !ApiSource {
main.api.initThreadPrng(random_seed);
const source = try ApiSource.init(alloc, test_config, db);
return source;
}
test "login as root" {
const alloc = std.testing.allocator;
var arena = std.heap.ArenaAllocator.init(alloc);
defer arena.deinit();
var db = try makeDb(alloc);
var src = try makeApi(alloc, &db);
std.debug.print("\npassword: {s}\n", .{root_password});
var api = try src.connectUnauthorized(admin_host, arena.allocator());
defer api.close();
_ = try api.login("root", root_password);
}