Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/lib/ParseApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,12 +585,22 @@ export default class ParseApp {
});
}

runJob(job) {
async runJob(job) {
let description = 'Executing from job schedule web console.';
try {
const response = await fetch(window.PARSE_DASHBOARD_PATH || '/');
const dashboardUser = response.headers.get('username');
if (dashboardUser) {
description = `Executing from job schedule web console by ${dashboardUser}.`;
}
} catch {
// If the dashboard user cannot be determined, fall back to the default description.
}
Comment on lines +589 to +598
return Parse._request(
'POST',
'jobs',
{
description: 'Executing from job schedule web console.',
description,
input: JSON.parse(job.params || '{}'),
jobName: job.jobName,
when: 0,
Expand Down
61 changes: 61 additions & 0 deletions src/lib/tests/ParseApp.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @jest-environment jsdom
*/
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
jest.dontMock('../ParseApp');

const Parse = require('parse');
const ParseApp = require('../ParseApp').default;

function newApp() {
return new ParseApp({
appName: 'test',
appId: 'appId',
supportedPushLocales: [],
});
}

function mockFetchWithUser(username) {
global.fetch = jest.fn(() =>
Promise.resolve({
headers: { get: header => (header === 'username' ? username : null) },
})
);
}

describe('ParseApp.runJob', () => {
beforeEach(() => {
Parse._request = jest.fn(() => Promise.resolve({}));
window.PARSE_DASHBOARD_PATH = '/';
});
Comment on lines +32 to +36

it('includes the dashboard user in the description when available', async () => {
mockFetchWithUser('alice');
await newApp().runJob({ jobName: 'myJob', params: '{}' });
expect(Parse._request.mock.calls[0][2].description).toBe(
'Executing from job schedule web console by alice.'
);
});

it('falls back to the default description when no dashboard user is set', async () => {
mockFetchWithUser(null);
await newApp().runJob({ jobName: 'myJob', params: '{}' });
expect(Parse._request.mock.calls[0][2].description).toBe(
'Executing from job schedule web console.'
);
});

it('falls back to the default description when the user lookup fails', async () => {
global.fetch = jest.fn(() => Promise.reject(new Error('network error')));
await newApp().runJob({ jobName: 'myJob', params: '{}' });
expect(Parse._request.mock.calls[0][2].description).toBe(
'Executing from job schedule web console.'
);
});
});