2022-05-26 06:47:26 +00:00
|
|
|
import {isOwner} from 'app/common/roles';
|
2020-07-21 13:20:51 +00:00
|
|
|
import {ManagerDelta, PermissionDelta, UserAPI} from 'app/common/UserAPI';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A utility to reset an organization into the state it would have when first
|
|
|
|
* created - no docs, one workspace called "Home", a single user. Should be
|
|
|
|
* called by a user who is both an owner of the org and a billing manager.
|
|
|
|
*/
|
|
|
|
export async function resetOrg(api: UserAPI, org: string|number) {
|
|
|
|
const session = await api.getSessionActive();
|
2022-05-26 06:47:26 +00:00
|
|
|
if (!isOwner(session.org)) {
|
2020-07-21 13:20:51 +00:00
|
|
|
throw new Error('user must be an owner of the org to be reset');
|
|
|
|
}
|
|
|
|
const billing = api.getBillingAPI();
|
(core) move more tests to grist-core
Summary:
* Tie build and run-time docker base images to a consistent version (buster)
* Extend the test login system activated by GRIST_TEST_LOGIN to ease porting tests that currently rely on cognito (many)
* Make org resets work in absence of billing endpoints
* When in-memory session caches are used, add missing invalidation steps
* Pass org information through sign-ups/sign-ins more carefully
* For CORS, explicitly trust GRIST_HOST origin when set
* Move some fixtures and tests to core, focussing on tests that cover existing failures or are in the set of tests run on deployments
* Retain regular `test` target to run the test suite directly, without docker
* Add a `test:smoke` target to run a single simple test without `GRIST_TEST_LOGIN` activated
* Add a `test:docker` target to run the tests against a grist-core docker image - since tests rely on certain fixture teams/docs, added `TEST_SUPPORT_API_KEY` and `TEST_ADD_SAMPLES` flags to ease porting
The tests ported were `nbrowser` tests: `ActionLog.ts` (the first test I tend to port to anything, out of habit), `Fork.ts` (exercises a lot of doc creation paths), `HomeIntro.ts` (a lot of DocMenu exercise), and `DuplicateDocument.ts` (covers a feature known to be failing prior to this diff, the CORS tweak resolves it).
Test Plan: Manually tested via `buildtools/build_core.sh`. In follow up, I want to add running the `test:docker` target in grist-core's workflows. In jenkins, only the smoke test is run. There'd be an argument for running all tests, but they include particularly slow tests, and are duplicates of tests already run (in different configuration admittedly), so I'd like to try first just using them in grist-core to gate updates to any packaged version of Grist (the docker image currently).
Reviewers: alexmojaki
Reviewed By: alexmojaki
Subscribers: alexmojaki
Differential Revision: https://phab.getgrist.com/D3176
2021-12-10 22:42:54 +00:00
|
|
|
// If billing api is not available, don't bother setting billing manager.
|
|
|
|
const account = await billing.getBillingAccount().catch(e => null);
|
|
|
|
if (account && !account.managers.some(manager => (manager.id === session.user.id))) {
|
2020-07-21 13:20:51 +00:00
|
|
|
throw new Error('user must be a billing manager');
|
|
|
|
}
|
|
|
|
const wss = await api.getOrgWorkspaces(org);
|
|
|
|
for (const ws of wss) {
|
|
|
|
if (!ws.isSupportWorkspace) {
|
|
|
|
await api.deleteWorkspace(ws.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await api.newWorkspace({name: 'Home'}, org);
|
|
|
|
const permissions: PermissionDelta = { users: {} };
|
|
|
|
for (const user of (await api.getOrgAccess(org)).users) {
|
|
|
|
if (user.id !== session.user.id) {
|
|
|
|
permissions.users![user.email] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await api.updateOrgPermissions(org, permissions);
|
|
|
|
// For non-individual accounts, update billing managers (individual accounts will
|
|
|
|
// throw an error if we try to do this).
|
(core) move more tests to grist-core
Summary:
* Tie build and run-time docker base images to a consistent version (buster)
* Extend the test login system activated by GRIST_TEST_LOGIN to ease porting tests that currently rely on cognito (many)
* Make org resets work in absence of billing endpoints
* When in-memory session caches are used, add missing invalidation steps
* Pass org information through sign-ups/sign-ins more carefully
* For CORS, explicitly trust GRIST_HOST origin when set
* Move some fixtures and tests to core, focussing on tests that cover existing failures or are in the set of tests run on deployments
* Retain regular `test` target to run the test suite directly, without docker
* Add a `test:smoke` target to run a single simple test without `GRIST_TEST_LOGIN` activated
* Add a `test:docker` target to run the tests against a grist-core docker image - since tests rely on certain fixture teams/docs, added `TEST_SUPPORT_API_KEY` and `TEST_ADD_SAMPLES` flags to ease porting
The tests ported were `nbrowser` tests: `ActionLog.ts` (the first test I tend to port to anything, out of habit), `Fork.ts` (exercises a lot of doc creation paths), `HomeIntro.ts` (a lot of DocMenu exercise), and `DuplicateDocument.ts` (covers a feature known to be failing prior to this diff, the CORS tweak resolves it).
Test Plan: Manually tested via `buildtools/build_core.sh`. In follow up, I want to add running the `test:docker` target in grist-core's workflows. In jenkins, only the smoke test is run. There'd be an argument for running all tests, but they include particularly slow tests, and are duplicates of tests already run (in different configuration admittedly), so I'd like to try first just using them in grist-core to gate updates to any packaged version of Grist (the docker image currently).
Reviewers: alexmojaki
Reviewed By: alexmojaki
Subscribers: alexmojaki
Differential Revision: https://phab.getgrist.com/D3176
2021-12-10 22:42:54 +00:00
|
|
|
if (account && !account.individual) {
|
2020-07-21 13:20:51 +00:00
|
|
|
const managers: ManagerDelta = { users: {} };
|
|
|
|
for (const user of account.managers) {
|
|
|
|
if (user.id !== session.user.id) {
|
|
|
|
managers.users[user.email] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await billing.updateBillingManagers(managers);
|
|
|
|
}
|
|
|
|
return api;
|
|
|
|
}
|