Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend configuration prevent testing #419

Open
thewilkybarkid opened this issue Oct 12, 2021 · 0 comments
Open

Backend configuration prevent testing #419

thewilkybarkid opened this issue Oct 12, 2021 · 0 comments

Comments

@thewilkybarkid
Copy link
Member

Background configuration is read by a single module and is then directly used by various places in the codebase, such as

roles.isMemberOf = async (group, id) => {
log.debug(`Checking if ${id} is a member of ${group}.`);
if (
config.adminUsers &&
group === 'admins' &&
isString(id) &&
orcidUtils.isValid(id)
) {
return (
config.adminUsers.includes(id) ||
(await groups.isMemberOf('admins', id))
);
} else {
return groups.isMemberOf(group, id);
}
};

This module contains all sorts of things not used (e.g. command-line options), but the main problem is that it reads from the environment/.env file and doesn't allow it to be changed programmatically, making testing impossible (refs #388, #400 (comment)).

Configuration needs to be changed so that it's passed in from the outside, allow tests to set it as needed.

thewilkybarkid added a commit that referenced this issue Oct 28, 2021
This change adds a test case for when a preprint cannot be found.

The test case is straightforward but requires changes to be able to support it. As there's a strong coupling between the code and the ORM (e.g. through `getFields`), we can't replace the repositories with in-memory versions. Worse, some of the code requires Postgres SQL, rather than the generic type we can use across different databases.

As a result, we have to try and use an actual Postgres instance. Doing so makes the tests slower and harder to develop. (Docker solves the problem for CI, but Jest tests should be isolated enough that they can be run locally, such as in the developer's IDE.)

To try and reduce the complexity, this uses pg-mem (https://github.com/oguimbal/pg-mem), an in-memory emulation of Postgres. I've not used this before, and the readme clearly labels it as experimental, but it's worked for what I've tried so far. It has a nice rollback feature, which allows resetting the database before each test. There's probably going to be pitfuls, but it seems to be worth continuing.

I had to change the codebase to allow passing in pg-mem's customised MikroORM. Unfortunately, there isn't a clean separation between the ORM and its database connection; I'd be much happier just passing in the latter to the app, but this will have to do for now.

Refs #388, #391, #419
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant