|
| 1 | +/* |
| 2 | + * Copyright 2025 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +// @ts-nocheck |
| 14 | + |
| 15 | +import assert from 'node:assert'; |
| 16 | +import { DEFAULT_CONTEXT } from '../../fixtures/context.js'; |
| 17 | +import handler from '../../../src/routes/auth/update.js'; |
| 18 | + |
| 19 | +describe('routes/auth update tests', () => { |
| 20 | + it('should reject invalid token', async () => { |
| 21 | + const ctx = DEFAULT_CONTEXT({ |
| 22 | + data: { token: null }, |
| 23 | + env: { |
| 24 | + KEYS: { |
| 25 | + // @ts-ignore |
| 26 | + get: async () => 'test-key', |
| 27 | + }, |
| 28 | + }, |
| 29 | + }); |
| 30 | + const resp = await handler(ctx); |
| 31 | + assert.equal(resp.status, 400); |
| 32 | + assert.equal(resp.headers.get('x-error'), 'missing or invalid token'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should update token', async () => { |
| 36 | + const ctx = DEFAULT_CONTEXT({ |
| 37 | + data: { token: 'new-key' }, |
| 38 | + env: { |
| 39 | + KEYS: { |
| 40 | + get: async () => 'test-key', |
| 41 | + put: async () => undefined, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }); |
| 45 | + const resp = await handler(ctx); |
| 46 | + assert.equal(resp.status, 200); |
| 47 | + assert.equal(resp.headers.get('Content-Type'), 'application/json'); |
| 48 | + const { token } = await resp.json(); |
| 49 | + assert.equal(token, 'new-key'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should handle errors during token put', async () => { |
| 53 | + const ctx = DEFAULT_CONTEXT({ |
| 54 | + data: { token: 'new-key' }, |
| 55 | + env: { |
| 56 | + KEYS: { |
| 57 | + get: async () => 'test-key', |
| 58 | + put: async () => { throw Error('bad'); }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + let e; |
| 64 | + try { |
| 65 | + await handler(ctx); |
| 66 | + } catch (ee) { |
| 67 | + e = ee; |
| 68 | + } |
| 69 | + assert.equal(e.response.status, 503); |
| 70 | + assert.equal(e.response.headers.get('x-error'), 'failed to update token'); |
| 71 | + }); |
| 72 | +}); |
0 commit comments