open-nomad/ui/tests/unit/abilities/recommendation-test.js
Buck Doyle 8b5b2116ec
Fix job detail crash when recommendations off (#9269)
Without this, visiting any job detail page on Nomad OSS would crash with
an error like this:

Error: Ember Data Request GET
/v1/recommendations?job=ping%F0%9F%A5%B3&namespace=default returned a
404 Payload (text/xml)

The problem was twofold.

1. The recommendation ability didn’t include anything about checking
whether the feature was present. This adds a request to
/v1/operator/license on application load to determine which features are
present and store them in the system service. The ability now looks for
'Dynamic Application Sizing' in that feature list.

2. Second, I didn’t check permissions at all in the job-fetching or job
detail templates.
2020-11-06 08:21:38 -06:00

82 lines
2.3 KiB
JavaScript

/* eslint-disable ember/avoid-leaking-state-in-ember-objects */
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 | recommendation', function(hooks) {
setupTest(hooks);
setupAbility('recommendation')(hooks);
module('when the Dynamic Application Sizing feature is present', function(hooks) {
hooks.beforeEach(function() {
const mockSystem = Service.extend({
features: ['Dynamic Application Sizing'],
});
this.owner.register('service:system', mockSystem);
});
test('it permits accepting recommendations when ACLs are disabled', function(assert) {
const mockToken = Service.extend({
aclEnabled: false,
});
this.owner.register('service:token', mockToken);
assert.ok(this.ability.canAccept);
});
test('it permits accepting recommendations for client tokens where any namespace has submit-job capabilities', function(assert) {
this.owner.lookup('service:system').set('activeNamespace', {
name: 'anotherNamespace',
});
const mockToken = Service.extend({
aclEnabled: true,
selfToken: { type: 'client' },
selfTokenPolicies: [
{
rulesJSON: {
Namespaces: [
{
Name: 'aNamespace',
Capabilities: [],
},
{
Name: 'bNamespace',
Capabilities: ['submit-job'],
},
],
},
},
],
});
this.owner.register('service:token', mockToken);
assert.ok(this.ability.canAccept);
});
});
module('when the Dynamic Application Sizing feature is not present', function(hooks) {
hooks.beforeEach(function() {
const mockSystem = Service.extend({
features: [],
});
this.owner.register('service:system', mockSystem);
});
test('it does not permit accepting recommendations regardless of ACL status', function(assert) {
const mockToken = Service.extend({
aclEnabled: false,
});
this.owner.register('service:token', mockToken);
assert.notOk(this.ability.canAccept);
});
});
});