feat(graphql): add check connection read and write utils

This commit is contained in:
Sam Tolmay 2020-11-18 14:42:00 +02:00
parent b2b7aede7f
commit 5452d86832
18 changed files with 94 additions and 91 deletions

View File

@ -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 = {

View File

@ -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.'
);
});

View File

@ -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;

View File

@ -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.'
);
});

View File

@ -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 });

View File

@ -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 });

View File

@ -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 });

View File

@ -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 });

View File

@ -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 });

View File

@ -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 });

View File

@ -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 });

View File

@ -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 });

View File

@ -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 });

View File

@ -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.'
);
});

View File

@ -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;

View File

@ -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.');
});

View File

@ -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;

View File

@ -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.');
});