github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/acceptance/job-run-test.js (about) 1 import { currentURL } from '@ember/test-helpers'; 2 import { assign } from '@ember/polyfills'; 3 import { module, test } from 'qunit'; 4 import { setupApplicationTest } from 'ember-qunit'; 5 import { setupMirage } from 'ember-cli-mirage/test-support'; 6 import a11yAudit from 'nomad-ui/tests/helpers/a11y-audit'; 7 import setupCodeMirror from 'nomad-ui/tests/helpers/codemirror'; 8 import JobRun from 'nomad-ui/tests/pages/jobs/run'; 9 10 const newJobName = 'new-job'; 11 const newJobTaskGroupName = 'redis'; 12 const newJobNamespace = 'default'; 13 14 let managementToken, clientToken; 15 16 const jsonJob = (overrides) => { 17 return JSON.stringify( 18 assign( 19 {}, 20 { 21 Name: newJobName, 22 Namespace: newJobNamespace, 23 Datacenters: ['dc1'], 24 Priority: 50, 25 TaskGroups: [ 26 { 27 Name: newJobTaskGroupName, 28 Tasks: [ 29 { 30 Name: 'redis', 31 Driver: 'docker', 32 }, 33 ], 34 }, 35 ], 36 }, 37 overrides 38 ), 39 null, 40 2 41 ); 42 }; 43 44 module('Acceptance | job run', function (hooks) { 45 setupApplicationTest(hooks); 46 setupMirage(hooks); 47 setupCodeMirror(hooks); 48 49 hooks.beforeEach(function () { 50 // Required for placing allocations (a result of creating jobs) 51 server.create('node'); 52 53 managementToken = server.create('token'); 54 clientToken = server.create('token'); 55 56 window.localStorage.nomadTokenSecret = managementToken.secretId; 57 }); 58 59 test('it passes an accessibility audit', async function (assert) { 60 assert.expect(1); 61 62 await JobRun.visit(); 63 await a11yAudit(assert); 64 }); 65 66 test('visiting /jobs/run', async function (assert) { 67 await JobRun.visit(); 68 69 assert.equal(currentURL(), '/jobs/run'); 70 assert.equal(document.title, 'Run a job - Nomad'); 71 }); 72 73 test('when submitting a job, the site redirects to the new job overview page', async function (assert) { 74 const spec = jsonJob(); 75 76 await JobRun.visit(); 77 78 await JobRun.editor.editor.fillIn(spec); 79 await JobRun.editor.plan(); 80 await JobRun.editor.run(); 81 assert.equal( 82 currentURL(), 83 `/jobs/${newJobName}@${newJobNamespace}`, 84 `Redirected to the job overview page for ${newJobName}` 85 ); 86 }); 87 88 test('when submitting a job to a different namespace, the redirect to the job overview page takes namespace into account', async function (assert) { 89 const newNamespace = 'second-namespace'; 90 91 server.create('namespace', { id: newNamespace }); 92 const spec = jsonJob({ Namespace: newNamespace }); 93 94 await JobRun.visit(); 95 96 await JobRun.editor.editor.fillIn(spec); 97 await JobRun.editor.plan(); 98 await JobRun.editor.run(); 99 assert.equal( 100 currentURL(), 101 `/jobs/${newJobName}@${newNamespace}`, 102 `Redirected to the job overview page for ${newJobName} and switched the namespace to ${newNamespace}` 103 ); 104 }); 105 106 test('when the user doesn’t have permission to run a job, redirects to the job overview page', async function (assert) { 107 window.localStorage.nomadTokenSecret = clientToken.secretId; 108 109 await JobRun.visit(); 110 assert.equal(currentURL(), '/jobs'); 111 }); 112 113 test('when using client token user can still go to job page if they have correct permissions', async function (assert) { 114 const clientTokenWithPolicy = server.create('token'); 115 const newNamespace = 'second-namespace'; 116 117 server.create('namespace', { id: newNamespace }); 118 server.create('job', { 119 groupCount: 0, 120 createAllocations: false, 121 shallow: true, 122 noActiveDeployment: true, 123 namespaceId: newNamespace, 124 }); 125 126 const policy = server.create('policy', { 127 id: 'something', 128 name: 'something', 129 rulesJSON: { 130 Namespaces: [ 131 { 132 Name: newNamespace, 133 Capabilities: ['scale-job', 'submit-job', 'read-job', 'list-jobs'], 134 }, 135 ], 136 }, 137 }); 138 139 clientTokenWithPolicy.policyIds = [policy.id]; 140 clientTokenWithPolicy.save(); 141 window.localStorage.nomadTokenSecret = clientTokenWithPolicy.secretId; 142 143 await JobRun.visit({ namespace: newNamespace }); 144 assert.equal(currentURL(), `/jobs/run?namespace=${newNamespace}`); 145 }); 146 });