refactor: Add mirage route handling for authed user sessions

This commit is contained in:
Susmitha Girumala committed 2021-01-21 16:43:11 -05:00
1 parent b43efc3b36
commit d168f1d459
5 files changed
+444 -436

No files matched your search

+10 -3
View File
@@ -325,14 +325,21 @@ export default function() {
this.get('/sessions', function ({ sessions }, { queryParams: { scope_id } }) {
// To simulate changes to `session.status` that may occur in the backend,
// we quietly randomize the value of the field on GET.
// To populate sessions for logged in user,
// update alternate sessions to auth user.
// But only if not in testing mode.
// In tests, we need deterministic statuses.
if (!Ember.testing) {
sessions.where(session => session.scopeId === scope_id)
.models
.forEach(session => session.update({
status: pickRandomStatusString()
}));
.forEach(session => {
session.update({
status: pickRandomStatusString()
});
if(session.id.split('-').pop() % 2) session.update({
userId: 'authenticateduser'
});
});
}
return sessions.where(session => session.scopeId === scope_id)
});
+2 -5
View File
@@ -1,4 +1,4 @@
import factory from '../generated/factories/target';
import factory from '../generated/factories/session';
import { trait } from 'ember-cli-mirage';
import { random } from 'faker';
@@ -26,10 +26,7 @@ export default factory.extend({
const scopeId = record.scopeId;
if (record.scope.type === 'project') {
const orgScopeId = record.scope?.scope.id;
const orgUser =
server.schema.users.where({ scopeId: orgScopeId }).models[0];
const mockedAuthPayloadUser = server.schema.users.findOrCreateBy({ description: 'authenticated-user' });
const user = random.arrayElement([orgUser, mockedAuthPayloadUser]);
const user = server.schema.users.where({ scopeId: orgScopeId }).models[0];
if (user) record.update({ user });
}
const target = server.schema.targets.where({ scopeId }).models[0];
+2 -6
View File
@@ -23,7 +23,7 @@ import { Response } from 'miragejs';
// }
// }
export function authHandler({ scopes, authMethods, users }, request) {
export function authHandler({ scopes, authMethods }, request) {
const payload = JSON.parse(request.requestBody);
if (payload.credentials.login_name === 'error') {
return new Response(400);
@@ -33,16 +33,12 @@ export function authHandler({ scopes, authMethods, users }, request) {
const scope = scopes.find(authMethod.scopeId);
const scopeAttrs =
this.serialize(scopes.find(scope.id));
// Create and tag an auth user to be used in auth payload.
const user = users.findOrCreateBy({
description: 'authenticated-user'
});
return new Response(200, {}, {
scope: scopeAttrs,
id: 'token123',
token: 'thetokenstring',
account_id: '1',
user_id: user.id,
user_id: 'authenticateduser',
auth_method_id: 'authmethod123',
created_time: '',
updated_time: '',
+2
View File
@@ -13,6 +13,8 @@ export default function(server) {
scope: { id: globalScope.id, type: globalScope.type }
}, 'withChildren')[0];
server.create('user', { id: 'authenticateduser' });
// Auth
server.createList('auth-method', 1, { scope: globalScope }, 'withAccountsAndUsers');
server.createList('auth-method', 3, { scope: orgScope }, 'withAccountsAndUsers');
+428 -422
View File
File diff suppressed because it is too large. Load diff