diff --git a/ui/core/app/components/form/target/index.hbs b/ui/core/app/components/form/target/index.hbs new file mode 100644 index 000000000..afbfc76b0 --- /dev/null +++ b/ui/core/app/components/form/target/index.hbs @@ -0,0 +1,115 @@ + + + + + + {{#if @model.errors.name}} + + {{#each @model.errors.name as |error|}} + {{error.message}} + {{/each}} + + {{/if}} + + + + {{#if @model.errors.description}} + + {{#each @model.errors.description as |error|}} + {{error.message}} + {{/each}} + + {{/if}} + + + + {{#if @model.errors.session_max_seconds}} + + {{#each @model.errors.session_max_seconds as |error|}} + {{error.message}} + {{/each}} + + {{/if}} + + + + {{#if @model.errors.session_connection_limit}} + + {{#each @model.errors.session_connection_limit as |error|}} + {{error.message}} + {{/each}} + + {{/if}} + + + {{!-- TODO this is a type-specific field --}} + + {{!-- TODO need to figure out how to reference `attributes.errors` here --}} + {{#if @model.errors.default_port}} + + {{#each @model.errors.default_port as |error|}} + {{error.message}} + {{/each}} + + {{/if}} + + + + + diff --git a/ui/core/app/router.js b/ui/core/app/router.js index 434fad587..a88062809 100644 --- a/ui/core/app/router.js +++ b/ui/core/app/router.js @@ -9,13 +9,14 @@ export default class Router extends EmberRouter { Router.map(function () { this.route('scopes', function () { this.route('scope', { path: ':scope_id' }, function () { + this.route('authenticate', function () { this.route('method', { path: ':auth_method_id' }); }); - this.route('orgs', function () { - this.route('new'); - }); + this.route('scopes', function() {}); + this.route('edit'); + this.route('new'); this.route('users', function () { this.route('user', { path: ':user_id' }, function () { @@ -53,10 +54,14 @@ Router.map(function () { }); this.route('new'); }); - this.route('scopes', function() {}); - this.route('edit'); - this.route('new'); + this.route('sessions', function() {}); + + this.route('targets', function() { + this.route('target', { path: ':target_id' }, function() {}); + this.route('new'); + }); + }); }); diff --git a/ui/core/app/routes/scopes/scope/targets.js b/ui/core/app/routes/scopes/scope/targets.js new file mode 100644 index 000000000..30f049682 --- /dev/null +++ b/ui/core/app/routes/scopes/scope/targets.js @@ -0,0 +1,67 @@ +import Route from '@ember/routing/route'; +import { inject as service } from '@ember/service'; +import { action } from '@ember/object'; +import loading from 'ember-loading/decorator'; +import { confirm } from '../../../decorators/confirm'; +import { notifySuccess, notifyError } from '../../../decorators/notify'; + +export default class ScopesScopeTargetsRoute extends Route { + // =services + + @service intl; + @service notify; + + // =methods + + /** + * Loads all targets under current scope. + * @return {Promise{[TargetModel]}} + */ + async model() { + const { id: scope_id } = this.modelFor('scopes.scope'); + return this.store.query('target', { scope_id }); + } + + // =actions + + /** + * Rollback changes on a target. + * @param {TargetModel} target + */ + @action + cancel(target) { + const { isNew } = target; + target.rollbackAttributes(); + if (isNew) this.transitionTo('scopes.scope.targets'); + } + + /** + * Handle save. + * @param {TargetModel} target + * @param {Event} e + */ + @action + @loading + @notifyError(({ message }) => message) + @notifySuccess(({ isNew }) => isNew ? 'notifications.create-success' : 'notifications.save-success') + async save(target) { + await target.save(); + await this.transitionTo('scopes.scope.targets.target', target); + this.refresh(); + } + + /** + * Deletes a target and redirects to targets index. + * @param {TargetModel} target + */ + @action + @loading + @confirm('questions.delete-confirm') + @notifyError(({ message }) => message, { catch: true }) + @notifySuccess('notifications.delete-success') + async delete(target) { + await target.destroyRecord(); + await this.replaceWith('scopes.scope.targets'); + this.refresh(); + } +} diff --git a/ui/core/app/routes/scopes/scope/targets/index.js b/ui/core/app/routes/scopes/scope/targets/index.js new file mode 100644 index 000000000..3902b6c03 --- /dev/null +++ b/ui/core/app/routes/scopes/scope/targets/index.js @@ -0,0 +1,4 @@ +import Route from '@ember/routing/route'; + +export default class ScopesScopeTargetsIndexRoute extends Route { +} diff --git a/ui/core/app/routes/scopes/scope/targets/new.js b/ui/core/app/routes/scopes/scope/targets/new.js new file mode 100644 index 000000000..5e3d97603 --- /dev/null +++ b/ui/core/app/routes/scopes/scope/targets/new.js @@ -0,0 +1,17 @@ +import Route from '@ember/routing/route'; + +export default class ScopesScopeTargetsNewRoute extends Route { + // =methods + + /** + * Creates a new unsaved target in current scope. + * @return {TargetModel} + */ + model() { + const scopeModel = this.modelFor('scopes.scope'); + return this.store.createRecord('target', { + type: 'tcp', + scopeModel, + }); + } +} diff --git a/ui/core/app/routes/scopes/scope/targets/target.js b/ui/core/app/routes/scopes/scope/targets/target.js new file mode 100644 index 000000000..4702bb059 --- /dev/null +++ b/ui/core/app/routes/scopes/scope/targets/target.js @@ -0,0 +1,39 @@ +import Route from '@ember/routing/route'; + +export default class ScopesScopeTargetsTargetRoute extends Route { + // =methods + + /** + * Load a target. + * @param {object} params + * @param {string} params.target_id + * @return {TargetModel} + */ + async model({ target_id }) { + return this.store.findRecord('target', target_id, { reload: true }); + } + + /** + * Renders the target-specific templates for header, navigation, + * and actions page sections. + * @override + */ + renderTemplate() { + super.renderTemplate(...arguments); + + this.render('scopes/scope/targets/target/-header', { + into: 'scopes/scope/targets/target', + outlet: 'header', + }); + + this.render('scopes/scope/targets/target/-navigation', { + into: 'scopes/scope/targets/target', + outlet: 'navigation', + }); + + this.render('scopes/scope/targets/target/-actions', { + into: 'scopes/scope/targets/target', + outlet: 'actions', + }); + } +} diff --git a/ui/core/app/routes/scopes/scope/targets/target/index.js b/ui/core/app/routes/scopes/scope/targets/target/index.js new file mode 100644 index 000000000..2f80e30b7 --- /dev/null +++ b/ui/core/app/routes/scopes/scope/targets/target/index.js @@ -0,0 +1,4 @@ +import Route from '@ember/routing/route'; + +export default class ScopesScopeTargetsTargetIndexRoute extends Route { +} diff --git a/ui/core/app/templates/scopes/scope.hbs b/ui/core/app/templates/scopes/scope.hbs index cdd095a45..e735d5640 100644 --- a/ui/core/app/templates/scopes/scope.hbs +++ b/ui/core/app/templates/scopes/scope.hbs @@ -75,14 +75,14 @@ @size="medium" /> {{t "resources.session.title_plural"}} - {{!-- - + {{t "resources.target.title_plural"}} - + {{!-- + diff --git a/ui/core/app/templates/scopes/scope/targets.hbs b/ui/core/app/templates/scopes/scope/targets.hbs new file mode 100644 index 000000000..8b5774916 --- /dev/null +++ b/ui/core/app/templates/scopes/scope/targets.hbs @@ -0,0 +1,3 @@ +{{title (t "resources.target.title_plural")}} + +{{outlet}} diff --git a/ui/core/app/templates/scopes/scope/targets/index.hbs b/ui/core/app/templates/scopes/scope/targets/index.hbs new file mode 100644 index 000000000..2aa1d6376 --- /dev/null +++ b/ui/core/app/templates/scopes/scope/targets/index.hbs @@ -0,0 +1,88 @@ + + + + + + + + + {{t "resources.target.title_plural"}} + + + {{#if @model}} + {{t "resources.target.description"}} + {{/if}} + + + + {{#if @model}} + + {{t "titles.new"}} + + {{/if}} + + + + + {{#if @model}} + + + + + {{t "form.name.label"}} + {{t "form.type.label"}} + {{t "form.id.label"}} + + + + {{#each @model as |target|}} + + + + {{target.displayName}} + + {{target.description}} + + + {{#if target.type}} + + {{t (concat "resources.target.types." target.type)}} + + {{/if}} + + + + {{target.id}} + + + + {{/each}} + + + + {{else}} + + + + + {{t "resources.target.description"}} + + + + {{t "titles.new"}} + + + + + {{/if}} + + + + diff --git a/ui/core/app/templates/scopes/scope/targets/new.hbs b/ui/core/app/templates/scopes/scope/targets/new.hbs new file mode 100644 index 000000000..f161f252b --- /dev/null +++ b/ui/core/app/templates/scopes/scope/targets/new.hbs @@ -0,0 +1,24 @@ +{{title (t 'titles.new')}} + + + + + + + + + + {{t "titles.new"}} + + + + + + + + + diff --git a/ui/core/app/templates/scopes/scope/targets/target.hbs b/ui/core/app/templates/scopes/scope/targets/target.hbs new file mode 100644 index 000000000..2a878ff51 --- /dev/null +++ b/ui/core/app/templates/scopes/scope/targets/target.hbs @@ -0,0 +1,25 @@ +{{title @model.displayName}} + + + + + + + + + {{outlet "header"}} + + + + {{outlet "navigation"}} + + + + {{outlet "actions"}} + + + + {{outlet}} + + + diff --git a/ui/core/app/templates/scopes/scope/targets/target/-actions.hbs b/ui/core/app/templates/scopes/scope/targets/target/-actions.hbs new file mode 100644 index 000000000..0b07c9565 --- /dev/null +++ b/ui/core/app/templates/scopes/scope/targets/target/-actions.hbs @@ -0,0 +1,15 @@ + + {{!-- + + {{t 'resources.target.actions.add-host-sets'}} + + --}} + + + {{t 'resources.target.actions.delete'}} + + diff --git a/ui/core/app/templates/scopes/scope/targets/target/-header.hbs b/ui/core/app/templates/scopes/scope/targets/target/-header.hbs new file mode 100644 index 000000000..14e01c1de --- /dev/null +++ b/ui/core/app/templates/scopes/scope/targets/target/-header.hbs @@ -0,0 +1,12 @@ + + {{t "resources.target.title"}} + + +{{t "resources.target.description"}} + + {{@model.id}} + diff --git a/ui/core/app/templates/scopes/scope/targets/target/-navigation.hbs b/ui/core/app/templates/scopes/scope/targets/target/-navigation.hbs new file mode 100644 index 000000000..1e0071737 --- /dev/null +++ b/ui/core/app/templates/scopes/scope/targets/target/-navigation.hbs @@ -0,0 +1,10 @@ + + + {{t 'titles.details'}} + + {{!-- + + {{t 'resources.host-set.title_plural'}} + + --}} + diff --git a/ui/core/app/templates/scopes/scope/targets/target/index.hbs b/ui/core/app/templates/scopes/scope/targets/target/index.hbs new file mode 100644 index 000000000..83fc83598 --- /dev/null +++ b/ui/core/app/templates/scopes/scope/targets/target/index.hbs @@ -0,0 +1,5 @@ + diff --git a/ui/core/tests/acceptance/scopes-test.js b/ui/core/tests/acceptance/scopes-test.js index 175099bb3..9af9e1da8 100644 --- a/ui/core/tests/acceptance/scopes-test.js +++ b/ui/core/tests/acceptance/scopes-test.js @@ -41,6 +41,7 @@ module('Acceptance | scopes', function (hooks) { }; hooks.beforeEach(function () { + // Generate resources instances.scopes.global = this.server.create('scope', { id: 'global' }); instances.scopes.org = this.server.create('scope', { type: 'org', @@ -70,7 +71,7 @@ module('Acceptance | scopes', function (hooks) { urls.project2Scope = `/scopes/${instances.scopes.project2.id}`; urls.projectScopeEdit = `/scopes/${instances.scopes.project.id}/edit`; urls.project2ScopeEdit = `/scopes/${instances.scopes.project2.id}/edit`; - // Generate scope couners + // Generate resource couner getScopeCount = (type) => this.server.schema.scopes.where({ type }).length; authenticateSession({}); }); diff --git a/ui/core/tests/acceptance/targets-test.js b/ui/core/tests/acceptance/targets-test.js new file mode 100644 index 000000000..d8e9c811b --- /dev/null +++ b/ui/core/tests/acceptance/targets-test.js @@ -0,0 +1,274 @@ +import { module, test } from 'qunit'; +import { visit, currentURL, find, click, fillIn } from '@ember/test-helpers'; +import { setupApplicationTest } from 'ember-qunit'; +import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; +import a11yAudit from 'ember-a11y-testing/test-support/audit'; +import { Response } from 'miragejs'; +import { resolve, reject } from 'rsvp'; +import sinon from 'sinon'; +import { + authenticateSession, + // These are left here intentionally for future reference. + //currentSession, + //invalidateSession, +} from 'ember-simple-auth/test-support'; + +module('Acceptance | targets', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + let getTargetCount; + + const instances = { + scopes: { + global: null, + org: null, + org2: null, + project: null, + project2: null, + } + }; + const urls = { + globalScope: null, + orgScope: null, + projectScope: null, + targets: null, + target: null, + newTarget: null, + }; + + hooks.beforeEach(function () { + // Generate resources + instances.scopes.global = this.server.create('scope', { id: 'global' }); + instances.scopes.org = this.server.create('scope', { + type: 'org', + scope: { id: 'global', type: 'global' }, + }); + instances.scopes.project = this.server.create('scope', { + type: 'project', + scope: { id: instances.scopes.org.id, type: 'org' }, + }); + instances.target = this.server.create('target', { + scope: instances.scopes.project, + }); + // Generate route URLs for resources + urls.globalScope = `/scopes/global/scopes`; + urls.orgScope = `/scopes/${instances.scopes.org.id}/scopes`; + urls.projectScope = `/scopes/${instances.scopes.project.id}`; + urls.targets = `/scopes/${instances.scopes.project.id}/targets`; + urls.target = `/scopes/${instances.scopes.project.id}/targets/${instances.target.id}`; + urls.unknownTarget = `/scopes/${instances.scopes.project.id}/targets/foo`; + urls.newTarget = `/scopes/${instances.scopes.project.id}/targets/new`; + // Generate resource couner + getTargetCount = () => this.server.schema.targets.all().models.length; + authenticateSession({}); + }); + + test('visiting targets', async function (assert) { + assert.expect(2); + await visit(urls.targets); + await a11yAudit(); + assert.equal(currentURL(), urls.targets); + await visit(urls.target); + await a11yAudit(); + assert.equal(currentURL(), urls.target); + }); + + test('visiting an unknown target displays 404 message', async function (assert) { + assert.expect(1); + await visit(urls.unknownTarget); + await a11yAudit(); + assert.ok(find('.rose-message-subtitle').textContent.trim(), 'Error 404'); + }); + + test('can create new targets', async function (assert) { + assert.expect(1); + const count = getTargetCount(); + await visit(urls.newTarget); + await fillIn('[name="name"]', 'random string'); + await click('[type="submit"]'); + assert.equal(getTargetCount(), count + 1); + }); + + test('can cancel create new targets', async function (assert) { + assert.expect(2); + const count = getTargetCount(); + await visit(urls.newTarget); + await fillIn('[name="name"]', 'random string'); + await click('.rose-form-actions [type="button"]'); + assert.equal(currentURL(), urls.targets); + assert.equal(getTargetCount(), count); + }); + + test('saving a new target with invalid fields displays error messages', async function (assert) { + assert.expect(2); + this.server.post('/targets', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'The request was invalid.', + details: { + request_fields: [ + { + name: 'name', + description: 'Name is required.', + }, + ], + }, + } + ); + }); + await visit(urls.newTarget); + await click('[type="submit"]'); + assert.ok(find('[role="alert"]').textContent.trim(), 'The request was invalid.'); + assert.ok( + find('.rose-form-error-message').textContent.trim(), + 'Name is required.' + ); + }); + + test('can save changes to existing target', async function (assert) { + assert.expect(3); + assert.notEqual(instances.target.name, 'random string'); + await visit(urls.target); + await click('form [type="button"]', 'Activate edit mode'); + await fillIn('[name="name"]', 'random string'); + await click('.rose-form-actions [type="submit"]'); + assert.equal(currentURL(), urls.target); + assert.equal(this.server.schema.targets.all().models[0].name, 'random string'); + }); + + test('can cancel changes to existing target', async function (assert) { + assert.expect(2); + await visit(urls.target); + await click('form [type="button"]', 'Activate edit mode'); + await fillIn('[name="name"]', 'random string'); + await click('.rose-form-actions [type="button"]'); + assert.notEqual(instances.target.name, 'random string'); + assert.equal(find('[name="name"]').value, instances.target.name); + }); + + test('saving an existing target with invalid fields displays error messages', async function (assert) { + assert.expect(2); + this.server.patch('/targets/:id', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'The request was invalid.', + details: { + request_fields: [ + { + name: 'name', + description: 'Name is required.', + }, + ], + }, + } + ); + }); + await visit(urls.target); + await click('form [type="button"]', 'Activate edit mode'); + await fillIn('[name="name"]', 'random string'); + await click('[type="submit"]'); + assert.ok(find('[role="alert"]').textContent.trim(), 'The request was invalid.'); + assert.ok( + find('.rose-form-error-message').textContent.trim(), + 'Name is required.' + ); + }); + + test('can discard unsaved target changes via dialog', async function (assert) { + assert.expect(5); + const confirmService = this.owner.lookup('service:confirm'); + confirmService.enabled = true; + assert.notEqual(instances.target.name, 'random string'); + await visit(urls.target); + await click('form [type="button"]', 'Activate edit mode'); + await fillIn('[name="name"]', 'random string'); + assert.equal(currentURL(), urls.target); + try { + await visit(urls.targets); + } catch (e) { + assert.ok(find('.rose-dialog')); + await click('.rose-dialog-footer button:first-child'); + assert.equal(currentURL(), urls.targets); + assert.notEqual(this.server.schema.targets.all().models[0].name, 'random string'); + } + }); + + test('can cancel discard unsaved target changes via dialog', async function (assert) { + assert.expect(5); + const confirmService = this.owner.lookup('service:confirm'); + confirmService.enabled = true; + assert.notEqual(instances.target.name, 'random string'); + await visit(urls.target); + await click('form [type="button"]', 'Activate edit mode'); + await fillIn('[name="name"]', 'random string'); + assert.equal(currentURL(), urls.target); + try { + await visit(urls.targets); + } catch (e) { + assert.ok(find('.rose-dialog')); + await click('.rose-dialog-footer button:last-child'); + assert.equal(currentURL(), urls.target); + assert.notEqual(this.server.schema.targets.all().models[0].name, 'random string'); + } + }); + + test('can delete target', async function (assert) { + assert.expect(1); + const count = getTargetCount(); + await visit(urls.target); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.equal(getTargetCount(), count - 1); + }); + + test('can accept delete target via dialog', async function (assert) { + assert.expect(2); + const confirmService = this.owner.lookup('service:confirm'); + confirmService.enabled = true; + confirmService.confirm = sinon.fake.returns(resolve()); + const count = getTargetCount(); + await visit(urls.target); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.equal(getTargetCount(), count - 1); + assert.ok(confirmService.confirm.calledOnce); + }); + + test('cannot cancel delete target via dialog', async function (assert) { + assert.expect(2); + const confirmService = this.owner.lookup('service:confirm'); + confirmService.enabled = true; + confirmService.confirm = sinon.fake.returns(reject()); + const count = getTargetCount(); + await visit(urls.target); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.equal(getTargetCount(), count); + assert.ok(confirmService.confirm.calledOnce); + }); + + test('deleting a target which errors displays error messages', async function (assert) { + assert.expect(1); + this.server.del('/targets/:id', () => { + return new Response( + 490, + {}, + { + status: 490, + code: 'error', + message: 'Oops.', + } + ); + }); + await visit(urls.target); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.ok(find('[role="alert"]').textContent.trim(), 'Oops.'); + }); + +}); diff --git a/ui/core/tests/unit/routes/scopes/scope/targets-test.js b/ui/core/tests/unit/routes/scopes/scope/targets-test.js new file mode 100644 index 000000000..45d66036b --- /dev/null +++ b/ui/core/tests/unit/routes/scopes/scope/targets-test.js @@ -0,0 +1,11 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Route | scopes/scope/targets', function(hooks) { + setupTest(hooks); + + test('it exists', function(assert) { + let route = this.owner.lookup('route:scopes/scope/targets'); + assert.ok(route); + }); +}); diff --git a/ui/core/tests/unit/routes/scopes/scope/targets/index-test.js b/ui/core/tests/unit/routes/scopes/scope/targets/index-test.js new file mode 100644 index 000000000..fea08576c --- /dev/null +++ b/ui/core/tests/unit/routes/scopes/scope/targets/index-test.js @@ -0,0 +1,11 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Route | scopes/scope/targets/index', function(hooks) { + setupTest(hooks); + + test('it exists', function(assert) { + let route = this.owner.lookup('route:scopes/scope/targets/index'); + assert.ok(route); + }); +}); diff --git a/ui/core/tests/unit/routes/scopes/scope/targets/new-test.js b/ui/core/tests/unit/routes/scopes/scope/targets/new-test.js new file mode 100644 index 000000000..1c1402d57 --- /dev/null +++ b/ui/core/tests/unit/routes/scopes/scope/targets/new-test.js @@ -0,0 +1,11 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Route | scopes/scope/targets/new', function(hooks) { + setupTest(hooks); + + test('it exists', function(assert) { + let route = this.owner.lookup('route:scopes/scope/targets/new'); + assert.ok(route); + }); +}); diff --git a/ui/core/tests/unit/routes/scopes/scope/targets/target-test.js b/ui/core/tests/unit/routes/scopes/scope/targets/target-test.js new file mode 100644 index 000000000..d2cb9f9fa --- /dev/null +++ b/ui/core/tests/unit/routes/scopes/scope/targets/target-test.js @@ -0,0 +1,11 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Route | scopes/scope/targets/target', function(hooks) { + setupTest(hooks); + + test('it exists', function(assert) { + let route = this.owner.lookup('route:scopes/scope/targets/target'); + assert.ok(route); + }); +}); diff --git a/ui/core/tests/unit/routes/scopes/scope/targets/target/index-test.js b/ui/core/tests/unit/routes/scopes/scope/targets/target/index-test.js new file mode 100644 index 000000000..6f4f3b248 --- /dev/null +++ b/ui/core/tests/unit/routes/scopes/scope/targets/target/index-test.js @@ -0,0 +1,11 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Route | scopes/scope/targets/target/index', function(hooks) { + setupTest(hooks); + + test('it exists', function(assert) { + let route = this.owner.lookup('route:scopes/scope/targets/target/index'); + assert.ok(route); + }); +});
{{t "resources.target.description"}}
{{target.description}}
{{target.id}}
{{@model.id}}