trim extensions from filename when uploading a policy file (#4693)

This commit is contained in:
madalynrose 2018-06-05 11:30:32 -04:00 committed by Matthew Irish
parent d499eeac51
commit 08c7cddef9
3 changed files with 36 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import Ember from 'ember';
import trimRight from 'vault/utils/trim-right';
import PolicyEditController from 'vault/mixins/policy-edit-controller';
export default Ember.Controller.extend(PolicyEditController, {
@ -11,7 +12,8 @@ export default Ember.Controller.extend(PolicyEditController, {
let model = this.get('model');
model.set('policy', value);
if (!model.get('name')) {
model.set('name', fileName);
let trimmedFileName = trimRight(fileName, ['.json','.txt', '.hcl', '.policy']);
model.set('name', trimmedFileName);
}
this.set('showFileUpload', false);
},

View File

@ -0,0 +1,4 @@
export default function(fileName, toTrimArray=[]){
const extension = new RegExp(toTrimArray.join('$|'));
return fileName.replace(extension, "");
}

View File

@ -0,0 +1,29 @@
import trimRight from 'vault/utils/trim-right';
import { module, test } from 'qunit';
module('Unit | Util | trim right');
test('it trims extension array from end of string', function(assert) {
const trimmedName = trimRight("my-file-name-is-cool.json", ['.json','.txt', '.hcl', '.policy']);
assert.equal(trimmedName, 'my-file-name-is-cool');
});
test('it only trims extension array from the very end of string', function(assert) {
const trimmedName = trimRight("my-file-name.json-is-cool.json", ['.json','.txt', '.hcl', '.policy']);
assert.equal(trimmedName, 'my-file-name.json-is-cool');
});
test('it returns string as is if trim array is empty', function(assert) {
const trimmedName = trimRight("my-file-name-is-cool.json", []);
assert.equal(trimmedName, 'my-file-name-is-cool.json');
});
test('it returns string as is if trim array is not passed in', function(assert) {
const trimmedName = trimRight("my-file-name-is-cool.json");
assert.equal(trimmedName, 'my-file-name-is-cool.json');
});