-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAuditEventTypesTest.php
More file actions
168 lines (145 loc) · 5.57 KB
/
AuditEventTypesTest.php
File metadata and controls
168 lines (145 loc) · 5.57 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace Tests\OpenTelemetry;
use App\Audit\AuditLogOtlpStrategy;
use App\Audit\AuditContext;
use OpenTelemetry\API\Trace\StatusCode;
class AuditEventTypesTest extends OpenTelemetryTestCase
{
private const MOCK_ENTITY_ID = 999;
private const MOCK_ENTITY_TITLE = 'Test Entity';
private const NEW_ENTITY_TITLE = 'New Entity';
private const TEST_TYPE = 'test';
private const OLD_TITLE = 'Old Title';
private const NEW_TITLE = 'New Title';
private const DELETED_ID = 999;
private const CLEANUP_REASON = 'Test cleanup';
private const USER_ID = 999;
private const USER_EMAIL = 'test@example.com';
private const USER_FIRST_NAME = 'Test';
private const USER_LAST_NAME = 'User';
private const TEST_APP = 'test-app';
private const TEST_FLOW = 'test-flow';
private const TEST_ROUTE = 'api.test';
private const TEST_HTTP_METHOD = 'POST';
private const TEST_CLIENT_IP = '127.0.0.1';
private const TEST_USER_AGENT = 'Test-Agent/1.0';
private const SPAN_CREATION = 'test.audit.creation';
private const SPAN_UPDATE = 'test.audit.update';
private const SPAN_DELETION = 'test.audit.deletion';
private const CREATION_COMPLETED = 'Creation audit completed';
private const UPDATE_COMPLETED = 'Update audit completed';
private const DELETION_COMPLETED = 'Deletion audit completed';
private AuditLogOtlpStrategy $auditStrategy;
protected function setUp(): void
{
parent::setUp();
$this->auditStrategy = $this->app->make(AuditLogOtlpStrategy::class);
}
public function testEntityCreationAudit(): void
{
if (!$this->isOpenTelemetryEnabled()) {
$this->markTestSkipped('OpenTelemetry is disabled');
}
$tracer = $this->app->make(\OpenTelemetry\API\Trace\TracerInterface::class);
$span = $tracer->spanBuilder(self::SPAN_CREATION)->startSpan();
$spanScope = $span->activate();
try {
$mockEntity = (object) ['id' => self::MOCK_ENTITY_ID, 'title' => self::MOCK_ENTITY_TITLE];
$data = ['title' => self::NEW_ENTITY_TITLE, 'type' => self::TEST_TYPE];
$ctx = $this->createAuditContext();
$this->auditStrategy->audit(
$mockEntity,
$data,
AuditLogOtlpStrategy::EVENT_ENTITY_CREATION,
$ctx
);
$span->setStatus(StatusCode::STATUS_OK, self::CREATION_COMPLETED);
$this->assertTrue(true);
} catch (\Exception $e) {
$span->recordException($e);
throw $e;
} finally {
$span->end();
$spanScope->detach();
}
}
public function testEntityUpdateAudit(): void
{
if (!$this->isOpenTelemetryEnabled()) {
$this->markTestSkipped('OpenTelemetry is disabled');
}
$tracer = $this->app->make(\OpenTelemetry\API\Trace\TracerInterface::class);
$span = $tracer->spanBuilder(self::SPAN_UPDATE)->startSpan();
$spanScope = $span->activate();
try {
$mockEntity = (object) ['id' => self::MOCK_ENTITY_ID, 'title' => self::MOCK_ENTITY_TITLE];
$data = ['title' => [self::OLD_TITLE, self::NEW_TITLE]];
$ctx = $this->createAuditContext();
$this->auditStrategy->audit(
$mockEntity,
$data,
AuditLogOtlpStrategy::EVENT_ENTITY_UPDATE,
$ctx
);
$span->setStatus(StatusCode::STATUS_OK, self::UPDATE_COMPLETED);
$this->assertTrue(true);
} catch (\Exception $e) {
$span->recordException($e);
throw $e;
} finally {
$span->end();
$spanScope->detach();
}
}
public function testEntityDeletionAudit(): void
{
if (!$this->isOpenTelemetryEnabled()) {
$this->markTestSkipped('OpenTelemetry is disabled');
}
$tracer = $this->app->make(\OpenTelemetry\API\Trace\TracerInterface::class);
$span = $tracer->spanBuilder(self::SPAN_DELETION)->startSpan();
$spanScope = $span->activate();
try {
$mockEntity = (object) ['id' => self::MOCK_ENTITY_ID, 'title' => self::MOCK_ENTITY_TITLE];
$data = ['deleted_id' => self::DELETED_ID, 'reason' => self::CLEANUP_REASON];
$ctx = $this->createAuditContext();
$this->auditStrategy->audit(
$mockEntity,
$data,
AuditLogOtlpStrategy::EVENT_ENTITY_DELETION,
$ctx
);
$span->setStatus(StatusCode::STATUS_OK, self::DELETION_COMPLETED);
$this->assertTrue(true);
} catch (\Exception $e) {
$span->recordException($e);
throw $e;
} finally {
$span->end();
$spanScope->detach();
}
}
/**
* Check if OpenTelemetry is enabled
*/
private function isOpenTelemetryEnabled(): bool
{
return getenv('OTEL_SERVICE_ENABLED') === 'true';
}
private function createAuditContext(): AuditContext
{
return new AuditContext(
userId: self::USER_ID,
userEmail: self::USER_EMAIL,
userFirstName: self::USER_FIRST_NAME,
userLastName: self::USER_LAST_NAME,
uiApp: self::TEST_APP,
uiFlow: self::TEST_FLOW,
route: self::TEST_ROUTE,
rawRoute: self::TEST_ROUTE,
httpMethod: self::TEST_HTTP_METHOD,
clientIp: self::TEST_CLIENT_IP,
userAgent: self::TEST_USER_AGENT,
);
}
}