From a49e66e3592c91c0388732bbf542df8ce7cdc35c Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 27 Oct 2020 22:16:34 -0700 Subject: [PATCH] fix bugs in auth mocking for tests --- src/server/auth.js | 2 +- src/server/query-tests/Item.test.js | 20 +- .../__snapshots__/Item.test.js.snap | 443 +++--------------- src/server/query-tests/setup.js | 11 +- 4 files changed, 83 insertions(+), 393 deletions(-) diff --git a/src/server/auth.js b/src/server/auth.js index 1b3c430..cd197e3 100644 --- a/src/server/auth.js +++ b/src/server/auth.js @@ -30,7 +30,7 @@ async function getUserIdFromToken(token) { algorithms: ["RS256"], }); } catch (e) { - console.error(`Invalid auth token: ${token}\n${e}`); + console.error(`Invalid auth token: ${token}`, e); return null; } diff --git a/src/server/query-tests/Item.test.js b/src/server/query-tests/Item.test.js index 27945bc..4cdeb0a 100644 --- a/src/server/query-tests/Item.test.js +++ b/src/server/query-tests/Item.test.js @@ -366,18 +366,32 @@ describe("Item", () => { }, Object { "currentUserOwnsThis": false, - "currentUserWantsThis": false, + "currentUserWantsThis": true, "id": "39945", }, Object { - "currentUserOwnsThis": false, + "currentUserOwnsThis": true, "currentUserWantsThis": false, "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 () => { diff --git a/src/server/query-tests/__snapshots__/Item.test.js.snap b/src/server/query-tests/__snapshots__/Item.test.js.snap index 4b104b4..699cdb4 100644 --- a/src/server/query-tests/__snapshots__/Item.test.js.snap +++ b/src/server/query-tests/__snapshots__/Item.test.js.snap @@ -737,16 +737,6 @@ Object { }, ], }, - Object { - "id": "78105", - "name": "Baby Blue Striped Dress", - "speciesThatNeedModels": Array [ - Object { - "id": "55", - "name": "Vandagyre", - }, - ], - }, Object { "id": "81136", "name": "Baby Blue Fabric Dress", @@ -861,10 +851,6 @@ Object { "id": "81777", "name": "Dyeworks Cream: Cosy Baby Onesie", "speciesThatNeedModels": Array [ - Object { - "id": "1", - "name": "Acara", - }, Object { "id": "4", "name": "Bori", @@ -909,10 +895,6 @@ Object { "id": "25", "name": "Koi", }, - Object { - "id": "29", - "name": "Kyrii", - }, Object { "id": "30", "name": "Lenny", @@ -975,10 +957,6 @@ Object { "id": "81778", "name": "Dyeworks Pink: Cosy Baby Onesie", "speciesThatNeedModels": Array [ - Object { - "id": "1", - "name": "Acara", - }, Object { "id": "4", "name": "Bori", @@ -1027,10 +1005,6 @@ Object { "id": "25", "name": "Koi", }, - Object { - "id": "29", - "name": "Kyrii", - }, Object { "id": "30", "name": "Lenny", @@ -1059,10 +1033,6 @@ Object { "id": "39", "name": "Poogle", }, - Object { - "id": "40", - "name": "Pteri", - }, Object { "id": "41", "name": "Quiggle", @@ -1105,10 +1075,6 @@ Object { "id": "81779", "name": "Dyeworks Black: Cosy Baby Onesie", "speciesThatNeedModels": Array [ - Object { - "id": "1", - "name": "Acara", - }, Object { "id": "4", "name": "Bori", @@ -1161,10 +1127,6 @@ Object { "id": "25", "name": "Koi", }, - Object { - "id": "29", - "name": "Kyrii", - }, Object { "id": "30", "name": "Lenny", @@ -1633,10 +1595,6 @@ Object { "id": "81795", "name": "Dyeworks Purple: Maraquan Wig with Negg Accessory", "speciesThatNeedModels": Array [ - Object { - "id": "2", - "name": "Aisha", - }, Object { "id": "5", "name": "Bruce", @@ -1673,10 +1631,6 @@ Object { "id": "16", "name": "Grarrl", }, - Object { - "id": "17", - "name": "Grundo", - }, Object { "id": "18", "name": "Hissi", @@ -1685,10 +1639,6 @@ Object { "id": "20", "name": "Jetsam", }, - Object { - "id": "22", - "name": "Kacheek", - }, Object { "id": "23", "name": "Kau", @@ -1709,10 +1659,6 @@ Object { "id": "30", "name": "Lenny", }, - Object { - "id": "31", - "name": "Lupe", - }, Object { "id": "34", "name": "Moehog", @@ -1729,10 +1675,6 @@ Object { "id": "41", "name": "Quiggle", }, - Object { - "id": "42", - "name": "Ruki", - }, Object { "id": "45", "name": "Skeith", @@ -1767,10 +1709,6 @@ Object { "id": "81796", "name": "Dyeworks Green: Maraquan Wig with Negg Accessory", "speciesThatNeedModels": Array [ - Object { - "id": "2", - "name": "Aisha", - }, Object { "id": "5", "name": "Bruce", @@ -1783,10 +1721,6 @@ Object { "id": "7", "name": "Chia", }, - Object { - "id": "8", - "name": "Chomby", - }, Object { "id": "11", "name": "Elephante", @@ -1807,10 +1741,6 @@ Object { "id": "16", "name": "Grarrl", }, - Object { - "id": "17", - "name": "Grundo", - }, Object { "id": "18", "name": "Hissi", @@ -1823,10 +1753,6 @@ Object { "id": "21", "name": "Jubjub", }, - Object { - "id": "22", - "name": "Kacheek", - }, Object { "id": "23", "name": "Kau", @@ -1847,10 +1773,6 @@ Object { "id": "30", "name": "Lenny", }, - Object { - "id": "31", - "name": "Lupe", - }, Object { "id": "32", "name": "Lutari", @@ -1875,10 +1797,6 @@ Object { "id": "41", "name": "Quiggle", }, - Object { - "id": "42", - "name": "Ruki", - }, Object { "id": "45", "name": "Skeith", @@ -1929,10 +1847,6 @@ Object { "id": "7", "name": "Chia", }, - Object { - "id": "9", - "name": "Cybunny", - }, Object { "id": "11", "name": "Elephante", @@ -1953,10 +1867,6 @@ Object { "id": "16", "name": "Grarrl", }, - Object { - "id": "17", - "name": "Grundo", - }, Object { "id": "18", "name": "Hissi", @@ -1965,10 +1875,6 @@ Object { "id": "20", "name": "Jetsam", }, - Object { - "id": "22", - "name": "Kacheek", - }, Object { "id": "23", "name": "Kau", @@ -1989,10 +1895,6 @@ Object { "id": "30", "name": "Lenny", }, - Object { - "id": "31", - "name": "Lupe", - }, Object { "id": "34", "name": "Moehog", @@ -2013,10 +1915,6 @@ Object { "id": "41", "name": "Quiggle", }, - Object { - "id": "42", - "name": "Ruki", - }, Object { "id": "45", "name": "Skeith", @@ -2065,14 +1963,6 @@ Object { "id": "2", "name": "Aisha", }, - Object { - "id": "3", - "name": "Blumaroo", - }, - Object { - "id": "5", - "name": "Bruce", - }, Object { "id": "8", "name": "Chomby", @@ -2117,14 +2007,6 @@ Object { "id": "36", "name": "Nimmo", }, - Object { - "id": "37", - "name": "Ogrin", - }, - Object { - "id": "38", - "name": "Peophin", - }, Object { "id": "39", "name": "Poogle", @@ -2175,18 +2057,10 @@ Object { "id": "2", "name": "Aisha", }, - Object { - "id": "3", - "name": "Blumaroo", - }, Object { "id": "4", "name": "Bori", }, - Object { - "id": "5", - "name": "Bruce", - }, Object { "id": "8", "name": "Chomby", @@ -2235,14 +2109,6 @@ Object { "id": "36", "name": "Nimmo", }, - Object { - "id": "37", - "name": "Ogrin", - }, - Object { - "id": "38", - "name": "Peophin", - }, Object { "id": "40", "name": "Pteri", @@ -2255,10 +2121,6 @@ Object { "id": "43", "name": "Scorchio", }, - Object { - "id": "45", - "name": "Skeith", - }, Object { "id": "47", "name": "Tonu", @@ -2293,18 +2155,10 @@ Object { "id": "2", "name": "Aisha", }, - Object { - "id": "3", - "name": "Blumaroo", - }, Object { "id": "4", "name": "Bori", }, - Object { - "id": "5", - "name": "Bruce", - }, Object { "id": "8", "name": "Chomby", @@ -2353,14 +2207,6 @@ Object { "id": "36", "name": "Nimmo", }, - Object { - "id": "37", - "name": "Ogrin", - }, - Object { - "id": "38", - "name": "Peophin", - }, Object { "id": "39", "name": "Poogle", @@ -2377,10 +2223,6 @@ Object { "id": "43", "name": "Scorchio", }, - Object { - "id": "45", - "name": "Skeith", - }, Object { "id": "47", "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 { "id": "81144", "name": "MME25-S5a: Storage Gear Trinket", @@ -6913,6 +6741,10 @@ Object { "id": "6", "name": "Buzz", }, + Object { + "id": "7", + "name": "Chia", + }, Object { "id": "8", "name": "Chomby", @@ -7071,6 +6903,10 @@ Object { "id": "2", "name": "Aisha", }, + Object { + "id": "4", + "name": "Bori", + }, Object { "id": "5", "name": "Bruce", @@ -7079,6 +6915,10 @@ Object { "id": "6", "name": "Buzz", }, + Object { + "id": "7", + "name": "Chia", + }, Object { "id": "8", "name": "Chomby", @@ -7405,26 +7245,6 @@ Object { "id": "4", "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 { "id": "12", "name": "Eyrie", @@ -7437,30 +7257,14 @@ Object { "id": "18", "name": "Hissi", }, - Object { - "id": "20", - "name": "Jetsam", - }, Object { "id": "21", "name": "Jubjub", }, - Object { - "id": "23", - "name": "Kau", - }, - Object { - "id": "24", - "name": "Kiko", - }, Object { "id": "25", "name": "Koi", }, - Object { - "id": "33", - "name": "Meerca", - }, Object { "id": "34", "name": "Moehog", @@ -7469,18 +7273,10 @@ Object { "id": "36", "name": "Nimmo", }, - Object { - "id": "37", - "name": "Ogrin", - }, Object { "id": "40", "name": "Pteri", }, - Object { - "id": "41", - "name": "Quiggle", - }, Object { "id": "43", "name": "Scorchio", @@ -7547,10 +7343,6 @@ Object { "id": "33", "name": "Meerca", }, - Object { - "id": "44", - "name": "Shoyru", - }, Object { "id": "54", "name": "Zafara", @@ -7565,14 +7357,6 @@ Object { "id": "1", "name": "Acara", }, - Object { - "id": "4", - "name": "Bori", - }, - Object { - "id": "7", - "name": "Chia", - }, Object { "id": "24", "name": "Kiko", @@ -7585,10 +7369,6 @@ Object { "id": "42", "name": "Ruki", }, - Object { - "id": "44", - "name": "Shoyru", - }, ], }, Object { @@ -7606,64 +7386,8 @@ Object { ], }, Object { - "id": "81743", - "name": "Haunted Maiden Gown", - "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", + "id": "81753", + "name": "Staff of the Dead", "speciesThatNeedModels": Array [ Object { "id": "4", @@ -7678,8 +7402,48 @@ Object { "name": "Chia", }, Object { - "id": "20", - "name": "Jetsam", + "id": "8", + "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", "name": "Dyeworks Orange: Playful Scarecrow Makeup", "speciesThatNeedModels": Array [ - Object { - "id": "1", - "name": "Acara", - }, - Object { - "id": "8", - "name": "Chomby", - }, Object { "id": "15", "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`] = ` 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 [ "36907", "42448", @@ -7863,7 +7541,6 @@ Array [ "77441", "77442", "78813", - "80553", "81144", "81145", "81229", @@ -7884,15 +7561,11 @@ Array [ "81709", "81710", "81736", - "81743", - "81748", - "81767", + "81753", "81783", "81785", "81805", - "81896", "72260", - "78105", "81136", "81515", "81516", diff --git a/src/server/query-tests/setup.js b/src/server/query-tests/setup.js index 95347fa..2e7383e 100644 --- a/src/server/query-tests/setup.js +++ b/src/server/query-tests/setup.js @@ -5,7 +5,8 @@ const { ApolloServer } = require("apollo-server"); const { createTestClient } = require("apollo-server-testing"); const { AuthenticationClient } = require("auth0"); -const { getUserIdFromToken } = require("../auth"); +const auth = require("../auth"); +const actualAuth = jest.requireActual("../auth"); const connectToDb = require("../db"); const actualConnectToDb = jest.requireActual("../db"); const { config } = require("../index"); @@ -74,13 +75,15 @@ beforeAll(() => { }); // 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); + Date.now = () => NOW.getTime(); }); beforeEach(() => { // Restore auth values to default state. accessTokenForQueries = null; - getUserIdFromToken.mockRestore(); + auth.getUserIdFromToken.mockImplementation(actualAuth.getUserIdFromToken); // Restore db values to default state. if (dbExecuteFn) { @@ -131,7 +134,7 @@ async function logInAsTestUser() { ); // 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"; } else { throw new Error(`unexpected dbEnvironment ${dbEnvironment}`);