- Added support for workflow variables in automations, allowing users to define reusable values. - Implemented validation for Generate nodes to ensure intended image counts align with selected accounts. - Updated automation models and requests to handle new variables, including encryption for sensitive data. - Enhanced UI to display variables and their management within the automation editor. - Improved error handling for webhook and HTTP nodes to prevent requests to invalid URLs. - Refactored various components for better context resolution during automation runs.
138 lines
4.3 KiB
PHP
138 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Actions\Automation\Node\RunWebhookNode;
|
|
use App\Enums\Automation\NodeRun\Status;
|
|
use App\Models\AutomationRun;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
it('posts interpolated payload to the configured url', function () {
|
|
Http::fake([
|
|
'1.1.1.1/*' => Http::response(['ok' => true], 200),
|
|
]);
|
|
|
|
$run = AutomationRun::factory()->create([
|
|
'context' => ['trigger' => ['title' => 'Hello'], 'generated' => ['post_url' => 'https://t.it/p/1']],
|
|
]);
|
|
|
|
$result = app(RunWebhookNode::class)($run, [
|
|
'url' => 'https://1.1.1.1/test',
|
|
'method' => 'POST',
|
|
'headers' => ['X-Source' => 'TryPost'],
|
|
'payload_template' => '{"title":"{{ trigger.title }}","post_url":"{{ generated.post_url }}"}',
|
|
]);
|
|
|
|
expect($result->status)->toBe(Status::Completed);
|
|
Http::assertSent(fn ($request) => $request['title'] === 'Hello' && $request['post_url'] === 'https://t.it/p/1');
|
|
});
|
|
|
|
it('sends the branded user-agent header', function () {
|
|
Http::fake([
|
|
'1.1.1.1/*' => Http::response(['ok' => true], 200),
|
|
]);
|
|
|
|
$run = AutomationRun::factory()->create();
|
|
|
|
app(RunWebhookNode::class)($run, [
|
|
'url' => 'https://1.1.1.1/test',
|
|
'method' => 'POST',
|
|
'headers' => ['User-Agent' => 'user-supplied-agent'],
|
|
'payload_template' => '{}',
|
|
]);
|
|
|
|
Http::assertSent(fn ($request) => $request->hasHeader('User-Agent', config('trypost.user_agent')));
|
|
});
|
|
|
|
it('fails on 5xx response', function () {
|
|
Http::fake(['1.1.1.1/*' => Http::response('err', 500)]);
|
|
|
|
$run = AutomationRun::factory()->create();
|
|
|
|
$result = app(RunWebhookNode::class)($run, [
|
|
'url' => 'https://1.1.1.1/test',
|
|
'method' => 'POST',
|
|
'payload_template' => '{}',
|
|
]);
|
|
|
|
expect($result->status)->toBe(Status::Failed);
|
|
});
|
|
|
|
it('fails on malformed payload json instead of silently sending an empty body', function () {
|
|
Http::fake(['1.1.1.1/*' => Http::response(['ok' => true], 200)]);
|
|
|
|
$run = AutomationRun::factory()->create();
|
|
|
|
$result = app(RunWebhookNode::class)($run, [
|
|
'url' => 'https://1.1.1.1/test',
|
|
'method' => 'POST',
|
|
'payload_template' => '{ "a": }',
|
|
]);
|
|
|
|
expect($result->status)->toBe(Status::Failed);
|
|
expect($result->error['reason'])->toBe('invalid_payload_json');
|
|
Http::assertNothingSent();
|
|
});
|
|
|
|
it('does not fire a real request on a dry run', function () {
|
|
Http::fake(['1.1.1.1/*' => Http::response(['ok' => true], 200)]);
|
|
|
|
$run = AutomationRun::factory()->create(['is_dry_run' => true]);
|
|
|
|
$result = app(RunWebhookNode::class)($run, [
|
|
'url' => 'https://1.1.1.1/test',
|
|
'method' => 'POST',
|
|
'payload_template' => '{"a":1}',
|
|
]);
|
|
|
|
expect($result->status)->toBe(Status::Completed);
|
|
expect($result->output['webhook']['dry_run'])->toBeTrue();
|
|
Http::assertNothingSent();
|
|
});
|
|
|
|
it('still validates payload json on a dry run', function () {
|
|
Http::fake(['1.1.1.1/*' => Http::response(['ok' => true], 200)]);
|
|
|
|
$run = AutomationRun::factory()->create(['is_dry_run' => true]);
|
|
|
|
$result = app(RunWebhookNode::class)($run, [
|
|
'url' => 'https://1.1.1.1/test',
|
|
'method' => 'POST',
|
|
'payload_template' => '{ "a": }',
|
|
]);
|
|
|
|
expect($result->status)->toBe(Status::Failed);
|
|
expect($result->error['reason'])->toBe('invalid_payload_json');
|
|
Http::assertNothingSent();
|
|
});
|
|
|
|
it('blocks a request to a private or reserved address', function () {
|
|
Http::fake();
|
|
|
|
$run = AutomationRun::factory()->create();
|
|
|
|
$result = app(RunWebhookNode::class)($run, [
|
|
'url' => 'http://169.254.169.254/latest/meta-data/',
|
|
'method' => 'POST',
|
|
'payload_template' => '{}',
|
|
]);
|
|
|
|
expect($result->status)->toBe(Status::Failed);
|
|
expect($result->error['reason'])->toBe('url_not_allowed');
|
|
Http::assertNothingSent();
|
|
});
|
|
|
|
it('treats 4xx responses as completed (only 5xx fails)', function () {
|
|
Http::fake(['1.1.1.1/*' => Http::response('not found', 404)]);
|
|
|
|
$run = AutomationRun::factory()->create();
|
|
|
|
$result = app(RunWebhookNode::class)($run, [
|
|
'url' => 'https://1.1.1.1/test',
|
|
'method' => 'POST',
|
|
'payload_template' => '{}',
|
|
]);
|
|
|
|
expect($result->status->value)->toBe('completed');
|
|
expect($result->output['webhook']['status'])->toBe(404);
|
|
});
|