Get integration tests working
This commit is contained in:
parent
b79be4bfd1
commit
23da0c6857
5 changed files with 113 additions and 39 deletions
|
@ -1,45 +1,120 @@
|
|||
const std = @import("std");
|
||||
const main = @import("main");
|
||||
const api = @import("api");
|
||||
const migrations = @import("main").migrations;
|
||||
const sql = @import("sql");
|
||||
const util = @import("util");
|
||||
|
||||
const test_config = .{
|
||||
.db = .{
|
||||
.sqlite = .{
|
||||
.db_file = ":memory:",
|
||||
.sqlite_file_path = ":memory:",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const ApiSource = main.api.ApiSource;
|
||||
const ApiSource = 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 makeDb(alloc: std.mem.Allocator) !sql.Conn {
|
||||
try util.seedThreadPrng();
|
||||
var db = try sql.Conn.open(test_config.db);
|
||||
try migrations.up(try db.acquire());
|
||||
try api.setupAdmin(try db.acquire(), admin_origin, root_user, root_password, alloc);
|
||||
return db;
|
||||
}
|
||||
|
||||
fn makeApi(alloc: std.mem.Allocator, db: *sql.Db) !ApiSource {
|
||||
main.api.initThreadPrng(random_seed);
|
||||
fn connectAndLogin(
|
||||
api_source: *api.ApiSource,
|
||||
host: []const u8,
|
||||
username: []const u8,
|
||||
password: []const u8,
|
||||
alloc: std.mem.Allocator,
|
||||
) !api.LoginResponse {
|
||||
var conn = try api_source.connectUnauthorized(host, alloc);
|
||||
defer conn.close();
|
||||
|
||||
const source = try ApiSource.init(alloc, test_config, db);
|
||||
return source;
|
||||
return try util.deepClone(alloc, try conn.login(username, password));
|
||||
}
|
||||
|
||||
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);
|
||||
var src = try ApiSource.init(&db);
|
||||
|
||||
std.debug.print("\npassword: {s}\n", .{root_password});
|
||||
var api = try src.connectUnauthorized(admin_host, arena.allocator());
|
||||
defer api.close();
|
||||
const login = try connectAndLogin(&src, admin_host, root_user, root_password, alloc);
|
||||
defer util.deepFree(alloc, login);
|
||||
|
||||
_ = try api.login("root", root_password);
|
||||
var conn = try src.connectToken(admin_host, login.token, alloc);
|
||||
defer conn.close();
|
||||
const auth = try conn.verifyAuthorization();
|
||||
|
||||
try std.testing.expectEqual(login.user_id, auth.id);
|
||||
try std.testing.expectEqualStrings(root_user, auth.username);
|
||||
try std.testing.expectEqualStrings(admin_host, auth.host);
|
||||
}
|
||||
|
||||
test "create community" {
|
||||
const alloc = std.testing.allocator;
|
||||
var db = try makeDb(alloc);
|
||||
var src = try ApiSource.init(&db);
|
||||
|
||||
const login = try connectAndLogin(&src, admin_host, root_user, root_password, alloc);
|
||||
defer util.deepFree(alloc, login);
|
||||
|
||||
var conn = try src.connectToken(admin_host, login.token, alloc);
|
||||
defer conn.close();
|
||||
|
||||
const host = "fedi.example.com";
|
||||
const community = try conn.createCommunity("https://" ++ host);
|
||||
|
||||
try std.testing.expectEqual(api.Community.Scheme.https, community.scheme);
|
||||
try std.testing.expectEqual(api.Community.Kind.local, community.kind);
|
||||
try std.testing.expect(community.owner_id == null);
|
||||
try std.testing.expectEqualStrings(host, community.host);
|
||||
try std.testing.expectEqualStrings(host, community.name);
|
||||
}
|
||||
|
||||
test "create community and transfer to new owner" {
|
||||
const alloc = std.testing.allocator;
|
||||
var db = try makeDb(alloc);
|
||||
var src = try ApiSource.init(&db);
|
||||
|
||||
const root_login = try connectAndLogin(&src, admin_host, root_user, root_password, alloc);
|
||||
defer util.deepFree(alloc, root_login);
|
||||
|
||||
const host = "fedi.example.com";
|
||||
const invite = blk: {
|
||||
var conn = try src.connectToken(admin_host, root_login.token, alloc);
|
||||
defer conn.close();
|
||||
|
||||
const community = try conn.createCommunity("https://" ++ host);
|
||||
const invite = try conn.createInvite(.{ .to_community = community.id, .kind = .community_owner });
|
||||
break :blk try util.deepClone(alloc, invite);
|
||||
};
|
||||
defer util.deepFree(alloc, invite);
|
||||
|
||||
const username = "testuser";
|
||||
const password = root_password;
|
||||
{
|
||||
var conn = try src.connectUnauthorized(host, alloc);
|
||||
defer conn.close();
|
||||
|
||||
_ = try conn.register(username, password, .{ .invite_code = invite.code });
|
||||
}
|
||||
|
||||
const login = try connectAndLogin(&src, host, username, password, alloc);
|
||||
defer util.deepFree(alloc, login);
|
||||
|
||||
var conn = try src.connectToken(host, login.token, alloc);
|
||||
defer conn.close();
|
||||
|
||||
const auth = try conn.verifyAuthorization();
|
||||
|
||||
try std.testing.expectEqual(login.user_id, auth.id);
|
||||
try std.testing.expectEqualStrings(username, auth.username);
|
||||
try std.testing.expectEqualStrings(host, auth.host);
|
||||
try std.testing.expectEqual(invite.community_id, auth.community_id);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue