mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 07:18:59 +00:00
feat(team): add integrations settings with turntable and gotenberg (#471)
* feat(team): add integrations settings with turntable and gotenberg * fix(upload): pass teamId to gotenberg availability check during upload preflight
This commit is contained in:
@@ -0,0 +1,271 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import {
|
||||
IntegrationDefinition,
|
||||
resolveIntegrationConfig,
|
||||
getIntegrationSettings,
|
||||
updateIntegrationSettings,
|
||||
resolveTestConnectionConfig,
|
||||
getIntegrationBasicAuthHeader,
|
||||
} from './helper'
|
||||
|
||||
const TEST_INTEGRATION_DEF: IntegrationDefinition<'turntable'> = {
|
||||
name: 'TestIntegration',
|
||||
settingsKey: 'turntable',
|
||||
envKeys: {
|
||||
url: ['TEST_INTEGRATION_URL', 'FALLBACK_INTEGRATION_URL'],
|
||||
username: ['TEST_INTEGRATION_USERNAME'],
|
||||
password: ['TEST_INTEGRATION_PASSWORD'],
|
||||
},
|
||||
}
|
||||
|
||||
describe('Integration Config Helper', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
const originalEnv = process.env
|
||||
|
||||
beforeEach(() => {
|
||||
process.env = { ...originalEnv }
|
||||
delete process.env.TEST_INTEGRATION_URL
|
||||
delete process.env.FALLBACK_INTEGRATION_URL
|
||||
delete process.env.TEST_INTEGRATION_USERNAME
|
||||
delete process.env.TEST_INTEGRATION_PASSWORD
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
process.env = originalEnv
|
||||
})
|
||||
|
||||
describe('resolveIntegrationConfig', () => {
|
||||
it('prioritizes environment variables over database settings', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: {
|
||||
name: 'Test Team',
|
||||
settings: {
|
||||
turntable: {
|
||||
url: 'http://db-server:1234',
|
||||
username: 'db-user',
|
||||
password: 'db-password',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
process.env.TEST_INTEGRATION_URL = 'http://env-server:5678/'
|
||||
process.env.TEST_INTEGRATION_USERNAME = 'env-user'
|
||||
process.env.TEST_INTEGRATION_PASSWORD = 'env-pass'
|
||||
|
||||
const config = await resolveIntegrationConfig(TEST_INTEGRATION_DEF, team.id)
|
||||
expect(config).toEqual({
|
||||
url: 'http://env-server:5678',
|
||||
username: 'env-user',
|
||||
password: 'env-pass',
|
||||
isEnv: true,
|
||||
})
|
||||
})
|
||||
|
||||
it('falls back to second env key if first is missing', async () => {
|
||||
process.env.FALLBACK_INTEGRATION_URL = 'http://fallback-server:8080'
|
||||
|
||||
const config = await resolveIntegrationConfig(TEST_INTEGRATION_DEF)
|
||||
expect(config.url).toBe('http://fallback-server:8080')
|
||||
expect(config.isEnv).toBe(true)
|
||||
})
|
||||
|
||||
it('reads from team settings when environment variables are not set', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: {
|
||||
name: 'Test Team',
|
||||
settings: {
|
||||
turntable: {
|
||||
url: 'http://db-server:1234/',
|
||||
username: 'db-user',
|
||||
password: 'db-password',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const config = await resolveIntegrationConfig(TEST_INTEGRATION_DEF, team.id)
|
||||
expect(config).toEqual({
|
||||
url: 'http://db-server:1234',
|
||||
username: 'db-user',
|
||||
password: 'db-password',
|
||||
isEnv: false,
|
||||
})
|
||||
})
|
||||
|
||||
it('returns isEnv: false and undefined fields when neither env nor DB are set', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Empty Team' },
|
||||
})
|
||||
|
||||
const config = await resolveIntegrationConfig(TEST_INTEGRATION_DEF, team.id)
|
||||
expect(config).toEqual({
|
||||
isEnv: false,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('getIntegrationSettings', () => {
|
||||
it('returns safe response masking password', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: {
|
||||
name: 'Test Team',
|
||||
settings: {
|
||||
turntable: {
|
||||
url: 'http://db-server:1234',
|
||||
username: 'db-user',
|
||||
password: 'db-password',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const res = await getIntegrationSettings(TEST_INTEGRATION_DEF, team.id)
|
||||
expect(res).toEqual({
|
||||
url: 'http://db-server:1234',
|
||||
username: 'db-user',
|
||||
hasPassword: true,
|
||||
isEnvConfigured: false,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('updateIntegrationSettings', () => {
|
||||
it('throws error when environment variable is configured', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'Test Team' },
|
||||
})
|
||||
|
||||
process.env.TEST_INTEGRATION_URL = 'http://env-server:5678'
|
||||
|
||||
await expect(
|
||||
updateIntegrationSettings(TEST_INTEGRATION_DEF, team.id, {
|
||||
url: 'http://new-url:1234',
|
||||
}),
|
||||
).rejects.toThrow(
|
||||
'TestIntegration settings are configured via environment variables and cannot be edited',
|
||||
)
|
||||
})
|
||||
|
||||
it('updates settings and preserves existing password when not supplied', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: {
|
||||
name: 'Test Team',
|
||||
settings: {
|
||||
turntable: {
|
||||
url: 'http://original:1234',
|
||||
username: 'original-user',
|
||||
password: 'secret-password',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const res = await updateIntegrationSettings(TEST_INTEGRATION_DEF, team.id, {
|
||||
url: 'http://updated:5678/',
|
||||
username: 'new-user',
|
||||
})
|
||||
|
||||
expect(res).toEqual({
|
||||
url: 'http://updated:5678',
|
||||
username: 'new-user',
|
||||
hasPassword: true,
|
||||
isEnvConfigured: false,
|
||||
})
|
||||
|
||||
const config = await resolveIntegrationConfig(TEST_INTEGRATION_DEF, team.id)
|
||||
expect(config.password).toBe('secret-password')
|
||||
})
|
||||
|
||||
it('clears settings in database when URL is empty or whitespace', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: {
|
||||
name: 'Test Team',
|
||||
settings: {
|
||||
turntable: {
|
||||
url: 'http://to-be-cleared:1234',
|
||||
username: 'user',
|
||||
password: 'pwd',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const res = await updateIntegrationSettings(TEST_INTEGRATION_DEF, team.id, {
|
||||
url: ' ',
|
||||
})
|
||||
|
||||
expect(res).toEqual({
|
||||
url: undefined,
|
||||
username: undefined,
|
||||
hasPassword: false,
|
||||
isEnvConfigured: false,
|
||||
})
|
||||
|
||||
const updatedTeam = await prisma.team.findUnique({
|
||||
where: { id: team.id },
|
||||
})
|
||||
// Check that turntable is removed from team settings
|
||||
expect((updatedTeam?.settings as Record<string, unknown>)?.turntable).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('resolveTestConnectionConfig', () => {
|
||||
it('uses provided credentials directly when supplied', async () => {
|
||||
const target = await resolveTestConnectionConfig(TEST_INTEGRATION_DEF, {
|
||||
url: 'http://direct:1234',
|
||||
username: 'direct-user',
|
||||
password: 'direct-password',
|
||||
})
|
||||
|
||||
expect(target).toEqual({
|
||||
resolvedUrl: 'http://direct:1234',
|
||||
resolvedUsername: 'direct-user',
|
||||
resolvedPassword: 'direct-password',
|
||||
})
|
||||
})
|
||||
|
||||
it('falls back to saved database settings when testing with partial inputs', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: {
|
||||
name: 'Test Team',
|
||||
settings: {
|
||||
turntable: {
|
||||
url: 'http://db:1234',
|
||||
username: 'db-user',
|
||||
password: 'db-password',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const target = await resolveTestConnectionConfig(
|
||||
TEST_INTEGRATION_DEF,
|
||||
{ url: 'http://new-url:9999' },
|
||||
team.id,
|
||||
)
|
||||
|
||||
expect(target).toEqual({
|
||||
resolvedUrl: 'http://new-url:9999',
|
||||
resolvedUsername: 'db-user',
|
||||
resolvedPassword: 'db-password',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('getIntegrationBasicAuthHeader', () => {
|
||||
it('generates base64 basic auth header when username and password are provided', () => {
|
||||
const header = getIntegrationBasicAuthHeader({ username: 'admin', password: 'secret' })
|
||||
expect(header).toBe(`Basic ${Buffer.from('admin:secret').toString('base64')}`)
|
||||
})
|
||||
|
||||
it('returns undefined when username or password is missing', () => {
|
||||
expect(getIntegrationBasicAuthHeader({ username: 'admin' })).toBeUndefined()
|
||||
expect(getIntegrationBasicAuthHeader({ password: 'secret' })).toBeUndefined()
|
||||
expect(getIntegrationBasicAuthHeader({})).toBeUndefined()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,188 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import '@shumai/db/src/prisma-json-types'
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
|
||||
export interface IntegrationEnvKeys {
|
||||
url: string[]
|
||||
username?: string[]
|
||||
password?: string[]
|
||||
}
|
||||
|
||||
export interface IntegrationDefinition<TKey extends keyof PrismaJson.Settings> {
|
||||
name: string
|
||||
settingsKey: TKey
|
||||
envKeys: IntegrationEnvKeys
|
||||
}
|
||||
|
||||
export interface ResolvedIntegrationConfig {
|
||||
url?: string
|
||||
username?: string
|
||||
password?: string
|
||||
isEnv: boolean
|
||||
}
|
||||
|
||||
export interface IntegrationSettingsResponse {
|
||||
url?: string
|
||||
username?: string
|
||||
hasPassword: boolean
|
||||
isEnvConfigured: boolean
|
||||
}
|
||||
|
||||
export interface UpdateIntegrationSettingsRequest {
|
||||
url?: string
|
||||
username?: string
|
||||
password?: string
|
||||
}
|
||||
|
||||
function getFirstEnvValue(keys?: string[]): string | undefined {
|
||||
if (!keys) return undefined
|
||||
for (const key of keys) {
|
||||
const val = process.env[key]?.trim()
|
||||
if (val) return val
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
export async function resolveIntegrationConfig<TKey extends keyof PrismaJson.Settings>(
|
||||
def: IntegrationDefinition<TKey>,
|
||||
teamId?: string,
|
||||
): Promise<ResolvedIntegrationConfig> {
|
||||
const envUrl = getFirstEnvValue(def.envKeys.url)
|
||||
const envUsername = getFirstEnvValue(def.envKeys.username)
|
||||
const envPassword = getFirstEnvValue(def.envKeys.password)
|
||||
|
||||
if (envUrl) {
|
||||
return {
|
||||
url: envUrl.replace(/\/+$/, ''),
|
||||
username: envUsername || undefined,
|
||||
password: envPassword || undefined,
|
||||
isEnv: true,
|
||||
}
|
||||
}
|
||||
|
||||
if (teamId) {
|
||||
try {
|
||||
const team = await prisma.team.findUnique({
|
||||
where: { id: teamId },
|
||||
select: { settings: true },
|
||||
})
|
||||
const teamSettings = team?.settings as PrismaJson.Settings | null
|
||||
const serviceSettings = teamSettings?.[def.settingsKey] as
|
||||
| { url?: string; username?: string; password?: string }
|
||||
| undefined
|
||||
|
||||
if (serviceSettings?.url?.trim()) {
|
||||
return {
|
||||
url: serviceSettings.url.trim().replace(/\/+$/, ''),
|
||||
username: serviceSettings.username?.trim() || undefined,
|
||||
password: serviceSettings.password?.trim() || undefined,
|
||||
isEnv: false,
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error({ err, teamId }, `Failed to load team ${def.name} settings`)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
isEnv: false,
|
||||
}
|
||||
}
|
||||
|
||||
export async function getIntegrationSettings<TKey extends keyof PrismaJson.Settings>(
|
||||
def: IntegrationDefinition<TKey>,
|
||||
teamId: string,
|
||||
): Promise<IntegrationSettingsResponse> {
|
||||
const config = await resolveIntegrationConfig(def, teamId)
|
||||
return {
|
||||
url: config.url,
|
||||
username: config.username,
|
||||
hasPassword: Boolean(config.password),
|
||||
isEnvConfigured: config.isEnv,
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateIntegrationSettings<TKey extends keyof PrismaJson.Settings>(
|
||||
def: IntegrationDefinition<TKey>,
|
||||
teamId: string,
|
||||
req: UpdateIntegrationSettingsRequest,
|
||||
): Promise<IntegrationSettingsResponse> {
|
||||
const currentConfig = await resolveIntegrationConfig(def, teamId)
|
||||
if (currentConfig.isEnv) {
|
||||
throw new Error(
|
||||
`${def.name} settings are configured via environment variables and cannot be edited`,
|
||||
)
|
||||
}
|
||||
|
||||
const team = await prisma.team.findUnique({
|
||||
where: { id: teamId },
|
||||
select: { settings: true },
|
||||
})
|
||||
const settings = { ...((team?.settings || {}) as PrismaJson.Settings) }
|
||||
const prevSettings = (settings[def.settingsKey] || {}) as {
|
||||
url?: string
|
||||
username?: string
|
||||
password?: string
|
||||
}
|
||||
|
||||
const trimmedUrl = req.url?.trim()
|
||||
if (!trimmedUrl) {
|
||||
// If URL is empty/unset, clear the integration settings
|
||||
delete settings[def.settingsKey]
|
||||
} else {
|
||||
const updatedServiceSettings: { url: string; username?: string; password?: string } = {
|
||||
url: trimmedUrl.replace(/\/+$/, ''),
|
||||
username:
|
||||
req.username !== undefined ? req.username.trim() || undefined : prevSettings.username,
|
||||
password: req.password !== undefined ? req.password : prevSettings.password,
|
||||
}
|
||||
settings[def.settingsKey] = updatedServiceSettings as unknown as PrismaJson.Settings[TKey]
|
||||
}
|
||||
|
||||
await prisma.team.update({
|
||||
where: { id: teamId },
|
||||
data: { settings },
|
||||
})
|
||||
|
||||
return getIntegrationSettings(def, teamId)
|
||||
}
|
||||
|
||||
export async function resolveTestConnectionConfig<TKey extends keyof PrismaJson.Settings>(
|
||||
def: IntegrationDefinition<TKey>,
|
||||
config?: UpdateIntegrationSettingsRequest,
|
||||
teamId?: string,
|
||||
): Promise<{ resolvedUrl?: string; resolvedUsername?: string; resolvedPassword?: string }> {
|
||||
let resolvedUrl = config?.url?.trim()
|
||||
let resolvedUsername = config?.username?.trim()
|
||||
let resolvedPassword = config?.password
|
||||
|
||||
if (teamId && (!resolvedUrl || resolvedPassword === undefined)) {
|
||||
const savedConfig = await resolveIntegrationConfig(def, teamId)
|
||||
if (!resolvedUrl) {
|
||||
resolvedUrl = savedConfig.url
|
||||
}
|
||||
if (!resolvedUsername && savedConfig.username) {
|
||||
resolvedUsername = savedConfig.username
|
||||
}
|
||||
if (resolvedPassword === undefined) {
|
||||
resolvedPassword = savedConfig.password
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
resolvedUrl,
|
||||
resolvedUsername,
|
||||
resolvedPassword,
|
||||
}
|
||||
}
|
||||
|
||||
export function getIntegrationBasicAuthHeader(config: {
|
||||
username?: string
|
||||
password?: string
|
||||
}): string | undefined {
|
||||
if (config.username && config.password) {
|
||||
const credentials = Buffer.from(`${config.username}:${config.password}`).toString('base64')
|
||||
return `Basic ${credentials}`
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
Reference in New Issue
Block a user