Skip to content

Commit c31503b

Browse files
committed
test: add a push parsing test based on captured data
1 parent 53bf7a3 commit c31503b

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

src/proxy/routes/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import { processUrlPath, validGitRequest, getAllProxiedHosts } from './helper';
77
import { ProxyOptions } from 'express-http-proxy';
88

99
enum ActionType {
10-
1110
ALLOWED = 'Allowed',
12-
11+
1312
ERROR = 'Error',
14-
13+
1514
BLOCKED = 'Blocked',
1615
}
1716

test/fixtures/captured-push.bin

670 Bytes
Binary file not shown.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Captured push data
2+
3+
The captured-push.bin file contains a single captured push request used to test push file parsing. It was captured by adding the following to the `exec` function in _src/proxy/processors/push-action/parsePush.ts_:
4+
5+
```typescript
6+
fs.writeFileSync('./.tmp/captured-pack.bin', req.body);
7+
```
8+
9+
The push that was captured was generated by checking out the git proxy repository (a second copy) and running the following in its root directory to generate changes to commit:
10+
11+
```bash
12+
echo "New content for pack testing $(date)" > new-file.txt
13+
echo "# Updated README $(date)" >> README.md
14+
git add .
15+
git commit -m "test: test commit for pack capture $(date)"
16+
```
17+
18+
The commit was then pushed into a locally running copy of git proxy (where the committing user was added as a contributor to the git-proxy repo). The push is processed as normally and its body written to file by the above modification.

test/testParsePush.test.js

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const { expect } = require('chai');
22
const sinon = require('sinon');
33
const zlib = require('zlib');
44
const { createHash } = require('crypto');
5+
const fs = require('fs');
6+
const path = require('path');
57

68
const {
79
exec,
@@ -464,7 +466,7 @@ describe('parsePackFile', () => {
464466
expect(action.setCommit.calledOnceWith(oldCommit, newCommit)).to.be.true;
465467
});
466468

467-
it('should successfully parse a valid push request', async () => {
469+
it('should successfully parse a valid push request (simulated)', async () => {
468470
const oldCommit = 'a'.repeat(40);
469471
const newCommit = 'b'.repeat(40);
470472
const ref = 'refs/heads/main';
@@ -518,7 +520,61 @@ describe('parsePackFile', () => {
518520
});
519521
});
520522

521-
it('should successfully parse a valid multi-object push request', async () => {
523+
it('should successfully parse a valid push request (captured)', async () => {
524+
const oldCommit = '640bd00d63208466021143366adbc926824ba66f';
525+
const newCommit = '93ca160407a9660c5ef81b951892b7a9ab1c41ca';
526+
const ref = 'refs/heads/main';
527+
const numEntries = 4;
528+
const tree = 'e4dbd7b12566edee6840bf053b70a81897bcf9cd';
529+
const parent = '640bd00d63208466021143366adbc926824ba66f';
530+
const author = 'Kris West';
531+
const timestamp = '1758647093';
532+
const message = 'test: test commit for pack capture Tue Sep 23 18:04:53 BST 2025';
533+
534+
// see ../fixtures/captured-push.bin for details of how the content of this file were captured
535+
const capturedPushPath = path.join(__dirname, 'fixtures', 'captured-push.bin');
536+
537+
console.log(`Reading captured pack file from ${capturedPushPath}`);
538+
const pushBuffer = fs.readFileSync(capturedPushPath);
539+
console.log(`Got buffer length: ${pushBuffer.length}`);
540+
541+
req.body = pushBuffer;
542+
543+
const result = await exec(req, action);
544+
expect(result).to.equal(action);
545+
546+
// Check step and action properties
547+
const step = action.steps.find((s) => s.stepName === 'parsePackFile');
548+
expect(step).to.exist;
549+
expect(step.error).to.be.false;
550+
expect(step.errorMessage).to.be.null;
551+
552+
expect(action.branch).to.equal(ref);
553+
expect(action.setCommit.calledOnceWith(oldCommit, newCommit)).to.be.true;
554+
expect(action.commitFrom).to.equal(oldCommit);
555+
expect(action.commitTo).to.equal(newCommit);
556+
expect(action.user).to.equal(author);
557+
558+
// Check parsed commit data
559+
const commitMessages = action.commitData.map((commit) => commit.message);
560+
expect(action.commitData).to.be.an('array').with.lengthOf(1);
561+
expect(commitMessages[0]).to.equal(message);
562+
563+
const parsedCommit = action.commitData[0];
564+
expect(parsedCommit.tree).to.equal(tree);
565+
expect(parsedCommit.parent).to.equal(parent);
566+
expect(parsedCommit.author).to.equal(author);
567+
expect(parsedCommit.committer).to.equal(author);
568+
expect(parsedCommit.commitTimestamp).to.equal(timestamp);
569+
expect(parsedCommit.message).to.equal(message);
570+
expect(step.content.meta).to.deep.equal({
571+
sig: PACK_SIGNATURE,
572+
version: 2,
573+
entries: numEntries,
574+
});
575+
});
576+
577+
it('should successfully parse a valid multi-object push request (simulated)', async () => {
522578
const oldCommit = 'a'.repeat(40);
523579
const newCommit = 'b'.repeat(40);
524580
const ref = 'refs/heads/main';

0 commit comments

Comments
 (0)