From a41d4ee8eb0ae8724363ab143485e40a1f579d9d Mon Sep 17 00:00:00 2001 From: Manuel Ruck Date: Sat, 13 Jul 2024 19:53:57 +0200 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=A7=AA=20add=20some=20more=20test?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Manuel Ruck --- src/express/auth/permissions.ts | 4 +- src/graphql/resolvers/Activity.integ.ts | 49 ++- src/graphql/resolvers/Device.integ.ts | 477 +++++++++++++++------- src/graphql/resolvers/Procedure.integ.ts | 10 +- src/graphql/resolvers/SearchTerm.integ.ts | 126 ++++++ src/graphql/resolvers/Vote.integ.ts | 106 ++++- 6 files changed, 610 insertions(+), 162 deletions(-) create mode 100644 src/graphql/resolvers/SearchTerm.integ.ts diff --git a/src/express/auth/permissions.ts b/src/express/auth/permissions.ts index 7e27636..38679ad 100644 --- a/src/express/auth/permissions.ts +++ b/src/express/auth/permissions.ts @@ -8,7 +8,7 @@ const isLoggedin = rule({ cache: 'no_cache' })(async (parent, args, { user, devi logger.graphql('isLoggedin', { user, device }); if (!user || !device) { logger.warn('Permission denied: You need to login with your Device'); - return false; + return Error('Not Authorised!'); } return true; }); @@ -16,7 +16,7 @@ const isLoggedin = rule({ cache: 'no_cache' })(async (parent, args, { user, devi const isVerified = rule({ cache: 'no_cache' })(async (parent, args, { user, phone }) => { if (!user || (CONFIG.SMS_VERIFICATION && (!user.isVerified() || !phone))) { logger.warn('Permission denied: isVerified = false'); - return false; + return Error('Not Verified!'); } return true; }); diff --git a/src/graphql/resolvers/Activity.integ.ts b/src/graphql/resolvers/Activity.integ.ts index 9295f28..2d9deaa 100644 --- a/src/graphql/resolvers/Activity.integ.ts +++ b/src/graphql/resolvers/Activity.integ.ts @@ -111,13 +111,17 @@ describe('Activity Resolvers', () => { describe('Mutations', () => { describe('increaseActivity', () => { const DEVICE_HASH = 'SOME_DEVICE_HASH_ACTIVITY_RESOLVER_INCREASE_ACTIVITY'; + let device: Device; + const DEVICE_HASH_NOT_VERIFIED = + 'SOME_DEVICE_HASH_ACTIVITY_RESOLVER_INCREASE_ACTIVITY_NOT_VERIFIED'; + let deviceNotVerified: Device; const PHONE_NUMBER = `+49111111111`; const xPhoneHash = crypto.createHash('sha256').update(PHONE_NUMBER).digest('hex'); const phoneHash = crypto.createHash('sha256').update(xPhoneHash).digest('hex'); let procedure: IProcedure; - let device: Device; let phone: Phone; let user: User; + let userNotVerified: User; beforeAll(async () => { await connectDB(config.DB_URL, { debug: false }); @@ -139,10 +143,19 @@ describe('Activity Resolvers', () => { deviceHash: DEVICE_HASH, }); + deviceNotVerified = await DeviceModel.create({ + deviceHash: DEVICE_HASH_NOT_VERIFIED, + }); + phone = await PhoneModel.create({ phoneHash, }); + userNotVerified = await UserModel.create({ + verified: false, + device: deviceNotVerified, + }); + // create tmp user user = await UserModel.create({ verified: true, @@ -158,6 +171,8 @@ describe('Activity Resolvers', () => { phone.remove(), device.remove(), user.remove(), + deviceNotVerified.remove(), + userNotVerified.remove(), ]); await disconnectDB(); @@ -226,6 +241,38 @@ describe('Activity Resolvers', () => { expect(data.increaseActivity.activityIndex).toBeDefined(); expect(data.increaseActivity.active).toBeTruthy(); }); + + it('not verified', async () => { + const response = await axios.post( + GRAPHQL_API_URL, + { + query: ` + mutation IncreaseActivity($procedureId: String!) { + increaseActivity(procedureId: $procedureId) { + activityIndex + active + } + } + `, + variables: { + procedureId: '0000000', + }, + }, + { + headers: { + 'Content-Type': 'application/json', + 'x-device-hash': deviceNotVerified.deviceHash, + }, + }, + ); + + const { data, errors } = response.data; + + expect(data).toBeDefined(); + expect(data.increaseActivity).toBeNull(); + expect(errors).toBeDefined(); + expect(errors[0].message).toBe('Not Verified!'); + }); }); }); }); diff --git a/src/graphql/resolvers/Device.integ.ts b/src/graphql/resolvers/Device.integ.ts index 8b8c0d0..0c1807e 100644 --- a/src/graphql/resolvers/Device.integ.ts +++ b/src/graphql/resolvers/Device.integ.ts @@ -48,14 +48,13 @@ describe('Device GraphQL API', () => { await disconnectDB(); }); - describe('notification settings', () => { - describe('Query', () => { - describe('notificationSettings', () => { - it('get notification settings', async () => { - const response = await axios.post( - GRAPHQL_API_URL, - { - query: ` + describe('Query', () => { + describe('notificationSettings', () => { + it('get notification settings', async () => { + const response = await axios.post( + GRAPHQL_API_URL, + { + query: ` query NotificationSettings { notificationSettings { enabled @@ -66,106 +65,166 @@ describe('Device GraphQL API', () => { } } `, + }, + { + headers: { + 'Content-Type': 'application/json', + 'x-device-hash': SOME_DEVICE_HASH, }, - { - headers: { - 'Content-Type': 'application/json', - 'x-device-hash': SOME_DEVICE_HASH, - }, + }, + ); + + const { data, error } = response.data; + + expect(error).toBeUndefined(); + expect(data).toBeDefined(); + expect(data.notificationSettings).toBeDefined(); + expect(data.notificationSettings.enabled).toBeTruthy(); + expect(data.notificationSettings.conferenceWeekPushs).toBeTruthy(); + expect(data.notificationSettings.voteConferenceWeekPushs).toBeFalsy(); + expect(data.notificationSettings.voteTOP100Pushs).toBeFalsy(); + expect(data.notificationSettings.outcomePushs).toBeFalsy(); + }); + + it('is not allowed to get notification settings without device hash', async () => { + const response = await axios.post( + GRAPHQL_API_URL, + { + query: ` + query NotificationSettings { + notificationSettings { + enabled + conferenceWeekPushs + voteConferenceWeekPushs + voteTOP100Pushs + outcomePushs + } + } + `, + }, + { + headers: { + 'Content-Type': 'application/json', }, - ); - - const { data } = response.data; - - expect(data).toBeDefined(); - expect(data.notificationSettings).toBeDefined(); - expect(data.notificationSettings.enabled).toBeTruthy(); - expect(data.notificationSettings.conferenceWeekPushs).toBeTruthy(); - expect(data.notificationSettings.voteConferenceWeekPushs).toBeFalsy(); - expect(data.notificationSettings.voteTOP100Pushs).toBeFalsy(); - expect(data.notificationSettings.outcomePushs).toBeFalsy(); - }); + }, + ); + + const { data, errors } = response.data; + + expect(data).toBeDefined(); + expect(data.notificationSettings).toBeNull(); + expect(errors).toBeDefined(); + expect(errors[0].message).toBe('Not Authorised!'); }); }); - describe('Mutation', () => { - it('update notification settings', async () => { + }); + describe('Mutation', () => { + describe('requestCode', () => { + it('Not Authorised!', async () => { const response = await axios.post( GRAPHQL_API_URL, { query: ` - mutation UpdateNotificationSettings($enabled: Boolean!, $conferenceWeekPushs: Boolean!, $voteConferenceWeekPushs: Boolean!, $voteTOP100Pushs: Boolean!, $outcomePushs: Boolean!) { - updateNotificationSettings(enabled: $enabled, conferenceWeekPushs: $conferenceWeekPushs, voteConferenceWeekPushs: $voteConferenceWeekPushs, voteTOP100Pushs: $voteTOP100Pushs, outcomePushs: $outcomePushs) { - enabled - conferenceWeekPushs - voteConferenceWeekPushs - voteTOP100Pushs - outcomePushs - } + mutation RequestCode($newPhone: String!, $oldPhoneHash: String) { + requestCode(newPhone: $newPhone, oldPhoneHash: $oldPhoneHash) { + reason + allowNewUser + succeeded + resendTime + expireTime } - `, + } + `, variables: { - enabled: true, - conferenceWeekPushs: true, - voteConferenceWeekPushs: true, - voteTOP100Pushs: true, - outcomePushs: true, + newPhone: PHONE_NUMBER, + oldPhoneHash: '', }, }, { headers: { 'Content-Type': 'application/json', - 'x-device-hash': SOME_DEVICE_HASH, }, }, ); - const { data } = response.data; + const { data, errors } = response.data; expect(data).toBeDefined(); - expect(data.updateNotificationSettings).toBeDefined(); - expect(data.updateNotificationSettings.enabled).toBeTruthy(); - expect(data.updateNotificationSettings.conferenceWeekPushs).toBeTruthy(); - expect(data.updateNotificationSettings.voteConferenceWeekPushs).toBeTruthy(); - expect(data.updateNotificationSettings.voteTOP100Pushs).toBeTruthy(); - expect(data.updateNotificationSettings.outcomePushs).toBeTruthy(); + expect(data).toBeNull(); + expect(errors).toBeDefined(); + expect(errors[0].message).toBe('Not Authorised!'); }); + }); - it('get updated notification settings', async () => { + describe('requestVerification', () => { + it('Not Authorised!', async () => { const response = await axios.post( GRAPHQL_API_URL, { query: ` - query NotificationSettings { - notificationSettings { - enabled - conferenceWeekPushs - voteConferenceWeekPushs - voteTOP100Pushs - outcomePushs - } + mutation RequestVerification($code: String!, $newPhoneHash: String!, $newUser: Boolean) { + requestVerification(code: $code, newPhoneHash: $newPhoneHash, newUser: $newUser) { + reason + succeeded + } + } + `, + variables: { + code: '000000', + newPhoneHash: phoneHash, + }, + }, + { + headers: { + 'Content-Type': 'application/json', + }, + }, + ); + + const { data, errors } = response.data; + + expect(data).toBeDefined(); + expect(data).toBeNull(); + expect(errors).toBeDefined(); + expect(errors[0].message).toBe('Not Authorised!'); + }); + }); + + describe('addToken', () => { + it('Not Authorised!', async () => { + const response = await axios.post( + GRAPHQL_API_URL, + { + query: ` + mutation AddToken($token: String!, $os: String!) { + addToken(token: $token, os: $os) { + succeeded + } } - `, + `, + variables: { + token: '000000', + os: 'ios', + }, }, { headers: { 'Content-Type': 'application/json', - 'x-device-hash': SOME_DEVICE_HASH, }, }, ); - const { data } = response.data; + const { data, errors } = response.data; expect(data).toBeDefined(); - expect(data.notificationSettings).toBeDefined(); - expect(data.notificationSettings.enabled).toBeTruthy(); - expect(data.notificationSettings.conferenceWeekPushs).toBeTruthy(); - expect(data.notificationSettings.voteConferenceWeekPushs).toBeTruthy(); - expect(data.notificationSettings.voteTOP100Pushs).toBeTruthy(); - expect(data.notificationSettings.outcomePushs).toBeTruthy(); + expect(data).toBeNull(); + expect(errors).toBeDefined(); + expect(errors[0].message).toBe('Not Authorised!'); }); + }); - it('is not allowed to update notification settings without device hash', async () => { + describe('updateNotificationSettings', () => { + it('Not Authorised!', async () => { const response = await axios.post( GRAPHQL_API_URL, { @@ -195,59 +254,131 @@ describe('Device GraphQL API', () => { }, ); - const error = response.data.errors.find((e) => - e.path.includes('updateNotificationSettings'), - ); - if (error) { - expect(error.message).toBe('Not Authorised!'); - expect(response.data.notificationSettings).toBeUndefined(); - } else { - expect(true).toBeFalsy(); - } + const { data, errors } = response.data; + + expect(data).toBeDefined(); + expect(data.updateNotificationSettings).toBeNull(); + expect(errors).toBeDefined(); + expect(errors[0].message).toBe('Not Authorised!'); }); }); - describe('verify device', () => { - it('request verification code via sms', async () => { + describe('toggleNotification', () => { + it('Not Authorised!', async () => { const response = await axios.post( GRAPHQL_API_URL, { query: ` - mutation RequestSmsCode($newPhone: String!, $oldPhoneHash: String) { - requestCode(newPhone: $newPhone, oldPhoneHash: $oldPhoneHash) { - reason - allowNewUser - succeeded - resendTime - expireTime + mutation ToggleNotification($procedureId: String!) { + toggleNotification(procedureId: $procedureId) { + title + } } - } - `, + `, variables: { - newPhone: PHONE_NUMBER, - oldPhoneHash: '', + procedureId: '0000000', }, }, { headers: { 'Content-Type': 'application/json', - 'x-device-hash': SOME_DEVICE_HASH, }, }, ); - const { data } = response.data; + const { data, errors } = response.data; expect(data).toBeDefined(); - expect(data.requestCode).toBeDefined(); - expect(data.requestCode.succeeded).toBeTruthy(); + expect(data.toggleNotification).toBeNull(); + expect(errors).toBeDefined(); + expect(errors[0].message).toBe('Not Authorised!'); }); + }); - it('request fast second verification code via sms', async () => { - const response = await axios.post( - GRAPHQL_API_URL, - { - query: ` + it('update notification settings', async () => { + const response = await axios.post( + GRAPHQL_API_URL, + { + query: ` + mutation UpdateNotificationSettings($enabled: Boolean!, $conferenceWeekPushs: Boolean!, $voteConferenceWeekPushs: Boolean!, $voteTOP100Pushs: Boolean!, $outcomePushs: Boolean!) { + updateNotificationSettings(enabled: $enabled, conferenceWeekPushs: $conferenceWeekPushs, voteConferenceWeekPushs: $voteConferenceWeekPushs, voteTOP100Pushs: $voteTOP100Pushs, outcomePushs: $outcomePushs) { + enabled + conferenceWeekPushs + voteConferenceWeekPushs + voteTOP100Pushs + outcomePushs + } + } + `, + variables: { + enabled: true, + conferenceWeekPushs: true, + voteConferenceWeekPushs: true, + voteTOP100Pushs: true, + outcomePushs: true, + }, + }, + { + headers: { + 'Content-Type': 'application/json', + 'x-device-hash': SOME_DEVICE_HASH, + }, + }, + ); + + const { data } = response.data; + + expect(data).toBeDefined(); + expect(data.updateNotificationSettings).toBeDefined(); + expect(data.updateNotificationSettings.enabled).toBeTruthy(); + expect(data.updateNotificationSettings.conferenceWeekPushs).toBeTruthy(); + expect(data.updateNotificationSettings.voteConferenceWeekPushs).toBeTruthy(); + expect(data.updateNotificationSettings.voteTOP100Pushs).toBeTruthy(); + expect(data.updateNotificationSettings.outcomePushs).toBeTruthy(); + }); + + it('get updated notification settings', async () => { + const response = await axios.post( + GRAPHQL_API_URL, + { + query: ` + query NotificationSettings { + notificationSettings { + enabled + conferenceWeekPushs + voteConferenceWeekPushs + voteTOP100Pushs + outcomePushs + } + } + `, + }, + { + headers: { + 'Content-Type': 'application/json', + 'x-device-hash': SOME_DEVICE_HASH, + }, + }, + ); + + const { data } = response.data; + + expect(data).toBeDefined(); + expect(data.notificationSettings).toBeDefined(); + expect(data.notificationSettings.enabled).toBeTruthy(); + expect(data.notificationSettings.conferenceWeekPushs).toBeTruthy(); + expect(data.notificationSettings.voteConferenceWeekPushs).toBeTruthy(); + expect(data.notificationSettings.voteTOP100Pushs).toBeTruthy(); + expect(data.notificationSettings.outcomePushs).toBeTruthy(); + }); + }); + + describe('verify device', () => { + it('request verification code via sms', async () => { + const response = await axios.post( + GRAPHQL_API_URL, + { + query: ` mutation RequestSmsCode($newPhone: String!, $oldPhoneHash: String) { requestCode(newPhone: $newPhone, oldPhoneHash: $oldPhoneHash) { reason @@ -258,32 +389,67 @@ describe('Device GraphQL API', () => { } } `, - variables: { - newPhone: PHONE_NUMBER, - oldPhoneHash: '', - }, + variables: { + newPhone: PHONE_NUMBER, + oldPhoneHash: '', }, - { - headers: { - 'Content-Type': 'application/json', - 'x-device-hash': SOME_DEVICE_HASH, - }, + }, + { + headers: { + 'Content-Type': 'application/json', + 'x-device-hash': SOME_DEVICE_HASH, }, - ); + }, + ); - const { data } = response.data; + const { data } = response.data; - expect(data).toBeDefined(); - expect(data.requestCode).toBeDefined(); - expect(data.requestCode.succeeded).toBeFalsy(); - }); + expect(data).toBeDefined(); + expect(data.requestCode).toBeDefined(); + expect(data.requestCode.succeeded).toBeTruthy(); + }); - it('try to verify phone number with wrong code', async () => { - const newPhoneHash = crypto.createHash('sha256').update(PHONE_NUMBER).digest('hex'); - const response = await axios.post( - GRAPHQL_API_URL, - { - query: ` + it('request fast second verification code via sms', async () => { + const response = await axios.post( + GRAPHQL_API_URL, + { + query: ` + mutation RequestSmsCode($newPhone: String!, $oldPhoneHash: String) { + requestCode(newPhone: $newPhone, oldPhoneHash: $oldPhoneHash) { + reason + allowNewUser + succeeded + resendTime + expireTime + } + } + `, + variables: { + newPhone: PHONE_NUMBER, + oldPhoneHash: '', + }, + }, + { + headers: { + 'Content-Type': 'application/json', + 'x-device-hash': SOME_DEVICE_HASH, + }, + }, + ); + + const { data } = response.data; + + expect(data).toBeDefined(); + expect(data.requestCode).toBeDefined(); + expect(data.requestCode.succeeded).toBeFalsy(); + }); + + it('try to verify phone number with wrong code', async () => { + const newPhoneHash = crypto.createHash('sha256').update(PHONE_NUMBER).digest('hex'); + const response = await axios.post( + GRAPHQL_API_URL, + { + query: ` mutation RequestVerification($code: String!, $newPhoneHash: String!, $newUser: Boolean) { requestVerification(code: $code, newPhoneHash: $newPhoneHash, newUser: $newUser) { reason @@ -291,32 +457,32 @@ describe('Device GraphQL API', () => { } } `, - variables: { - code: '123456', - newPhoneHash, - }, + variables: { + code: '123456', + newPhoneHash, }, - { - headers: { - 'Content-Type': 'application/json', - 'x-device-hash': SOME_DEVICE_HASH, - }, + }, + { + headers: { + 'Content-Type': 'application/json', + 'x-device-hash': SOME_DEVICE_HASH, }, - ); + }, + ); - const { data } = response.data; + const { data } = response.data; - expect(data).toBeDefined(); - expect(data.requestVerification).toBeDefined(); - expect(data.requestVerification.succeeded).toBeFalsy(); - }); + expect(data).toBeDefined(); + expect(data.requestVerification).toBeDefined(); + expect(data.requestVerification.succeeded).toBeFalsy(); + }); - it('verify phone number', async () => { - const newPhoneHash = crypto.createHash('sha256').update(PHONE_NUMBER).digest('hex'); - const response = await axios.post( - GRAPHQL_API_URL, - { - query: ` + it('verify phone number', async () => { + const newPhoneHash = crypto.createHash('sha256').update(PHONE_NUMBER).digest('hex'); + const response = await axios.post( + GRAPHQL_API_URL, + { + query: ` mutation RequestVerification($code: String!, $newPhoneHash: String!, $newUser: Boolean) { requestVerification(code: $code, newPhoneHash: $newPhoneHash, newUser: $newUser) { reason @@ -324,25 +490,24 @@ describe('Device GraphQL API', () => { } } `, - variables: { - code: '000000', - newPhoneHash, - }, + variables: { + code: '000000', + newPhoneHash, }, - { - headers: { - 'Content-Type': 'application/json', - 'x-device-hash': SOME_DEVICE_HASH, - }, + }, + { + headers: { + 'Content-Type': 'application/json', + 'x-device-hash': SOME_DEVICE_HASH, }, - ); + }, + ); - const { data } = response.data; + const { data } = response.data; - expect(data).toBeDefined(); - expect(data.requestVerification).toBeDefined(); - expect(data.requestVerification.succeeded).toBeTruthy(); - }); + expect(data).toBeDefined(); + expect(data.requestVerification).toBeDefined(); + expect(data.requestVerification.succeeded).toBeTruthy(); }); }); }); diff --git a/src/graphql/resolvers/Procedure.integ.ts b/src/graphql/resolvers/Procedure.integ.ts index bcbb56c..e35107e 100644 --- a/src/graphql/resolvers/Procedure.integ.ts +++ b/src/graphql/resolvers/Procedure.integ.ts @@ -123,10 +123,13 @@ describe('Resolver: Procedure', () => { }, ); - const { data } = response.data; + const { data, errors } = response.data; expect(data).toBeDefined(); expect(data).toBeNull(); + expect(errors).toBeDefined(); + expect(errors.length).toStrictEqual(1); + expect(errors[0].message).toStrictEqual('Not Verified!'); }); }); describe('notifiedProcedures', () => { @@ -176,10 +179,13 @@ describe('Resolver: Procedure', () => { }, ); - const { data } = response.data; + const { data, errors } = response.data; expect(data).toBeDefined(); expect(data).toBeNull(); + expect(errors).toBeDefined(); + expect(errors.length).toStrictEqual(1); + expect(errors[0].message).toStrictEqual('Not Authorised!'); }); }); }); diff --git a/src/graphql/resolvers/SearchTerm.integ.ts b/src/graphql/resolvers/SearchTerm.integ.ts new file mode 100644 index 0000000..e598533 --- /dev/null +++ b/src/graphql/resolvers/SearchTerm.integ.ts @@ -0,0 +1,126 @@ +import { + IProcedure, + Device, + Phone, + User, + ProcedureModel, + DeviceModel, + PhoneModel, + UserModel, +} from '@democracy-deutschland/democracy-common'; +import axios from 'axios'; +import crypto from 'crypto'; +import { connectDB, disconnectDB } from '../../services/mongoose'; +import config from '../../config'; + +const GRAPHQL_API_URL = process.env.GRAPHQL_API_URL || 'http://localhost:3000'; + +describe('SearchTerm Resolvers', () => { + describe('Mutations', () => { + describe('finishSearch', () => { + let procedure: IProcedure; + const procedureId = '0006010'; + let user: User; + let device: Device; + const DEVICE_HASH = 'SOME_DEVICE_HASH_SEARCH_TERM_RESOLVER_FINISH_SEARCH'; + + const PHONE_NUMBER = `+49112113112`; + const xPhoneHash = crypto.createHash('sha256').update(PHONE_NUMBER).digest('hex'); + const phoneHash = crypto.createHash('sha256').update(xPhoneHash).digest('hex'); + let phone: Phone; + let searchTerm: string; + + beforeAll(async () => { + await connectDB(config.DB_URL, { debug: false }); + + // create tmp procedure + procedure = await ProcedureModel.create({ + procedureId, + title: 'tmp procedure for finishSearch test', + period: 1, + type: 'Antrag', + voteResults: { + yes: 0, + no: 0, + abstination: 0, + }, + }); + + device = await DeviceModel.create({ + deviceHash: DEVICE_HASH, + }); + + phone = await PhoneModel.create({ + phoneHash: phoneHash, + }); + + // create tmp user + user = await UserModel.create({ + verified: true, + device, + phone, + }); + + searchTerm = 'test'; + }); + + afterAll(async () => { + await Promise.all([procedure.remove(), user.remove(), phone.remove(), device.remove()]); + await disconnectDB(); + }); + + it('should finish search', async () => { + const response = await axios.post( + GRAPHQL_API_URL, + { + query: ` + mutation FinishSearch($term: String!) { + finishSearch(term: $term) { + term + } + } + `, + variables: { + term: searchTerm, + }, + }, + { + headers: { + 'x-device-hash': device.deviceHash, + 'x-phone-hash': xPhoneHash, + }, + }, + ); + + const { data, errors } = response.data; + + expect(errors).toBeUndefined(); + expect(data).toBeDefined(); + expect(data.finishSearch).toBeDefined(); + expect(data.finishSearch.term).toEqual(searchTerm); + }); + + it('Not Authorised!', async () => { + const response = await axios.post(GRAPHQL_API_URL, { + query: ` + mutation FinishSearch($term: String!) { + finishSearch(term: $term) { + term + } + } + `, + variables: { + term: searchTerm, + }, + }); + + const { data, errors } = response.data; + + expect(data).toBeDefined(); + expect(data).toBeNull(); + expect(errors).toBeDefined(); + expect(errors[0].message).toEqual('Not Authorised!'); + }); + }); + }); +}); diff --git a/src/graphql/resolvers/Vote.integ.ts b/src/graphql/resolvers/Vote.integ.ts index c47b61f..a62a1aa 100644 --- a/src/graphql/resolvers/Vote.integ.ts +++ b/src/graphql/resolvers/Vote.integ.ts @@ -1,4 +1,5 @@ import { + ActivityModel, Device, DeviceModel, IProcedure, @@ -9,6 +10,7 @@ import { UserModel, Vote, VoteModel, + VoteSelection, } from '@democracy-deutschland/democracy-common'; import config from '../../config'; import crypto from 'crypto'; @@ -22,6 +24,7 @@ describe('Resolver: Vote', () => { let device: Device; const DEVICE_HASH = 'SOME_DEVICE_HASH_VOTE'; let procedure: IProcedure; + let procedureNotVoted: IProcedure; const PHONE_NUMBER = `+49111113112`; const xPhoneHash = crypto.createHash('sha256').update(PHONE_NUMBER).digest('hex'); const phoneHash = crypto.createHash('sha256').update(xPhoneHash).digest('hex'); @@ -44,6 +47,18 @@ describe('Resolver: Vote', () => { }, }); + procedureNotVoted = await ProcedureModel.create({ + procedureId: '0001102', + title: 'tmp procedure for increaseActivity test', + period: 1, + type: 'Antrag', + voteResults: { + yes: 0, + no: 0, + abstination: 0, + }, + }); + device = await DeviceModel.create({ deviceHash: DEVICE_HASH, }); @@ -99,9 +114,12 @@ describe('Resolver: Vote', () => { vote.remove(), device.remove(), procedure.remove(), + procedureNotVoted.remove(), userVerified.remove(), userNotVerified.remove(), phone.remove(), + ActivityModel.deleteOne({ procedure: procedureNotVoted }), + VoteModel.deleteOne({ procedure: procedureNotVoted }), ]); await disconnectDB(); @@ -171,10 +189,12 @@ describe('Resolver: Vote', () => { }, }); - const { data } = response.data; + const { data, errors } = response.data; expect(data).toBeDefined(); expect(data.votes).toBeNull(); + expect(errors).toBeDefined(); + expect(errors[0].message).toEqual('Not Authorised!'); }); }); @@ -232,4 +252,88 @@ describe('Resolver: Vote', () => { }); }); }); + + describe('Mutation', () => { + describe('vote', () => { + it('Not Verified', async () => { + const response = await axios.post( + GRAPHQL_API_URL, + { + query: ` + mutation Vote($procedure: ID!, $selection: VoteSelection!) { + vote(procedure: $procedure, selection: $selection) { + voted + voteResults { + yes + no + abstination + total + } + } + } + `, + variables: { + procedure: procedureNotVoted._id, + selection: VoteSelection.Yes, + }, + }, + { + headers: { + 'x-device-hash': device.deviceHash, + }, + }, + ); + + const { data, errors } = response.data; + + expect(data).toBeDefined(); + expect(data).toBeNull(); + expect(errors).toBeDefined(); + expect(errors[0].message).toEqual('Not Verified!'); + }); + + it('should vote', async () => { + const response = await axios.post( + GRAPHQL_API_URL, + { + query: ` + mutation Vote($procedure: ID!, $selection: VoteSelection!) { + vote(procedure: $procedure, selection: $selection) { + voted + voteResults { + yes + no + abstination + total + } + } + } + `, + variables: { + procedure: procedureNotVoted._id, + selection: VoteSelection.Yes, + }, + }, + { + headers: { + 'x-device-hash': device.deviceHash, + 'x-phone-hash': xPhoneHash, + }, + }, + ); + + const { data, errors } = response.data; + + expect(errors).toBeUndefined(); + expect(data).toBeDefined(); + expect(data.vote).toBeDefined(); + expect(data.vote.voted).toBeTruthy(); + expect(data.vote.voteResults).toBeDefined(); + expect(data.vote.voteResults.yes).toEqual(1); + expect(data.vote.voteResults.no).toEqual(0); + expect(data.vote.voteResults.abstination).toEqual(0); + expect(data.vote.voteResults.total).toEqual(1); + }); + }); + }); });