|
4 | 4 | * you may not use this file except in compliance with the Elastic License. |
5 | 5 | */ |
6 | 6 |
|
7 | | -import { coreMock } from 'src/core/server/mocks'; |
8 | | -import { registerLimitedConcurrencyRoutes } from './limited_concurrency'; |
| 7 | +import { coreMock, httpServerMock, httpServiceMock } from 'src/core/server/mocks'; |
| 8 | +import { |
| 9 | + createLimitedPreAuthHandler, |
| 10 | + isLimitedRoute, |
| 11 | + registerLimitedConcurrencyRoutes, |
| 12 | +} from './limited_concurrency'; |
9 | 13 | import { IngestManagerConfigType } from '../index'; |
10 | 14 |
|
11 | 15 | describe('registerLimitedConcurrencyRoutes', () => { |
@@ -33,3 +37,183 @@ describe('registerLimitedConcurrencyRoutes', () => { |
33 | 37 | expect(mockSetup.http.registerOnPreAuth).toHaveBeenCalledTimes(1); |
34 | 38 | }); |
35 | 39 | }); |
| 40 | + |
| 41 | +// assertions for calls to .decrease are commented out because it's called on the |
| 42 | +// "req.events.aborted$ observable (which) will never emit from a mocked request in a jest unit test environment" |
| 43 | +// https://github.com/elastic/kibana/pull/72338#issuecomment-661908791 |
| 44 | +describe('preAuthHandler', () => { |
| 45 | + test(`ignores routes when !isMatch`, async () => { |
| 46 | + const mockMaxCounter = { |
| 47 | + increase: jest.fn(), |
| 48 | + decrease: jest.fn(), |
| 49 | + lessThanMax: jest.fn(), |
| 50 | + }; |
| 51 | + const preAuthHandler = createLimitedPreAuthHandler({ |
| 52 | + isMatch: jest.fn().mockImplementation(() => false), |
| 53 | + maxCounter: mockMaxCounter, |
| 54 | + }); |
| 55 | + |
| 56 | + const mockRequest = httpServerMock.createKibanaRequest({ |
| 57 | + path: '/no/match', |
| 58 | + }); |
| 59 | + const mockResponse = httpServerMock.createResponseFactory(); |
| 60 | + const mockPreAuthToolkit = httpServiceMock.createOnPreAuthToolkit(); |
| 61 | + |
| 62 | + await preAuthHandler(mockRequest, mockResponse, mockPreAuthToolkit); |
| 63 | + |
| 64 | + expect(mockMaxCounter.increase).not.toHaveBeenCalled(); |
| 65 | + expect(mockMaxCounter.decrease).not.toHaveBeenCalled(); |
| 66 | + expect(mockMaxCounter.lessThanMax).not.toHaveBeenCalled(); |
| 67 | + expect(mockPreAuthToolkit.next).toHaveBeenCalledTimes(1); |
| 68 | + }); |
| 69 | + |
| 70 | + test(`ignores routes which don't have the correct tag`, async () => { |
| 71 | + const mockMaxCounter = { |
| 72 | + increase: jest.fn(), |
| 73 | + decrease: jest.fn(), |
| 74 | + lessThanMax: jest.fn(), |
| 75 | + }; |
| 76 | + const preAuthHandler = createLimitedPreAuthHandler({ |
| 77 | + isMatch: isLimitedRoute, |
| 78 | + maxCounter: mockMaxCounter, |
| 79 | + }); |
| 80 | + |
| 81 | + const mockRequest = httpServerMock.createKibanaRequest({ |
| 82 | + path: '/no/match', |
| 83 | + }); |
| 84 | + const mockResponse = httpServerMock.createResponseFactory(); |
| 85 | + const mockPreAuthToolkit = httpServiceMock.createOnPreAuthToolkit(); |
| 86 | + |
| 87 | + await preAuthHandler(mockRequest, mockResponse, mockPreAuthToolkit); |
| 88 | + |
| 89 | + expect(mockMaxCounter.increase).not.toHaveBeenCalled(); |
| 90 | + expect(mockMaxCounter.decrease).not.toHaveBeenCalled(); |
| 91 | + expect(mockMaxCounter.lessThanMax).not.toHaveBeenCalled(); |
| 92 | + expect(mockPreAuthToolkit.next).toHaveBeenCalledTimes(1); |
| 93 | + }); |
| 94 | + |
| 95 | + test(`processes routes which have the correct tag`, async () => { |
| 96 | + const mockMaxCounter = { |
| 97 | + increase: jest.fn(), |
| 98 | + decrease: jest.fn(), |
| 99 | + lessThanMax: jest.fn().mockImplementation(() => true), |
| 100 | + }; |
| 101 | + const preAuthHandler = createLimitedPreAuthHandler({ |
| 102 | + isMatch: isLimitedRoute, |
| 103 | + maxCounter: mockMaxCounter, |
| 104 | + }); |
| 105 | + |
| 106 | + const mockRequest = httpServerMock.createKibanaRequest({ |
| 107 | + path: '/should/match', |
| 108 | + routeTags: ['ingest:limited-concurrency'], |
| 109 | + }); |
| 110 | + const mockResponse = httpServerMock.createResponseFactory(); |
| 111 | + const mockPreAuthToolkit = httpServiceMock.createOnPreAuthToolkit(); |
| 112 | + |
| 113 | + await preAuthHandler(mockRequest, mockResponse, mockPreAuthToolkit); |
| 114 | + |
| 115 | + // will call lessThanMax because isMatch succeeds |
| 116 | + expect(mockMaxCounter.lessThanMax).toHaveBeenCalledTimes(1); |
| 117 | + // will not error because lessThanMax is true |
| 118 | + expect(mockResponse.customError).not.toHaveBeenCalled(); |
| 119 | + expect(mockPreAuthToolkit.next).toHaveBeenCalledTimes(1); |
| 120 | + }); |
| 121 | + |
| 122 | + test(`updates the counter when isMatch & lessThanMax`, async () => { |
| 123 | + const mockMaxCounter = { |
| 124 | + increase: jest.fn(), |
| 125 | + decrease: jest.fn(), |
| 126 | + lessThanMax: jest.fn().mockImplementation(() => true), |
| 127 | + }; |
| 128 | + const preAuthHandler = createLimitedPreAuthHandler({ |
| 129 | + isMatch: jest.fn().mockImplementation(() => true), |
| 130 | + maxCounter: mockMaxCounter, |
| 131 | + }); |
| 132 | + |
| 133 | + const mockRequest = httpServerMock.createKibanaRequest(); |
| 134 | + const mockResponse = httpServerMock.createResponseFactory(); |
| 135 | + const mockPreAuthToolkit = httpServiceMock.createOnPreAuthToolkit(); |
| 136 | + |
| 137 | + await preAuthHandler(mockRequest, mockResponse, mockPreAuthToolkit); |
| 138 | + |
| 139 | + expect(mockMaxCounter.increase).toHaveBeenCalled(); |
| 140 | + // expect(mockMaxCounter.decrease).toHaveBeenCalled(); |
| 141 | + expect(mockPreAuthToolkit.next).toHaveBeenCalledTimes(1); |
| 142 | + }); |
| 143 | + |
| 144 | + test(`lessThanMax ? next : error`, async () => { |
| 145 | + const mockMaxCounter = { |
| 146 | + increase: jest.fn(), |
| 147 | + decrease: jest.fn(), |
| 148 | + lessThanMax: jest |
| 149 | + .fn() |
| 150 | + // call 1 |
| 151 | + .mockImplementationOnce(() => true) |
| 152 | + // calls 2, 3, 4 |
| 153 | + .mockImplementationOnce(() => false) |
| 154 | + .mockImplementationOnce(() => false) |
| 155 | + .mockImplementationOnce(() => false) |
| 156 | + // calls 5+ |
| 157 | + .mockImplementationOnce(() => true) |
| 158 | + .mockImplementation(() => true), |
| 159 | + }; |
| 160 | + |
| 161 | + const preAuthHandler = createLimitedPreAuthHandler({ |
| 162 | + isMatch: isLimitedRoute, |
| 163 | + maxCounter: mockMaxCounter, |
| 164 | + }); |
| 165 | + |
| 166 | + function makeRequestExpectNext() { |
| 167 | + const request = httpServerMock.createKibanaRequest({ |
| 168 | + path: '/should/match/', |
| 169 | + routeTags: ['ingest:limited-concurrency'], |
| 170 | + }); |
| 171 | + const response = httpServerMock.createResponseFactory(); |
| 172 | + const toolkit = httpServiceMock.createOnPreAuthToolkit(); |
| 173 | + |
| 174 | + preAuthHandler(request, response, toolkit); |
| 175 | + expect(toolkit.next).toHaveBeenCalledTimes(1); |
| 176 | + expect(response.customError).not.toHaveBeenCalled(); |
| 177 | + } |
| 178 | + |
| 179 | + function makeRequestExpectError() { |
| 180 | + const request = httpServerMock.createKibanaRequest({ |
| 181 | + path: '/should/match/', |
| 182 | + routeTags: ['ingest:limited-concurrency'], |
| 183 | + }); |
| 184 | + const response = httpServerMock.createResponseFactory(); |
| 185 | + const toolkit = httpServiceMock.createOnPreAuthToolkit(); |
| 186 | + |
| 187 | + preAuthHandler(request, response, toolkit); |
| 188 | + expect(toolkit.next).not.toHaveBeenCalled(); |
| 189 | + expect(response.customError).toHaveBeenCalledTimes(1); |
| 190 | + expect(response.customError).toHaveBeenCalledWith({ |
| 191 | + statusCode: 429, |
| 192 | + body: 'Too Many Requests', |
| 193 | + }); |
| 194 | + } |
| 195 | + |
| 196 | + // request 1 succeeds |
| 197 | + makeRequestExpectNext(); |
| 198 | + expect(mockMaxCounter.increase).toHaveBeenCalledTimes(1); |
| 199 | + // expect(mockMaxCounter.decrease).toHaveBeenCalledTimes(1); |
| 200 | + |
| 201 | + // requests 2, 3, 4 fail |
| 202 | + makeRequestExpectError(); |
| 203 | + makeRequestExpectError(); |
| 204 | + makeRequestExpectError(); |
| 205 | + |
| 206 | + // requests 5+ succeed |
| 207 | + makeRequestExpectNext(); |
| 208 | + expect(mockMaxCounter.increase).toHaveBeenCalledTimes(2); |
| 209 | + // expect(mockMaxCounter.decrease).toHaveBeenCalledTimes(2); |
| 210 | + |
| 211 | + makeRequestExpectNext(); |
| 212 | + expect(mockMaxCounter.increase).toHaveBeenCalledTimes(3); |
| 213 | + // expect(mockMaxCounter.decrease).toHaveBeenCalledTimes(3); |
| 214 | + |
| 215 | + makeRequestExpectNext(); |
| 216 | + expect(mockMaxCounter.increase).toHaveBeenCalledTimes(4); |
| 217 | + // expect(mockMaxCounter.decrease).toHaveBeenCalledTimes(4); |
| 218 | + }); |
| 219 | +}); |
0 commit comments