gristlabs_grist-core/app/server/lib/TestLogin.ts
Florent 16ebc32611
Add tests for UsersManager (#1149)
Context

HomeDBManager lacks of direct tests, which makes hard to make rework or refactorations.
Proposed solution

Specifically here, I introduce tests which call exposed UsersManager methods directly and check their result.

Also:

    I removed updateUserName which seems to me useless (updateUser does the same work)
    Taking a look at the getUserByLogin methods, it appears that Typescirpt infers it returns a Promise<User|null> while in no case it may resolve a nullish value, therefore I have forced to return a Promise<User> and have changed the call sites to reflect the change.

Related issues

I make this change for then working on #870
2024-09-05 16:30:04 -04:00

40 lines
1.4 KiB
TypeScript

import {SUPPORT_EMAIL} from 'app/gen-server/lib/homedb/HomeDBManager';
import {GristLoginSystem, GristServer} from 'app/server/lib/GristServer';
import {Request} from 'express';
/**
* Return a login system for testing. Just enough to use the test/login endpoint
* available when GRIST_TEST_LOGIN=1 is set.
*/
export async function getTestLoginSystem(): Promise<GristLoginSystem> {
return {
async getMiddleware(gristServer: GristServer) {
async function getLoginRedirectUrl(req: Request, url: URL) {
const target = new URL(gristServer.getHomeUrl(req, 'test/login'));
target.searchParams.append('next', url.href);
return target.href || url.href;
}
return {
getLoginRedirectUrl,
async getLogoutRedirectUrl(req: Request, url: URL) {
return url.href;
},
getSignUpRedirectUrl: getLoginRedirectUrl,
async addEndpoints() {
// Make sure support user has a test api key if needed.
if (process.env.TEST_SUPPORT_API_KEY) {
const dbManager = gristServer.getHomeDBManager();
const user = await dbManager.getUserByLogin(SUPPORT_EMAIL);
user.apiKey = process.env.TEST_SUPPORT_API_KEY;
await user.save();
}
return "test-login";
},
};
},
async deleteUser() {
// nothing to do
},
};
}