Skip to content

Commit

Permalink
Check file type and file extension when importing csv (#7185)
Browse files Browse the repository at this point in the history
issue #7144
- added a check for file type and file extension
- added an error message to the localization file
- added integration test
  • Loading branch information
twalling authored and ErisDS committed Aug 11, 2016
1 parent cd4fb88 commit a028830
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
5 changes: 4 additions & 1 deletion core/server/api/subscribers.js
Expand Up @@ -275,7 +275,10 @@ subscribers = {
return Promise.reject(new errors.ValidationError(i18n.t('errors.api.db.selectFileToImport')));
}

// TODO: check for valid entries
// Check for valid file type
if (!utils.checkFileIsValid(options, ['text/csv','application/csv'], ['.csv'])) {
return Promise.reject(new errors.ValidationError(i18n.t('errors.api.subscribers.selectValidFile')));
}

return options;
}
Expand Down
3 changes: 2 additions & 1 deletion core/server/translations/en.json
Expand Up @@ -347,7 +347,8 @@
},
"subscribers": {
"subscriberNotFound": "Subscriber not found.",
"subscriberAlreadyExists": "Email address is already subscribed."
"subscriberAlreadyExists": "Email address is already subscribed.",
"selectValidFile": "Please select a valid CSV file to import."
},
"tags": {
"tagNotFound": "Tag not found."
Expand Down
21 changes: 19 additions & 2 deletions core/test/integration/api/api_subscription_spec.js
Expand Up @@ -238,13 +238,15 @@ describe('Subscribers API', function () {
});

describe('Read CSV', function () {
var scope = {};
var scope = {},
stub;

beforeEach(function () {
sinon.stub(fs, 'unlink', function (path, cb) {
cb();
});
sinon.stub(apiUtils, 'checkFileExists').returns(true);
stub = sinon.stub(apiUtils, 'checkFileIsValid').returns(true);
sinon.stub(serverUtils, 'readCSV', function () {
if (scope.csvError) {
return Promise.reject(new Error('csv'));
Expand All @@ -257,7 +259,9 @@ describe('Subscribers API', function () {
afterEach(function () {
fs.unlink.restore();
apiUtils.checkFileExists.restore();
apiUtils.checkFileIsValid.restore();
serverUtils.readCSV.restore();
scope.csvError = false;
});

it('check that fn works in general', function (done) {
Expand Down Expand Up @@ -286,7 +290,7 @@ describe('Subscribers API', function () {
.catch(done);
});

it('read csv throws an error', function (done) {
it('read csv throws a not found error', function (done) {
scope.csvError = true;

SubscribersAPI.importCSV(_.merge(testUtils.context.internal, {path: '/somewhere'}))
Expand All @@ -298,5 +302,18 @@ describe('Subscribers API', function () {
done();
});
});

it('throws an invalid file error', function (done) {
stub.returns(false);

SubscribersAPI.importCSV(_.merge(testUtils.context.internal, {path: '/somewhere'}))
.then(function () {
done(new Error('we expected an error here!'));
})
.catch(function (err) {
err.message.should.eql('Please select a valid CSV file to import.');
done();
});
});
});
});

0 comments on commit a028830

Please sign in to comment.