2020-09-18 05:50:17 -07:00
|
|
|
const fs = require("fs");
|
|
|
|
|
const path = require("path");
|
|
|
|
|
|
2020-04-25 23:03:58 -07:00
|
|
|
const { ApolloServer } = require("apollo-server");
|
|
|
|
|
const { createTestClient } = require("apollo-server-testing");
|
2020-09-02 23:00:16 -07:00
|
|
|
const { AuthenticationClient } = require("auth0");
|
2020-04-25 23:03:58 -07:00
|
|
|
|
2020-10-27 22:16:34 -07:00
|
|
|
const auth = require("../auth");
|
|
|
|
|
const actualAuth = jest.requireActual("../auth");
|
2020-04-25 23:03:58 -07:00
|
|
|
const connectToDb = require("../db");
|
|
|
|
|
const actualConnectToDb = jest.requireActual("../db");
|
|
|
|
|
const { config } = require("../index");
|
|
|
|
|
|
2020-09-02 23:00:16 -07:00
|
|
|
let accessTokenForQueries = null;
|
|
|
|
|
|
2020-10-22 20:35:06 -07:00
|
|
|
const { query, mutate } = createTestClient(
|
2020-09-02 23:00:16 -07:00
|
|
|
new ApolloServer({
|
|
|
|
|
...config,
|
|
|
|
|
context: () =>
|
|
|
|
|
config.context({
|
|
|
|
|
req: {
|
|
|
|
|
headers: {
|
|
|
|
|
authorization: accessTokenForQueries
|
|
|
|
|
? `Bearer ${accessTokenForQueries}`
|
|
|
|
|
: undefined,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
);
|
2020-04-25 23:03:58 -07:00
|
|
|
|
|
|
|
|
// Spy on db.execute, so we can snapshot the queries we run. This can help us
|
|
|
|
|
// keep an eye on perf - watch for tests with way too many queries!
|
|
|
|
|
jest.mock("../db");
|
|
|
|
|
let dbExecuteFn;
|
|
|
|
|
let db;
|
2020-09-18 05:50:17 -07:00
|
|
|
let dbEnvironment = "production";
|
2020-09-18 07:34:41 -07:00
|
|
|
let dbSetupDone = false;
|
2020-09-18 05:50:17 -07:00
|
|
|
const dbSetupScripts = [
|
|
|
|
|
fs
|
|
|
|
|
.readFileSync(
|
|
|
|
|
path.join(__dirname, "../../../scripts/setup-mysql-dev-constants.sql")
|
|
|
|
|
)
|
|
|
|
|
.toString(),
|
|
|
|
|
fs
|
|
|
|
|
.readFileSync(
|
|
|
|
|
path.join(__dirname, "../../../scripts/setup-mysql-dev-schema.sql")
|
|
|
|
|
)
|
|
|
|
|
.toString(),
|
|
|
|
|
];
|
2020-04-25 23:03:58 -07:00
|
|
|
beforeAll(() => {
|
2020-09-18 05:50:17 -07:00
|
|
|
connectToDb.mockImplementation(async () => {
|
|
|
|
|
let options;
|
|
|
|
|
|
|
|
|
|
if (dbEnvironment === "test") {
|
|
|
|
|
options = {
|
|
|
|
|
host: "localhost",
|
|
|
|
|
user: "impress_2020_test",
|
|
|
|
|
password: "impress_2020_test",
|
|
|
|
|
database: "impress_2020_test",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db = await actualConnectToDb(options);
|
|
|
|
|
|
2020-09-18 07:34:41 -07:00
|
|
|
if (dbEnvironment === "test" && !dbSetupDone) {
|
2020-09-18 05:50:17 -07:00
|
|
|
for (const script of dbSetupScripts) {
|
|
|
|
|
await db.query(script);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-18 07:34:41 -07:00
|
|
|
dbSetupDone = true;
|
2020-09-18 05:50:17 -07:00
|
|
|
|
2020-04-25 23:03:58 -07:00
|
|
|
dbExecuteFn = jest.spyOn(db, "execute");
|
|
|
|
|
return db;
|
|
|
|
|
});
|
2020-09-18 07:34:41 -07:00
|
|
|
|
|
|
|
|
// Mock out a current "now" date, for consistent snapshots
|
2020-10-27 22:16:34 -07:00
|
|
|
const ActualDate = Date;
|
|
|
|
|
const NOW = new ActualDate("2020-01-01T00:00:00.000Z");
|
2020-09-18 07:34:41 -07:00
|
|
|
jest.spyOn(global, "Date").mockImplementation(() => NOW);
|
2020-10-27 22:16:34 -07:00
|
|
|
Date.now = () => NOW.getTime();
|
2020-04-25 23:03:58 -07:00
|
|
|
});
|
2020-09-02 23:00:16 -07:00
|
|
|
beforeEach(() => {
|
2020-10-22 20:35:06 -07:00
|
|
|
// Restore auth values to default state.
|
2020-09-02 23:00:16 -07:00
|
|
|
accessTokenForQueries = null;
|
2020-10-27 22:16:34 -07:00
|
|
|
auth.getUserIdFromToken.mockImplementation(actualAuth.getUserIdFromToken);
|
2020-10-22 20:35:06 -07:00
|
|
|
|
|
|
|
|
// Restore db values to default state.
|
2020-08-21 16:22:16 -07:00
|
|
|
if (dbExecuteFn) {
|
|
|
|
|
dbExecuteFn.mockClear();
|
|
|
|
|
}
|
2020-09-18 05:50:17 -07:00
|
|
|
dbEnvironment = "production";
|
2020-09-18 07:34:41 -07:00
|
|
|
dbSetupDone = false;
|
2020-09-19 02:42:37 -07:00
|
|
|
db = null;
|
2020-04-25 23:03:58 -07:00
|
|
|
});
|
|
|
|
|
afterAll(() => {
|
2020-08-21 16:22:16 -07:00
|
|
|
if (db) {
|
|
|
|
|
db.end();
|
|
|
|
|
}
|
2020-09-18 07:34:41 -07:00
|
|
|
Date.mockRestore();
|
2020-04-25 23:03:58 -07:00
|
|
|
});
|
2020-08-21 16:22:16 -07:00
|
|
|
const getDbCalls = () => (dbExecuteFn ? dbExecuteFn.mock.calls : []);
|
2020-09-18 05:50:17 -07:00
|
|
|
const clearDbCalls = () => dbExecuteFn?.mockClear();
|
|
|
|
|
|
|
|
|
|
function useTestDb() {
|
|
|
|
|
if (db) {
|
|
|
|
|
throw new Error(`can't call useTestDb() if db mock already exists`);
|
|
|
|
|
}
|
|
|
|
|
dbEnvironment = "test";
|
|
|
|
|
}
|
2020-04-25 23:03:58 -07:00
|
|
|
|
2020-10-22 20:35:06 -07:00
|
|
|
jest.mock("../auth");
|
2020-09-02 23:00:16 -07:00
|
|
|
async function logInAsTestUser() {
|
2020-10-22 20:35:06 -07:00
|
|
|
if (dbEnvironment === "production") {
|
|
|
|
|
const auth0 = new AuthenticationClient({
|
|
|
|
|
domain: "openneo.us.auth0.com",
|
|
|
|
|
clientId: process.env.AUTH0_TEST_CLIENT_ID,
|
|
|
|
|
clientSecret: process.env.AUTH0_TEST_CLIENT_SECRET,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const res = await auth0.passwordGrant({
|
|
|
|
|
username: "dti-test",
|
|
|
|
|
password: process.env.DTI_TEST_USER_PASSWORD,
|
|
|
|
|
audience: "https://impress-2020.openneo.net/api",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
accessTokenForQueries = res.access_token;
|
|
|
|
|
} else if (dbEnvironment === "test") {
|
|
|
|
|
// Create a test user record. Most of these values don't matter.
|
|
|
|
|
const db = await connectToDb();
|
|
|
|
|
await db.query(
|
|
|
|
|
`INSERT INTO users (id, name, auth_server_id, remote_id)
|
|
|
|
|
VALUES (1, "test-user-1", 1, 1)`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Mock the server's auth code to return user ID 1.
|
2020-10-27 22:16:34 -07:00
|
|
|
auth.getUserIdFromToken.mockImplementation(async () => "1");
|
2020-10-22 20:35:06 -07:00
|
|
|
accessTokenForQueries = "mock-access-token-test-user-1";
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`unexpected dbEnvironment ${dbEnvironment}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-02 23:00:16 -07:00
|
|
|
|
2020-10-22 20:35:06 -07:00
|
|
|
async function createItem(id) {
|
|
|
|
|
if (dbEnvironment !== "test") {
|
|
|
|
|
throw new Error(`Please only use createItem in test db!`);
|
|
|
|
|
}
|
2020-09-02 23:00:16 -07:00
|
|
|
|
2020-10-22 20:35:06 -07:00
|
|
|
const name = `Test Item ${id}`;
|
|
|
|
|
|
|
|
|
|
const db = await connectToDb();
|
|
|
|
|
await Promise.all([
|
|
|
|
|
db.query(
|
|
|
|
|
`INSERT INTO items (id, zones_restrict, thumbnail_url, category,
|
|
|
|
|
type, rarity_index, price, weight_lbs)
|
|
|
|
|
VALUES (?, "00000000000000000000000000000000000000000000000",
|
|
|
|
|
"http://example.com/favicon.ico", "Clothes", "Clothes", 101,
|
|
|
|
|
0, 1);
|
|
|
|
|
`,
|
|
|
|
|
[id]
|
|
|
|
|
),
|
|
|
|
|
db.query(
|
|
|
|
|
`INSERT INTO item_translations (item_id, locale, name, description,
|
|
|
|
|
rarity)
|
|
|
|
|
VALUES (?, "en", ?, "This is a test item.", "Special")
|
|
|
|
|
`,
|
|
|
|
|
[id, name]
|
|
|
|
|
),
|
|
|
|
|
]);
|
2020-09-02 23:00:16 -07:00
|
|
|
}
|
|
|
|
|
|
2020-04-25 23:03:58 -07:00
|
|
|
// Add a new `expect(res).toHaveNoErrors()` to call after GraphQL calls!
|
|
|
|
|
expect.extend({
|
|
|
|
|
toHaveNoErrors(res) {
|
|
|
|
|
if (res.errors) {
|
|
|
|
|
return {
|
|
|
|
|
message: () =>
|
|
|
|
|
`expected no GraphQL errors, but got:\n ${res.errors}`,
|
|
|
|
|
pass: false,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
message: () => `expected GraphQL errors, but there were none`,
|
|
|
|
|
pass: true,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2020-10-07 07:49:27 -07:00
|
|
|
// Use the new modeling code, even though it's disabled in most environments,
|
|
|
|
|
// in order to test it.
|
|
|
|
|
process.env["USE_NEW_MODELING"] = "1";
|
|
|
|
|
|
2020-09-18 05:50:17 -07:00
|
|
|
module.exports = {
|
|
|
|
|
query,
|
2020-10-22 20:35:06 -07:00
|
|
|
mutate,
|
2020-09-18 05:50:17 -07:00
|
|
|
getDbCalls,
|
|
|
|
|
clearDbCalls,
|
2020-09-19 02:42:37 -07:00
|
|
|
connectToDb,
|
2020-09-18 05:50:17 -07:00
|
|
|
useTestDb,
|
|
|
|
|
logInAsTestUser,
|
2020-10-22 20:35:06 -07:00
|
|
|
createItem,
|
2020-09-18 05:50:17 -07:00
|
|
|
};
|