Skip to content

Commit d7df2f2

Browse files
test(cloudrun): correct unit tests
1 parent 6e4451d commit d7df2f2

1 file changed

Lines changed: 42 additions & 51 deletions

File tree

run/service-auth/test/receive.test.js

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,69 +14,60 @@
1414

1515
'use strict';
1616

17-
const {expect} = require('chai');
17+
const { expect } = require('chai');
1818
const sinon = require('sinon');
19-
const {OAuth2Client} = require('google-auth-library');
20-
const {main} = require('../receive');
19+
const { OAuth2Client } = require('google-auth-library');
20+
const { main } = require('../receive');
2121

22-
describe('receiveRequestAndParseAuthHeader sample', () => {
23-
let verifyStub, consoleStub;
24-
25-
beforeEach(() => {
26-
verifyStub = sinon.stub(OAuth2Client.prototype, 'verifyIdToken');
27-
consoleStub = sinon.stub(console, 'log');
28-
});
22+
const TEST_VALID_TOKEN = 'test-valid-token';
23+
const TEST_INVALID_TOKEN = 'test-invalid-token';
2924

30-
afterEach(() => {
31-
sinon.restore();
32-
});
25+
describe('receiveRequestAndParseAuthHeader sample', () => {
26+
let verifyStub, consoleStub;
3327

34-
it('should log greeting if token is valid', async () => {
35-
const mockReq = {
36-
headers: {
37-
authorization: 'Bearer valid-token',
38-
},
39-
};
28+
beforeEach(() => {
29+
verifyStub = sinon.stub(OAuth2Client.prototype, 'verifyIdToken');
30+
consoleStub = sinon.stub(console, 'log');
31+
});
4032

41-
verifyStub.resolves({
42-
getPayload: () => ({email: 'test@example.com'}),
33+
afterEach(() => {
34+
sinon.restore();
4335
});
4436

45-
await main(mockReq);
46-
expect(consoleStub.calledWith('Hello, test@example.com!\n')).to.be.true;
47-
});
37+
it('should log greeting if token is valid', async () => {
38+
const mockReq = {
39+
headers: {
40+
authorization: `Bearer ${TEST_VALID_TOKEN}`,
41+
},
42+
};
4843

49-
it('should log error message if token is invalid', async () => {
50-
const mockReq = {
51-
headers: {
52-
authorization: 'Bearer invalid-token',
53-
},
54-
};
44+
verifyStub.resolves({
45+
getPayload: () => ({ email: 'test@example.com' }),
46+
});
5547

56-
verifyStub.rejects(new Error('invalid'));
48+
await main(mockReq);
49+
expect(consoleStub.calledWith('Hello, test@example.com!\n')).to.be.true;
50+
});
5751

58-
await main(mockReq);
59-
expect(consoleStub.calledWithMatch(/Invalid token: invalid/)).to.be.true;
60-
});
52+
it('should log error message if token is invalid', async () => {
53+
const mockReq = {
54+
headers: {
55+
authorization: `Bearer ${TEST_INVALID_TOKEN}`,
56+
},
57+
};
6158

62-
it('should log anonymous message if no auth header', async () => {
63-
const mockReq = {
64-
headers: {},
65-
};
59+
verifyStub.rejects(new Error('invalid'));
6660

67-
await main(mockReq);
68-
expect(consoleStub.calledWith('Hello, anonymous user.\n')).to.be.true;
69-
});
61+
await main(mockReq);
62+
expect(consoleStub.calledWithMatch(/Invalid token: invalid/)).to.be.true;
63+
});
7064

71-
it('should log unhandled format message if auth is not bearer', async () => {
72-
const mockReq = {
73-
headers: {
74-
authorization: 'Basic something',
75-
},
76-
};
65+
it('should log anonymous message if no auth header', async () => {
66+
const mockReq = {
67+
headers: {},
68+
};
7769

78-
await main(mockReq);
79-
expect(consoleStub.calledWith('Unhandled header format (Basic).\n')).to.be
80-
.true;
81-
});
70+
await main(mockReq);
71+
expect(consoleStub.calledWith('Hello, anonymous user.\n')).to.be.true;
72+
});
8273
});

0 commit comments

Comments
 (0)