forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetricsEndpointTest.php
More file actions
142 lines (104 loc) · 4.31 KB
/
MetricsEndpointTest.php
File metadata and controls
142 lines (104 loc) · 4.31 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
<?php
use App\Models\Result;
use App\Services\PrometheusMetricsService;
use App\Settings\DataIntegrationSettings;
use Illuminate\Support\Facades\Cache;
beforeEach(function () {
Cache::flush();
});
describe('metrics endpoint', function () {
test('returns 404 when prometheus is disabled', function () {
app(DataIntegrationSettings::class)->fill(['prometheus_enabled' => false])->save();
$response = $this->get('/prometheus');
$response->assertNotFound();
});
test('returns metrics when prometheus is enabled and no IP restrictions', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => [],
])->save();
$result = Result::factory()->create();
// Simulate the listener updating metrics
app(PrometheusMetricsService::class)->updateMetrics($result);
$response = $this->get('/prometheus');
$response->assertSuccessful();
$response->assertHeader('Content-Type', 'text/plain; version=0.0.4; charset=utf-8');
});
test('returns 403 when IP is not in allowed list', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => ['192.168.1.100', '10.0.0.1'],
])->save();
$response = $this->get('/prometheus', [
'REMOTE_ADDR' => '192.168.1.50',
]);
$response->assertForbidden();
});
test('returns metrics when IP is in allowed list', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => ['192.168.1.100', '10.0.0.1'],
])->save();
$result = Result::factory()->create();
// Simulate the listener updating metrics
app(PrometheusMetricsService::class)->updateMetrics($result);
$response = $this->get('/prometheus', [
'REMOTE_ADDR' => '192.168.1.100',
]);
$response->assertSuccessful();
$response->assertHeader('Content-Type', 'text/plain; version=0.0.4; charset=utf-8');
});
test('allows access with empty array', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => [],
])->save();
$result = Result::factory()->create();
// Simulate the listener updating metrics
app(PrometheusMetricsService::class)->updateMetrics($result);
$response = $this->get('/prometheus', [
'REMOTE_ADDR' => '10.0.0.1',
]);
$response->assertSuccessful();
});
test('allows access when IP is in CIDR range', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => ['192.168.1.0/24'],
])->save();
$result = Result::factory()->create();
// Simulate the listener updating metrics
app(PrometheusMetricsService::class)->updateMetrics($result);
$response = $this->get('/prometheus', [
'REMOTE_ADDR' => '192.168.1.150',
]);
$response->assertSuccessful();
});
test('denies access when IP is not in CIDR range', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => ['192.168.1.0/24'],
])->save();
$response = $this->get('/prometheus', [
'REMOTE_ADDR' => '192.168.2.1',
]);
$response->assertForbidden();
});
test('supports mixed IP addresses and CIDR ranges', function () {
app(DataIntegrationSettings::class)->fill([
'prometheus_enabled' => true,
'prometheus_allowed_ips' => ['10.0.0.1', '192.168.1.0/24'],
])->save();
$result = Result::factory()->create();
// Simulate the listener updating metrics
app(PrometheusMetricsService::class)->updateMetrics($result);
$response = $this->get('/prometheus', [
'REMOTE_ADDR' => '192.168.1.50',
]);
$response->assertSuccessful();
$response = $this->get('/prometheus', [
'REMOTE_ADDR' => '10.0.0.1',
]);
$response->assertSuccessful();
});
});