Hello from MCP server
import { ClientResponseError } from "pocketbase";
import { check, getApi, getPb, wait, waitMs, users } from "./common.js";
export async function stricklandTests(api) {
await check(async () => {
return await api.register(users.buck);
}, "Buck Strickland can register");
await check(async () => {
return await api.register(users.hank);
}, "Hank Hill can register");
await check(async () => {
const currencies = await api.listCurrencies(users.hank);
console.log(currencies);
}, "Hank Hill can list currencies");
await check(async () => {
try {
await api.register(users.bobby);
throw new Error("Was expected to fail");
} catch (e) {
if (e instanceof ClientResponseError) {
return "Failed as expected";
} else {
throw e;
}
}
}, "Bobby can't register because his password isn't 10 chars");
await check(async () => {
return await api.login(users.buck);
}, "Buck can login");
await check(async () => {
return await api.login(users.hank);
}, "Hank can login");
await check(async () => {
const r = await api.createOrg(users.buck);
users.buck.orgId = r.id;
}, "Buck can create the Strickland Propane organization");
await check(async () => {
return await api.viewOrg(users.buck, users.buck.orgId);
}, "Buck can view the Strickland Propane organization");
await check(async () => {
return await api.joinOrg(users.hank, users.buck.orgId);
}, "Hank can join Strickland Propane");
await check(async () => {
return await api.register(users.peggy);
}, "Peggy Hill can register, even though there's not reason for her to");
await check(async () => {
return await api.joinOrg(users.peggy, users.buck.orgId);
}, "If she gets the link, Peggy can join Strickland Propane");
await check(async () => {
try {
await api.viewOrg(users.peggy, users.buck.orgId);
throw new Error("Expected fail");
} catch (e) {
if (e instanceof ClientResponseError) {
return "Failed as expected";
} else {
throw e;
}
}
}, "But Peggy can't see anything because she isn't assigned a role in the org");
await check(async () => {
return await api.register(users.thatherton);
}, "Thatherton can register, too");
await check(async () => {
const r = await api.createOrg(users.thatherton);
users.thatherton.orgId = r.id;
}, "And create the Thatherton Fuels organization");
await check(async () => {
const profiles = await api.listOrgProfiles(users.buck);
const names = [users.hank.name, users.peggy.name, users.buck.name];
const foundNames = [];
for (const profile of profiles) {
foundNames.push(profile.expand.user.name);
if (!names.includes(profile.expand.user.name)) {
console.log(names, foundNames);
throw new Error("Viewing profiles outside the organization");
}
}
for (const name of names) {
if (!foundNames.includes(name)) {
console.log(names, foundNames);
throw new Error("Missing profiles from the organization");
}
}
return profiles;
}, "Bucks can see ALL the profiles in his org, but ONLY the ones in his org");
await check(async () => {
// TODO: also check length of profiles response
const profiles = await api.listOrgProfiles(users.thatherton);
const names = [users.thatherton.name];
const foundNames = profiles.map((p) => p.expand.user.name);
for (const name of foundNames) {
if (!names.includes(name)) {
console.log(names, foundNames);
throw new Error("Viewing profiles outside the organization");
}
}
for (const name of names) {
if (!foundNames.includes(name)) {
console.log(names, foundNames);
throw new Error("Missing profiles from the organization");
}
}
return profiles;
}, "Thatherton cannot see any Strickland Propane profiles, only Thatherton Fuels");
await check(async () => {
const roles = await api.listOrgRoles(users.buck);
for (const role of roles) {
if (role.org != users.buck.orgId) {
throw new Error("Buck should only see Strickland roles");
}
}
}, "Buck can only see the Strickland Propane roles");
await check(async () => {
await api.removeFromOrg(users.buck, users.peggy);
const profiles = await api.listOrgProfiles(users.buck);
const names = profiles.map((p) => p.expand.user.name);
if (names.includes(users.peggy.name)) {
throw new Error("Removing profile failed");
}
return profiles;
}, "Buck can remove Peggy's Strickland profile");
await check(async () => {
await api.assignRole(users.buck, users.hank, "tech");
const updated = await api.getOrgProfile(users.buck, users.hank);
const roleNames = updated.expand.roles.map((e) => e.name);
if (!roleNames.includes("tech")) {
throw new Error("Hank should have the tech role");
}
}, "Buck can assign the tech role to Hank");
await check(async () => {
await api.assignRole(users.buck, users.hank, "admin");
const updated = await api.getOrgProfile(users.buck, users.hank);
const roleNames = updated.expand.roles.map((e) => e.name);
if (!roleNames.includes("tech") || !roleNames.includes("admin")) {
throw new Error("Hank should have both the tech and admin role");
}
}, "Buck can additionally assign admin role to Hank");
await check(async () => {
await api.assignRole(users.buck, users.hank, "author");
const updated = await api.getOrgProfile(users.buck, users.hank);
const roleNames = updated.expand.roles.map((e) => e.name);
if (!roleNames.includes("author")) {
throw new Error("Hank should have the author role");
}
}, "Buck can additionally assign the author role to Hank");
await check(async () => {
await api.register(users.joejack);
await api.joinOrg(users.joejack, users.buck.orgId);
}, "Joe Jack can register and join Strickland");
await check(async () => {
await api.register(users.debbie);
await api.joinOrg(users.debbie, users.buck.orgId);
}, "Debbie can register and join Strickland");
await check(async () => {
await api.assignRole(users.hank, users.joejack, "tech");
const updated = await api.getOrgProfile(users.hank, users.joejack);
const roleNames = updated.expand.roles.map((e) => e.name);
if (!roleNames.includes("tech")) {
throw new Error("Joe Jack should have the tech role");
}
}, "Hank the admin can assign the tech role to Joe Jack");
await check(async () => {
const currencies = await api.listCurrencies(users.joejack);
console.log(currencies);
}, "Joe Jack can list currencies");
await check(async () => {
await api.assignRole(users.hank, users.debbie, "author");
const updated = await api.getOrgProfile(users.hank, users.debbie);
const roleNames = updated.expand.roles.map((e) => e.name);
if (!roleNames.includes("author")) {
throw new Error("Debbie should have the author role");
}
}, "Hank the admin can assign the author role to Debbie");
await check(async () => {
try {
await api.listOrgProfiles(users.joejack);
throw new Error("Joe Jack should be unauthorized");
} catch (e) {
return "Failed as expected";
}
}, "Joe Jack can NOT list the Strickland profiles");
}