Skip to content

Commit

Permalink
[ENG-4681] Add mirage for v2 api (#2051)
Browse files Browse the repository at this point in the history
## Purpose

Make the v2 endpoints work with mirage. This includes a lot of normalization of the API but not the extra features such as extended attributes for providers nor getting the folder lists.

## Summary of Changes

1. Add mirage
2. Adjust models
  • Loading branch information
brianjgeiger authored Nov 13, 2023
1 parent 81d0846 commit f8b33f1
Show file tree
Hide file tree
Showing 20 changed files with 410 additions and 5 deletions.
11 changes: 9 additions & 2 deletions app/models/external-accounts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { attr } from '@ember-data/model';
import { AsyncBelongsTo, attr, belongsTo } from '@ember-data/model';

import AddonModel from 'ember-osf-web/models/addon';
import UserModel from 'ember-osf-web/models/user';
import OsfModel from './osf-model';

export default class ExternalAccountsModel extends OsfModel {
@attr('string') provider!: string;
@attr('fixstring') profileUrl?: string;
@attr('fixstring') displayName!: string;

@belongsTo('addon', { inverse: null })
provider!: AsyncBelongsTo<AddonModel> & AddonModel;

@belongsTo('user', { inverse: null} )
user!: AsyncBelongsTo<UserModel> & UserModel;
}

declare module 'ember-data/types/registries/model' {
Expand Down
12 changes: 10 additions & 2 deletions app/models/node-addon.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { attr } from '@ember-data/model';
import { AsyncBelongsTo, attr, belongsTo } from '@ember-data/model';

import ExternalAccountsModel from 'ember-osf-web/models/external-accounts';
import NodeModel from 'ember-osf-web/models/node';

import OsfModel from './osf-model';

export default class NodeAddonModel extends OsfModel {
@attr('boolean') nodeHasAuth!: boolean;
@attr('boolean') configured!: boolean;
@attr('string') externalAccountId!: string;
@attr('string') folderId?: string;
@attr('string') folderPath?: string;

@belongsTo('node', { inverse: 'nodeAddons' })
node!: AsyncBelongsTo<NodeModel> & NodeModel;

@belongsTo('external-account', { inverse: null })
externalAccount!: AsyncBelongsTo<ExternalAccountsModel> & ExternalAccountsModel;
}

declare module 'ember-data/types/registries/model' {
Expand Down
4 changes: 4 additions & 0 deletions app/models/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Intl from 'ember-intl/services/intl';
import getRelatedHref from 'ember-osf-web/utils/get-related-href';

import AbstractNodeModel from 'ember-osf-web/models/abstract-node';
import NodeAddonModel from 'ember-osf-web/models/node-addon';
import CitationModel from './citation';
import CommentModel from './comment';
import ContributorModel from './contributor';
Expand Down Expand Up @@ -177,6 +178,9 @@ export default class NodeModel extends AbstractNodeModel.extend(Validations, Col
@hasMany('subject', { inverse: null, async: false })
subjects!: SubjectModel[];

@hasMany('node-addon', { inverse: 'node' })
nodeAddons!: AsyncHasMany<NodeAddonModel>;

// These are only computeds because maintaining separate flag values on
// different classes would be a headache TODO: Improve.

Expand Down
10 changes: 9 additions & 1 deletion app/models/user-addon.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { AsyncHasMany, attr, hasMany } from '@ember-data/model';
import { AsyncBelongsTo, AsyncHasMany, attr, belongsTo, hasMany } from '@ember-data/model';

import AddonModel from 'ember-osf-web/models/addon';
import ExternalAccountsModel from 'ember-osf-web/models/external-accounts';
import UserModel from 'ember-osf-web/models/user';

import OsfModel from './osf-model';

Expand All @@ -10,6 +12,12 @@ export default class UserAddonModel extends OsfModel {

@hasMany('external-accounts', { inverse: null })
externalAccounts!: AsyncHasMany<ExternalAccountsModel> & ExternalAccountsModel[];

@belongsTo('user', { inverse: 'userAddons' })
user!: AsyncBelongsTo<UserModel> & UserModel;

@belongsTo('addon', { inverse: null })
addon!: AsyncBelongsTo<AddonModel> & AddonModel;
}

declare module 'ember-data/types/registries/model' {
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Link } from 'jsonapi-typescript';

import PreprintModel from 'ember-osf-web/models/preprint';
import SparseNodeModel from 'ember-osf-web/models/sparse-node';
import UserAddonModel from 'ember-osf-web/models/user-addon';
import ContributorModel from './contributor';
import DraftRegistrationModel from './draft-registration';
import InstitutionModel from './institution';
Expand Down Expand Up @@ -127,6 +128,9 @@ export default class UserModel extends OsfModel.extend(Validations) {
@hasMany('sparse-node', { inverse: null })
sparseNodes!: AsyncHasMany<SparseNodeModel>;

@hasMany('user-addon', { inverse: 'user' })
userAddons!: AsyncHasMany<UserAddonModel>;

// Calculated fields
@alias('links.html') profileURL!: string;
@alias('links.profile_image') profileImage!: string;
Expand Down
14 changes: 14 additions & 0 deletions mirage/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Server } from 'ember-cli-mirage';
import config from 'ember-osf-web/config/environment';

import { externalAccountDetail, externalAccountList } from 'ember-osf-web/mirage/views/external-account';
import { nodeAddonDetail, nodeAddonList } from 'ember-osf-web/mirage/views/node-addon';
import { createReviewAction } from 'ember-osf-web/mirage/views/review-action';
import { createResource, updateResource } from 'ember-osf-web/mirage/views/resource';
import { getCitation } from './views/citation';
Expand Down Expand Up @@ -72,10 +74,13 @@ export default function(this: Server) {

this.get('/', rootDetail);

osfResource(this, 'addon', { only: ['index']});
osfResource(this, 'developer-app', { path: 'applications', except: ['create', 'update'] });
this.post('/applications', createDeveloperApp);
this.patch('/applications/:id', updateDeveloperApp);

osfResource(this, 'external-account', {only: ['index', 'show', 'create']});

osfResource(this, 'file', { only: ['show', 'update'] });

this.get('/guids/:id', guidDetail);
Expand Down Expand Up @@ -110,6 +115,9 @@ export default function(this: Server) {
defaultSortKey: 'index',
onCreate: createBibliographicContributor,
});
this.get('/nodes/:parentID/addons/:id', nodeAddonDetail);
this.get('/nodes/:parentID/addons/', nodeAddonList);
osfNestedResource(this, 'node', 'nodeAddons', {except: [ 'index', 'show' ]});

this.get('/nodes/:parentID/files', nodeFileProviderList); // Node file providers list
this.get('/nodes/:parentID/files/:fileProviderId', nodeFilesListForProvider); // Node files list for file provider
Expand Down Expand Up @@ -280,6 +288,12 @@ export default function(this: Server) {
this.post('/users/:id/settings/export', userSettings.requestExport);
this.post('/users/:parentID/settings/password/', updatePassword);
this.post('/users/:parentID/claim/', claimUnregisteredUser);
osfNestedResource(this, 'user', 'userAddons', {
path: '/users/:parentID/addons/',
relatedModelName: 'user-addon',
});
this.get('/users/:userID/addons/:addonID/accounts', externalAccountList);
this.get('/users/:userID/addons/:addonID/accounts/:accountID', externalAccountDetail);

osfResource(this, 'external-identity', {
path: '/users/me/settings/identities',
Expand Down
24 changes: 24 additions & 0 deletions mirage/factories/external-account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Factory } from 'ember-cli-mirage';
import faker from 'faker';

import ExternalAccountsModel from 'ember-osf-web/models/external-accounts';

export default Factory.extend<ExternalAccountsModel>({
profileUrl: faker.internet.url,

displayName() {
return faker.name.findName();
},
});

declare module 'ember-cli-mirage/types/registries/model' {
export default interface MirageModelRegistry {
'external-account': ExternalAccountsModel;
} // eslint-disable-line semi
}

declare module 'ember-cli-mirage/types/registries/schema' {
export default interface MirageSchemaRegistry {
externalAccounts: ExternalAccountsModel;
} // eslint-disable-line semi
}
22 changes: 22 additions & 0 deletions mirage/factories/node-addon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Factory } from 'ember-cli-mirage';

import NodeAddonModel from 'ember-osf-web/models/node-addon';

export default Factory.extend<NodeAddonModel>({
nodeHasAuth: false,
configured: false,
folderId: null,
folderPath: null,
});

declare module 'ember-cli-mirage/types/registries/model' {
export default interface MirageModelRegistry {
'node-addon': NodeAddonModel;
} // eslint-disable-line semi
}

declare module 'ember-cli-mirage/types/registries/schema' {
export default interface MirageSchemaRegistry {
nodeAddons: NodeAddonModel;
} // eslint-disable-line semi
}
19 changes: 19 additions & 0 deletions mirage/factories/user-addon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Factory } from 'ember-cli-mirage';

import UserAddonModel from 'ember-osf-web/models/user-addon';

export default Factory.extend<UserAddonModel>({
userHasAuth: true,
});

declare module 'ember-cli-mirage/types/registries/model' {
export default interface MirageModelRegistry {
'user-addon': UserAddonModel;
} // eslint-disable-line semi
}

declare module 'ember-cli-mirage/types/registries/schema' {
export default interface MirageSchemaRegistry {
userAddons: UserAddonModel;
} // eslint-disable-line semi
}
93 changes: 93 additions & 0 deletions mirage/fixtures/addons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
export default [
{
id: 'box',
name: 'Box',
categories: [
'storage',
],
},
{
id: 'dataverse',
name: 'Dataverse',
categories: [
'storage',
],
},
{
id: 'dropbox',
name: 'Dropbox',
categories: [
'storage',
],
},
{
id: 'figshare',
name: 'figshare',
categories: [
'storage',
],
},
{
id: 'github',
name: 'GitHub',
categories: [
'storage',
],
},
{
id: 'gitlab',
name: 'GitLab',
categories: [
'storage',
],
},
{
id: 'mendeley',
name: 'Mendeley',
categories: [
'citations',
],
},
{
id: 'zotero',
name: 'Zotero',
categories: [
'citations',
],
},
{
id: 'owncloud',
name: 'ownCloud',
categories: [
'storage',
],
},
{
id: 'onedrive',
name: 'OneDrive',
categories: [
'storage',
],
},
{
id: 's3',
name: 'Amazon S3',
categories: [
'storage',
],
},
{
id: 'googledrive',
name: 'Google Drive',
categories: [
'storage',
],
},
{
id: 'bitbucket',
name: 'Bitbucket',
categories: [
'storage',
],
},
];
26 changes: 26 additions & 0 deletions mirage/scenarios/dashboard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ModelInstance, Server } from 'ember-cli-mirage';
import config from 'ember-osf-web/config/environment';
import AddonModel from 'ember-osf-web/models/addon';

import { Permission } from 'ember-osf-web/models/osf-model';
import User from 'ember-osf-web/models/user';
Expand Down Expand Up @@ -57,6 +58,31 @@ export function dashboardScenario(server: Server, currentUser: ModelInstance<Use
index: 0,
});

// Addons for filesNode
const dropbox = server.schema.addons.find('dropbox') as ModelInstance<AddonModel>;
const dropboxAccount = server.create('external-account', {
displayName: 'Bugs Bunny',
provider: dropbox,
});
const dropboxAccountTwo = server.create('external-account', {
displayName: 'Daffy Duck',
provider: dropbox,
});
server.create('user-addon', {
id: 'dropbox',
externalAccounts: [ dropboxAccount, dropboxAccountTwo ],
userHasAuth: true,
user: currentUser,
addon: dropbox,
});
server.create('node-addon', {
nodeHasAuth: true,
folderId: '/',
folderPath: '/',
externalAccount: dropboxAccount,
node: filesNode,
});

// NOTE: Some institutions are already created by this point
server.createList('institution', 20);
// Create a specific institution to test institutional dashboard with; should be ID 29 at this point
Expand Down
1 change: 1 addition & 0 deletions mirage/scenarios/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default function(server: Server) {
server.loadFixtures('regions');
server.loadFixtures('preprint-providers');
server.loadFixtures('licenses');
server.loadFixtures('addons');
// server.loadFixtures('registration-providers');

const userTraits = !mirageScenarios.includes('loggedIn') ? []
Expand Down
6 changes: 6 additions & 0 deletions mirage/serializers/addon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import AddonModel from 'ember-osf-web/models/addon';

import ApplicationSerializer from './application';

export default class AddonSerializer extends ApplicationSerializer<AddonModel> {
}
16 changes: 16 additions & 0 deletions mirage/serializers/external-account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ModelInstance } from 'ember-cli-mirage';
import config from 'ember-osf-web/config/environment';

import ExternalAccountsModel from 'ember-osf-web/models/external-accounts';

import ApplicationSerializer from './application';

const { OSF: { apiUrl } } = config;

export default class ExternalAccountSerializer extends ApplicationSerializer<ExternalAccountsModel> {
buildNormalLinks(model: ModelInstance<ExternalAccountsModel>) {
return {
self: `${apiUrl}/v2/users/me/addons/${model.provider}/accounts/${model.id}`,
};
}
}
Loading

0 comments on commit f8b33f1

Please sign in to comment.