-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #313 from OpenBeta/kao-ticksbyusername
Query ticks by username
- Loading branch information
Showing
10 changed files
with
144 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ import { jest } from '@jest/globals' | |
import { queryAPI, setUpServer } from '../utils/testUtils.js' | ||
import { muuidToString } from '../utils/helpers.js' | ||
import { TickInput } from '../db/TickTypes.js' | ||
import TickDataSource from '../model/TickDataSource.js' | ||
import UserDataSource from '../model/UserDataSource.js' | ||
import { UpdateProfileGQLInput } from '../db/UserTypes.js' | ||
|
||
jest.setTimeout(60000) | ||
|
||
|
@@ -14,13 +17,13 @@ describe('ticks API', () => { | |
let inMemoryDB | ||
|
||
// Mongoose models for mocking pre-existing state. | ||
let ticks: TickDataSource | ||
let users: UserDataSource | ||
let tickOne: TickInput | ||
|
||
beforeAll(async () => { | ||
({ server, inMemoryDB } = await setUpServer()) | ||
// Auth0 serializes uuids in "relaxed" mode, resulting in this hex string format | ||
// "59f1d95a-627d-4b8c-91b9-389c7424cb54" instead of base64 "WfHZWmJ9S4yRuTicdCTLVA==". | ||
user = muuid.mode('relaxed').v4() | ||
user = muuid.v4() | ||
userUuid = muuidToString(user) | ||
|
||
tickOne = { | ||
|
@@ -37,6 +40,8 @@ describe('ticks API', () => { | |
}) | ||
|
||
beforeEach(async () => { | ||
ticks = TickDataSource.getInstance() | ||
users = UserDataSource.getInstance() | ||
await inMemoryDB.clear() | ||
}) | ||
|
||
|
@@ -45,6 +50,62 @@ describe('ticks API', () => { | |
await inMemoryDB.close() | ||
}) | ||
|
||
describe('queries', () => { | ||
const userQuery = ` | ||
query userTicks($userId: MUUID, $username: String) { | ||
userTicks(userId: $userId, username: $username) { | ||
_id | ||
name | ||
notes | ||
climbId | ||
style | ||
attemptType | ||
dateClimbed | ||
grade | ||
userId | ||
} | ||
} | ||
` | ||
|
||
it('queries by userId', async () => { | ||
const userProfileInput: UpdateProfileGQLInput = { | ||
userUuid, | ||
username: 'cat.dog', | ||
email: '[email protected]' | ||
} | ||
await users.createOrUpdateUserProfile(user, userProfileInput) | ||
await ticks.addTick(tickOne) | ||
const response = await queryAPI({ | ||
query: userQuery, | ||
variables: { userId: userUuid }, | ||
userUuid | ||
}) | ||
expect(response.statusCode).toBe(200) | ||
const res = response.body.data.userTicks | ||
expect(res).toHaveLength(1) | ||
expect(res[0].name).toBe(tickOne.name) | ||
}) | ||
|
||
it('queries by username', async () => { | ||
const userProfileInput: UpdateProfileGQLInput = { | ||
userUuid, | ||
username: 'cat.dog', | ||
email: '[email protected]' | ||
} | ||
await users.createOrUpdateUserProfile(user, userProfileInput) | ||
await ticks.addTick(tickOne) | ||
const response = await queryAPI({ | ||
query: userQuery, | ||
variables: { username: 'cat.dog' }, | ||
userUuid | ||
}) | ||
expect(response.statusCode).toBe(200) | ||
const res = response.body.data.userTicks | ||
expect(res).toHaveLength(1) | ||
expect(res[0].name).toBe(tickOne.name) | ||
}) | ||
}) | ||
|
||
describe('mutations', () => { | ||
const createQuery = ` | ||
mutation ($input: Tick!) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ export const createContext = async ({ req }): Promise<any> => { | |
payload = await verifyJWT(token) | ||
} catch (e) { | ||
logger.error(`Can't verify JWT token ${e.toString() as string}`) | ||
throw new Error('An unxpected error has occurred. Please notify us at [email protected].') | ||
throw new Error('An unexpected error has occurred. Please notify us at [email protected].') | ||
} | ||
|
||
user.isBuilder = payload?.scope?.includes('builder:default') ?? false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
import mongoose from 'mongoose' | ||
import { produce } from 'immer' | ||
import TickDataSource from '../TickDataSource.js' | ||
import { connectDB, getTickModel } from '../../db/index.js' | ||
import { connectDB, getTickModel, getUserModel } from '../../db/index.js' | ||
import { TickInput } from '../../db/TickTypes.js' | ||
import muuid from 'uuid-mongodb' | ||
import UserDataSource from '../UserDataSource.js' | ||
import { UpdateProfileGQLInput } from '../../db/UserTypes.js' | ||
|
||
const userId = muuid.v4() | ||
|
||
const toTest: TickInput = { | ||
name: 'Small Dog', | ||
notes: 'Sandbagged', | ||
climbId: 'c76d2083-6b8f-524a-8fb8-76e1dc79833f', | ||
userId: 'abc123', | ||
userId: userId.toUUID().toString(), | ||
style: 'Lead', | ||
attemptType: 'Onsight', | ||
dateClimbed: new Date('2012-12-12'), | ||
|
@@ -20,7 +25,7 @@ const toTest2: TickInput = { | |
name: 'Sloppy Peaches', | ||
notes: 'v sloppy', | ||
climbId: 'b767d949-0daf-5af3-b1f1-626de8c84b2a', | ||
userId: 'abc123', | ||
userId: userId.toUUID().toString(), | ||
style: 'Lead', | ||
attemptType: 'Flash', | ||
dateClimbed: new Date('2012-10-15'), | ||
|
@@ -42,17 +47,21 @@ describe('Ticks', () => { | |
let ticks: TickDataSource | ||
const tickModel = getTickModel() | ||
|
||
let users: UserDataSource | ||
|
||
beforeAll(async () => { | ||
console.log('#BeforeAll Ticks') | ||
await connectDB() | ||
|
||
try { | ||
await getTickModel().collection.drop() | ||
await getUserModel().collection.drop() | ||
} catch (e) { | ||
console.log('Cleaning db') | ||
} | ||
|
||
ticks = new TickDataSource(mongoose.connection.db.collection('ticks')) | ||
ticks = TickDataSource.getInstance() | ||
users = UserDataSource.getInstance() | ||
}) | ||
|
||
afterAll(async () => { | ||
|
@@ -122,13 +131,19 @@ describe('Ticks', () => { | |
}) | ||
|
||
it('should grab all ticks by userId', async () => { | ||
const userProfileInput: UpdateProfileGQLInput = { | ||
userUuid: userId.toUUID().toString(), | ||
username: 'cat.dog', | ||
email: '[email protected]' | ||
} | ||
await users.createOrUpdateUserProfile(userId, userProfileInput) | ||
const tick = await ticks.addTick(toTest) | ||
|
||
if (tick == null) { | ||
fail('Should add a new tick') | ||
} | ||
|
||
const newTicks = await ticks.ticksByUser('abc123') | ||
const newTicks = await ticks.ticksByUser({ userId }) | ||
|
||
expect(newTicks.length).toEqual(1) | ||
}) | ||
|
@@ -141,33 +156,31 @@ describe('Ticks', () => { | |
if (tick == null || tick2 == null) { | ||
fail('Should add a new tick') | ||
} | ||
const userClimbTicks = await ticks.ticksByUserAndClimb('abc123', climbId) | ||
const userClimbTicks = await ticks.ticksByUserIdAndClimb(userId.toUUID().toString(), climbId) | ||
expect(userClimbTicks.length).toEqual(1) | ||
}) | ||
|
||
it('should delete all ticks with the specified userId', async () => { | ||
const userId = 'abc123' | ||
const newTicks = await ticks.importTicks(testImport) | ||
|
||
if (newTicks == null) { | ||
fail('Should add 3 new ticks') | ||
} | ||
|
||
await ticks.deleteAllTicks(userId) | ||
await ticks.deleteAllTicks(userId.toUUID().toString()) | ||
const newTick = await tickModel.findOne({ userId }) | ||
expect(newTick).toBeNull() | ||
}) | ||
|
||
it('should only delete MP imports', async () => { | ||
const userId = 'abc123' | ||
const MPTick = await ticks.addTick(toTest) | ||
const OBTick = await ticks.addTick(tickUpdate) | ||
|
||
if (MPTick == null || OBTick == null) { | ||
fail('Should add two new ticks') | ||
} | ||
|
||
await ticks.deleteImportedTicks(userId) | ||
await ticks.deleteImportedTicks(userId.toUUID().toString()) | ||
const newTick = await tickModel.findOne({ _id: OBTick._id }) | ||
expect(newTick?._id).toEqual(OBTick._id) | ||
expect(newTick?.notes).toEqual('Not sandbagged') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters