Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a check for an extra column and tests #18

Merged
merged 1 commit into from
Sep 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,13 @@ validator.validatePutRequest = function(req, schema) {
throw new Error('Invalid query. No schema for ' + req.table);
}

var missingColumn = Object.keys(req.attributes).find(function(attrName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we separate this into its own method (and then call it from this one), so that we can reuse it in the Cassandra module and reduce code duplication?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be dealt with differently in the Cassandra module, so retracting my question.

return !schema.attributes[attrName];
});
if (missingColumn) {
throw new Error('Unknown attribute ' + missingColumn);
}

schema.iKeys.forEach(function(key) {
if (req.attributes[key] === undefined && key !== schema.tid) {
throw new Error("Index attribute " + JSON.stringify(key) + " missing in "
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "restbase-mod-table-spec",
"version": "0.0.6",
"version": "0.0.7",
"description": "Tests and specification for RESTBase backend modules",
"main": "index.js",
"repository": {
Expand Down
36 changes: 36 additions & 0 deletions test/functional/invalid_requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,40 @@ describe('Invalid request handling', function() {
deepEqual(response.status, 500);
});
});

it('fails to insert unknown field', function() {
return router.request({
method: 'put',
uri: '/restbase.cassandra.test.local/sys/table/extraFieldSchema',
body: {
domain: 'restbase.cassandra.test.local',
table: 'extraFieldSchema',
attributes: {
key: 'string',
value: 'string'
},
index: [
{ attribute: 'key', type: 'hash' }
]
}
})
.then(function(res) {
deepEqual(res.status, 201);
return router.request({
uri: '/restbase.cassandra.test.local/sys/table/extraFieldSchema/',
method: 'put',
body: {
table: 'extraFieldSchema',
attributes: {
key: 'key',
value: 'value',
extra_field: 'extra_value'
}
}
});
})
.then(function(res) {
deepEqual(res.status, 500);
});
});
});
15 changes: 15 additions & 0 deletions test/lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,21 @@ describe('Unit tests for validation methods', function() {
}, sampleSchema);
}, /Index attribute/);
});

it('all attributes must exist in schema', function() {
test(function() {
validator.validatePutRequest({
table: 'test',
attributes: {
key: 'key',
tid: 'timeuuid',
latestTid: 'timeuuid',
range: 'string',
extra_column: 'extra'
}
}, sampleSchema);
}, /Unknown attribute extra_column/);
});
});

describe('GET request validation', function() {
Expand Down