Skip to content

Commit

Permalink
remove server abstraction layer
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarf committed May 29, 2024
1 parent ae90bce commit 8311258
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 237 deletions.
4 changes: 3 additions & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const transactionRoutes = require('./routes/transactions');
const automationSchedulerRoutes = require('./routes/automation');
const path = require('path');
const apolloServer = require('./graphql-server');
const setupMockAutomationSchedulerServer = require('./tests/util/mock-automation-scheduler-server');
const {
setupMockAutomationSchedulerServer
} = require('./tests/util/mock-automation-scheduler-server');
const transactionMiddleware = require('./middleware/transactionMiddleware');
const app = express();

Expand Down
3 changes: 0 additions & 3 deletions server/controllers/AutomationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ const getGraphQLContext = require('../graphql-context');
const httpAgent = new http.Agent({ family: 4 });

const axiosConfig = {
headers: {
'x-automation-secret': process.env.AUTOMATION_SCHEDULER_SECRET
},
timeout: 1000,
httpAgent
};
Expand Down
19 changes: 14 additions & 5 deletions server/middleware/verifyAutomationScheduler.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
const verifyAutomationScheduler = (req, res, next) => {
const {
getCollectionJobById
} = require('../models/services/CollectionJobService');

const verifyAutomationScheduler = async (req, res, next) => {
const incomingSecret = req.headers['x-automation-secret'];
const { jobId: id } = req.params;

if (!id) return res.status(404).json({ error: 'unknown jobId param' });
const job = await getCollectionJobById(id);
if (!job) return res.status(404).json({ error: `Unknown job ${id}` });

// store the collection job on the request to avoid a second lookup
req.collectionJob = job;

if (
incomingSecret &&
incomingSecret === process.env.AUTOMATION_SCHEDULER_SECRET
) {
if (incomingSecret && incomingSecret === job.secret) {
next();
} else {
res.status(403).json({ error: 'Unauthorized' });
Expand Down
26 changes: 26 additions & 0 deletions server/migrations/20240525041559-addCollectionJobSecret.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
return queryInterface.sequelize.transaction(async transaction => {
await queryInterface.addColumn(
'CollectionJob',
'secret',
{
type: Sequelize.DataTypes.UUID,
allowNull: false,
defaultValue: Sequelize.literal('gen_random_uuid()')
},
{ transaction }
);
});
},
async down(queryInterface) {
return queryInterface.sequelize.transaction(async transaction => {
await queryInterface.removeColumn('CollectionJob', 'secret', {
transaction
});
});
}
};
5 changes: 5 additions & 0 deletions server/models/CollectionJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ module.exports = function (sequelize, DataTypes) {
onDelete: 'SET NULL',
allowNull: true,
unique: true
},
secret: {
type: DataTypes.UUID,
allowNull: false,
defaultValue: sequelize.literal('gen_random_uuid()')
}
},
{
Expand Down
20 changes: 8 additions & 12 deletions server/models/services/CollectionJobService.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ const {
const { getTestPlanReportById } = require('./TestPlanReportService');
const { HttpQueryError } = require('apollo-server-core');
const { default: axios } = require('axios');

const {
default: createGithubWorkflow,
isEnabled: isGithubWorkflowEnabled
} = require('../../services/GithubWorkflowService');

const {
startCollectionJobSimulation
} = require('../../tests/util/mock-automation-scheduler-server');

const runnableTestsResolver = require('../../resolvers/TestPlanReport/runnableTestsResolver');
const getGraphQLContext = require('../../graphql-context');
const { getBotUserByAtId } = require('./UserService');
Expand Down Expand Up @@ -255,7 +261,7 @@ const createCollectionJob = async ({
})),
transaction
});

console.log('created job', collectionJobResult, transaction.id);
return ModelService.getById(CollectionJob, {
id: collectionJobResult.id,
attributes: collectionJobAttributes,
Expand Down Expand Up @@ -402,17 +408,7 @@ const triggerWorkflow = async (job, testIds, { transaction }) => {
// TODO: pass the reduced list of testIds along / deal with them somehow
await createGithubWorkflow({ job, directory, gitSha });
} else {
await axios.post(
`${process.env.AUTOMATION_SCHEDULER_URL}/jobs/new`,
{
testPlanVersionGitSha: gitSha,
testIds,
testPlanName: directory,
jobId: job.id,
transactionId: transaction.id
},
axiosConfig
);
await startCollectionJobSimulation(job, transaction);
}
} catch (error) {
console.error(error);
Expand Down
2 changes: 1 addition & 1 deletion server/services/GithubWorkflowService.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const createGithubWorkflow = async ({ job, directory, gitSha }) => {
const inputs = {
callback_url: `https://${callbackUrlHostname}/api/jobs/${job.id}/test/:testRowNumber`,
status_url: `https://${callbackUrlHostname}/api/jobs/${job.id}`,
callback_header: `x-automation-secret:${process.env.AUTOMATION_SCHEDULER_SECRET}`,
callback_header: `x-automation-secret:${job.secret}`,
work_dir: `tests/${directory}`,
aria_at_ref: gitSha
};
Expand Down
11 changes: 8 additions & 3 deletions server/tests/integration/automation-scheduler.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const startSupertestServer = require('../util/api-server');
const automationRoutes = require('../../routes/automation');
const setupMockAutomationSchedulerServer = require('../util/mock-automation-scheduler-server');
const {
setupMockAutomationSchedulerServer
} = require('../util/mock-automation-scheduler-server');
const db = require('../../models/index');
const { query, mutate } = require('../util/graphql-test-utilities');
const dbCleaner = require('../util/db-cleaner');
Expand All @@ -18,7 +20,7 @@ let mockAutomationSchedulerServer;
let apiServer;
let sessionAgent;

const testPlanReportId = '4';
const testPlanReportId = '18';

beforeAll(async () => {
apiServer = await startSupertestServer({
Expand Down Expand Up @@ -495,9 +497,12 @@ describe('Automation controller', () => {
const testResultsNumber =
collectionJob.testPlanRun.testResults.length;
const selectedTestIndex = 0;
const selectedTestRowNumber = 1;

const selectedTest = tests[selectedTestIndex];
const selectedTestRowNumber = selectedTest.rowNumber;
console.log(tests);
console.log(tests.map(({ rowNumber }) => rowNumber));

const numberOfScenarios = selectedTest.scenarios.filter(
scenario => scenario.atId === at.id
).length;
Expand Down
Loading

0 comments on commit 8311258

Please sign in to comment.