-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathindex.spec.js
More file actions
90 lines (78 loc) · 3.01 KB
/
Copy pathindex.spec.js
File metadata and controls
90 lines (78 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict'
const assert = require('node:assert/strict')
const path = require('path')
const { after, afterEach, before, beforeEach, describe, it } = require('mocha')
const { sandboxCwd, useSandbox, FakeAgent, spawnProc, stopProc } = require('../helpers')
const { assertObjectContains } = require('../helpers')
const startApiMock = require('./api-mock')
const { executeRequest } = require('./util')
describe('AIGuard SDK integration tests', () => {
let cwd, appFile, agent, proc, api, url
useSandbox(['express'])
before(async function () {
cwd = sandboxCwd()
appFile = path.join(cwd, 'aiguard/server.js')
api = await startApiMock()
})
after(async () => {
await api.close()
})
beforeEach(async () => {
agent = await new FakeAgent().start()
proc = await spawnProc(appFile, {
cwd,
env: {
DD_SERVICE: 'ai_guard_integration_test',
DD_ENV: 'test',
DD_TRACING_ENABLED: 'true',
DD_TRACE_AGENT_PORT: agent.port,
DD_AI_GUARD_ENABLED: 'true',
DD_AI_GUARD_ENDPOINT: `http://localhost:${api.address().port}`,
DD_API_KEY: 'DD_API_KEY',
DD_APP_KEY: 'DD_APP_KEY',
},
})
url = `${proc.url}`
})
afterEach(async () => {
await stopProc(proc)
await agent.stop()
})
const testSuite = [
{ endpoint: '/allow', action: 'ALLOW', reason: 'The prompt looks harmless' },
{ endpoint: '/deny', action: 'DENY', reason: 'I am feeling suspicious today' },
{ endpoint: '/abort', action: 'ABORT', reason: 'The user is trying to destroy me' },
].flatMap(r => [
{ ...r, blocking: true },
{ ...r, blocking: false },
])
it('test default options honors remote blocking', async () => {
const response = await executeRequest(`${url}/deny-default-options`, 'GET')
assert.strictEqual(response.status, 403)
assertObjectContains(response.body, 'I am feeling suspicious today')
await agent.assertMessageReceived(({ headers, payload }) => {
const span = payload[0].find(span => span.name === 'ai_guard')
assert.notStrictEqual(span, null)
assert.strictEqual(span.meta['ai_guard.action'], 'DENY')
assert.strictEqual(span.meta['ai_guard.blocked'], 'true')
})
})
for (const { endpoint, action, reason, blocking } of testSuite) {
it(`test evaluate with ${action} response (blocking ${blocking})`, async () => {
const headers = blocking ? { 'x-blocking-enabled': true } : null
const response = await executeRequest(`${url}${endpoint}`, 'GET', headers)
if (blocking && action !== 'ALLOW') {
assert.strictEqual(response.status, 403)
assertObjectContains(response.body, reason)
} else {
assert.strictEqual(response.status, 200)
assert.strictEqual(response.body?.action, action)
assert.strictEqual(response.body?.reason, reason)
}
await agent.assertMessageReceived(({ headers, payload }) => {
const span = payload[0].find(span => span.name === 'ai_guard')
assert.notStrictEqual(span, null)
})
})
}
})