|
| 1 | +import { expect, test, describe } from 'vitest' |
| 2 | +import { build } from '../src/server/app.js' |
| 3 | +import { TEST_CONNECTION_STRING } from './lib/utils.js' |
| 4 | + |
| 5 | +describe('server/routes/extensions', () => { |
| 6 | + test('should list extensions', async () => { |
| 7 | + const app = build() |
| 8 | + const response = await app.inject({ |
| 9 | + method: 'GET', |
| 10 | + url: '/extensions', |
| 11 | + headers: { |
| 12 | + pg: TEST_CONNECTION_STRING, |
| 13 | + }, |
| 14 | + }) |
| 15 | + expect(response.statusCode).toBe(200) |
| 16 | + expect(Array.isArray(JSON.parse(response.body))).toBe(true) |
| 17 | + await app.close() |
| 18 | + }) |
| 19 | + |
| 20 | + test('should list extensions with query parameters', async () => { |
| 21 | + const app = build() |
| 22 | + const response = await app.inject({ |
| 23 | + method: 'GET', |
| 24 | + url: '/extensions?limit=5&offset=0', |
| 25 | + headers: { |
| 26 | + pg: TEST_CONNECTION_STRING, |
| 27 | + }, |
| 28 | + }) |
| 29 | + expect(response.statusCode).toBe(200) |
| 30 | + expect(Array.isArray(JSON.parse(response.body))).toBe(true) |
| 31 | + await app.close() |
| 32 | + }) |
| 33 | + |
| 34 | + test('should return 404 for non-existent extension', async () => { |
| 35 | + const app = build() |
| 36 | + const response = await app.inject({ |
| 37 | + method: 'GET', |
| 38 | + url: '/extensions/non-existent-extension', |
| 39 | + headers: { |
| 40 | + pg: TEST_CONNECTION_STRING, |
| 41 | + }, |
| 42 | + }) |
| 43 | + expect(response.statusCode).toBe(404) |
| 44 | + await app.close() |
| 45 | + }) |
| 46 | + |
| 47 | + test('should create extension, retrieve, update, delete', async () => { |
| 48 | + const app = build() |
| 49 | + const response = await app.inject({ |
| 50 | + method: 'POST', |
| 51 | + url: '/extensions', |
| 52 | + headers: { |
| 53 | + pg: TEST_CONNECTION_STRING, |
| 54 | + }, |
| 55 | + payload: { name: 'pgcrypto', version: '1.3' }, |
| 56 | + }) |
| 57 | + expect(response.statusCode).toBe(200) |
| 58 | + expect(response.json()).toMatchInlineSnapshot(` |
| 59 | + { |
| 60 | + "comment": "cryptographic functions", |
| 61 | + "default_version": "1.3", |
| 62 | + "installed_version": "1.3", |
| 63 | + "name": "pgcrypto", |
| 64 | + "schema": "public", |
| 65 | + } |
| 66 | + `) |
| 67 | + |
| 68 | + const retrieveResponse = await app.inject({ |
| 69 | + method: 'GET', |
| 70 | + url: '/extensions/pgcrypto', |
| 71 | + headers: { |
| 72 | + pg: TEST_CONNECTION_STRING, |
| 73 | + }, |
| 74 | + }) |
| 75 | + expect(retrieveResponse.statusCode).toBe(200) |
| 76 | + expect(retrieveResponse.json()).toMatchInlineSnapshot(` |
| 77 | + { |
| 78 | + "comment": "cryptographic functions", |
| 79 | + "default_version": "1.3", |
| 80 | + "installed_version": "1.3", |
| 81 | + "name": "pgcrypto", |
| 82 | + "schema": "public", |
| 83 | + } |
| 84 | + `) |
| 85 | + |
| 86 | + const updateResponse = await app.inject({ |
| 87 | + method: 'PATCH', |
| 88 | + url: '/extensions/pgcrypto', |
| 89 | + headers: { |
| 90 | + pg: TEST_CONNECTION_STRING, |
| 91 | + }, |
| 92 | + payload: { schema: 'public' }, |
| 93 | + }) |
| 94 | + expect(updateResponse.statusCode).toBe(200) |
| 95 | + expect(updateResponse.json()).toMatchInlineSnapshot(` |
| 96 | + { |
| 97 | + "comment": "cryptographic functions", |
| 98 | + "default_version": "1.3", |
| 99 | + "installed_version": "1.3", |
| 100 | + "name": "pgcrypto", |
| 101 | + "schema": "public", |
| 102 | + } |
| 103 | + `) |
| 104 | + |
| 105 | + const deleteResponse = await app.inject({ |
| 106 | + method: 'DELETE', |
| 107 | + url: '/extensions/pgcrypto', |
| 108 | + headers: { |
| 109 | + pg: TEST_CONNECTION_STRING, |
| 110 | + }, |
| 111 | + }) |
| 112 | + expect(deleteResponse.statusCode).toBe(200) |
| 113 | + expect(deleteResponse.json()).toMatchInlineSnapshot(` |
| 114 | + { |
| 115 | + "comment": "cryptographic functions", |
| 116 | + "default_version": "1.3", |
| 117 | + "installed_version": "1.3", |
| 118 | + "name": "pgcrypto", |
| 119 | + "schema": "public", |
| 120 | + } |
| 121 | + `) |
| 122 | + |
| 123 | + await app.close() |
| 124 | + }) |
| 125 | + |
| 126 | + test('should return 400 for invalid extension name', async () => { |
| 127 | + const app = build() |
| 128 | + const response = await app.inject({ |
| 129 | + method: 'POST', |
| 130 | + url: '/extensions', |
| 131 | + headers: { |
| 132 | + pg: TEST_CONNECTION_STRING, |
| 133 | + }, |
| 134 | + payload: { name: 'invalid-extension', version: '1.3' }, |
| 135 | + }) |
| 136 | + expect(response.statusCode).toBe(400) |
| 137 | + expect(response.json()).toMatchInlineSnapshot(` |
| 138 | + { |
| 139 | + "error": "could not open extension control file "/usr/share/postgresql/14/extension/invalid-extension.control": No such file or directory", |
| 140 | + } |
| 141 | + `) |
| 142 | + await app.close() |
| 143 | + }) |
| 144 | +}) |
0 commit comments