Skip to content

Commit 8bb4568

Browse files
committed
Removed auditLogHelper.js and Printer.js would not create an audit log and skip the test if config/json.PRINTING is false.
1 parent fb31dd3 commit 8bb4568

File tree

3 files changed

+23
-69
lines changed

3 files changed

+23
-69
lines changed

api/main_endpoints/routes/Printer.js

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const {
2222
PRINTING = {}
2323
} = require('../../config/config.json');
2424
const AuditLogActions = require('../util/auditLogActions.js');
25-
const { createAuditLog } = require('../util/auditLogHelpers.js');
25+
const AuditLog = require('../models/AuditLog.js');
2626

2727
// see https://github.com/SCE-Development/Quasar/tree/dev/docker-compose.dev.yml#L11
2828
let PRINTER_URL = process.env.PRINTER_URL || 'http://localhost:14000';
@@ -62,7 +62,6 @@ router.get('/healthCheck', async (req, res) => {
6262
logger.warn(
6363
'Printing is disabled, returning 200 to mock the printing server'
6464
);
65-
6665
return res.sendStatus(OK);
6766
}
6867
await axios
@@ -79,8 +78,6 @@ router.get('/healthCheck', async (req, res) => {
7978

8079
router.post('/sendPrintRequest', upload.single('chunk'), async (req, res) => {
8180
let totalFileSize = 0;
82-
const { copies, sides, id } = req.body;
83-
const action = AuditLogActions.PRINT_PAGE;
8481

8582
if (!checkIfTokenSent(req)) {
8683
logger.warn('/sendPrintRequest was requested without a token');
@@ -91,29 +88,13 @@ router.post('/sendPrintRequest', upload.single('chunk'), async (req, res) => {
9188
logger.warn('/sendPrintRequest was requested with an invalid token');
9289
return res.sendStatus(UNAUTHORIZED);
9390
}
94-
95-
const details = {
96-
copies: parseInt(copies),
97-
sides,
98-
fileSize: totalFileSize,
99-
userEmail: user.email,
100-
printedAt: new Date(),
101-
printJobId: id,
102-
status: 'success' || 'fail'
103-
};
104-
10591
if (!PRINTING.ENABLED) {
106-
details.status = 'mocked';
107-
// create audit log on print
108-
await createAuditLog({
109-
user,
110-
action,
111-
details
112-
});
11392
logger.warn('Printing is disabled, returning 200 to mock the printing server');
11493
return res.sendStatus(OK);
11594
}
11695

96+
const { copies, sides, id } = req.body;
97+
11798
const chunks = await fs.promises.readdir(dir);
11899
const assembledPdfFromChunks = path.join(dir, id + '.pdf');
119100

@@ -145,13 +126,20 @@ router.post('/sendPrintRequest', upload.single('chunk'), async (req, res) => {
145126
}
146127
})
147128
.then( async () => {
148-
details.status = 'success';
129+
149130
// create audit log on print
150-
await createAuditLog({
151-
user,
152-
action,
153-
details
154-
});
131+
await AuditLog.create({
132+
userId: user._id,
133+
action: AuditLogActions.PRINT_PAGE,
134+
details: {
135+
copies: parseInt(copies),
136+
sides,
137+
fileSize: totalFileSize,
138+
userEmail: user.email,
139+
printedAt: new Date(),
140+
printJobId: id
141+
}
142+
}).catch(logger.error);
155143

156144
// delete file from temp folder after printing
157145
fs.unlink(file.path, (err) => {
@@ -160,10 +148,8 @@ router.post('/sendPrintRequest', upload.single('chunk'), async (req, res) => {
160148
}
161149
});
162150
res.sendStatus(OK);
163-
}).catch(async (err) => {
151+
}).catch((err) => {
164152
logger.error('/sendPrintRequest had an error: ', err);
165-
details.status = 'fail';
166-
await createAuditLog({ user, action, details });
167153
res.sendStatus(SERVER_ERROR);
168154
});
169155
});

api/main_endpoints/util/auditLogHelpers.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

test/api/Printer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
process.env.NODE_ENV = 'test';
2+
const { PRINTING = {} } = require('../../api/config/config.json');
23
const mongoose = require('mongoose');
34
const chai = require('chai');
45
const chaiHttp = require('chai-http');
@@ -168,6 +169,11 @@ describe('Printer', () => {
168169
});
169170

170171
it('Should create only one audit log in the database when the response status is 200', async () => {
172+
// Skip tests if printing is disabled
173+
if (!PRINTING.ENABLED) {
174+
return;
175+
}
176+
171177
const userId = new mongoose.Types.ObjectId();
172178
const user = new User({
173179
_id: userId,

0 commit comments

Comments
 (0)