0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-28 18:26:15 +01:00
posthog/plugin-server/tests/helpers/plugins.ts
Marius Andra 2c2e9ce48e
feat(apps): rename web.ts to site.ts (#12179)
* site.ts

* Update snapshots

* fix file in test archive

* Update snapshots

* more renames

* plugin source file updated at

* fix test

* last renames

* add null

* blank

* remove what we don't need

* get test to pass

* use new posthog-js

* new version

Co-authored-by: mariusandra <mariusandra@users.noreply.github.com>
2022-10-11 10:27:21 +00:00

140 lines
4.2 KiB
TypeScript

import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import { Plugin, PluginAttachmentDB, PluginConfig } from '../../src/types'
export const commonUserId = 1001
export const commonOrganizationMembershipId = '0177364a-fc7b-0000-511c-137090b9e4e1'
export const commonOrganizationId = 'ca30f2ec-e9a4-4001-bf27-3ef194086068'
export const commonUserUuid = '797757a4-baed-4fa8-b73b-2b6cf0300299'
export const plugin60: Plugin = {
id: 60,
organization_id: commonOrganizationId,
plugin_type: 'custom',
name: 'test-maxmind-plugin',
description: 'Ingest GeoIP data via MaxMind',
url: 'https://www.npmjs.com/package/posthog-maxmind-plugin',
config_schema: {},
tag: '0.0.2',
error: undefined,
from_json: false,
from_web: false,
is_global: false,
is_preinstalled: false,
is_stateless: false,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
capabilities: {}, // inferred on setup
metrics: {},
...mockSourceFileFields('test-maxmind-plugin', {
indexJs:
'function processEvent (event) { if (event.properties) { event.properties.processed = true } return event }',
}),
}
export const pluginAttachment1: PluginAttachmentDB = {
id: 42666,
key: 'maxmindMmdb',
content_type: 'application/octet-stream',
file_name: 'test.txt',
file_size: 4,
contents: Buffer.from('test'),
plugin_config_id: 39,
team_id: 2,
}
export const pluginConfig39: PluginConfig = {
id: 39,
team_id: 2,
plugin_id: 60,
enabled: true,
order: 0,
config: { localhostIP: '94.224.212.175' },
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
}
function mockSourceFileFields(
name: string,
{ indexJs, pluginJson }: { indexJs?: string; pluginJson?: string }
): Pick<Plugin, 'source__plugin_json' | 'source__index_ts' | 'source__frontend_tsx' | 'source__site_ts'> {
const fields: Pick<
Plugin,
'source__plugin_json' | 'source__index_ts' | 'source__frontend_tsx' | 'source__site_ts'
> = {}
if (indexJs) {
fields['source__index_ts'] = indexJs
}
fields['source__plugin_json'] =
pluginJson ||
JSON.stringify({
name,
description: 'just for testing',
url: 'http://example.com/plugin',
config: {},
main: 'index.js',
})
return fields
}
export const mockPluginWithSourceFiles = (indexJs: string, pluginJson?: string): Plugin => ({
...plugin60,
...mockSourceFileFields('posthog-maxmind-plugin', { indexJs, pluginJson }),
})
export const makePluginObjects = (
indexJs = ''
): {
pluginRows: Omit<Plugin, 'id'>[]
pluginConfigRows: Omit<PluginConfig, 'id'>[]
pluginAttachmentRows: Omit<PluginAttachmentDB, 'id'>[]
} => ({
pluginRows: [mockPluginWithSourceFiles(indexJs)],
pluginConfigRows: [
{ ...pluginConfig39, created_at: new Date().toISOString(), updated_at: new Date().toISOString() },
],
pluginAttachmentRows: [pluginAttachment1],
})
export function mockPluginTempFolder(indexJs: string, pluginJson?: string): [Plugin, () => void] {
const folder = fs.mkdtempSync(path.join(os.tmpdir(), 'foo-'))
fs.writeFileSync(path.join(folder, 'index.js'), indexJs)
fs.writeFileSync(
path.join(folder, 'plugin.json'),
pluginJson ||
JSON.stringify({
name: 'posthog-maxmind-plugin',
description: 'just for testing',
url: 'http://example.com/plugin',
config: {},
main: 'index.js',
})
)
return [
{ ...plugin60, plugin_type: 'local', url: `file:${folder}` },
() => {
fs.rmSync(folder, { recursive: true })
},
]
}
export const mockPluginSourceCode = (): Plugin => ({
...plugin60,
plugin_type: 'source',
url: undefined,
})
export const plugin70 = {
...plugin60,
id: 70,
...mockSourceFileFields('test-plugin', {
indexJs: `
import { RetryError } from '@posthog/plugin-scaffold'
export function setupPlugin () { throw new RetryError('I always fail!') }
export function processEvent (event) { return event }`,
}),
}