Skip to content

Commit f2b5ff8

Browse files
authored
Merge pull request #159 from fingerprintjs/refactor-functional-tests-INTER-1173
Don't use outdated events in functional tests
2 parents dc859e3 + b917a03 commit f2b5ff8

File tree

3 files changed

+54
-27
lines changed

3 files changed

+54
-27
lines changed

.env.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FP_PRIVATE_API_KEY=<FP_PRIVATE_API_KEY>
2-
FP_VISITOR_ID=<FP_VISITOR_ID>
3-
FP_REQUEST_ID=<FP_REQUEST_ID>
2+
FP_VISITOR_ID_TO_DELETE=<FP_VISITOR_ID>
3+
FP_REQUEST_ID_TO_UPDATE=<FP_REQUEST_ID>
44
# "eu" or "ap", the default is "us"
5-
FP_REGION=<FP_REGION>
5+
FP_REGION=<FP_REGION>

.github/workflows/functional.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: Functional tests
22

33
on:
4-
pull_request_target:
5-
branches:
6-
- '*'
4+
push:
5+
branches-ignore:
6+
- main
77
workflow_dispatch:
88
schedule:
99
- cron: '0 5 * * *'
@@ -34,8 +34,6 @@ jobs:
3434
env:
3535
FP_PRIVATE_API_KEY: "${{ secrets.FP_PRIVATE_API_KEY }}"
3636
FP_API_HOST: "${{ secrets.FP_API_HOST }}"
37-
FP_VISITOR_ID: "${{ secrets.FP_VISITOR_ID }}"
38-
FP_REQUEST_ID: "${{ secrets.FP_REQUEST_ID }}"
3937

4038
report-status:
4139
needs: functional_tests

run_checks.php

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
$dotenv->safeLoad();
2020

2121
$api_key = $_ENV['FP_PRIVATE_API_KEY'] ?? getenv('FP_PRIVATE_API_KEY') ?? 'Private API Key not defined';
22-
$visitor_id = $_ENV['FP_VISITOR_ID'] ?? getenv('FP_VISITOR_ID') ?? 'Visitor ID not defined';
2322
$visitor_id_to_delete = $_ENV['FP_VISITOR_ID_TO_DELETE'] ?? getenv('FP_VISITOR_ID_TO_DELETE') ?? false;
24-
$request_id = $_ENV['FP_REQUEST_ID'] ?? getenv('FP_REQUEST_ID') ?? 'Request ID not defined';
2523
$request_id_to_update = $_ENV['FP_REQUEST_ID_TO_UPDATE'] ?? getenv('FP_REQUEST_ID_TO_UPDATE') ?? false;
2624
$region_env = $_ENV['FP_REGION'] ?? getenv('FP_REGION') ?? 'us';
2725

@@ -47,6 +45,28 @@
4745
// https://github.com/swagger-api/swagger-codegen/issues/11820
4846
error_reporting(error_reporting() & ~E_DEPRECATED);
4947

48+
// FingerprintApi->searchEvents usage example
49+
try {
50+
// 3 month from now
51+
$start = (new DateTime())->sub(new DateInterval('P3M'));
52+
$end = new DateTime();
53+
54+
/** @var SearchEventsResponse $result */
55+
list($result, $response) = $client->searchEvents(10, start: $start->getTimestamp() * 1000, end: $end->getTimestamp() * 1000);
56+
if (!is_countable($result->getEvents()) || count($result->getEvents()) === 0) {
57+
throw new Exception('No events found');
58+
}
59+
$identification_data = $result->getEvents()[0]->getProducts()->getIdentification()->getData();
60+
$visitor_id = $identification_data->getVisitorId();
61+
$request_id = $identification_data->getRequestId();
62+
fwrite(STDOUT, sprintf("\n\nGot events: %s \n", $response->getBody()->getContents()));
63+
} catch (Exception $e) {
64+
fwrite(STDERR, sprintf("\n\nException when calling FingerprintApi->searchEvents: %s\n", $e->getMessage()));
65+
66+
exit(1);
67+
}
68+
69+
// FingerprintApi->getVisits usage example
5070
try {
5171
/** @var VisitorsGetResponse $result */
5272
list($result, $response) = $client->getVisits($visitor_id);
@@ -60,6 +80,7 @@
6080
exit(1);
6181
}
6282

