Skip to content

Commit bf19afa

Browse files
minimal refactor of the jest.global-setup.js to avoid driver to not being closed
1 parent a8b0065 commit bf19afa

File tree

1 file changed

+78
-70
lines changed

1 file changed

+78
-70
lines changed

packages/graphql/jest.global-setup.js

Lines changed: 78 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,81 @@ module.exports = async function globalSetup() {
2727
const { NEO_USER = "neo4j", NEO_PASSWORD = "password", NEO_URL = "neo4j://localhost:7687/neo4j" } = process.env;
2828
const auth = neo4j.auth.basic(NEO_USER, NEO_PASSWORD);
2929
const driver = neo4j.driver(NEO_URL, auth);
30+
try {
31+
await setupTestDatabase(driver, NEO_URL);
32+
33+
if (process.env.USE_RESTRICTED_USER === "true") {
34+
await dropTestUserAndRole(driver);
35+
await createTestUserAndRole(driver);
36+
}
37+
} finally {
38+
await driver.close();
39+
}
40+
};
41+
42+
async function createTestUserAndRole(driver) {
43+
let session = null;
44+
// Some tests use different DBs, so using "*" for now
45+
const dbName = "*";
46+
// GRANTS READ/WRITE SERVICE ACCOUNT
47+
const readWriteGrants = [
48+
`GRANT ACCESS ON DATABASE * TO ${INT_TEST_ROLE_NAME}`,
49+
`GRANT SHOW CONSTRAINT ON DATABASE ${dbName} TO ${INT_TEST_ROLE_NAME}`,
50+
`GRANT SHOW INDEX ON DATABASE ${dbName} TO ${INT_TEST_ROLE_NAME}`,
51+
`GRANT MATCH {*} ON GRAPH ${dbName} TO ${INT_TEST_ROLE_NAME}`,
52+
`GRANT EXECUTE PROCEDURE * ON DBMS TO ${INT_TEST_ROLE_NAME}`,
53+
`GRANT EXECUTE FUNCTION * ON DBMS TO ${INT_TEST_ROLE_NAME}`,
54+
`GRANT WRITE ON GRAPH ${dbName} TO ${INT_TEST_ROLE_NAME}`,
55+
`GRANT NAME MANAGEMENT ON DATABASE ${dbName} TO ${INT_TEST_ROLE_NAME}`,
56+
];
57+
58+
try {
59+
session = driver.session();
60+
await session.run(cypherCreateUser);
61+
await session.run(cypherCreateRole);
62+
await session.run(cypherGrantRole);
63+
64+
for (const cypherGrant of readWriteGrants) {
65+
await session.run(cypherGrant);
66+
}
67+
} catch (error) {
68+
if (errorHasGQLStatus("42NFF")) {
69+
console.log(
70+
`\nJest /packages/graphql setup: Will NOT create a separate integration test user and role as the command is not supported in the current environment.`
71+
);
72+
} else {
73+
throw error;
74+
}
75+
} finally {
76+
if (session) {
77+
await session.close();
78+
}
79+
}
80+
}
81+
82+
async function dropTestUserAndRole(driver) {
83+
let session = null;
84+
85+
try {
86+
session = driver.session();
87+
await session.run(cypherDropUser);
88+
await session.run(cypherDropRole);
89+
} catch (error) {
90+
if (errorHasGQLStatus("50N42")) {
91+
console.log(
92+
`\nJest /packages/graphql setup: Failure to drop test user/role, this is expected if the user/role does not exist. Error: ${error.message}`
93+
);
94+
} else {
95+
throw error;
96+
}
97+
} finally {
98+
if (session) {
99+
await session.close();
100+
}
101+
}
102+
}
103+
104+
async function setupTestDatabase(driver, neoURL) {
30105
const cypherCreateDb = `CREATE OR REPLACE DATABASE ${INT_TEST_DB_NAME} WAIT`;
31106
let session = null;
32107

@@ -54,7 +129,7 @@ module.exports = async function globalSetup() {
54129
);
55130
} else {
56131
console.log(
57-
`\nJest /packages/graphql setup: Failure to create test DB on neo4j @ ${NEO_URL}, cypher: "${cypherCreateDb}", Error: ${error.message}. Falling back to drop data.`
132+
`\nJest /packages/graphql setup: Failure to create test DB on neo4j @ ${neoURL}, cypher: "${cypherCreateDb}", Error: ${error.message}. Falling back to drop data.`
58133
);
59134
await dropDataAndIndexes(session);
60135
}
@@ -63,81 +138,14 @@ module.exports = async function globalSetup() {
63138
await session.close();
64139
}
65140
}
66-
if (process.env.USE_RESTRICTED_USER === "true") {
67-
// Some tests use different DBs, so using "*" for now
68-
const dbName = "*";
69-
// GRANTS READ/WRITE SERVICE ACCOUNT
70-
const readWriteGrants = [
71-
`GRANT ACCESS ON DATABASE * TO ${INT_TEST_ROLE_NAME}`,
72-
`GRANT SHOW CONSTRAINT ON DATABASE ${dbName} TO ${INT_TEST_ROLE_NAME}`,
73-
`GRANT SHOW INDEX ON DATABASE ${dbName} TO ${INT_TEST_ROLE_NAME}`,
74-
`GRANT MATCH {*} ON GRAPH ${dbName} TO ${INT_TEST_ROLE_NAME}`,
75-
`GRANT EXECUTE PROCEDURE * ON DBMS TO ${INT_TEST_ROLE_NAME}`,
76-
`GRANT EXECUTE FUNCTION * ON DBMS TO ${INT_TEST_ROLE_NAME}`,
77-
`GRANT WRITE ON GRAPH ${dbName} TO ${INT_TEST_ROLE_NAME}`,
78-
`GRANT NAME MANAGEMENT ON DATABASE ${dbName} TO ${INT_TEST_ROLE_NAME}`,
79-
];
80-
81-
try {
82-
session = driver.session();
83-
await dropUserAndRole(session);
84-
} catch (error) {
85-
if (errorHasGQLStatus("50N42")) {
86-
console.log(
87-
`\nJest /packages/graphql setup: Failure to drop test user/role, this is expected if the user/role does not exist. Error: ${error.message}`
88-
);
89-
} else {
90-
throw error;
91-
}
92-
} finally {
93-
if (session) {
94-
await session.close();
95-
}
96-
}
97-
98-
try {
99-
session = driver.session();
100-
await createUserAndRole(session);
101-
102-
for (const cypherGrant of readWriteGrants) {
103-
await session.run(cypherGrant);
104-
}
105-
} catch (error) {
106-
if (errorHasGQLStatus("42NFF")) {
107-
console.log(
108-
`\nJest /packages/graphql setup: Will NOT create a separate integration test user and role as the command is not supported in the current environment.`
109-
);
110-
} else {
111-
throw error;
112-
}
113-
} finally {
114-
if (session) {
115-
await session.close();
116-
}
117-
}
118-
}
119-
if (driver) {
120-
await driver.close();
121-
}
122-
};
141+
}
123142

124143
async function dropDataAndIndexes(session) {
125144
await session.run(cypherDropData);
126145
await session.run(cypherDropIndexes);
127146
}
128147

129-
async function dropUserAndRole(session) {
130-
await session.run(cypherDropUser);
131-
await session.run(cypherDropRole);
132-
}
133-
134-
async function createUserAndRole(session) {
135-
await session.run(cypherCreateUser);
136-
await session.run(cypherCreateRole);
137-
await session.run(cypherGrantRole);
138-
}
139-
140-
/* Dummy Javascript copy of the utility function available at: ./src/utils/error-has-gql-status */
148+
/* Javascript copy of the utility function available at: ./src/utils/error-has-gql-status */
141149
function errorHasGQLStatus(error, gqlStatus) {
142150
if (error.gqlStatus === gqlStatus) {
143151
return true;

0 commit comments

Comments
 (0)