How to use GraphQLInteraction method in pact-foundation-pact

Best JavaScript code snippet using pact-foundation-pact

training.e2e-spec.ts

Source:training.e2e-spec.ts Github

copy

Full Screen

1import { INestApplication } from '@nestjs/common';2import { QueryRunner } from 'typeorm';3import { GeneratorGraphqlService } from '../src/seed/generator-graphql/generator-graphql.service';4import { createApp, destroyApp } from './helpers/module.helper';5import { graphQLInteraction } from './helpers/interaction';6import { loginTestUser, makeGraphQLRequest } from './helpers/http.helper';7import { Role } from '../src/graphql/ts/types';8import { testList } from '../src/test/list.tester';9import { testTraining } from '../src/test/training.tester';10import { TrainingSetService } from '../src/training/training-set/training-set.service';11import { MediaService } from '../src/media/media/media.service';12import { testMedia } from '../src/test/media.tester';13import { TrainingService } from '../src/training/training/training.service';14describe('Training integration', () => {15 let app: INestApplication;16 let queryRunner: QueryRunner;17 let gqlGenerator: GeneratorGraphqlService;18 let trainingSetService: TrainingSetService;19 let trainingService: TrainingService;20 let localMediaService: MediaService;21 beforeEach(async () => {22 const deps = await createApp();23 app = deps.app;24 queryRunner = deps.queryRunner;25 gqlGenerator = app.get<GeneratorGraphqlService>(GeneratorGraphqlService);26 trainingSetService = app.get<TrainingSetService>(TrainingSetService);27 trainingService = app.get<TrainingService>(TrainingService);28 localMediaService = app.get<MediaService>(MediaService);29 });30 it('should contain valid structure', async () => {31 expect(gqlGenerator).toBeDefined();32 expect(queryRunner).toBeDefined();33 expect(trainingSetService).toBeDefined();34 expect(trainingService).toBeDefined();35 expect(localMediaService).toBeDefined();36 expect(graphQLInteraction).toHaveProperty('allTrainings');37 expect(graphQLInteraction).toHaveProperty('training');38 expect(graphQLInteraction).toHaveProperty('createTraining');39 expect(graphQLInteraction).toHaveProperty('updateTraining');40 expect(graphQLInteraction).toHaveProperty('removeMediaFromTraining');41 expect(graphQLInteraction).toHaveProperty('removeTrainingFromTrainingSet');42 });43 describe('Read', () => {44 it('should fetch all trainings as admin', async () => {45 const result = await makeGraphQLRequest(46 app,47 graphQLInteraction.allTrainings({ pagination: { limit: 10 } }),48 { userRole: Role.ADMIN },49 );50 expect(result.body.errors).toBeUndefined();51 testList(result.body.data.allTrainings);52 await Promise.all(53 result.body.data.allTrainings.entries.map((training) =>54 testTraining(training),55 ),56 );57 });58 it('should fail fetching all trainings as user', async () => {59 const result = await makeGraphQLRequest(60 app,61 graphQLInteraction.allTrainings({ pagination: { limit: 10 } }),62 { userRole: Role.USER },63 );64 expect(result.body.errors).toEqual(expect.any(Array));65 });66 it('should fetch training as admin', async () => {67 const trainingSet = (68 await makeGraphQLRequest(69 app,70 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),71 { userRole: Role.ADMIN },72 )73 ).body.data.allTrainingSets.entries[0];74 const result = await makeGraphQLRequest(75 app,76 graphQLInteraction.training(trainingSet.trainings.entries[0].id),77 { userRole: Role.ADMIN },78 );79 expect(result.body.errors).toBeUndefined();80 await testTraining(result.body.data.training);81 });82 it('should fetch training as owner', async () => {83 const trainingSet = (84 await makeGraphQLRequest(85 app,86 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),87 { userRole: Role.USER },88 )89 ).body.data.allTrainingSets.entries[0];90 const result = await makeGraphQLRequest(91 app,92 graphQLInteraction.training(trainingSet.trainings.entries[0].id),93 { userRole: Role.USER },94 );95 expect(result.body.errors).toBeUndefined();96 await testTraining(result.body.data.training);97 });98 it('should fail fetching training as non-owner', async () => {99 const trainingSet = (100 await makeGraphQLRequest(101 app,102 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),103 { userRole: Role.USER, demoAccount: 2 },104 )105 ).body.data.allTrainingSets.entries[0];106 const result = await makeGraphQLRequest(107 app,108 graphQLInteraction.training(trainingSet.trainings.entries[0].id),109 { userRole: Role.USER },110 );111 expect(result.body.errors).toEqual(expect.any(Array));112 });113 it('should fail fetching non-existing training', async () => {114 const result = await makeGraphQLRequest(115 app,116 graphQLInteraction.training('non-existing-id'),117 { userRole: Role.USER },118 );119 expect(result.body.errors).toEqual(expect.any(Array));120 });121 });122 describe('Create', () => {123 it('should create training as admin', async () => {124 const trainingSet = (125 await makeGraphQLRequest(126 app,127 graphQLInteraction.createTrainingSet(gqlGenerator.trainingSetInput()),128 { userRole: Role.USER },129 )130 ).body.data.createTrainingSet;131 const medias = (132 await makeGraphQLRequest(133 app,134 graphQLInteraction.findMedia({135 local: true,136 query: 'workout',137 pagination: { limit: 5 },138 }),139 { userRole: Role.ADMIN },140 )141 ).body.data.findMedia;142 const [[localMedia]] = await localMediaService.findAndCount({143 pagination: { limit: 1 },144 });145 const lms = await localMedia.source;146 medias.entries.push({147 ...localMedia,148 source: { ...lms },149 });150 const training = gqlGenerator.trainingDraftInput();151 training.media = medias.entries.map((media) => ({152 sourceType: media.source.sourceType,153 id: media.source.resourceId,154 label: media.label,155 }));156 training.idTrainingSet = trainingSet.id;157 const result = await makeGraphQLRequest(158 app,159 graphQLInteraction.createTraining(training),160 { userRole: Role.ADMIN },161 );162 expect(result.body.errors).toBeUndefined();163 await testTraining(result.body.data.createTraining);164 await Promise.all(165 result.body.data.createTraining.media.entries.map(testMedia),166 );167 });168 it('should create training as owner of training set', async () => {169 const auth = await loginTestUser(app, Role.USER);170 const trainingSet = (171 await makeGraphQLRequest(172 app,173 graphQLInteraction.createTrainingSet(gqlGenerator.trainingSetInput()),174 { token: auth.token },175 )176 ).body.data.createTrainingSet;177 const medias = (178 await makeGraphQLRequest(179 app,180 graphQLInteraction.findMedia({181 local: true,182 query: 'workout',183 pagination: { limit: 5 },184 }),185 { token: auth.token },186 )187 ).body.data.findMedia;188 const [[localMedia]] = await localMediaService.findAndCount({189 pagination: { limit: 1 },190 });191 const lms = await localMedia.source;192 medias.entries.push({193 ...localMedia,194 source: { ...lms },195 });196 const training = gqlGenerator.trainingDraftInput();197 training.media = medias.entries.map((media) => ({198 sourceType: media.source.sourceType,199 id: media.source.resourceId,200 label: media.label,201 }));202 training.idTrainingSet = trainingSet.id;203 const result = await makeGraphQLRequest(204 app,205 graphQLInteraction.createTraining(training),206 { token: auth.token },207 );208 expect(result.body.errors).toBeUndefined();209 await testTraining(result.body.data.createTraining);210 await Promise.all(211 result.body.data.createTraining.media.entries.map(testMedia),212 );213 });214 it('should create training as owner of existing training set', async () => {215 const auth = await loginTestUser(app, Role.USER);216 const trainingSet = (217 await makeGraphQLRequest(218 app,219 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),220 { token: auth.token },221 )222 ).body.data.allTrainingSets.entries[0];223 const medias = (224 await makeGraphQLRequest(225 app,226 graphQLInteraction.findMedia({227 local: true,228 query: 'workout',229 pagination: { limit: 5 },230 }),231 { token: auth.token },232 )233 ).body.data.findMedia;234 const [[localMedia]] = await localMediaService.findAndCount({235 pagination: { limit: 1 },236 });237 const lms = await localMedia.source;238 medias.entries.push({239 ...localMedia,240 source: { ...lms },241 });242 const training = gqlGenerator.trainingDraftInput();243 training.media = medias.entries.map((media) => ({244 sourceType: media.source.sourceType,245 id: media.source.resourceId,246 label: media.label,247 }));248 training.idTrainingSet = trainingSet.id;249 const result = await makeGraphQLRequest(250 app,251 graphQLInteraction.createTraining(training),252 { token: auth.token },253 );254 expect(result.body.errors).toBeUndefined();255 await testTraining(result.body.data.createTraining);256 await Promise.all(257 result.body.data.createTraining.media.entries.map(testMedia),258 );259 const tSet = await makeGraphQLRequest(260 app,261 graphQLInteraction.trainingSet(training.idTrainingSet),262 { userRole: Role.USER },263 );264 expect(tSet.body.errors).toBeUndefined();265 expect(tSet.body.data.trainingSet.trainings.entries.length).toEqual(266 trainingSet.trainings.meta.totalCount + 1,267 );268 });269 it('should fail creating training within non-existing training set', async () => {270 const medias = (271 await makeGraphQLRequest(272 app,273 graphQLInteraction.findMedia({274 local: true,275 query: 'workout',276 pagination: { limit: 5 },277 }),278 { userRole: Role.ADMIN },279 )280 ).body.data.findMedia;281 const [[localMedia]] = await localMediaService.findAndCount({282 pagination: { limit: 1 },283 });284 const lms = await localMedia.source;285 medias.entries.push({286 ...localMedia,287 source: { ...lms },288 });289 const training = gqlGenerator.trainingDraftInput();290 training.media = medias.entries.map((media) => ({291 sourceType: media.source.sourceType,292 id: media.source.resourceId,293 label: media.label,294 }));295 training.idTrainingSet = 'non-existing-id';296 const result = await makeGraphQLRequest(297 app,298 graphQLInteraction.createTraining(training),299 { userRole: Role.ADMIN },300 );301 expect(result.body.errors).toEqual(expect.any(Array));302 });303 it('should fail creating training as non-owner of training set', async () => {304 const auth = await loginTestUser(app, Role.USER, 3);305 const trainingSet = (306 await makeGraphQLRequest(307 app,308 graphQLInteraction.createTrainingSet(gqlGenerator.trainingSetInput()),309 { userRole: Role.USER },310 )311 ).body.data.createTrainingSet;312 const medias = (313 await makeGraphQLRequest(314 app,315 graphQLInteraction.findMedia({316 local: true,317 query: 'workout',318 pagination: { limit: 5 },319 }),320 { token: auth.token },321 )322 ).body.data.findMedia;323 const [[localMedia]] = await localMediaService.findAndCount({324 pagination: { limit: 1 },325 });326 const lms = await localMedia.source;327 medias.entries.push({328 ...localMedia,329 source: { ...lms },330 });331 const training = gqlGenerator.trainingDraftInput();332 training.media = medias.entries.map((media) => ({333 sourceType: media.source.sourceType,334 id: media.source.resourceId,335 label: media.label,336 }));337 training.idTrainingSet = trainingSet.id;338 const result = await makeGraphQLRequest(339 app,340 graphQLInteraction.createTraining(training),341 { token: auth.token },342 );343 expect(result.body.errors).toEqual(expect.any(Array));344 });345 });346 describe('Update', () => {347 it('should update training as admin', async () => {348 const trainingSet = (349 await makeGraphQLRequest(350 app,351 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),352 { userRole: Role.ADMIN },353 )354 ).body.data.allTrainingSets.entries[0];355 const training = trainingSet.trainings.entries[0];356 const medias = (357 await makeGraphQLRequest(358 app,359 graphQLInteraction.findMedia({360 local: true,361 query: 'workout',362 pagination: { limit: 5 },363 }),364 { userRole: Role.ADMIN },365 )366 ).body.data.findMedia;367 const [[localMedia]] = await localMediaService.findAndCount({368 pagination: { limit: 1, offset: 10 },369 });370 const lms = await localMedia.source;371 medias.entries.push({372 ...localMedia,373 source: { ...lms },374 });375 const input = gqlGenerator.trainingUpdateInput();376 input.media = medias.entries.map((media) => ({377 sourceType: media.source.sourceType,378 id: media.source.resourceId,379 label: media.label,380 }));381 const result = await makeGraphQLRequest(382 app,383 graphQLInteraction.updateTraining(training.id, input),384 { userRole: Role.ADMIN },385 );386 expect(387 result.body.data.updateTraining.media.entries.map((e) => ({388 id: e.source.resourceId,389 sourceType: e.source.sourceType,390 label: e.label,391 })),392 ).toEqual(input.media);393 expect(result.body.errors).toBeUndefined();394 await testTraining(result.body.data.updateTraining);395 const trainingResult = await makeGraphQLRequest(396 app,397 graphQLInteraction.training(training.id),398 { userRole: Role.ADMIN },399 );400 expect(result.body.data.updateTraining).toEqual(401 expect.objectContaining({402 ...trainingResult.body.data.training,403 updatedAt: {404 humanReadable: expect.any(String),405 iso: expect.any(String),406 },407 media: {408 ...trainingResult.body.data.training.media,409 entries: expect.arrayContaining(410 trainingResult.body.data.training.media.entries,411 ),412 },413 }),414 );415 });416 it('should update training as owner', async () => {417 const trainingSet = (418 await makeGraphQLRequest(419 app,420 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),421 { userRole: Role.USER },422 )423 ).body.data.allTrainingSets.entries[0];424 const training = trainingSet.trainings.entries[0];425 const medias = (426 await makeGraphQLRequest(427 app,428 graphQLInteraction.findMedia({429 local: true,430 query: 'workout',431 pagination: { limit: 5 },432 }),433 { userRole: Role.USER },434 )435 ).body.data.findMedia;436 const [[localMedia]] = await localMediaService.findAndCount({437 pagination: { limit: 1, offset: 10 },438 });439 const lms = await localMedia.source;440 medias.entries.push({441 ...localMedia,442 source: { ...lms },443 });444 const input = gqlGenerator.trainingUpdateInput();445 input.media = medias.entries.map((media) => ({446 sourceType: media.source.sourceType,447 id: media.source.resourceId,448 label: media.label,449 }));450 const result = await makeGraphQLRequest(451 app,452 graphQLInteraction.updateTraining(training.id, input),453 { userRole: Role.USER },454 );455 expect(456 result.body.data.updateTraining.media.entries.map((e) => ({457 id: e.source.resourceId,458 sourceType: e.source.sourceType,459 label: e.label,460 })),461 ).toEqual(input.media);462 expect(result.body.errors).toBeUndefined();463 await testTraining(result.body.data.updateTraining);464 const trainingResult = await makeGraphQLRequest(465 app,466 graphQLInteraction.training(training.id),467 { userRole: Role.USER },468 );469 expect(result.body.data.updateTraining).toEqual(470 expect.objectContaining({471 ...trainingResult.body.data.training,472 updatedAt: {473 humanReadable: expect.any(String),474 iso: expect.any(String),475 },476 media: {477 ...trainingResult.body.data.training.media,478 entries: expect.arrayContaining(479 trainingResult.body.data.training.media.entries,480 ),481 },482 }),483 );484 });485 it('should fail updating training as non-owner', async () => {486 const trainingSet = (487 await makeGraphQLRequest(488 app,489 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),490 { userRole: Role.USER },491 )492 ).body.data.allTrainingSets.entries[0];493 const training = trainingSet.trainings.entries[0];494 const medias = (495 await makeGraphQLRequest(496 app,497 graphQLInteraction.findMedia({498 local: true,499 query: 'workout',500 pagination: { limit: 5 },501 }),502 { userRole: Role.USER },503 )504 ).body.data.findMedia;505 const [[localMedia]] = await localMediaService.findAndCount({506 pagination: { limit: 1 },507 });508 const lms = await localMedia.source;509 medias.entries.push({510 ...localMedia,511 source: { ...lms },512 });513 const input = gqlGenerator.trainingUpdateInput();514 input.media = medias.entries.map((media) => ({515 sourceType: media.source.sourceType,516 id: media.source.resourceId,517 label: media.label,518 }));519 const result = await makeGraphQLRequest(520 app,521 graphQLInteraction.updateTraining(training.id, input),522 { userRole: Role.USER, demoAccount: 3 },523 );524 expect(result.body.errors).toEqual(expect.any(Array));525 });526 it('should fail updating non-existing training', async () => {527 const medias = (528 await makeGraphQLRequest(529 app,530 graphQLInteraction.findMedia({531 local: true,532 query: 'workout',533 pagination: { limit: 5 },534 }),535 { userRole: Role.ADMIN },536 )537 ).body.data.findMedia;538 const [[localMedia]] = await localMediaService.findAndCount({539 pagination: { limit: 1 },540 });541 const lms = await localMedia.source;542 medias.entries.push({543 ...localMedia,544 source: { ...lms },545 });546 const input = gqlGenerator.trainingUpdateInput();547 input.media = medias.entries.map((media) => ({548 sourceType: media.source.sourceType,549 id: media.source.resourceId,550 label: media.label,551 }));552 const result = await makeGraphQLRequest(553 app,554 graphQLInteraction.updateTraining('non-existing-id', input),555 { userRole: Role.ADMIN },556 );557 expect(result.body.errors).toEqual(expect.any(Array));558 });559 });560 describe('Remove', () => {561 it('should remove media from training as admin', async () => {562 const [[training]] = await trainingService.findAndCount({563 pagination: { limit: 1 },564 });565 const medias = await training.medias;566 const result = await makeGraphQLRequest(567 app,568 graphQLInteraction.removeMediaFromTraining(training.id, medias[0].id),569 { userRole: Role.ADMIN },570 );571 expect(result.body.errors).toBeUndefined();572 await testTraining(result.body.data.removeMediaFromTraining);573 expect(result.body.data.removeMediaFromTraining.media.entries).toEqual(574 expect.arrayContaining(575 medias.slice(1).map((o) => expect.objectContaining(o)),576 ),577 );578 });579 it('should fail removing non-existing media from training as admin', async () => {580 const [[training]] = await trainingService.findAndCount({581 pagination: { limit: 1 },582 });583 const result = await makeGraphQLRequest(584 app,585 graphQLInteraction.removeMediaFromTraining(586 training.id,587 'non-existing-media-id',588 ),589 { userRole: Role.ADMIN },590 );591 expect(result.body.errors).toEqual(expect.any(Array));592 });593 it('should fail removing media from non-existing training as admin', async () => {594 const result = await makeGraphQLRequest(595 app,596 graphQLInteraction.removeMediaFromTraining(597 'non-existing-training-id',598 'non-existing-media-id',599 ),600 { userRole: Role.ADMIN },601 );602 expect(result.body.errors).toEqual(expect.any(Array));603 });604 it('should remove media from training as user', async () => {605 const all = await makeGraphQLRequest(606 app,607 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),608 { userRole: Role.USER },609 );610 const training =611 all.body.data.allTrainingSets.entries[0].trainings.entries[0];612 const medias = training.media.entries;613 const result = await makeGraphQLRequest(614 app,615 graphQLInteraction.removeMediaFromTraining(training.id, medias[0].id),616 { userRole: Role.USER },617 );618 expect(result.body.errors).toBeUndefined();619 await testTraining(result.body.data.removeMediaFromTraining);620 expect(result.body.data.removeMediaFromTraining.media.entries).toEqual(621 expect.arrayContaining(medias.slice(1)),622 );623 });624 it('should fail removing media from training as non-owner', async () => {625 const all = await makeGraphQLRequest(626 app,627 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),628 { userRole: Role.USER },629 );630 const training =631 all.body.data.allTrainingSets.entries[0].trainings.entries[0];632 const medias = training.media.entries;633 const result = await makeGraphQLRequest(634 app,635 graphQLInteraction.removeMediaFromTraining(training.id, medias[0].id),636 { userRole: Role.USER, demoAccount: 4 },637 );638 expect(result.body.errors).toEqual(expect.any(Array));639 });640 it('should remove training as admin', async () => {641 const trainingSetBefore = (642 await makeGraphQLRequest(643 app,644 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),645 { userRole: Role.ADMIN },646 )647 ).body.data.allTrainingSets.entries[0];648 const result = await makeGraphQLRequest(649 app,650 graphQLInteraction.removeTrainingFromTrainingSet(651 trainingSetBefore.trainings.entries[0].id,652 ),653 { userRole: Role.ADMIN },654 );655 expect(result.body.errors).toBeUndefined();656 const trainingSetAfter = (657 await makeGraphQLRequest(658 app,659 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),660 { userRole: Role.ADMIN },661 )662 ).body.data.allTrainingSets.entries[0];663 trainingSetBefore.trainings.entries = trainingSetBefore.trainings.entries.slice(664 1,665 );666 trainingSetBefore.trainings.meta.totalCount -= 1;667 expect(trainingSetAfter).toEqual(trainingSetBefore);668 });669 it('should fail removing non-existing training as admin', async () => {670 const result = await makeGraphQLRequest(671 app,672 graphQLInteraction.removeTrainingFromTrainingSet('non-existing-id'),673 { userRole: Role.ADMIN },674 );675 expect(result.body.errors).toEqual(expect.any(Array));676 });677 it('should remove training as owner', async () => {678 const trainingSetBefore = (679 await makeGraphQLRequest(680 app,681 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),682 { userRole: Role.USER },683 )684 ).body.data.allTrainingSets.entries[0];685 const result = await makeGraphQLRequest(686 app,687 graphQLInteraction.removeTrainingFromTrainingSet(688 trainingSetBefore.trainings.entries[0].id,689 ),690 { userRole: Role.USER },691 );692 expect(result.body.errors).toBeUndefined();693 const trainingSetAfter = (694 await makeGraphQLRequest(695 app,696 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),697 { userRole: Role.USER },698 )699 ).body.data.allTrainingSets.entries[0];700 trainingSetBefore.trainings.entries = trainingSetBefore.trainings.entries.slice(701 1,702 );703 trainingSetBefore.trainings.meta.totalCount -= 1;704 expect(trainingSetAfter).toEqual(trainingSetBefore);705 });706 it('should fail removing training as non-owner', async () => {707 const trainingSet = (708 await makeGraphQLRequest(709 app,710 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),711 { userRole: Role.USER },712 )713 ).body.data.allTrainingSets.entries[0];714 const result = await makeGraphQLRequest(715 app,716 graphQLInteraction.removeTrainingFromTrainingSet(717 trainingSet.trainings.entries[0].id,718 ),719 { userRole: Role.USER, demoAccount: 4 },720 );721 expect(result.body.errors).toEqual(expect.any(Array));722 });723 });724 afterEach(() => destroyApp({ app, queryRunner }));...

