fix bugs in auth mocking for tests

This commit is contained in:
Emi Matchu 2020-10-27 22:16:34 -07:00
parent 3ce5e74b09
commit a49e66e359
4 changed files with 83 additions and 393 deletions

View file

@ -30,7 +30,7 @@ async function getUserIdFromToken(token) {
algorithms: ["RS256"], algorithms: ["RS256"],
}); });
} catch (e) { } catch (e) {
console.error(`Invalid auth token: ${token}\n${e}`); console.error(`Invalid auth token: ${token}`, e);
return null; return null;
} }

View file

@ -366,18 +366,32 @@ describe("Item", () => {
}, },
Object { Object {
"currentUserOwnsThis": false, "currentUserOwnsThis": false,
"currentUserWantsThis": false, "currentUserWantsThis": true,
"id": "39945", "id": "39945",
}, },
Object { Object {
"currentUserOwnsThis": false, "currentUserOwnsThis": true,
"currentUserWantsThis": false, "currentUserWantsThis": false,
"id": "39948", "id": "39948",
}, },
], ],
} }
`); `);
expect(getDbCalls()).toMatchInlineSnapshot(`Array []`); expect(getDbCalls()).toMatchInlineSnapshot(`
Array [
Array [
"SELECT closet_hangers.*, item_translations.name as item_name FROM closet_hangers
INNER JOIN items ON items.id = closet_hangers.item_id
INNER JOIN item_translations ON
item_translations.item_id = items.id AND locale = \\"en\\"
WHERE user_id IN (?)
ORDER BY item_name",
Array [
"44743",
],
],
]
`);
}); });
it("does not own/want items if not logged in", async () => { it("does not own/want items if not logged in", async () => {

View file

@ -737,16 +737,6 @@ Object {
}, },
], ],
}, },
Object {
"id": "78105",
"name": "Baby Blue Striped Dress",
"speciesThatNeedModels": Array [
Object {
"id": "55",
"name": "Vandagyre",
},
],
},
Object { Object {
"id": "81136", "id": "81136",
"name": "Baby Blue Fabric Dress", "name": "Baby Blue Fabric Dress",
@ -861,10 +851,6 @@ Object {
"id": "81777", "id": "81777",
"name": "Dyeworks Cream: Cosy Baby Onesie", "name": "Dyeworks Cream: Cosy Baby Onesie",
"speciesThatNeedModels": Array [ "speciesThatNeedModels": Array [
Object {
"id": "1",
"name": "Acara",
},
Object { Object {
"id": "4", "id": "4",
"name": "Bori", "name": "Bori",
@ -909,10 +895,6 @@ Object {
"id": "25", "id": "25",
"name": "Koi", "name": "Koi",
}, },
Object {
"id": "29",
"name": "Kyrii",
},
Object { Object {
"id": "30", "id": "30",
"name": "Lenny", "name": "Lenny",
@ -975,10 +957,6 @@ Object {
"id": "81778", "id": "81778",
"name": "Dyeworks Pink: Cosy Baby Onesie", "name": "Dyeworks Pink: Cosy Baby Onesie",
"speciesThatNeedModels": Array [ "speciesThatNeedModels": Array [
Object {
"id": "1",
"name": "Acara",
},
Object { Object {
"id": "4", "id": "4",
"name": "Bori", "name": "Bori",
@ -1027,10 +1005,6 @@ Object {
"id": "25", "id": "25",
"name": "Koi", "name": "Koi",
}, },
Object {
"id": "29",
"name": "Kyrii",
},
Object { Object {
"id": "30", "id": "30",
"name": "Lenny", "name": "Lenny",
@ -1059,10 +1033,6 @@ Object {
"id": "39", "id": "39",
"name": "Poogle", "name": "Poogle",
}, },
Object {
"id": "40",
"name": "Pteri",
},
Object { Object {
"id": "41", "id": "41",
"name": "Quiggle", "name": "Quiggle",
@ -1105,10 +1075,6 @@ Object {
"id": "81779", "id": "81779",
"name": "Dyeworks Black: Cosy Baby Onesie", "name": "Dyeworks Black: Cosy Baby Onesie",
"speciesThatNeedModels": Array [ "speciesThatNeedModels": Array [
Object {
"id": "1",
"name": "Acara",
},
Object { Object {
"id": "4", "id": "4",
"name": "Bori", "name": "Bori",
@ -1161,10 +1127,6 @@ Object {
"id": "25", "id": "25",
"name": "Koi", "name": "Koi",
}, },
Object {
"id": "29",
"name": "Kyrii",
},
Object { Object {
"id": "30", "id": "30",
"name": "Lenny", "name": "Lenny",
@ -1633,10 +1595,6 @@ Object {
"id": "81795", "id": "81795",
"name": "Dyeworks Purple: Maraquan Wig with Negg Accessory", "name": "Dyeworks Purple: Maraquan Wig with Negg Accessory",
"speciesThatNeedModels": Array [ "speciesThatNeedModels": Array [
Object {
"id": "2",
"name": "Aisha",
},
Object { Object {
"id": "5", "id": "5",
"name": "Bruce", "name": "Bruce",
@ -1673,10 +1631,6 @@ Object {
"id": "16", "id": "16",
"name": "Grarrl", "name": "Grarrl",
}, },
Object {
"id": "17",
"name": "Grundo",
},
Object { Object {
"id": "18", "id": "18",
"name": "Hissi", "name": "Hissi",
@ -1685,10 +1639,6 @@ Object {
"id": "20", "id": "20",
"name": "Jetsam", "name": "Jetsam",
}, },
Object {
"id": "22",
"name": "Kacheek",
},
Object { Object {
"id": "23", "id": "23",
"name": "Kau", "name": "Kau",
@ -1709,10 +1659,6 @@ Object {
"id": "30", "id": "30",
"name": "Lenny", "name": "Lenny",
}, },
Object {
"id": "31",
"name": "Lupe",
},
Object { Object {
"id": "34", "id": "34",
"name": "Moehog", "name": "Moehog",
@ -1729,10 +1675,6 @@ Object {
"id": "41", "id": "41",
"name": "Quiggle", "name": "Quiggle",
}, },
Object {
"id": "42",
"name": "Ruki",
},
Object { Object {
"id": "45", "id": "45",
"name": "Skeith", "name": "Skeith",
@ -1767,10 +1709,6 @@ Object {
"id": "81796", "id": "81796",
"name": "Dyeworks Green: Maraquan Wig with Negg Accessory", "name": "Dyeworks Green: Maraquan Wig with Negg Accessory",
"speciesThatNeedModels": Array [ "speciesThatNeedModels": Array [
Object {
"id": "2",
"name": "Aisha",
},
Object { Object {
"id": "5", "id": "5",
"name": "Bruce", "name": "Bruce",
@ -1783,10 +1721,6 @@ Object {
"id": "7", "id": "7",
"name": "Chia", "name": "Chia",
}, },
Object {
"id": "8",
"name": "Chomby",
},
Object { Object {
"id": "11", "id": "11",
"name": "Elephante", "name": "Elephante",
@ -1807,10 +1741,6 @@ Object {
"id": "16", "id": "16",
"name": "Grarrl", "name": "Grarrl",
}, },
Object {
"id": "17",
"name": "Grundo",
},
Object { Object {
"id": "18", "id": "18",
"name": "Hissi", "name": "Hissi",
@ -1823,10 +1753,6 @@ Object {
"id": "21", "id": "21",
"name": "Jubjub", "name": "Jubjub",
}, },
Object {
"id": "22",
"name": "Kacheek",
},
Object { Object {
"id": "23", "id": "23",
"name": "Kau", "name": "Kau",
@ -1847,10 +1773,6 @@ Object {
"id": "30", "id": "30",
"name": "Lenny", "name": "Lenny",
}, },
Object {
"id": "31",
"name": "Lupe",
},
Object { Object {
"id": "32", "id": "32",
"name": "Lutari", "name": "Lutari",
@ -1875,10 +1797,6 @@ Object {
"id": "41", "id": "41",
"name": "Quiggle", "name": "Quiggle",
}, },
Object {
"id": "42",
"name": "Ruki",
},
Object { Object {
"id": "45", "id": "45",
"name": "Skeith", "name": "Skeith",
@ -1929,10 +1847,6 @@ Object {
"id": "7", "id": "7",
"name": "Chia", "name": "Chia",
}, },
Object {
"id": "9",
"name": "Cybunny",
},
Object { Object {
"id": "11", "id": "11",
"name": "Elephante", "name": "Elephante",
@ -1953,10 +1867,6 @@ Object {
"id": "16", "id": "16",
"name": "Grarrl", "name": "Grarrl",
}, },
Object {
"id": "17",
"name": "Grundo",
},
Object { Object {
"id": "18", "id": "18",
"name": "Hissi", "name": "Hissi",
@ -1965,10 +1875,6 @@ Object {
"id": "20", "id": "20",
"name": "Jetsam", "name": "Jetsam",
}, },
Object {
"id": "22",
"name": "Kacheek",
},
Object { Object {
"id": "23", "id": "23",
"name": "Kau", "name": "Kau",
@ -1989,10 +1895,6 @@ Object {
"id": "30", "id": "30",
"name": "Lenny", "name": "Lenny",
}, },
Object {
"id": "31",
"name": "Lupe",
},
Object { Object {
"id": "34", "id": "34",
"name": "Moehog", "name": "Moehog",
@ -2013,10 +1915,6 @@ Object {
"id": "41", "id": "41",
"name": "Quiggle", "name": "Quiggle",
}, },
Object {
"id": "42",
"name": "Ruki",
},
Object { Object {
"id": "45", "id": "45",
"name": "Skeith", "name": "Skeith",
@ -2065,14 +1963,6 @@ Object {
"id": "2", "id": "2",
"name": "Aisha", "name": "Aisha",
}, },
Object {
"id": "3",
"name": "Blumaroo",
},
Object {
"id": "5",
"name": "Bruce",
},
Object { Object {
"id": "8", "id": "8",
"name": "Chomby", "name": "Chomby",
@ -2117,14 +2007,6 @@ Object {
"id": "36", "id": "36",
"name": "Nimmo", "name": "Nimmo",
}, },
Object {
"id": "37",
"name": "Ogrin",
},
Object {
"id": "38",
"name": "Peophin",
},
Object { Object {
"id": "39", "id": "39",
"name": "Poogle", "name": "Poogle",
@ -2175,18 +2057,10 @@ Object {
"id": "2", "id": "2",
"name": "Aisha", "name": "Aisha",
}, },
Object {
"id": "3",
"name": "Blumaroo",
},
Object { Object {
"id": "4", "id": "4",
"name": "Bori", "name": "Bori",
}, },
Object {
"id": "5",
"name": "Bruce",
},
Object { Object {
"id": "8", "id": "8",
"name": "Chomby", "name": "Chomby",
@ -2235,14 +2109,6 @@ Object {
"id": "36", "id": "36",
"name": "Nimmo", "name": "Nimmo",
}, },
Object {
"id": "37",
"name": "Ogrin",
},
Object {
"id": "38",
"name": "Peophin",
},
Object { Object {
"id": "40", "id": "40",
"name": "Pteri", "name": "Pteri",
@ -2255,10 +2121,6 @@ Object {
"id": "43", "id": "43",
"name": "Scorchio", "name": "Scorchio",
}, },
Object {
"id": "45",
"name": "Skeith",
},
Object { Object {
"id": "47", "id": "47",
"name": "Tonu", "name": "Tonu",
@ -2293,18 +2155,10 @@ Object {
"id": "2", "id": "2",
"name": "Aisha", "name": "Aisha",
}, },
Object {
"id": "3",
"name": "Blumaroo",
},
Object { Object {
"id": "4", "id": "4",
"name": "Bori", "name": "Bori",
}, },
Object {
"id": "5",
"name": "Bruce",
},
Object { Object {
"id": "8", "id": "8",
"name": "Chomby", "name": "Chomby",
@ -2353,14 +2207,6 @@ Object {
"id": "36", "id": "36",
"name": "Nimmo", "name": "Nimmo",
}, },
Object {
"id": "37",
"name": "Ogrin",
},
Object {
"id": "38",
"name": "Peophin",
},
Object { Object {
"id": "39", "id": "39",
"name": "Poogle", "name": "Poogle",
@ -2377,10 +2223,6 @@ Object {
"id": "43", "id": "43",
"name": "Scorchio", "name": "Scorchio",
}, },
Object {
"id": "45",
"name": "Skeith",
},
Object { Object {
"id": "47", "id": "47",
"name": "Tonu", "name": "Tonu",
@ -6879,20 +6721,6 @@ Object {
}, },
], ],
}, },
Object {
"id": "80553",
"name": "Candy Vampire Plushie",
"speciesThatNeedModels": Array [
Object {
"id": "20",
"name": "Jetsam",
},
Object {
"id": "24",
"name": "Kiko",
},
],
},
Object { Object {
"id": "81144", "id": "81144",
"name": "MME25-S5a: Storage Gear Trinket", "name": "MME25-S5a: Storage Gear Trinket",
@ -6913,6 +6741,10 @@ Object {
"id": "6", "id": "6",
"name": "Buzz", "name": "Buzz",
}, },
Object {
"id": "7",
"name": "Chia",
},
Object { Object {
"id": "8", "id": "8",
"name": "Chomby", "name": "Chomby",
@ -7071,6 +6903,10 @@ Object {
"id": "2", "id": "2",
"name": "Aisha", "name": "Aisha",
}, },
Object {
"id": "4",
"name": "Bori",
},
Object { Object {
"id": "5", "id": "5",
"name": "Bruce", "name": "Bruce",
@ -7079,6 +6915,10 @@ Object {
"id": "6", "id": "6",
"name": "Buzz", "name": "Buzz",
}, },
Object {
"id": "7",
"name": "Chia",
},
Object { Object {
"id": "8", "id": "8",
"name": "Chomby", "name": "Chomby",
@ -7405,26 +7245,6 @@ Object {
"id": "4", "id": "4",
"name": "Bori", "name": "Bori",
}, },
Object {
"id": "5",
"name": "Bruce",
},
Object {
"id": "6",
"name": "Buzz",
},
Object {
"id": "7",
"name": "Chia",
},
Object {
"id": "8",
"name": "Chomby",
},
Object {
"id": "11",
"name": "Elephante",
},
Object { Object {
"id": "12", "id": "12",
"name": "Eyrie", "name": "Eyrie",
@ -7437,30 +7257,14 @@ Object {
"id": "18", "id": "18",
"name": "Hissi", "name": "Hissi",
}, },
Object {
"id": "20",
"name": "Jetsam",
},
Object { Object {
"id": "21", "id": "21",
"name": "Jubjub", "name": "Jubjub",
}, },
Object {
"id": "23",
"name": "Kau",
},
Object {
"id": "24",
"name": "Kiko",
},
Object { Object {
"id": "25", "id": "25",
"name": "Koi", "name": "Koi",
}, },
Object {
"id": "33",
"name": "Meerca",
},
Object { Object {
"id": "34", "id": "34",
"name": "Moehog", "name": "Moehog",
@ -7469,18 +7273,10 @@ Object {
"id": "36", "id": "36",
"name": "Nimmo", "name": "Nimmo",
}, },
Object {
"id": "37",
"name": "Ogrin",
},
Object { Object {
"id": "40", "id": "40",
"name": "Pteri", "name": "Pteri",
}, },
Object {
"id": "41",
"name": "Quiggle",
},
Object { Object {
"id": "43", "id": "43",
"name": "Scorchio", "name": "Scorchio",
@ -7547,10 +7343,6 @@ Object {
"id": "33", "id": "33",
"name": "Meerca", "name": "Meerca",
}, },
Object {
"id": "44",
"name": "Shoyru",
},
Object { Object {
"id": "54", "id": "54",
"name": "Zafara", "name": "Zafara",
@ -7565,14 +7357,6 @@ Object {
"id": "1", "id": "1",
"name": "Acara", "name": "Acara",
}, },
Object {
"id": "4",
"name": "Bori",
},
Object {
"id": "7",
"name": "Chia",
},
Object { Object {
"id": "24", "id": "24",
"name": "Kiko", "name": "Kiko",
@ -7585,10 +7369,6 @@ Object {
"id": "42", "id": "42",
"name": "Ruki", "name": "Ruki",
}, },
Object {
"id": "44",
"name": "Shoyru",
},
], ],
}, },
Object { Object {
@ -7606,64 +7386,8 @@ Object {
], ],
}, },
Object { Object {
"id": "81743", "id": "81753",
"name": "Haunted Maiden Gown", "name": "Staff of the Dead",
"speciesThatNeedModels": Array [
Object {
"id": "5",
"name": "Bruce",
},
Object {
"id": "11",
"name": "Elephante",
},
Object {
"id": "13",
"name": "Flotsam",
},
Object {
"id": "15",
"name": "Gnorbu",
},
Object {
"id": "17",
"name": "Grundo",
},
Object {
"id": "20",
"name": "Jetsam",
},
Object {
"id": "36",
"name": "Nimmo",
},
Object {
"id": "46",
"name": "Techo",
},
Object {
"id": "47",
"name": "Tonu",
},
Object {
"id": "53",
"name": "Yurble",
},
],
},
Object {
"id": "81748",
"name": "Ethereal Contacts",
"speciesThatNeedModels": Array [
Object {
"id": "41",
"name": "Quiggle",
},
],
},
Object {
"id": "81767",
"name": "Vintage Halloween Party Shoes",
"speciesThatNeedModels": Array [ "speciesThatNeedModels": Array [
Object { Object {
"id": "4", "id": "4",
@ -7678,8 +7402,48 @@ Object {
"name": "Chia", "name": "Chia",
}, },
Object { Object {
"id": "20", "id": "8",
"name": "Jetsam", "name": "Chomby",
},
Object {
"id": "17",
"name": "Grundo",
},
Object {
"id": "21",
"name": "Jubjub",
},
Object {
"id": "25",
"name": "Koi",
},
Object {
"id": "34",
"name": "Moehog",
},
Object {
"id": "37",
"name": "Ogrin",
},
Object {
"id": "40",
"name": "Pteri",
},
Object {
"id": "41",
"name": "Quiggle",
},
Object {
"id": "43",
"name": "Scorchio",
},
Object {
"id": "53",
"name": "Yurble",
},
Object {
"id": "54",
"name": "Zafara",
}, },
], ],
}, },
@ -7731,98 +7495,12 @@ Object {
"id": "81805", "id": "81805",
"name": "Dyeworks Orange: Playful Scarecrow Makeup", "name": "Dyeworks Orange: Playful Scarecrow Makeup",
"speciesThatNeedModels": Array [ "speciesThatNeedModels": Array [
Object {
"id": "1",
"name": "Acara",
},
Object {
"id": "8",
"name": "Chomby",
},
Object { Object {
"id": "15", "id": "15",
"name": "Gnorbu", "name": "Gnorbu",
}, },
], ],
}, },
Object {
"id": "81896",
"name": "Vampire Wings",
"speciesThatNeedModels": Array [
Object {
"id": "1",
"name": "Acara",
},
Object {
"id": "4",
"name": "Bori",
},
Object {
"id": "7",
"name": "Chia",
},
Object {
"id": "8",
"name": "Chomby",
},
Object {
"id": "9",
"name": "Cybunny",
},
Object {
"id": "13",
"name": "Flotsam",
},
Object {
"id": "15",
"name": "Gnorbu",
},
Object {
"id": "18",
"name": "Hissi",
},
Object {
"id": "20",
"name": "Jetsam",
},
Object {
"id": "21",
"name": "Jubjub",
},
Object {
"id": "24",
"name": "Kiko",
},
Object {
"id": "36",
"name": "Nimmo",
},
Object {
"id": "39",
"name": "Poogle",
},
Object {
"id": "40",
"name": "Pteri",
},
Object {
"id": "41",
"name": "Quiggle",
},
Object {
"id": "43",
"name": "Scorchio",
},
Object {
"id": "47",
"name": "Tonu",
},
Object {
"id": "53",
"name": "Yurble",
},
],
},
], ],
} }
`; `;
@ -7830,7 +7508,7 @@ Object {
exports[`Item loads items that need models 2`] = ` exports[`Item loads items that need models 2`] = `
Array [ Array [
Array [ Array [
"SELECT * FROM item_translations WHERE item_id IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) AND locale = \\"en\\"", "SELECT * FROM item_translations WHERE item_id IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) AND locale = \\"en\\"",
Array [ Array [
"36907", "36907",
"42448", "42448",
@ -7863,7 +7541,6 @@ Array [
"77441", "77441",
"77442", "77442",
"78813", "78813",
"80553",
"81144", "81144",
"81145", "81145",
"81229", "81229",
@ -7884,15 +7561,11 @@ Array [
"81709", "81709",
"81710", "81710",
"81736", "81736",
"81743", "81753",
"81748",
"81767",
"81783", "81783",
"81785", "81785",
"81805", "81805",
"81896",
"72260", "72260",
"78105",
"81136", "81136",
"81515", "81515",
"81516", "81516",

View file

@ -5,7 +5,8 @@ const { ApolloServer } = require("apollo-server");
const { createTestClient } = require("apollo-server-testing"); const { createTestClient } = require("apollo-server-testing");
const { AuthenticationClient } = require("auth0"); const { AuthenticationClient } = require("auth0");
const { getUserIdFromToken } = require("../auth"); const auth = require("../auth");
const actualAuth = jest.requireActual("../auth");
const connectToDb = require("../db"); const connectToDb = require("../db");
const actualConnectToDb = jest.requireActual("../db"); const actualConnectToDb = jest.requireActual("../db");
const { config } = require("../index"); const { config } = require("../index");
@ -74,13 +75,15 @@ beforeAll(() => {
}); });
// Mock out a current "now" date, for consistent snapshots // Mock out a current "now" date, for consistent snapshots
const NOW = new Date("2020-01-01T00:00:00.000Z"); const ActualDate = Date;
const NOW = new ActualDate("2020-01-01T00:00:00.000Z");
jest.spyOn(global, "Date").mockImplementation(() => NOW); jest.spyOn(global, "Date").mockImplementation(() => NOW);
Date.now = () => NOW.getTime();
}); });
beforeEach(() => { beforeEach(() => {
// Restore auth values to default state. // Restore auth values to default state.
accessTokenForQueries = null; accessTokenForQueries = null;
getUserIdFromToken.mockRestore(); auth.getUserIdFromToken.mockImplementation(actualAuth.getUserIdFromToken);
// Restore db values to default state. // Restore db values to default state.
if (dbExecuteFn) { if (dbExecuteFn) {
@ -131,7 +134,7 @@ async function logInAsTestUser() {
); );
// Mock the server's auth code to return user ID 1. // Mock the server's auth code to return user ID 1.
getUserIdFromToken.mockReturnValue("1"); auth.getUserIdFromToken.mockImplementation(async () => "1");
accessTokenForQueries = "mock-access-token-test-user-1"; accessTokenForQueries = "mock-access-token-test-user-1";
} else { } else {
throw new Error(`unexpected dbEnvironment ${dbEnvironment}`); throw new Error(`unexpected dbEnvironment ${dbEnvironment}`);