diff --git a/addons/api/mirage/factories/channel-recording.js b/addons/api/mirage/factories/channel-recording.js index 75ad07dcc..639e15448 100644 --- a/addons/api/mirage/factories/channel-recording.js +++ b/addons/api/mirage/factories/channel-recording.js @@ -6,6 +6,7 @@ import factory from '../generated/factories/channel-recording'; import generateId from '../helpers/id'; import { faker } from '@faker-js/faker'; +import { MIME_TYPE_ASCIICAST } from 'api/models/channel-recording'; export default factory.extend({ id: () => generateId('chr_'), @@ -14,4 +15,5 @@ export default factory.extend({ }, end_time: (i) => (i % 3 === 2 ? null : faker.date.recent()), duration: (i) => (i % 3 === 2 ? null : `${faker.datatype.number()}s`), + mime_types: (i) => (i % 3 === 2 ? null : [MIME_TYPE_ASCIICAST]), }); diff --git a/addons/api/mirage/generated/factories/channel-recording.js b/addons/api/mirage/generated/factories/channel-recording.js index 27ea5539f..4c6446150 100644 --- a/addons/api/mirage/generated/factories/channel-recording.js +++ b/addons/api/mirage/generated/factories/channel-recording.js @@ -5,7 +5,6 @@ import { Factory } from 'miragejs'; import { faker } from '@faker-js/faker'; -import { MIME_TYPE_ASCIICAST } from 'api/models/channel-recording'; /** * GeneratedChannelRecordingModelFactory @@ -16,5 +15,4 @@ export default Factory.extend({ bytes_down: () => faker.random.numeric(faker.datatype.number({ min: 1, max: 12 })), errors: () => faker.datatype.number({ max: 10 }), - mime_types: () => [MIME_TYPE_ASCIICAST], }); diff --git a/ui/admin/app/abilities/channel-recording.js b/ui/admin/app/abilities/channel-recording.js new file mode 100644 index 000000000..2ef34e93b --- /dev/null +++ b/ui/admin/app/abilities/channel-recording.js @@ -0,0 +1,30 @@ +/** + * Copyright (c) HashiCorp, Inc. + * SPDX-License-Identifier: MPL-2.0 + */ + +import ChannelRecordingAbility from 'api/abilities/channel-recording'; +import { inject as service } from '@ember/service'; + +export default class OverrideChannelRecordingAbility extends ChannelRecordingAbility { + // =service + @service features; + + /** + * returns true if session recording is completed and the channel + * has the correct mime type + */ + get canPlay() { + const { session_recording } = this.model.connection_recording; + return session_recording.isAvailable && this.model.isAsciicast; + } + + /** + * returns true if session recording is completed and the channel + * is not the correct mime type + */ + get canViewOnly() { + const { session_recording } = this.model.connection_recording; + return session_recording.isAvailable && !this.model.isAsciicast; + } +} diff --git a/ui/admin/app/templates/scopes/scope/session-recordings/session-recording/channels-by-connection/index.hbs b/ui/admin/app/templates/scopes/scope/session-recordings/session-recording/channels-by-connection/index.hbs index c6196ddc4..f8ef17c66 100644 --- a/ui/admin/app/templates/scopes/scope/session-recordings/session-recording/channels-by-connection/index.hbs +++ b/ui/admin/app/templates/scopes/scope/session-recordings/session-recording/channels-by-connection/index.hbs @@ -128,7 +128,7 @@ - {{#if @model.sessionRecording.isAvailable}} + {{#if (can 'play channel-recording' B.data)}} + {{else if (can 'viewOnly channel-recording' B.data)}} + {{/if}} diff --git a/ui/admin/tests/unit/abilities/channel-recording-test.js b/ui/admin/tests/unit/abilities/channel-recording-test.js new file mode 100644 index 000000000..8e0b49f5c --- /dev/null +++ b/ui/admin/tests/unit/abilities/channel-recording-test.js @@ -0,0 +1,60 @@ +/** + * Copyright (c) HashiCorp, Inc. + * SPDX-License-Identifier: MPL-2.0 + */ + +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; +import { STATE_SESSION_RECORDING_AVAILABLE } from 'api/models/session-recording'; +import { MIME_TYPE_ASCIICAST } from 'api/models/channel-recording'; + +module('Unit | Abilities | channel-recording', function (hooks) { + setupTest(hooks); + + let canService; + let store; + let sessionRecording; + let connectionRecording; + let asciicastChannel; + let noneAsciicastChannel; + + hooks.beforeEach(function () { + canService = this.owner.lookup('service:can'); + store = this.owner.lookup('service:store'); + + sessionRecording = store.createRecord('session-recording', { + state: STATE_SESSION_RECORDING_AVAILABLE, + }); + connectionRecording = store.createRecord('connection-recording', { + session_recording: sessionRecording, + }); + asciicastChannel = store.createRecord('channel-recording', { + mime_types: [MIME_TYPE_ASCIICAST], + connection_recording: connectionRecording, + }); + noneAsciicastChannel = store.createRecord('channel-recording', { + mime_types: null, + connection_recording: connectionRecording, + }); + }); + + test('returns true if session recording complete and channel mime type is asciicast', function (assert) { + assert.expect(2); + + assert.true(canService.can('play channel-recording', asciicastChannel)); + assert.false( + canService.can('play channel-recording', noneAsciicastChannel) + ); + }); + + test('returns false if session recording complete and channel mime type is not asciicast', function (assert) { + assert.expect(2); + + assert.true( + canService.can('viewOnly channel-recording', noneAsciicastChannel) + ); + assert.false( + canService.can('viewOnly channel-recording', asciicastChannel) + ); + }); +});