From fbb137bdb226fb5c30c9063276f9a663f845f8d5 Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Mon, 27 Jan 2020 17:32:29 -0800 Subject: [PATCH] Refactor ability tests to use a setup hook for ability lookup --- ui/tests/helpers/setup-ability.js | 9 +++++++++ ui/tests/unit/abilities/client-test.js | 13 ++----------- ui/tests/unit/abilities/job-test.js | 27 ++++++++++++-------------- 3 files changed, 23 insertions(+), 26 deletions(-) create mode 100644 ui/tests/helpers/setup-ability.js diff --git a/ui/tests/helpers/setup-ability.js b/ui/tests/helpers/setup-ability.js new file mode 100644 index 000000000..41f56f3a7 --- /dev/null +++ b/ui/tests/helpers/setup-ability.js @@ -0,0 +1,9 @@ +export default ability => hooks => { + hooks.beforeEach(function() { + this.ability = this.owner.lookup(`ability:${ability}`); + }); + + hooks.afterEach(function() { + delete this.ability; + }); +}; diff --git a/ui/tests/unit/abilities/client-test.js b/ui/tests/unit/abilities/client-test.js index 1e488709c..d4e0b2a3f 100644 --- a/ui/tests/unit/abilities/client-test.js +++ b/ui/tests/unit/abilities/client-test.js @@ -1,20 +1,11 @@ import { module, test } from 'qunit'; import { setupTest } from 'ember-qunit'; import Service from '@ember/service'; - -function setupAbility(ability, hooks) { - hooks.beforeEach(function() { - this.ability = this.owner.lookup(`ability:${ability}`); - }); - - hooks.afterEach(function() { - delete this.ability; - }); -} +import setupAbility from 'nomad-ui/tests/helpers/setup-ability'; module('Unit | Ability | client', function(hooks) { setupTest(hooks); - setupAbility('client', hooks); + setupAbility('client')(hooks); test('it permits client write for management tokens', function(assert) { const mockToken = Service.extend({ diff --git a/ui/tests/unit/abilities/job-test.js b/ui/tests/unit/abilities/job-test.js index f0e337192..63c393a06 100644 --- a/ui/tests/unit/abilities/job-test.js +++ b/ui/tests/unit/abilities/job-test.js @@ -1,9 +1,11 @@ import { module, test } from 'qunit'; import { setupTest } from 'ember-qunit'; import Service from '@ember/service'; +import setupAbility from 'nomad-ui/tests/helpers/setup-ability'; module('Unit | Ability | job', function(hooks) { setupTest(hooks); + setupAbility('job')(hooks); test('it permits job run for management tokens', function(assert) { const mockToken = Service.extend({ @@ -12,8 +14,7 @@ module('Unit | Ability | job', function(hooks) { this.owner.register('service:token', mockToken); - const jobAbility = this.owner.lookup('ability:job'); - assert.ok(jobAbility.canRun); + assert.ok(this.ability.canRun); }); test('it permits job run for client tokens with a policy that has namespace submit-job', function(assert) { @@ -42,8 +43,7 @@ module('Unit | Ability | job', function(hooks) { this.owner.register('service:system', mockSystem); this.owner.register('service:token', mockToken); - const jobAbility = this.owner.lookup('ability:job'); - assert.ok(jobAbility.canRun); + assert.ok(this.ability.canRun); }); test('it permits job run for client tokens with a policy that has default namespace submit-job and no capabilities for active namespace', function(assert) { @@ -76,8 +76,7 @@ module('Unit | Ability | job', function(hooks) { this.owner.register('service:system', mockSystem); this.owner.register('service:token', mockToken); - const jobAbility = this.owner.lookup('ability:job'); - assert.ok(jobAbility.canRun); + assert.ok(this.ability.canRun); }); test('it blocks job run for client tokens with a policy that has no submit-job capability', function(assert) { @@ -106,8 +105,7 @@ module('Unit | Ability | job', function(hooks) { this.owner.register('service:system', mockSystem); this.owner.register('service:token', mockToken); - const jobAbility = this.owner.lookup('ability:job'); - assert.notOk(jobAbility.canRun); + assert.notOk(this.ability.canRun); }); test('it handles globs in namespace names', function(assert) { @@ -156,28 +154,27 @@ module('Unit | Ability | job', function(hooks) { this.owner.register('service:system', mockSystem); this.owner.register('service:token', mockToken); - const jobAbility = this.owner.lookup('ability:job'); const systemService = this.owner.lookup('service:system'); systemService.set('activeNamespace.name', 'production-web'); - assert.notOk(jobAbility.canRun); + assert.notOk(this.ability.canRun); systemService.set('activeNamespace.name', 'production-api'); - assert.ok(jobAbility.canRun); + assert.ok(this.ability.canRun); systemService.set('activeNamespace.name', 'production-other'); - assert.ok(jobAbility.canRun); + assert.ok(this.ability.canRun); systemService.set('activeNamespace.name', 'something-suffixed'); - assert.ok(jobAbility.canRun); + assert.ok(this.ability.canRun); systemService.set('activeNamespace.name', 'something-more-suffixed'); assert.notOk( - jobAbility.canRun, + this.ability.canRun, 'expected the namespace with the greatest number of matched characters to be chosen' ); systemService.set('activeNamespace.name', '000-abc-999'); - assert.ok(jobAbility.canRun, 'expected to be able to match against more than one wildcard'); + assert.ok(this.ability.canRun, 'expected to be able to match against more than one wildcard'); }); });