|
| 1 | +import * as aws4sign from 'aws4'; |
1 | 2 | import { expect } from 'chai'; |
| 3 | +import * as sinon from 'sinon'; |
2 | 4 |
|
3 | 5 | import { aws4Sign, type AwsSigv4Options } from '../../src/cmap/auth/aws4'; |
4 | 6 |
|
@@ -31,43 +33,49 @@ describe('Verify AWS4 signature generation', () => { |
31 | 33 | date |
32 | 34 | }; |
33 | 35 |
|
| 36 | + beforeEach(() => { |
| 37 | + sinon.stub(aws4sign.RequestSigner.prototype, 'getDateTime').returns('20251215T123456Z'); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + sinon.restore(); |
| 42 | + }); |
| 43 | + |
34 | 44 | it('should generate correct credentials for permanent credentials', async () => { |
35 | 45 | const headers = await aws4Sign(request, awsCredentials); |
36 | 46 |
|
| 47 | + // Verify generated headers |
37 | 48 | expect(headers['X-Amz-Date']).to.exist; |
38 | 49 | expect(headers['X-Amz-Date']).to.equal('20251215T123456Z'); |
39 | 50 | expect(headers['Authorization']).to.exist; |
40 | 51 | expect(headers['Authorization']).to.equal( |
41 | 52 | 'AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20251215/us-east-1/sts/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date;x-mongodb-gs2-cb-flag;x-mongodb-server-nonce, Signature=48a66f9fc76829002a7a7ac5b92e4089395d9b88ea7d417ab146949b90eeab08' |
42 | 53 | ); |
43 | 54 |
|
44 | | - // Uncomment the following lines if you want to compare with the old aws4 library. |
45 | | - // Remember to import aws4 at the top of the file, like this: import * as aws4sign from 'aws4'; |
46 | | - |
47 | | - // const oldSigned = aws4sign.sign(request, awsCredentials); |
48 | | - // expect(oldSigned.headers['X-Amz-Date']).to.exist; |
49 | | - // expect(oldSigned.headers['X-Amz-Date']).to.equal(signed.headers['X-Amz-Date']); |
50 | | - // expect(oldSigned.headers['Authorization']).to.exist; |
51 | | - // expect(oldSigned.headers['Authorization']).to.equal(signed.headers['Authorization']); |
| 55 | + // Verify against aws4 library |
| 56 | + const oldSigned = aws4sign.sign(request, awsCredentials); |
| 57 | + expect(oldSigned.headers['X-Amz-Date']).to.exist; |
| 58 | + expect(oldSigned.headers['X-Amz-Date']).to.equal(headers['X-Amz-Date']); |
| 59 | + expect(oldSigned.headers['Authorization']).to.exist; |
| 60 | + expect(oldSigned.headers['Authorization']).to.equal(headers['Authorization']); |
52 | 61 | }); |
53 | 62 |
|
54 | 63 | it('should generate correct credentials for session credentials', async () => { |
55 | 64 | const headers = await aws4Sign(request, awsSessionCredentials); |
56 | 65 |
|
| 66 | + // Verify generated headers |
57 | 67 | expect(headers['X-Amz-Date']).to.exist; |
58 | 68 | expect(headers['X-Amz-Date']).to.equal('20251215T123456Z'); |
59 | 69 | expect(headers['Authorization']).to.exist; |
60 | 70 | expect(headers['Authorization']).to.equal( |
61 | | - 'AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20251215/us-east-1/sts/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date;x-mongodb-gs2-cb-flag;x-mongodb-server-nonce, Signature=7bfe0c6c8c0aa9f853eb10c5822ab42446ad87789e5b6e47a6fbd7a9bffc834a' |
| 71 | + 'AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20251215/us-east-1/sts/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date;x-amz-security-token;x-mongodb-gs2-cb-flag;x-mongodb-server-nonce, Signature=bbcb06e2feb8651dced329789743ba283f92ef1302d34a7398cb1d35808a1a66' |
62 | 72 | ); |
63 | 73 |
|
64 | | - // Uncomment the following lines if you want to compare with the old aws4 library. |
65 | | - // Remember to import aws4 at the top of the file, like this: import * as aws4sign from 'aws4'; |
66 | | - |
67 | | - // const oldSigned = aws4sign.sign(request, awsSessionCredentials); |
68 | | - // expect(oldSigned.headers['X-Amz-Date']).to.exist; |
69 | | - // expect(oldSigned.headers['X-Amz-Date']).to.equal(signed.headers['X-Amz-Date']); |
70 | | - // expect(oldSigned.headers['Authorization']).to.exist; |
71 | | - // expect(oldSigned.headers['Authorization']).to.equal(signed.headers['Authorization']); |
| 74 | + // Verify against aws4 library |
| 75 | + const oldSigned = aws4sign.sign(request, awsSessionCredentials); |
| 76 | + expect(oldSigned.headers['X-Amz-Date']).to.exist; |
| 77 | + expect(oldSigned.headers['X-Amz-Date']).to.equal(headers['X-Amz-Date']); |
| 78 | + expect(oldSigned.headers['Authorization']).to.exist; |
| 79 | + expect(oldSigned.headers['Authorization']).to.equal(headers['Authorization']); |
72 | 80 | }); |
73 | 81 | }); |
0 commit comments