|
| 1 | +import { module, test } from 'qunit'; |
| 2 | +import { setupTest } from 'ember-qunit'; |
| 3 | +import { setupMirage } from 'ember-cli-mirage/test-support'; |
| 4 | +import { TestContext } from 'ember-test-helpers'; |
| 5 | +import sinon from 'sinon'; |
| 6 | + |
| 7 | +import { Permission } from 'ember-osf-web/models/osf-model'; |
| 8 | +import GuidFileRoute from 'ember-osf-web/guid-file/route'; |
| 9 | +import { ConnectedStorageOperationNames } from 'ember-osf-web/models/addon-operation-invocation'; |
| 10 | + |
| 11 | +interface RouteTestContext extends TestContext { |
| 12 | + route: GuidFileRoute; |
| 13 | + sandbox: sinon.SinonSandbox; |
| 14 | +} |
| 15 | + |
| 16 | +module('Unit | Route | guid-file', function(hooks) { |
| 17 | + setupTest(hooks); |
| 18 | + setupMirage(hooks); |
| 19 | + |
| 20 | + // Helper functions |
| 21 | + function createFileWithMetadata( |
| 22 | + target: any, |
| 23 | + provider: string, |
| 24 | + name = `${provider}-file.txt`, |
| 25 | + metadataOverrides: any = {}, |
| 26 | + ) { |
| 27 | + const file = server.create('file', { |
| 28 | + target, |
| 29 | + provider, |
| 30 | + name, |
| 31 | + }); |
| 32 | + |
| 33 | + server.create('custom-file-metadata-record', { |
| 34 | + id: file.id, |
| 35 | + ...metadataOverrides, |
| 36 | + }); |
| 37 | + |
| 38 | + return file; |
| 39 | + } |
| 40 | + |
| 41 | + function createNodeWithPermissions(permissions: Permission[] = [Permission.Read], traits: string[] = []) { |
| 42 | + return server.create('node', { |
| 43 | + currentUserPermissions: permissions, |
| 44 | + }, ...traits); |
| 45 | + } |
| 46 | + |
| 47 | + function setupFeatures(context: RouteTestContext, features: Record<string, boolean> = {}) { |
| 48 | + const featuresService = context.owner.lookup('service:features'); |
| 49 | + featuresService.setup(features); |
| 50 | + } |
| 51 | + |
| 52 | + hooks.beforeEach(function(this: RouteTestContext) { |
| 53 | + this.sandbox = sinon.createSandbox(); |
| 54 | + this.route = this.owner.lookup('route:guid-file'); |
| 55 | + server.create('user', 'loggedIn'); |
| 56 | + |
| 57 | + const features = this.owner.lookup('service:features'); |
| 58 | + features.setup({}); |
| 59 | + }); |
| 60 | + |
| 61 | + hooks.afterEach(function(this: RouteTestContext) { |
| 62 | + this.sandbox.restore(); |
| 63 | + }); |
| 64 | + |
| 65 | + test('it exists', function(this: RouteTestContext, assert) { |
| 66 | + assert.ok(this.route, 'Route exists'); |
| 67 | + }); |
| 68 | + |
| 69 | + test('loads OSF storage file', async function(this: RouteTestContext, assert) { |
| 70 | + const node = createNodeWithPermissions([Permission.Read], ['withContributors', 'withAffiliatedInstitutions']); |
| 71 | + const file = createFileWithMetadata(node, 'osfstorage', 'test-file.txt', { |
| 72 | + title: 'Test Metadata Title', |
| 73 | + description: 'Test description', |
| 74 | + }); |
| 75 | + |
| 76 | + const model = await this.route.model({ guid: file.id }); |
| 77 | + |
| 78 | + assert.ok(model, 'Model loaded successfully'); |
| 79 | + assert.equal(model?.constructor.name, 'OsfStorageFile', 'Correct OSF storage file type created'); |
| 80 | + assert.equal(model?.fileModel.id, file.id, 'File model matches expected ID'); |
| 81 | + assert.equal(this.route.metadata.id, file.id, 'Metadata loaded correctly'); |
| 82 | + }); |
| 83 | + |
| 84 | + test('loads external storage files for all providers', async function(this: RouteTestContext, assert) { |
| 85 | + const providers = [ |
| 86 | + { provider: 'bitbucket', expectedClass: 'BitbucketFile' }, |
| 87 | + { provider: 'box', expectedClass: 'BoxFile' }, |
| 88 | + { provider: 'dataverse', expectedClass: 'DataverseFile' }, |
| 89 | + { provider: 'dropbox', expectedClass: 'DropboxFile' }, |
| 90 | + { provider: 'figshare', expectedClass: 'FigshareFile' }, |
| 91 | + { provider: 'github', expectedClass: 'GithubFile' }, |
| 92 | + { provider: 'gitlab', expectedClass: 'GitlabFile' }, |
| 93 | + { provider: 'googledrive', expectedClass: 'GoogleDriveFile' }, |
| 94 | + { provider: 'onedrive', expectedClass: 'OneDriveFile' }, |
| 95 | + { provider: 'owncloud', expectedClass: 'OwnCloudFile' }, |
| 96 | + { provider: 's3', expectedClass: 'S3File' }, |
| 97 | + ]; |
| 98 | + |
| 99 | + for (const { provider, expectedClass } of providers) { |
| 100 | + const node = createNodeWithPermissions(); |
| 101 | + const file = createFileWithMetadata(node, provider); |
| 102 | + |
| 103 | + const model = await this.route.model({ guid: file.id }); |
| 104 | + |
| 105 | + assert.ok(model, `${provider} file loaded successfully`); |
| 106 | + assert.equal(model?.constructor.name, expectedClass, `Correct ${provider} file type created`); |
| 107 | + assert.equal(model?.fileModel.id, file.id, `${provider} file model ID matches`); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + test('loads service file when gravy_waffle feature is enabled', async function(this: RouteTestContext, assert) { |
| 112 | + setupFeatures(this, { gravy_waffle: true }); |
| 113 | + |
| 114 | + const node = createNodeWithPermissions([Permission.Admin]); |
| 115 | + const file = createFileWithMetadata(node, 'azureblobstorage', 'service-file.txt'); |
| 116 | + const resourceReference = server.create('resource-reference', { id: node.id }); |
| 117 | + |
| 118 | + const azureblobstorage = server.create('external-storage-service', { |
| 119 | + id: 'azureblobstorage', |
| 120 | + displayName: 'Azure Blob Storage', |
| 121 | + supportedFeatures: [], |
| 122 | + }); |
| 123 | + server.create('configured-storage-addon', { |
| 124 | + id: 'azureblobstorage', |
| 125 | + displayName: 'Azure Blob Storage', |
| 126 | + rootFolder: '/woot/', |
| 127 | + externalServiceName: 'azureblobstorage', |
| 128 | + externalStorageService: azureblobstorage, |
| 129 | + authorizedResource: resourceReference, |
| 130 | + connectedOperationNames: [ |
| 131 | + ConnectedStorageOperationNames.HasRevisions, |
| 132 | + ], |
| 133 | + }); |
| 134 | + |
| 135 | + const model = await this.route.model({ guid: file.id }); |
| 136 | + |
| 137 | + assert.ok(model, 'Model loaded successfully with gravy_waffle'); |
| 138 | + assert.equal(model?.constructor?.name, 'ServiceFile', 'ServiceFile created when configured addon exists'); |
| 139 | + assert.equal(model?.fileModel?.id, file.id, 'ServiceFile has correct file model'); |
| 140 | + }); |
| 141 | + |
| 142 | + test('redirects to registration page when target is registration', async function(this: RouteTestContext, assert) { |
| 143 | + const registration = server.create('registration', { |
| 144 | + withdrawn: true, |
| 145 | + currentUserPermissions: [Permission.Read], |
| 146 | + }); |
| 147 | + |
| 148 | + const file = createFileWithMetadata(registration, 'osfstorage', 'registration-file.txt'); |
| 149 | + |
| 150 | + const transitionToStub = this.sandbox.stub(this.route, 'transitionTo'); |
| 151 | + |
| 152 | + await this.route.model({ guid: file.id }); |
| 153 | + |
| 154 | + assert.ok( |
| 155 | + (transitionToStub as any).calledWith('guid-registration', registration.id), |
| 156 | + 'Redirects to registration page for withdrawn registration', |
| 157 | + ); |
| 158 | + }); |
| 159 | + |
| 160 | + test('redirects to not-found for unknown provider', async function(this: RouteTestContext, assert) { |
| 161 | + const node = createNodeWithPermissions(); |
| 162 | + const file = createFileWithMetadata(node, 'unknown-provider', 'unknown-file.txt'); |
| 163 | + |
| 164 | + const transitionToStub = this.sandbox.stub(this.route, 'transitionTo'); |
| 165 | + |
| 166 | + await this.route.model({ guid: file.id }); |
| 167 | + |
| 168 | + assert.ok( |
| 169 | + (transitionToStub as any).calledWith('not-found', file.id), |
| 170 | + 'Redirects to not-found for unknown provider', |
| 171 | + ); |
| 172 | + }); |
| 173 | + |
| 174 | + test('handles gravy_waffle feature disabled correctly', async function(this: RouteTestContext, assert) { |
| 175 | + setupFeatures(this, { gravy_waffle: false }); |
| 176 | + |
| 177 | + const node = createNodeWithPermissions(); |
| 178 | + const file = createFileWithMetadata(node, 'github', 'feature-disabled-file.txt'); |
| 179 | + |
| 180 | + const model = await this.route.model({ guid: file.id }); |
| 181 | + |
| 182 | + assert.ok(model, 'Model loaded successfully'); |
| 183 | + assert.equal(model?.constructor?.name, 'GithubFile', 'Uses provider-specific file when gravy_waffle disabled'); |
| 184 | + }); |
| 185 | +}); |
0 commit comments