diff --git a/ui/core/app/router.js b/ui/core/app/router.js index e2bb6acda..434fad587 100644 --- a/ui/core/app/router.js +++ b/ui/core/app/router.js @@ -56,6 +56,7 @@ Router.map(function () { this.route('scopes', function() {}); this.route('edit'); this.route('new'); + this.route('sessions', function() {}); }); }); diff --git a/ui/core/app/routes/scopes/scope/sessions.js b/ui/core/app/routes/scopes/scope/sessions.js new file mode 100644 index 000000000..7f64fdb67 --- /dev/null +++ b/ui/core/app/routes/scopes/scope/sessions.js @@ -0,0 +1,103 @@ +import Route from '@ember/routing/route'; +import { inject as service } from '@ember/service'; +import { action } from '@ember/object'; +import { all, hash } from 'rsvp'; +import { task, timeout } from 'ember-concurrency'; +import { A } from '@ember/array'; +import { notifySuccess, notifyError } from '../../../decorators/notify'; +import config from '../../../config/environment'; + +const POLL_TIMEOUT_SECONDS = config.sessionPollingTimeoutSeconds; + +export default class ScopesScopeSessionsRoute extends Route { + + // =services + + @service intl; + @service notify; + + // =attributes + + /** + * A simple Ember Concurrency-based polling task that refreshes the route + * every POLL_TIMEOUT_SECONDS seconds. This is necessary to display changes + * to session `status` that may occur. + * + * NOTE: tasks are sort of attributes and sort of methods, but they are not + * language-level constructs. Thus we annotate this task as if it + * is an attribute. + * @type {Task} + */ + @task(function * () { + while(true) { + yield timeout(POLL_TIMEOUT_SECONDS * 1000); + yield this.refresh(); + } + }).drop() poller; + + // =methods + + /** + * Loads all sessions under the current scope and encapsulates them into + * an array of objects containing their associated users and targets. + * @return {Promise{[{session: SessionModel, user: UserModel, target: TargetModel}]}} + */ + async model() { + const { id: scope_id } = this.modelFor('scopes.scope'); + const sessions = await this.store.query('session', { scope_id }); + const sessionAggregates = await all( + sessions.map(session => hash({ + session, + user: session.user_id + ? ( + this.store.peekRecord('user', session.user_id) || + this.store.findRecord('user', session.user_id) + ) + : null, + target: session.target_id + ? ( + this.store.peekRecord('target', session.target_id) || + this.store.findRecord('target', session.target_id) + ) + : null, + })) + ); + // Sort sessions by time created... + let sortedSessionAggregates = + A(sessionAggregates).sortBy('session.created_time').reverse(); + // Then move active sessions to the top... + sortedSessionAggregates = [ + ...sortedSessionAggregates.filter((aggregate) => aggregate.session.status === 'active'), + ...sortedSessionAggregates.filter((aggregate) => aggregate.session.status !== 'active'), + ]; + return sortedSessionAggregates; + } + + /** + * When this route is activated (entered), begin polling for changes. + */ + activate() { + this.poller.perform(); + } + + /** + * When this route is deactivated (exited), stop polling for changes. + */ + deactivate() { + this.poller.cancelAll(); + } + + // =actions + + /** + * Cancels the specified session and notifies user of success or error. + * @param {SessionModel} + */ + @action + @notifyError(({ message }) => message, { catch: true }) + @notifySuccess('notifications.canceled-success') + async cancelSession(session) { + await session.cancelSession(); + } + +} diff --git a/ui/core/app/routes/scopes/scope/sessions/index.js b/ui/core/app/routes/scopes/scope/sessions/index.js new file mode 100644 index 000000000..bf1a6aeaf --- /dev/null +++ b/ui/core/app/routes/scopes/scope/sessions/index.js @@ -0,0 +1,4 @@ +import Route from '@ember/routing/route'; + +export default class ScopesScopeSessionsIndexRoute extends Route { +} diff --git a/ui/core/app/templates/scopes/scope.hbs b/ui/core/app/templates/scopes/scope.hbs index 8ae14473a..c86d0ac09 100644 --- a/ui/core/app/templates/scopes/scope.hbs +++ b/ui/core/app/templates/scopes/scope.hbs @@ -68,6 +68,28 @@ --}} + + + + {{t "resources.session.title_plural"}} + + {{!-- + + + {{t "resources.target.title_plural"}} + + + + {{t "resources.host-catalog.title_plural"}} + + --}} + {{/if}} {{#unless @model.isProject}} diff --git a/ui/core/app/templates/scopes/scope/sessions.hbs b/ui/core/app/templates/scopes/scope/sessions.hbs new file mode 100644 index 000000000..f7ffb23d4 --- /dev/null +++ b/ui/core/app/templates/scopes/scope/sessions.hbs @@ -0,0 +1,3 @@ +{{title (t "resources.session.title_plural")}} + +{{outlet}} diff --git a/ui/core/app/templates/scopes/scope/sessions/index.hbs b/ui/core/app/templates/scopes/scope/sessions/index.hbs new file mode 100644 index 000000000..8865b085f --- /dev/null +++ b/ui/core/app/templates/scopes/scope/sessions/index.hbs @@ -0,0 +1,82 @@ + + + + + + + +

+ {{t "resources.session.title_plural"}} + +

+

{{t "resources.session.description"}}

+
+ + + + {{#if @model}} + + + + + {{t "form.id.label"}} + {{t "resources.user.title"}} + {{t "resources.target.title"}} + {{t "form.started.label"}} + {{t "form.status.label"}} + {{t "titles.actions"}} + + + + {{#each @model as |sessionAggregate|}} + + + + + {{sessionAggregate.session.id}} + + + {{sessionAggregate.user.displayName}} + {{sessionAggregate.target.displayName}} + + + + + {{sessionAggregate.session.status}} + + + {{#if sessionAggregate.session.isCancelable}} + + {{t "actions.cancel"}} + + {{/if}} + + + {{/each}} + + + + {{else}} + + + + + {{t "resources.session.messages.none.description"}} + + + + + {{/if}} + + + +
diff --git a/ui/core/tests/acceptance/sessions-test.js b/ui/core/tests/acceptance/sessions-test.js new file mode 100644 index 000000000..6026eaa24 --- /dev/null +++ b/ui/core/tests/acceptance/sessions-test.js @@ -0,0 +1,104 @@ +import { module, test } from 'qunit'; +import { visit, currentURL, findAll, find, click } 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 { run, later } from '@ember/runloop'; +import { Response } from 'miragejs'; +import { + authenticateSession, + // These are left here intentionally for future reference. + //currentSession, + //invalidateSession, +} from 'ember-simple-auth/test-support'; + +module('Acceptance | sessions', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + const instances = { + scopes: { + global: null, + org: null, + }, + sessions: null, + }; + const urls = { + orgScope: null, + sessions: null, + }; + + hooks.beforeEach(function () { + authenticateSession({}); + 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: instances.scopes.org.type }, + }); + this.server.createList('group', 1, { scope: instances.scopes.org }, 'withMembers'); + this.server.createList('host-catalog', 1, { scope: instances.scopes.project }, 'withChildren'); + this.server.createList('target', 1, { scope: instances.scopes.project }, 'withRandomHostSets'); + instances.sessions = this.server.createList('session', 3, { + scope: instances.scopes.project, + status: 'active' + }, 'withAssociations'); + urls.sessions = `/scopes/${instances.scopes.project.id}/sessions`; + }); + + test('visiting sessions', async function (assert) { + assert.expect(2); + authenticateSession({}); + later(async () => { + run.cancelTimers(); + await a11yAudit(); + assert.equal(currentURL(), urls.sessions); + assert.equal(findAll('tbody tr').length, instances.sessions.length); + }, 750); + await visit(urls.sessions); + }); + + test('visiting sessions without users or targets is OK', async function (assert) { + assert.expect(2); + instances.sessions[0].update({ + userId: null, + targetId: null + }); + authenticateSession({}); + later(async () => { + run.cancelTimers(); + await a11yAudit(); + assert.equal(currentURL(), urls.sessions); + assert.equal(findAll('tbody tr').length, instances.sessions.length); + }, 750); + await visit(urls.sessions); + }); + + test('cancelling a session', async function (assert) { + assert.expect(2); + authenticateSession({}); + later(async () => { + run.cancelTimers(); + await click('tbody tr:first-child td:last-child button'); + assert.ok(find('[role="alert"].is-success')); + }, 750); + await visit(urls.sessions); + assert.equal(currentURL(), urls.sessions); + }); + + test('cancelling a session with error shows notification', async function (assert) { + assert.expect(2); + this.server.post('/sessions/:id_method', () => new Response(400)); + authenticateSession({}); + later(async () => { + run.cancelTimers(); + await click('tbody tr:first-child td:last-child button'); + assert.ok(find('[role="alert"].is-error')); + }, 750); + await visit(urls.sessions); + assert.equal(currentURL(), urls.sessions); + }); +}); diff --git a/ui/core/tests/unit/routes/scopes/scope/sessions-test.js b/ui/core/tests/unit/routes/scopes/scope/sessions-test.js new file mode 100644 index 000000000..078865278 --- /dev/null +++ b/ui/core/tests/unit/routes/scopes/scope/sessions-test.js @@ -0,0 +1,11 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Route | scopes/scope/sessions', function(hooks) { + setupTest(hooks); + + test('it exists', function(assert) { + let route = this.owner.lookup('route:scopes/scope/sessions'); + assert.ok(route); + }); +}); diff --git a/ui/core/tests/unit/routes/scopes/scope/sessions/index-test.js b/ui/core/tests/unit/routes/scopes/scope/sessions/index-test.js new file mode 100644 index 000000000..3d18fa306 --- /dev/null +++ b/ui/core/tests/unit/routes/scopes/scope/sessions/index-test.js @@ -0,0 +1,11 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Route | scopes/scope/sessions/index', function(hooks) { + setupTest(hooks); + + test('it exists', function(assert) { + let route = this.owner.lookup('route:scopes/scope/sessions/index'); + assert.ok(route); + }); +});