8000 feat: load secret from files by sweatybridge · Pull Request #337 · supabase/postgres-meta · GitHub
[go: up one dir, main page]

Skip to content

feat: load secret from files #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: fix tests by using dynamic imports
  • Loading branch information
sweatybridge committed Jan 4, 2023
commit 881c0d8d2340b5f1712e245c317657716bd18283
3 changes: 2 additions & 1 deletion src/lib/secrets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs/promises'
// Use dynamic import to support module mock
const fs = await import('fs/promises')

export const getSecret = async (key: string) => {
if (!key) {
Expand Down
2 changes: 1 addition & 1 deletion src/server/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSecret } from '../lib/secrets'
import { getSecret } from '../lib/secrets.js'

export const PG_META_HOST = process.env.PG_META_HOST || '0.0.0.0'
export const PG_META_PORT = Number(process.env.PG_META_PORT || 1337)
Expand Down
15 changes: 9 additions & 6 deletions test/lib/secrets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { readFile } from 'fs/promises'
import { getSecret } from '../../src/lib/secrets'
import { jest } from '@jest/globals'

jest.mock('fs/promises')
jest.unstable_mockModule('fs/promises', () => ({
readFile: jest.fn(),
}))
const { readFile } = await import('fs/promises')
const { getSecret } = await import('../../src/lib/secrets')

describe('getSecret', () => {
const value = 'dummy'
Expand All @@ -24,7 +27,7 @@ describe('getSecret', () => {

it('loads from file', async () => {
process.env.SECRET_FILE = '/run/secrets/db_password'
jest.mocked(readFile, true).mockResolvedValueOnce(value)
jest.mocked(readFile).mockResolvedValueOnce(value)
const res = await getSecret('SECRET')
expect(res).toBe(value)
})
Expand All @@ -38,7 +41,7 @@ describe('getSecret', () => {
process.env.SECRET_FILE = '/run/secrets/db_password'
const e: NodeJS.ErrnoException = new Error('no such file or directory')
e.code = 'ENOENT'
jest.mocked(readFile, true).mockRejectedValueOnce(e)
jest.mocked(readFile).mockRejectedValueOnce(e)
const res = await getSecret('SECRET')
expect(res).toBe('')
})
Expand All @@ -47,7 +50,7 @@ describe('getSecret', () => {
process.env.SECRET_FILE = '/run/secrets/db_password'
const e: NodeJS.ErrnoException = new Error('permission denied')
e.code = 'EACCES'
jest.mocked(readFile, true).mockRejectedValueOnce(e)
jest.mocked(readFile).mockRejectedValueOnce(e)
expect(getSecret('SECRET')).rejects.toThrow()
})
})
0