|
| 1 | +import { ApolloServer } from '@apollo/server' |
| 2 | +import muuid from 'uuid-mongodb' |
| 3 | +import { jest } from '@jest/globals' |
| 4 | +import MutableMediaDataSource from '../model/MutableMediaDataSource.js' |
| 5 | +import { MediaObject, MediaObjectGQLInput } from '../db/MediaObjectTypes.js' |
| 6 | +import { queryAPI, setUpServer } from '../utils/testUtils.js' |
| 7 | +import { muuidToString } from '../utils/helpers.js' |
| 8 | +import { InMemoryDB } from '../utils/inMemoryDB.js' |
| 9 | +import express from 'express' |
| 10 | +import UserDataSource from '../model/UserDataSource.js' |
| 11 | + |
| 12 | +jest.setTimeout(60000) |
| 13 | + |
| 14 | +describe('E2E tests to validate behavior of media queries', () => { |
| 15 | + let server: ApolloServer |
| 16 | + let user: muuid.MUUID |
| 17 | + let userUuid: string |
| 18 | + let app: express.Application |
| 19 | + let inMemoryDB: InMemoryDB |
| 20 | + let userDs: UserDataSource |
| 21 | + |
| 22 | + beforeAll(async () => { |
| 23 | + ({ server, inMemoryDB, app } = await setUpServer()) |
| 24 | + // Auth0 serializes uuids in "relaxed" mode, resulting in this hex string format |
| 25 | + // "59f1d95a-627d-4b8c-91b9-389c7424cb54" instead of base64 "WfHZWmJ9S4yRuTicdCTLVA==". |
| 26 | + user = muuid.mode('relaxed').v4() |
| 27 | + userUuid = muuidToString(user) |
| 28 | + }) |
| 29 | + |
| 30 | + beforeEach(async () => { |
| 31 | + await inMemoryDB.clear() |
| 32 | + userDs = UserDataSource.getInstance() |
| 33 | + const res = await userDs.createOrUpdateUserProfile(user, { |
| 34 | + userUuid, |
| 35 | + username: 'iwannaclimbv17oneday', |
| 36 | + |
| 37 | + displayName: 'jared' |
| 38 | + }) |
| 39 | + expect(res).toBe(true) |
| 40 | + }) |
| 41 | + |
| 42 | + afterAll(async () => { |
| 43 | + await server.stop() |
| 44 | + await inMemoryDB.close() |
| 45 | + }) |
| 46 | + |
| 47 | + async function insertMediaObjects (mediaCount: number): Promise<MediaObject[]> { |
| 48 | + const newMediaListInput: MediaObjectGQLInput[] = [] |
| 49 | + for (let i = 0; i < mediaCount; i++) { |
| 50 | + newMediaListInput.push({ |
| 51 | + userUuid, |
| 52 | + width: 800, |
| 53 | + height: 600, |
| 54 | + format: 'jpeg', |
| 55 | + size: 45000, |
| 56 | + mediaUrl: `/areaPhoto${i}.jpg` |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + const media = MutableMediaDataSource.getInstance() |
| 61 | + return await media.addMediaObjects(newMediaListInput) |
| 62 | + } |
| 63 | + |
| 64 | + describe('media queries', () => { |
| 65 | + it('can resolve known media', async () => { |
| 66 | + const [object] = await insertMediaObjects(1) |
| 67 | + const response = await queryAPI({ |
| 68 | + query: ` |
| 69 | + query Media($input: MediaInput) { |
| 70 | + media(input: $input) { |
| 71 | + id |
| 72 | + mediaUrl |
| 73 | + } |
| 74 | + } |
| 75 | + `, |
| 76 | + operationName: 'Media', |
| 77 | + variables: { |
| 78 | + input: { |
| 79 | + id: object._id.toString() |
| 80 | + } |
| 81 | + }, |
| 82 | + userUuid, |
| 83 | + app |
| 84 | + }) |
| 85 | + |
| 86 | + expect(response.statusCode).toBe(200) |
| 87 | + expect(response.error).toBe(false) |
| 88 | + const mediaResult = response.body.data.media |
| 89 | + expect(mediaResult.id).toBe(object._id.toHexString()) |
| 90 | + }) |
| 91 | + |
| 92 | + it('Media resolver can yield a simple username', async () => { |
| 93 | + const [object] = await insertMediaObjects(1) |
| 94 | + const response = await queryAPI({ |
| 95 | + query: ` |
| 96 | + query Media($input: MediaInput) { |
| 97 | + media(input: $input) { |
| 98 | + id |
| 99 | + mediaUrl |
| 100 | + username |
| 101 | + } |
| 102 | + } |
| 103 | + `, |
| 104 | + operationName: 'Media', |
| 105 | + variables: { |
| 106 | + input: { |
| 107 | + id: object._id.toString() |
| 108 | + } |
| 109 | + }, |
| 110 | + userUuid, |
| 111 | + app |
| 112 | + }) |
| 113 | + |
| 114 | + expect(response.statusCode).toBe(200) |
| 115 | + expect(response.error).toBe(false) |
| 116 | + console.log(response) |
| 117 | + const mediaResult = response.body.data.media |
| 118 | + expect(mediaResult.id).toBe(object._id.toHexString()) |
| 119 | + expect(mediaResult.mediaUrl).toBeTruthy() |
| 120 | + expect(mediaResult.username).not.toBeNull() |
| 121 | + expect(mediaResult.username).not.toBeUndefined() |
| 122 | + }) |
| 123 | + |
| 124 | + it('Media resolver can yield a user node', async () => { |
| 125 | + const [object] = await insertMediaObjects(1) |
| 126 | + const response = await queryAPI({ |
| 127 | + query: ` |
| 128 | + query Media($input: MediaInput) { |
| 129 | + media(input: $input) { |
| 130 | + id |
| 131 | + mediaUrl |
| 132 | + user { |
| 133 | + username |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + `, |
| 138 | + operationName: 'Media', |
| 139 | + variables: { |
| 140 | + input: { |
| 141 | + id: object._id.toString() |
| 142 | + } |
| 143 | + }, |
| 144 | + userUuid, |
| 145 | + app |
| 146 | + }) |
| 147 | + |
| 148 | + expect(response.statusCode).toBe(200) |
| 149 | + expect(response.error).toBe(false) |
| 150 | + console.log(response) |
| 151 | + const mediaResult = response.body.data.media |
| 152 | + expect(mediaResult.id).toBe(object._id.toHexString()) |
| 153 | + expect(mediaResult.mediaUrl).toBeTruthy() |
| 154 | + console.log(mediaResult) |
| 155 | + expect(mediaResult.user).not.toBeNull() |
| 156 | + expect(mediaResult.user.username).toBe('iwannaclimbv17oneday') |
| 157 | + }) |
| 158 | + }) |
| 159 | +}) |
0 commit comments