refactor: Use mirage created user in auth payload
Using mirage created user in auth payload allows for sessions for authenticated user to be created.
This commit is contained in:
4 files changed
+8
-34
No files matched your search
@@ -10,10 +10,7 @@ export default factory.extend({
|
||||
withAccountsAndUsers: trait({
|
||||
afterCreate(authMethod, server) {
|
||||
const { scope } = authMethod;
|
||||
const users = server.createList('user', 5, { scope });
|
||||
// Create authorized user: user123
|
||||
users.push(server.create('user', { scope, id: 'user123' }));
|
||||
users.map(user => {
|
||||
server.createList('user', 5, { scope }).map(user => {
|
||||
const { id } = server.create('account', { scope, authMethod });
|
||||
user.update({ accountIds: [id] });
|
||||
});
|
||||
|
||||
@@ -26,8 +26,10 @@ export default factory.extend({
|
||||
const scopeId = record.scopeId;
|
||||
if (record.scope.type === 'project') {
|
||||
const orgScopeId = record.scope?.scope.id;
|
||||
const user =
|
||||
const orgUser =
|
||||
server.schema.users.where({ scopeId: orgScopeId }).models[0];
|
||||
const authenticatedUser = server.schema.users.first();
|
||||
const user = random.arrayElement([orgUser, authenticatedUser]);
|
||||
if (user) record.update({ user });
|
||||
}
|
||||
const target = server.schema.targets.where({ scopeId }).models[0];
|
||||
@@ -41,29 +43,6 @@ export default factory.extend({
|
||||
}
|
||||
}
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Sessions generated with this trait will automatically form
|
||||
* associations to target, host set, and host (if available) within
|
||||
* the current scope and for specified user.
|
||||
*/
|
||||
withUserAssociations: trait({
|
||||
afterCreate(record, server) {
|
||||
const scopeId = record.scopeId;
|
||||
const user = record.user;
|
||||
record.update({ user });
|
||||
const target = server.schema.targets.where({ scopeId }).models[0];
|
||||
if (target) {
|
||||
record.update({ target });
|
||||
const hostSet = target.hostSets.models[0];
|
||||
if (hostSet) {
|
||||
record.update({ hostSet });
|
||||
const host = hostSet.hosts.models[0];
|
||||
if (host) record.update({ host });
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
});
|
||||
@@ -23,7 +23,7 @@ import { Response } from 'miragejs';
|
||||
// }
|
||||
// }
|
||||
|
||||
export function authHandler({ scopes, authMethods }, request) {
|
||||
export function authHandler({ scopes, authMethods, users }, request) {
|
||||
const payload = JSON.parse(request.requestBody);
|
||||
if (payload.credentials.login_name === 'error') {
|
||||
return new Response(400);
|
||||
@@ -33,12 +33,13 @@ export function authHandler({ scopes, authMethods }, request) {
|
||||
const scope = scopes.find(authMethod.scopeId);
|
||||
const scopeAttrs =
|
||||
this.serialize(scopes.find(scope.id));
|
||||
const user = users.first();
|
||||
return new Response(200, {}, {
|
||||
scope: scopeAttrs,
|
||||
id: 'token123',
|
||||
token: 'thetokenstring',
|
||||
account_id: '1',
|
||||
user_id: 'user123',
|
||||
user_id: user.id,
|
||||
auth_method_id: 'authmethod123',
|
||||
created_time: '',
|
||||
updated_time: '',
|
||||
|
||||
@@ -29,9 +29,6 @@ export default function(server) {
|
||||
server.createList('host-catalog', 2, { scope }, 'withChildren');
|
||||
server.createList('target', 2, { scope }, 'withRandomHostSets');
|
||||
server.createList('session', 4, { scope }, 'withAssociations');
|
||||
// Create sessions for all users
|
||||
server.schema.users.where({ scopeId: scope.scope.id }).models.forEach(user =>
|
||||
server.createList('session', 4, { scope, user }, 'withUserAssociations')
|
||||
);
|
||||
server.createList('session', 4, { scope }, 'withAssociations');
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user