Full Screen

Full Screen

auth.e2e-spec.ts

Source:auth.e2e-spec.ts Github

copy

Full Screen

1import { INestApplication } from '@nestjs/common';2import { testUser } from '../src/test/user.tester';3import {4 Auth as IAuth,5 ResetPassword,6 Role,7 TokenType,8 UserUpdateInput,9} from '../src/graphql/ts/types';10import { graphQLInteraction } from './helpers/interaction';11import { GeneratorGraphqlService } from '../src/seed/generator-graphql/generator-graphql.service';12import { QueryRunner, Repository } from 'typeorm';13import { createApp, destroyApp } from './helpers/module.helper';14import { loginTestUser, makeGraphQLRequest } from './helpers/http.helper';15import * as _ from 'lodash';16import { testList } from '../src/test/list.tester';17import { UserService } from '../src/user/user.service';18import * as moment from 'moment';19import { Token } from '../src/user/token/token.entity';20import { getRepositoryToken } from '@nestjs/typeorm';21import { envVars } from './helpers/env.helper';22describe('Auth integration', () => {23 let app: INestApplication;24 let queryRunner: QueryRunner;25 let userService: UserService;26 let gqlGenerator: GeneratorGraphqlService;27 let tokenRepository: Repository<Token>;28 beforeEach(async () => {29 const deps = await createApp();30 app = deps.app;31 queryRunner = deps.queryRunner;32 userService = app.get<UserService>(UserService);33 gqlGenerator = app.get<GeneratorGraphqlService>(GeneratorGraphqlService);34 tokenRepository = app.get<Repository<Token>>(getRepositoryToken(Token));35 });36 it('should contain valid structure', () => {37 expect(gqlGenerator).toBeDefined();38 expect(userService).toBeDefined();39 expect(queryRunner).toBeDefined();40 expect(tokenRepository).toBeDefined();41 expect(graphQLInteraction).toBeDefined();42 expect(graphQLInteraction).toHaveProperty('userRegister');43 expect(graphQLInteraction).toHaveProperty('userLogin');44 expect(graphQLInteraction).toHaveProperty('userRequestPasswordReset');45 });46 it('should register user', async () => {47 const userToRegister = gqlGenerator.userRegister();48 const query = graphQLInteraction.userRegister(userToRegister);49 const response = await makeGraphQLRequest<{ userRegister: IAuth }>(50 app,51 query,52 );53 const { data } = response.body;54 expect(response.body.errors).toBeUndefined();55 expect(data).toHaveProperty('userRegister');56 expect(data.userRegister).toHaveProperty('token');57 expect(data.userRegister).toHaveProperty('user');58 await testUser(data.userRegister.user);59 expect(data.userRegister.user.emailValidated).toBeFalsy();60 });61 it('should register and login user', async () => {62 const userToRegister = gqlGenerator.userRegister();63 const query = graphQLInteraction.userRegister(userToRegister);64 await makeGraphQLRequest<{ userRegister: IAuth }>(app, query);65 const data = await makeGraphQLRequest<{ userLogin: IAuth }>(66 app,67 graphQLInteraction.userLogin(68 userToRegister.userName,69 userToRegister.password,70 ),71 );72 expect(data.body.data.userLogin).toHaveProperty('token');73 expect(data.body.data.userLogin).toHaveProperty('user');74 await testUser(data.body.data.userLogin.user);75 expect(data.body.data.userLogin.user.emailValidated).toBeFalsy();76 });77 it('should fail login due to non-existing user', async () => {78 const data = await makeGraphQLRequest<{ userLogin: IAuth }>(79 app,80 graphQLInteraction.userLogin('user', 'pass'),81 );82 expect(data.body.errors).toEqual(expect.any(Array));83 });84 it('should fail login due to invalid credentials', async () => {85 const userToRegister = gqlGenerator.userRegister();86 const query = graphQLInteraction.userRegister(userToRegister);87 await makeGraphQLRequest<{ userRegister: IAuth }>(app, query);88 const data = await makeGraphQLRequest<{ userLogin: IAuth }>(89 app,90 graphQLInteraction.userLogin(userToRegister.userName, ''),91 );92 expect(data.body.errors).toEqual(expect.any(Array));93 });94 it('should call role protected method and pass', async () => {95 await Promise.all(96 _.times(10, () => userService.create(gqlGenerator.userRegister())),97 );98 const response = await makeGraphQLRequest(99 app,100 graphQLInteraction.allUsers(),101 { userRole: Role.ADMIN },102 );103 const { data } = response.body;104 testList(data.allUsers);105 await Promise.all(data.allUsers.entries.map(testUser));106 });107 it('should call auth protected method and fail', async () => {108 await Promise.all(109 _.times(10, () => userService.create(gqlGenerator.userRegister())),110 );111 const response = await makeGraphQLRequest(112 app,113 graphQLInteraction.allUsers(),114 );115 expect(response.body.errors).toEqual(expect.any(Array));116 });117 it('should call role protected method and fail', async () => {118 await Promise.all(119 _.times(10, () => userService.create(gqlGenerator.userRegister())),120 );121 const response = await makeGraphQLRequest(122 app,123 graphQLInteraction.allUsers(),124 { userRole: Role.USER },125 );126 expect(response.body.errors).toEqual(expect.any(Array));127 });128 it('should inject current user', async () => {129 const response = await makeGraphQLRequest(app, graphQLInteraction.me(), {130 userRole: Role.ADMIN,131 });132 expect(response.body.errors).toBeUndefined();133 });134 it('should update my name', async () => {135 const userToRegister = gqlGenerator.userRegister();136 const registerResponse = await makeGraphQLRequest<{ userRegister: IAuth }>(137 app,138 graphQLInteraction.userRegister(userToRegister),139 );140 expect(registerResponse.body.errors).toBeUndefined();141 const auth = registerResponse.body.data.userRegister;142 const update: UserUpdateInput = {143 firstName: 'first',144 lastName: 'last',145 };146 const updateResponse = await makeGraphQLRequest(147 app,148 graphQLInteraction.userUpdateMyself(update),149 { token: auth.token },150 );151 expect(updateResponse.body.errors).toBeUndefined();152 expect(updateResponse.body.data.userUpdateMyself).toEqual(153 expect.objectContaining(154 _.pick(update, _.keys(updateResponse.body.data.userUpdateMyself)),155 ),156 );157 });158 it('should update my password', async () => {159 const userToRegister = gqlGenerator.userRegister();160 const registerResponse = await makeGraphQLRequest<{ userRegister: IAuth }>(161 app,162 graphQLInteraction.userRegister(userToRegister),163 );164 expect(registerResponse.body.errors).toBeUndefined();165 const auth = registerResponse.body.data.userRegister;166 const update: UserUpdateInput = {167 password: 'newPass',168 passwordRepeat: 'newPass',169 };170 const updateResponse = await makeGraphQLRequest(171 app,172 graphQLInteraction.userUpdateMyself(update),173 { token: auth.token },174 );175 expect(updateResponse.body.errors).toBeUndefined();176 const loginResponse = await makeGraphQLRequest(177 app,178 graphQLInteraction.userLogin(userToRegister.email, update.password),179 );180 expect(loginResponse.body.errors).toBeUndefined();181 });182 it('should not update my password due to mismatched passwords', async () => {183 const userToRegister = gqlGenerator.userRegister();184 const registerResponse = await makeGraphQLRequest<{ userRegister: IAuth }>(185 app,186 graphQLInteraction.userRegister(userToRegister),187 );188 expect(registerResponse.body.errors).toBeUndefined();189 const auth = registerResponse.body.data.userRegister;190 const update: UserUpdateInput = {191 password: 'newPass',192 passwordRepeat: 'notNewPass',193 };194 const updateResponse = await makeGraphQLRequest(195 app,196 graphQLInteraction.userUpdateMyself(update),197 { token: auth.token },198 );199 expect(updateResponse.body.errors).toEqual(expect.any(Array));200 const loginResponse = await makeGraphQLRequest(201 app,202 graphQLInteraction.userLogin(userToRegister.email, update.password),203 );204 expect(loginResponse.body.errors).toEqual(expect.any(Array));205 });206 it('should not update myself due to non-logged user', async () => {207 const update: UserUpdateInput = {208 firstName: 'first',209 lastName: 'last',210 };211 const updateResponse = await makeGraphQLRequest(212 app,213 graphQLInteraction.userUpdateMyself(update),214 );215 expect(updateResponse.body.errors).toEqual(expect.any(Array));216 });217 it('should reset password', async () => {218 const auth = await loginTestUser(app, Role.USER);219 const resultRequest = await makeGraphQLRequest(220 app,221 graphQLInteraction.userRequestPasswordReset(auth.user.email),222 );223 expect(resultRequest.body.errors).toBeUndefined();224 const token = await tokenRepository.findOne({225 where: {226 user: { id: auth.user.id },227 tokenType: TokenType.PASSWORD_RESET,228 },229 });230 expect(token).toBeDefined();231 expect(+moment(token.validUntil.iso)).toBeGreaterThan(+moment());232 const newPass = 'newPass';233 const input: ResetPassword = {234 newPassword: newPass,235 passwordRepeat: newPass,236 token: token.token,237 };238 const resultReset = await makeGraphQLRequest(239 app,240 graphQLInteraction.userResetPassword(input),241 );242 expect(resultReset.body.errors).toBeUndefined();243 const resultLogin = await makeGraphQLRequest(244 app,245 graphQLInteraction.userLogin(auth.user.userName, newPass),246 );247 expect(resultLogin.body.errors).toBeUndefined();248 await makeGraphQLRequest(249 app,250 graphQLInteraction.userUpdateMyself({251 password: envVars.APP_DEMO_USER_PASSWORD,252 passwordRepeat: envVars.APP_DEMO_USER_PASSWORD,253 }),254 { token: auth.token },255 );256 });257 it('should not reset password due to invalid token', async () => {258 const newPass = 'newPass';259 const input: ResetPassword = {260 newPassword: newPass,261 passwordRepeat: newPass,262 token: 'invalid-token',263 };264 const resultReset = await makeGraphQLRequest(265 app,266 graphQLInteraction.userResetPassword(input),267 );268 expect(resultReset.body.errors).toEqual(expect.any(Array));269 });270 it('should not reset password due to non-matching passwords', async () => {271 const auth = await loginTestUser(app, Role.USER);272 const resultRequest = await makeGraphQLRequest(273 app,274 graphQLInteraction.userRequestPasswordReset(auth.user.email),275 );276 expect(resultRequest.body.errors).toBeUndefined();277 const token = await tokenRepository.findOne({278 where: {279 user: { id: auth.user.id },280 tokenType: TokenType.PASSWORD_RESET,281 },282 });283 expect(token).toBeDefined();284 expect(+moment(token.validUntil.iso)).toBeGreaterThan(+moment());285 const newPass = 'newPass';286 const input: ResetPassword = {287 newPassword: newPass,288 passwordRepeat: newPass + '-wrong-pass',289 token: token.token,290 };291 const resultReset = await makeGraphQLRequest(292 app,293 graphQLInteraction.userResetPassword(input),294 );295 expect(resultReset.body.errors).toEqual(expect.any(Array));296 const resultLogin = await makeGraphQLRequest(297 app,298 graphQLInteraction.userLogin(auth.user.userName, newPass),299 );300 expect(resultLogin.body.errors).toEqual(expect.any(Array));301 });302 afterEach(() => destroyApp({ app, queryRunner }));...

Full Screen

Full Screen

training-set.e2e-spec.ts

Source:training-set.e2e-spec.ts Github

copy

Full Screen

1import { INestApplication } from '@nestjs/common';2import { QueryRunner } from 'typeorm';3import { GeneratorGraphqlService } from '../src/seed/generator-graphql/generator-graphql.service';4import { createApp, destroyApp } from './helpers/module.helper';5import { graphQLInteraction } from './helpers/interaction';6import { loginTestUser, makeGraphQLRequest } from './helpers/http.helper';7import { Role } from '../src/graphql/ts/types';8import { testList } from '../src/test/list.tester';9import { testTrainingSet } from '../src/test/training-set.tester';10import { TrainingSetService } from '../src/training/training-set/training-set.service';11describe('Training Set integration', () => {12 let app: INestApplication;13 let queryRunner: QueryRunner;14 let gqlGenerator: GeneratorGraphqlService;15 let trainingSetService: TrainingSetService;16 beforeEach(async () => {17 const deps = await createApp();18 app = deps.app;19 queryRunner = deps.queryRunner;20 gqlGenerator = app.get<GeneratorGraphqlService>(GeneratorGraphqlService);21 trainingSetService = app.get<TrainingSetService>(TrainingSetService);22 });23 it('should contain valid structure', async () => {24 expect(gqlGenerator).toBeDefined();25 expect(queryRunner).toBeDefined();26 expect(graphQLInteraction).toBeDefined();27 expect(graphQLInteraction).toHaveProperty('allTrainingSets');28 expect(graphQLInteraction).toHaveProperty('trainingSet');29 expect(graphQLInteraction).toHaveProperty('createTrainingSet');30 expect(graphQLInteraction).toHaveProperty('updateTrainingSet');31 expect(graphQLInteraction).toHaveProperty('removeTrainingSet');32 });33 it('should fetch training sets as admin', async () => {34 const result = await makeGraphQLRequest(35 app,36 graphQLInteraction.allTrainingSets({37 pagination: {38 offset: 0,39 limit: 10,40 },41 }),42 { userRole: Role.ADMIN },43 );44 expect(result.body.errors).toBeUndefined();45 testList(result.body.data.allTrainingSets, testTrainingSet);46 });47 it('should fetch training sets as user', async () => {48 const auth = await loginTestUser(app, Role.USER);49 const result = await makeGraphQLRequest(50 app,51 graphQLInteraction.allTrainingSets(),52 { token: auth.token },53 );54 expect(result.body.errors).toBeUndefined();55 testList(result.body.data.allTrainingSets, testTrainingSet);56 result.body.data.allTrainingSets.entries.forEach((ts) =>57 expect(ts.owner.id).toEqual(auth.user.id),58 );59 });60 it('should fetch training set as admin', async () => {61 const [[ts]] = await trainingSetService.findAndCount({62 pagination: { limit: 1 },63 });64 const result = await makeGraphQLRequest(65 app,66 graphQLInteraction.trainingSet(ts.id),67 { userRole: Role.ADMIN },68 );69 expect(result.body.errors).toBeUndefined();70 await testTrainingSet(result.body.data.trainingSet);71 expect(result.body.data.trainingSet.id).toEqual(ts.id);72 });73 it('should fetch training set as owner', async () => {74 const auth = await loginTestUser(app, Role.USER);75 const [[ts]] = await trainingSetService.findAndCount({76 pagination: { limit: 1 },77 idUser: auth.user.id,78 });79 const result = await makeGraphQLRequest(80 app,81 graphQLInteraction.trainingSet(ts.id),82 { token: auth.token },83 );84 expect(result.body.errors).toBeUndefined();85 await testTrainingSet(result.body.data.trainingSet);86 expect(result.body.data.trainingSet.id).toEqual(ts.id);87 });88 it('should fail fetching non-existing training set', async () => {89 const auth = await loginTestUser(app, Role.USER);90 const result = await makeGraphQLRequest(91 app,92 graphQLInteraction.trainingSet('some-id'),93 { token: auth.token },94 );95 expect(result.body.errors).toEqual(expect.any(Array));96 });97 it('should fail fetching training set as non-owner', async () => {98 const demo1 = await loginTestUser(app, Role.USER);99 const demo2 = await loginTestUser(app, Role.USER, 2);100 const [[ts]] = await trainingSetService.findAndCount({101 pagination: { limit: 1 },102 idUser: demo2.user.id,103 });104 const result = await makeGraphQLRequest(105 app,106 graphQLInteraction.trainingSet(ts.id),107 { token: demo1.token },108 );109 expect(result.body.errors).toEqual(expect.any(Array));110 });111 it('should create training set', async () => {112 const input = gqlGenerator.trainingSetInput();113 const result = await makeGraphQLRequest(114 app,115 graphQLInteraction.createTrainingSet(input),116 { userRole: Role.USER },117 );118 expect(result.body.errors).toBeUndefined();119 expect(result.body.data.createTrainingSet.label).toEqual(input.label);120 await testTrainingSet(result.body.data.createTrainingSet);121 });122 it('should update training set as admin', async () => {123 const input = gqlGenerator.trainingSetInput();124 const create = await makeGraphQLRequest(125 app,126 graphQLInteraction.createTrainingSet(input),127 { userRole: Role.USER },128 );129 const result = await makeGraphQLRequest(130 app,131 graphQLInteraction.updateTrainingSet(132 create.body.data.createTrainingSet.id,133 input,134 ),135 { userRole: Role.ADMIN },136 );137 expect(result.body.errors).toBeUndefined();138 expect(result.body.data.updateTrainingSet.label).toEqual(input.label);139 await testTrainingSet(result.body.data.updateTrainingSet);140 });141 it('should update training set as owner', async () => {142 const input = gqlGenerator.trainingSetInput();143 const create = await makeGraphQLRequest(144 app,145 graphQLInteraction.createTrainingSet(input),146 { userRole: Role.USER },147 );148 const result = await makeGraphQLRequest(149 app,150 graphQLInteraction.updateTrainingSet(151 create.body.data.createTrainingSet.id,152 input,153 ),154 { userRole: Role.USER },155 );156 expect(result.body.errors).toBeUndefined();157 expect(result.body.data.updateTrainingSet.label).toEqual(input.label);158 await testTrainingSet(result.body.data.updateTrainingSet);159 });160 it('should fail updating training set as non-owner', async () => {161 const input = gqlGenerator.trainingSetInput();162 const create = await makeGraphQLRequest(163 app,164 graphQLInteraction.createTrainingSet(input),165 { userRole: Role.USER },166 );167 const result = await makeGraphQLRequest(168 app,169 graphQLInteraction.updateTrainingSet(170 create.body.data.createTrainingSet.id,171 input,172 ),173 { userRole: Role.USER, demoAccount: 2 },174 );175 expect(result.body.errors).toEqual(expect.any(Array));176 });177 it('should fail updating non-existing training set', async () => {178 const input = gqlGenerator.trainingSetInput();179 const result = await makeGraphQLRequest(180 app,181 graphQLInteraction.updateTrainingSet('non-existing-id', input),182 { userRole: Role.USER },183 );184 expect(result.body.errors).toEqual(expect.any(Array));185 });186 it('should remove training set as admin', async () => {187 const [trainingSet] = (188 await makeGraphQLRequest(189 app,190 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),191 { userRole: Role.ADMIN },192 )193 ).body.data.allTrainingSets.entries;194 const result = await makeGraphQLRequest(195 app,196 graphQLInteraction.removeTrainingSet(trainingSet.id),197 { userRole: Role.ADMIN },198 );199 expect(result.body.errors).toBeUndefined();200 await testTrainingSet(result.body.data.removeTrainingSet);201 const ts = await makeGraphQLRequest(202 app,203 graphQLInteraction.trainingSet(trainingSet.id),204 { userRole: Role.ADMIN },205 );206 expect(ts.body.errors).toEqual(expect.any(Array));207 });208 it('should fail removing non-existing training set as admin', async () => {209 const result = await makeGraphQLRequest(210 app,211 graphQLInteraction.removeTrainingSet('non-existing-id'),212 { userRole: Role.ADMIN },213 );214 expect(result.body.errors).toEqual(expect.any(Array));215 });216 it('should remove training set as owner', async () => {217 const [trainingSet] = (218 await makeGraphQLRequest(219 app,220 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),221 { userRole: Role.USER },222 )223 ).body.data.allTrainingSets.entries;224 const result = await makeGraphQLRequest(225 app,226 graphQLInteraction.removeTrainingSet(trainingSet.id),227 { userRole: Role.USER },228 );229 expect(result.body.errors).toBeUndefined();230 await testTrainingSet(result.body.data.removeTrainingSet);231 const ts = await makeGraphQLRequest(232 app,233 graphQLInteraction.trainingSet(trainingSet.id),234 { userRole: Role.USER },235 );236 expect(ts.body.errors).toEqual(expect.any(Array));237 });238 it('should fail removing training set as non-owner', async () => {239 const [trainingSet] = (240 await makeGraphQLRequest(241 app,242 graphQLInteraction.allTrainingSets({ pagination: { limit: 1 } }),243 { userRole: Role.USER, demoAccount: 3 },244 )245 ).body.data.allTrainingSets.entries;246 const result = await makeGraphQLRequest(247 app,248 graphQLInteraction.removeTrainingSet(trainingSet.id),249 { userRole: Role.USER },250 );251 expect(result.body.errors).toEqual(expect.any(Array));252 });253 afterEach(() => destroyApp({ app, queryRunner }));...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { GraphQLInteraction } = require("@pact-foundation/pact");2const { gql } = require("apollo-server");3const interaction = new GraphQLInteraction()4 .given("I have a list of users")5 .uponReceiving("a request for all users")6 .withRequest({7 query {8 users {9 }10 }11 })12 .willRespondWith({13 data: {14 {15 },16 {17 },18 },19 });20console.log(interaction);21module.exports = interaction;22const { GraphQLInteraction } = require("@pact-foundation/pact");23const { gql } = require("apollo-server");24const { graphql } = require("graphql");25const interaction = new GraphQLInteraction()26 .given("I have a list of users")27 .uponReceiving("a request for all users")28 .withRequest({29 query {30 users {31 }32 }33 })34 .willRespondWith({35 data: {36 {37 },38 {39 },40 },41 });42console.log(interaction);43module.exports = interaction;44const { Matchers } = require("@pact-foundation/pact");45const { graphql } = require("graphql");46const { makeExecutableSchema } = require("graphql-tools");47const interaction = require("./test");48const { query } = interaction.request;49const { data, errors } = interaction.response;50const schema = makeExecutableSchema({51 type User {52 }53 type Query {54 }55 resolvers: {56 Query: {57 users: () => [58 {59 },60 {61 },

Full Screen

Using AI Code Generation

copy

Full Screen

1const { GraphQLInteraction } = require('@pact-foundation/pact-node');2const interaction = new GraphQLInteraction()3 .given('a user with id 1 exists')4 .uponReceiving('a query for user with id 1')5 .withRequest({6 query: '{ user(id: 1) { name } }',7 })8 .willRespondWith({9 headers: { 'Content-Type': 'application/json' },10 body: {11 data: {12 user: {13 },14 },15 },16 });17module.exports = interaction;18const { Matchers } = require('@pact-foundation/pact');19const { GraphQLInteraction } = require('@pact-foundation/pact-node');20const { like } = Matchers;21const interaction = require('./test2.js');22const provider = require('./provider.js');23describe('Pact with GraphQL', () => {24 describe('test2', () => {25 it('returns a user', () => {26 return provider.addInteraction(interaction).then(() => {27 });28 });29 });30});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pact-foundation-pact automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful