|
14 | 14 |
|
15 | 15 | 'use strict'; |
16 | 16 |
|
17 | | -const {expect} = require('chai'); |
| 17 | +const { expect } = require('chai'); |
18 | 18 | 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'); |
21 | 21 |
|
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'; |
29 | 24 |
|
30 | | - afterEach(() => { |
31 | | - sinon.restore(); |
32 | | - }); |
| 25 | +describe('receiveRequestAndParseAuthHeader sample', () => { |
| 26 | + let verifyStub, consoleStub; |
33 | 27 |
|
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 | + }); |
40 | 32 |
|
41 | | - verifyStub.resolves({ |
42 | | - getPayload: () => ({email: 'test@example.com'}), |
| 33 | + afterEach(() => { |
| 34 | + sinon.restore(); |
43 | 35 | }); |
44 | 36 |
|
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 | + }; |
48 | 43 |
|
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 | + }); |
55 | 47 |
|
56 | | - verifyStub.rejects(new Error('invalid')); |
| 48 | + await main(mockReq); |
| 49 | + expect(consoleStub.calledWith('Hello, test@example.com!\n')).to.be.true; |
| 50 | + }); |
57 | 51 |
|
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 | + }; |
61 | 58 |
|
62 | | - it('should log anonymous message if no auth header', async () => { |
63 | | - const mockReq = { |
64 | | - headers: {}, |
65 | | - }; |
| 59 | + verifyStub.rejects(new Error('invalid')); |
66 | 60 |
|
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 | + }); |
70 | 64 |
|
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 | + }; |
77 | 69 |
|
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 | + }); |
82 | 73 | }); |
0 commit comments