83+
// FingerprintApi->deleteVisitorData usage example
6384
if ($visitor_id_to_delete) {
6485
try {
6586
list($model, $response) = $client->deleteVisitorData($visitor_id_to_delete);
@@ -70,6 +91,7 @@
7091
}
7192
}
7293

94+
// FingerprintApi->getEvent usage example
7395
try {
7496
/** @var EventsGetResponse $result */
7597
list($result, $response) = $client->getEvent($request_id);
@@ -83,23 +105,7 @@
83105
exit(1);
84106
}
85107

86-
try {
87-
// 2 years from now
88-
$start = (new DateTime())->sub(new DateInterval('P2Y'));
89-
$end = new DateTime();
90-
91-
/** @var SearchEventsResponse $result */
92-
list($result, $response) = $client->searchEvents(10, start: $start->getTimestamp() * 1000, end: $end->getTimestamp() * 1000);
93-
if (!is_countable($result->getEvents()) || count($result->getEvents()) === 0) {
94-
throw new Exception('No events found');
95-
}
96-
fwrite(STDOUT, sprintf("\n\nGot events: %s \n", $response->getBody()->getContents()));
97-
} catch (Exception $e) {
98-
fwrite(STDERR, sprintf("\n\nException when calling FingerprintApi->searchEvents: %s\n", $e->getMessage()));
99-
100-
exit(1);
101-
}
102-
108+
// FingerprintApi->updateEvent usage example
103109
if ($request_id_to_update) {
104110
try {
105111
$body = new EventsUpdateRequest([
@@ -113,6 +119,7 @@
113119
}
114120
}
115121

122+
// Call API asynchronously examples
116123
$eventPromise = $client->getEventAsync($request_id);
117124
$eventPromise->then(function ($tuple) use ($request_id) {
118125
list($result, $response) = $tuple;
@@ -139,6 +146,7 @@
139146
exit(1);
140147
})->wait();
141148

149+
// Webhook verification example
142150
$webhookSecret = 'secret';
143151
$webhookData = 'data';
144152
$webhookHeader = 'v1=1b2c16b75bd2a870c114153ccda5bcfca63314bc722fa160d690de133ccbb9db';
@@ -151,6 +159,27 @@
151159
exit(1);
152160
}
153161

162+
// Check that old events still match expected format
163+
try {
164+
list($result_old) = $client->searchEvents(1, start: $start->getTimestamp() * 1000, end: $end->getTimestamp() * 1000, reverse: true);
165+
if (!is_countable($result_old->getEvents()) || count($result_old->getEvents()) === 0) {
166+
throw new Exception('No old events found');
167+
}
168+
$identification_data_old = $result_old->getEvents()[0]->getProducts()->getIdentification()->getData();
169+
$visitor_id_old = $identification_data_old->getVisitorId();
170+
$request_id_old = $identification_data_old->getRequestId();
171+
172+
if ($visitor_id === $visitor_id_old || $request_id === $request_id_old) {
173+
throw new Exception('Old events are identical to new');
174+
}
175+
176+
list($result, $response) = $client->getEvent($request_id_old);
177+
list($result, $response) = $client->getVisits($visitor_id_old);
178+
fwrite(STDERR, sprintf("\n\nOld events are good\n"));
179+
} catch (Exception $e) {
180+
fwrite(STDERR, sprintf("\n\nException when trying to read old data: %s\n", $e->getMessage()));
181+
}
182+
154183
// Enable the deprecated ArrayAccess return type warning again if needed
155184
error_reporting(error_reporting() | E_DEPRECATED);
156185

0 commit comments

Comments
 (0)