|
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 | 22 | const TEST_VALID_TOKEN = 'test-valid-token'; |
23 | 23 | const TEST_INVALID_TOKEN = 'test-invalid-token'; |
24 | 24 |
|
25 | 25 | describe('receiveRequestAndParseAuthHeader sample', () => { |
26 | | - let verifyStub, consoleStub; |
| 26 | + let verifyStub, consoleStub; |
27 | 27 |
|
28 | | - beforeEach(() => { |
29 | | - verifyStub = sinon.stub(OAuth2Client.prototype, 'verifyIdToken'); |
30 | | - consoleStub = sinon.stub(console, 'log'); |
31 | | - }); |
32 | | - |
33 | | - afterEach(() => { |
34 | | - sinon.restore(); |
35 | | - }); |
| 28 | + beforeEach(() => { |
| 29 | + verifyStub = sinon.stub(OAuth2Client.prototype, 'verifyIdToken'); |
| 30 | + consoleStub = sinon.stub(console, 'log'); |
| 31 | + }); |
36 | 32 |
|
37 | | - it('should log greeting if token is valid', async () => { |
38 | | - const mockReq = { |
39 | | - headers: { |
40 | | - authorization: `Bearer ${TEST_VALID_TOKEN}`, |
41 | | - }, |
42 | | - }; |
| 33 | + afterEach(() => { |
| 34 | + sinon.restore(); |
| 35 | + }); |
43 | 36 |
|
44 | | - verifyStub.resolves({ |
45 | | - getPayload: () => ({ email: 'test@example.com' }), |
46 | | - }); |
| 37 | + it('should log greeting if token is valid', async () => { |
| 38 | + const mockReq = { |
| 39 | + headers: { |
| 40 | + authorization: `Bearer ${TEST_VALID_TOKEN}`, |
| 41 | + }, |
| 42 | + }; |
47 | 43 |
|
48 | | - await main(mockReq); |
49 | | - expect(consoleStub.calledWith('Hello, test@example.com!\n')).to.be.true; |
| 44 | + verifyStub.resolves({ |
| 45 | + getPayload: () => ({email: 'test@example.com'}), |
50 | 46 | }); |
51 | 47 |
|
52 | | - it('should log error message if token is invalid', async () => { |
53 | | - const mockReq = { |
54 | | - headers: { |
55 | | - authorization: `Bearer ${TEST_INVALID_TOKEN}`, |
56 | | - }, |
57 | | - }; |
| 48 | + await main(mockReq); |
| 49 | + expect(consoleStub.calledWith('Hello, test@example.com!\n')).to.be.true; |
| 50 | + }); |
58 | 51 |
|
59 | | - verifyStub.rejects(new Error('invalid')); |
| 52 | + it('should log error message if token is invalid', async () => { |
| 53 | + const mockReq = { |
| 54 | + headers: { |
| 55 | + authorization: `Bearer ${TEST_INVALID_TOKEN}`, |
| 56 | + }, |
| 57 | + }; |
60 | 58 |
|
61 | | - await main(mockReq); |
62 | | - expect(consoleStub.calledWithMatch(/Invalid token: invalid/)).to.be.true; |
63 | | - }); |
| 59 | + verifyStub.rejects(new Error('invalid')); |
64 | 60 |
|
65 | | - it('should log anonymous message if no auth header', async () => { |
66 | | - const mockReq = { |
67 | | - headers: {}, |
68 | | - }; |
| 61 | + await main(mockReq); |
| 62 | + expect(consoleStub.calledWithMatch(/Invalid token: invalid/)).to.be.true; |
| 63 | + }); |
69 | 64 |
|
70 | | - await main(mockReq); |
71 | | - expect(consoleStub.calledWith('Hello, anonymous user.\n')).to.be.true; |
72 | | - }); |
| 65 | + it('should log anonymous message if no auth header', async () => { |
| 66 | + const mockReq = { |
| 67 | + headers: {}, |
| 68 | + }; |
| 69 | + |
| 70 | + await main(mockReq); |
| 71 | + expect(consoleStub.calledWith('Hello, anonymous user.\n')).to.be.true; |
| 72 | + }); |
73 | 73 | }); |
0 commit comments