diff --git a/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedGetObject/AwsS3PresignedGetObject.js b/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedGetObject/AwsS3PresignedGetObject.js index cf0cbd194..182ba8ca9 100644 --- a/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedGetObject/AwsS3PresignedGetObject.js +++ b/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedGetObject/AwsS3PresignedGetObject.js @@ -15,19 +15,13 @@ */ import AWS from 'aws-sdk'; -import { get } from '@lowdefy/helpers'; import schema from './AwsS3PresignedGetObjectSchema.json'; - -function validateConnection({ connection, context }) { - if (!get(connection, 'read', { default: true })) { - throw new context.ConfigurationError('AWS S3 Bucket does not allow reads'); - } -} +import checkConnectionRead from '../../../utils/checkConnectionRead'; function awsS3PresignedGetObject({ request, connection, context }) { try { - validateConnection({ connection, context }); + checkConnectionRead({ connection, context, connectionType: 'AwsS3Bucket' }); const { accessKeyId, secretAccessKey, region, bucket } = connection; const { expires, key, versionId, responseContentDisposition, responseContentType } = request; const params = { diff --git a/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedGetObject/AwsS3PresignedGetObject.test.js b/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedGetObject/AwsS3PresignedGetObject.test.js index e500ff2eb..6f4db611f 100644 --- a/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedGetObject/AwsS3PresignedGetObject.test.js +++ b/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedGetObject/AwsS3PresignedGetObject.test.js @@ -125,7 +125,7 @@ test('bucket with read false', async () => { }; await expect(() => resolver({ request, connection, context })).toThrow(ConfigurationError); await expect(() => resolver({ request, connection, context })).toThrow( - 'AWS S3 Bucket does not allow reads' + 'AwsS3Bucket connection does not allow reads.' ); }); diff --git a/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedPostPolicy/AwsS3PresignedPostPolicy.js b/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedPostPolicy/AwsS3PresignedPostPolicy.js index 1c0017106..eab7aa1d5 100644 --- a/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedPostPolicy/AwsS3PresignedPostPolicy.js +++ b/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedPostPolicy/AwsS3PresignedPostPolicy.js @@ -15,18 +15,12 @@ */ import AWS from 'aws-sdk'; -import { get } from '@lowdefy/helpers'; import schema from './AwsS3PresignedPostPolicySchema.json'; - -function validateConnection({ connection, context }) { - if (!get(connection, 'write', { default: false })) { - throw new context.ConfigurationError('AWS S3 Bucket does not allow writes.'); - } -} +import checkConnectionWrite from '../../../utils/checkConnectionWrite'; function awsS3PresignedPostPolicy({ request, connection, context }) { - validateConnection({ connection, context }); + checkConnectionWrite({ connection, context, connectionType: 'AwsS3Bucket' }); try { const { accessKeyId, secretAccessKey, region, bucket } = connection; const { acl, conditions, expires, key } = request; diff --git a/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedPostPolicy/AwsS3PresignedPostPolicy.test.js b/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedPostPolicy/AwsS3PresignedPostPolicy.test.js index b8ca00e4e..b357467ee 100644 --- a/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedPostPolicy/AwsS3PresignedPostPolicy.test.js +++ b/packages/graphql/src/connections/AwsS3Bucket/AwsS3PresignedPostPolicy/AwsS3PresignedPostPolicy.test.js @@ -126,7 +126,7 @@ test('bucket with write false', async () => { }; await expect(() => resolver({ request, connection, context })).toThrow(ConfigurationError); await expect(() => resolver({ request, connection, context })).toThrow( - 'AWS S3 Bucket does not allow writes.' + 'AwsS3Bucket connection does not allow writes.' ); }); @@ -140,7 +140,7 @@ test('bucket with no write specified', async () => { }; await expect(() => resolver({ request, connection, context })).toThrow(ConfigurationError); await expect(() => resolver({ request, connection, context })).toThrow( - 'AWS S3 Bucket does not allow writes.' + 'AwsS3Bucket connection does not allow writes.' ); }); diff --git a/packages/graphql/src/connections/MongoDBCollection/MongoDBAggregation/MongoDBAggregation.js b/packages/graphql/src/connections/MongoDBCollection/MongoDBAggregation/MongoDBAggregation.js index 07c3c48c5..e801c7347 100644 --- a/packages/graphql/src/connections/MongoDBCollection/MongoDBAggregation/MongoDBAggregation.js +++ b/packages/graphql/src/connections/MongoDBCollection/MongoDBAggregation/MongoDBAggregation.js @@ -16,12 +16,12 @@ import getCollection from '../getCollection'; import { serialize, deserialize } from '../serialize'; -import checkRead from '../checkRead'; +import checkConnectionRead from '../../../utils/checkConnectionRead'; import schema from './MongoDBAggregationSchema.json'; async function mongodbAggregation({ request, connection, context }) { - checkRead({ connection, context }); + checkConnectionRead({ connection, context, connectionType: 'MongoDBCollection' }); const deserializedRequest = deserialize(request); const { pipeline, options } = deserializedRequest; const { collection, client } = await getCollection({ connection, context }); diff --git a/packages/graphql/src/connections/MongoDBCollection/MongoDBDeleteMany/MongoDBDeleteMany.js b/packages/graphql/src/connections/MongoDBCollection/MongoDBDeleteMany/MongoDBDeleteMany.js index 6189cadbb..672d2cfa6 100644 --- a/packages/graphql/src/connections/MongoDBCollection/MongoDBDeleteMany/MongoDBDeleteMany.js +++ b/packages/graphql/src/connections/MongoDBCollection/MongoDBDeleteMany/MongoDBDeleteMany.js @@ -16,12 +16,12 @@ import getCollection from '../getCollection'; import { serialize, deserialize } from '../serialize'; -import checkWrite from '../checkWrite'; +import checkConnectionWrite from '../../../utils/checkConnectionWrite'; import schema from './MongoDBDeleteManySchema.json'; async function mongodbDeleteMany({ request, connection, context }) { - checkWrite({ connection, context }); + checkConnectionWrite({ connection, context, connectionType: 'MongoDBCollection' }); const deserializedRequest = deserialize(request); const { filter, options } = deserializedRequest; const { collection, client } = await getCollection({ connection, context }); diff --git a/packages/graphql/src/connections/MongoDBCollection/MongoDBDeleteOne/MongoDBDeleteOne.js b/packages/graphql/src/connections/MongoDBCollection/MongoDBDeleteOne/MongoDBDeleteOne.js index eabdc33c1..cc2f876da 100644 --- a/packages/graphql/src/connections/MongoDBCollection/MongoDBDeleteOne/MongoDBDeleteOne.js +++ b/packages/graphql/src/connections/MongoDBCollection/MongoDBDeleteOne/MongoDBDeleteOne.js @@ -16,12 +16,12 @@ import getCollection from '../getCollection'; import { serialize, deserialize } from '../serialize'; -import checkWrite from '../checkWrite'; +import checkConnectionWrite from '../../../utils/checkConnectionWrite'; import schema from './MongoDBDeleteOneSchema.json'; async function mongodbDeleteOne({ request, connection, context }) { - checkWrite({ connection, context }); + checkConnectionWrite({ connection, context, connectionType: 'MongoDBCollection' }); const deserializedRequest = deserialize(request); const { filter, options } = deserializedRequest; const { collection, client } = await getCollection({ connection, context }); diff --git a/packages/graphql/src/connections/MongoDBCollection/MongoDBFind/MongoDBFind.js b/packages/graphql/src/connections/MongoDBCollection/MongoDBFind/MongoDBFind.js index d3349324f..1b545e71a 100644 --- a/packages/graphql/src/connections/MongoDBCollection/MongoDBFind/MongoDBFind.js +++ b/packages/graphql/src/connections/MongoDBCollection/MongoDBFind/MongoDBFind.js @@ -16,12 +16,12 @@ import getCollection from '../getCollection'; import { serialize, deserialize } from '../serialize'; -import checkRead from '../checkRead'; +import checkConnectionRead from '../../../utils/checkConnectionRead'; import schema from './MongoDBFindSchema.json'; async function mongodbFind({ request, connection, context }) { - checkRead({ connection, context }); + checkConnectionRead({ connection, context, connectionType: 'MongoDBCollection' }); const deserializedRequest = deserialize(request); const { query, options } = deserializedRequest; const { collection, client } = await getCollection({ connection, context }); diff --git a/packages/graphql/src/connections/MongoDBCollection/MongoDBFindOne/MongoDBFindOne.js b/packages/graphql/src/connections/MongoDBCollection/MongoDBFindOne/MongoDBFindOne.js index 3b896d40a..88811d4c0 100644 --- a/packages/graphql/src/connections/MongoDBCollection/MongoDBFindOne/MongoDBFindOne.js +++ b/packages/graphql/src/connections/MongoDBCollection/MongoDBFindOne/MongoDBFindOne.js @@ -16,12 +16,12 @@ import getCollection from '../getCollection'; import { serialize, deserialize } from '../serialize'; -import checkRead from '../checkRead'; +import checkConnectionRead from '../../../utils/checkConnectionRead'; import schema from './MongoDBFindOneSchema.json'; async function mongodbFindOne({ request, connection, context }) { - checkRead({ connection, context }); + checkConnectionRead({ connection, context, connectionType: 'MongoDBCollection' }); const deserializedRequest = deserialize(request); const { query, options } = deserializedRequest; const { collection, client } = await getCollection({ connection, context }); diff --git a/packages/graphql/src/connections/MongoDBCollection/MongoDBInsertMany/MongoDBInsertMany.js b/packages/graphql/src/connections/MongoDBCollection/MongoDBInsertMany/MongoDBInsertMany.js index 9fc6b9c18..321f6bef7 100644 --- a/packages/graphql/src/connections/MongoDBCollection/MongoDBInsertMany/MongoDBInsertMany.js +++ b/packages/graphql/src/connections/MongoDBCollection/MongoDBInsertMany/MongoDBInsertMany.js @@ -16,12 +16,12 @@ import getCollection from '../getCollection'; import { serialize, deserialize } from '../serialize'; -import checkWrite from '../checkWrite'; +import checkConnectionWrite from '../../../utils/checkConnectionWrite'; import schema from './MongoDBInsertManySchema.json'; async function mongodbInsertMany({ request, connection, context }) { - checkWrite({ connection, context }); + checkConnectionWrite({ connection, context, connectionType: 'MongoDBCollection' }); const deserializedRequest = deserialize(request); const { docs, options } = deserializedRequest; const { collection, client } = await getCollection({ connection, context }); diff --git a/packages/graphql/src/connections/MongoDBCollection/MongoDBInsertOne/MongoDBInsertOne.js b/packages/graphql/src/connections/MongoDBCollection/MongoDBInsertOne/MongoDBInsertOne.js index 3c739af75..6804adcad 100644 --- a/packages/graphql/src/connections/MongoDBCollection/MongoDBInsertOne/MongoDBInsertOne.js +++ b/packages/graphql/src/connections/MongoDBCollection/MongoDBInsertOne/MongoDBInsertOne.js @@ -16,12 +16,12 @@ import getCollection from '../getCollection'; import { serialize, deserialize } from '../serialize'; -import checkWrite from '../checkWrite'; +import checkConnectionWrite from '../../../utils/checkConnectionWrite'; import schema from './MongoDBInsertOneSchema.json'; async function mongodbInsertOne({ request, connection, context }) { - checkWrite({ connection, context }); + checkConnectionWrite({ connection, context, connectionType: 'MongoDBCollection' }); const deserializedRequest = deserialize(request); const { doc, options } = deserializedRequest; const { collection, client } = await getCollection({ connection, context }); diff --git a/packages/graphql/src/connections/MongoDBCollection/MongoDBUpdateMany/MongoDBUpdateMany.js b/packages/graphql/src/connections/MongoDBCollection/MongoDBUpdateMany/MongoDBUpdateMany.js index c83ce6d66..022c642df 100644 --- a/packages/graphql/src/connections/MongoDBCollection/MongoDBUpdateMany/MongoDBUpdateMany.js +++ b/packages/graphql/src/connections/MongoDBCollection/MongoDBUpdateMany/MongoDBUpdateMany.js @@ -16,12 +16,12 @@ import getCollection from '../getCollection'; import { serialize, deserialize } from '../serialize'; -import checkWrite from '../checkWrite'; +import checkConnectionWrite from '../../../utils/checkConnectionWrite'; import schema from './MongoDBUpdateManySchema.json'; async function mongodbUpdateMany({ request, connection, context }) { - checkWrite({ connection, context }); + checkConnectionWrite({ connection, context, connectionType: 'MongoDBCollection' }); const deserializedRequest = deserialize(request); const { filter, update, options } = deserializedRequest; const { collection, client } = await getCollection({ connection, context }); diff --git a/packages/graphql/src/connections/MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.js b/packages/graphql/src/connections/MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.js index 2fa28e1f0..5f5dd77e5 100644 --- a/packages/graphql/src/connections/MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.js +++ b/packages/graphql/src/connections/MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.js @@ -16,11 +16,12 @@ import getCollection from '../getCollection'; import { serialize, deserialize } from '../serialize'; -import checkWrite from '../checkWrite'; + +import checkConnectionWrite from '../../../utils/checkConnectionWrite'; import schema from './MongoDBUpdateOneSchema.json'; async function mongodbUpdateOne({ request, connection, context }) { - checkWrite({ connection, context }); + checkConnectionWrite({ connection, context, connectionType: 'MongoDBCollection' }); const deserializedRequest = deserialize(request); const { filter, update, options } = deserializedRequest; const { collection, client } = await getCollection({ connection, context }); diff --git a/packages/graphql/src/connections/MongoDBCollection/checkWrite.test.js b/packages/graphql/src/connections/MongoDBCollection/checkWrite.test.js deleted file mode 100644 index 1e193189c..000000000 --- a/packages/graphql/src/connections/MongoDBCollection/checkWrite.test.js +++ /dev/null @@ -1,40 +0,0 @@ -/* - Copyright 2020 Lowdefy, Inc - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -import { ConfigurationError } from '../../context/errors'; -import checkWrite from './checkWrite'; - -const context = { - ConfigurationError, -}; - -test('Write explicitly true', () => { - expect(checkWrite({ connection: { write: true }, context })).toBe(undefined); -}); - -test('Write not set', () => { - expect(() => checkWrite({ connection: {}, context })).toThrow(ConfigurationError); - expect(() => checkWrite({ connection: {}, context })).toThrow( - 'MongoDBCollection connection does not allow writes.' - ); -}); - -test('Write false', () => { - expect(() => checkWrite({ connection: { write: false }, context })).toThrow(ConfigurationError); - expect(() => checkWrite({ connection: { write: false }, context })).toThrow( - 'MongoDBCollection connection does not allow writes.' - ); -}); diff --git a/packages/graphql/src/connections/MongoDBCollection/checkRead.js b/packages/graphql/src/utils/checkConnectionRead.js similarity index 75% rename from packages/graphql/src/connections/MongoDBCollection/checkRead.js rename to packages/graphql/src/utils/checkConnectionRead.js index 5663004f6..4bd362a18 100644 --- a/packages/graphql/src/connections/MongoDBCollection/checkRead.js +++ b/packages/graphql/src/utils/checkConnectionRead.js @@ -14,10 +14,10 @@ limitations under the License. */ -function checkRead({ connection, context }) { +function checkConnectionRead({ connection, context, connectionType }) { if (connection.read === false) { - throw new context.ConfigurationError('MongoDBCollection connection does not allow reads.'); + throw new context.ConfigurationError(`${connectionType} connection does not allow reads.`); } } -export default checkRead; +export default checkConnectionRead; diff --git a/packages/graphql/src/connections/MongoDBCollection/checkRead.test.js b/packages/graphql/src/utils/checkConnectionRead.test.js similarity index 53% rename from packages/graphql/src/connections/MongoDBCollection/checkRead.test.js rename to packages/graphql/src/utils/checkConnectionRead.test.js index 2d5f88ba8..f5507199b 100644 --- a/packages/graphql/src/connections/MongoDBCollection/checkRead.test.js +++ b/packages/graphql/src/utils/checkConnectionRead.test.js @@ -14,24 +14,30 @@ limitations under the License. */ -import { ConfigurationError } from '../../context/errors'; -import checkRead from './checkRead'; +import { ConfigurationError } from '../context/errors'; +import checkConnectionRead from './checkConnectionRead'; const context = { ConfigurationError, }; +const connectionType = 'TestConnection'; + test('Read explicitly true', () => { - expect(checkRead({ connection: { read: true }, context })).toBe(undefined); + expect(checkConnectionRead({ connection: { read: true }, context, connectionType })).toBe( + undefined + ); }); test('Read not set', () => { - expect(checkRead({ connection: {}, context })).toBe(undefined); + expect(checkConnectionRead({ connection: {}, context, connectionType })).toBe(undefined); }); test('Read false', () => { - expect(() => checkRead({ connection: { read: false }, context })).toThrow(ConfigurationError); - expect(() => checkRead({ connection: { read: false }, context })).toThrow( - 'MongoDBCollection connection does not allow reads.' - ); + expect(() => + checkConnectionRead({ connection: { read: false }, context, connectionType }) + ).toThrow(ConfigurationError); + expect(() => + checkConnectionRead({ connection: { read: false }, context, connectionType }) + ).toThrow('TestConnection connection does not allow reads.'); }); diff --git a/packages/graphql/src/connections/MongoDBCollection/checkWrite.js b/packages/graphql/src/utils/checkConnectionWrite.js similarity index 75% rename from packages/graphql/src/connections/MongoDBCollection/checkWrite.js rename to packages/graphql/src/utils/checkConnectionWrite.js index 0e2ba18d7..5df5a23ef 100644 --- a/packages/graphql/src/connections/MongoDBCollection/checkWrite.js +++ b/packages/graphql/src/utils/checkConnectionWrite.js @@ -14,10 +14,10 @@ limitations under the License. */ -function checkWrite({ connection, context }) { +function checkConnectionWrite({ connection, context, connectionType }) { if (connection.write !== true) { - throw new context.ConfigurationError('MongoDBCollection connection does not allow writes.'); + throw new context.ConfigurationError(`${connectionType} connection does not allow writes.`); } } -export default checkWrite; +export default checkConnectionWrite; diff --git a/packages/graphql/src/utils/checkConnectionWrite.test.js b/packages/graphql/src/utils/checkConnectionWrite.test.js new file mode 100644 index 000000000..d87907a70 --- /dev/null +++ b/packages/graphql/src/utils/checkConnectionWrite.test.js @@ -0,0 +1,48 @@ +/* + Copyright 2020 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import { ConfigurationError } from '../context/errors'; +import checkConnectionWrite from './checkConnectionWrite'; + +const context = { + ConfigurationError, +}; + +const connectionType = 'TestConnection'; + +test('Write explicitly true', () => { + expect(checkConnectionWrite({ connection: { write: true }, context, connectionType })).toBe( + undefined + ); +}); + +test('Write not set', () => { + expect(() => checkConnectionWrite({ connection: {}, context, connectionType })).toThrow( + ConfigurationError + ); + expect(() => checkConnectionWrite({ connection: {}, context, connectionType })).toThrow( + 'TestConnection connection does not allow writes.' + ); +}); + +test('Write false', () => { + expect(() => + checkConnectionWrite({ connection: { write: false }, context, connectionType }) + ).toThrow(ConfigurationError); + expect(() => + checkConnectionWrite({ connection: { write: false }, context, connectionType }) + ).toThrow('TestConnection connection does not allow writes.'); +});