Skip to content

Commit

Permalink
trigger_payload: accept trigger_name key
Browse files Browse the repository at this point in the history
Astarte >= 1.2.0 sends an additional trigger_name key in the trigger payload.
This was making Edgehog crash since Ash doesn't accept additional input keys
by default.

Since 1.2.0 is the current stable version of Astarte, add the trigger_name key
to all existing tests and explicitly test support for the key missing to avoid
regressions with old Astarte versions.

Signed-off-by: Riccardo Binetti <[email protected]>
  • Loading branch information
rbino committed Oct 28, 2024
1 parent 6845d45 commit a71412f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.9.1] - Unreleased
### Fixed
- Allow receiving `trigger_name` key in trigger payload, which is sent by Astarte >= 1.2.0.

## [0.9.0] - 2024-10-25
### Fixed
- Correctly support automatic login attempts on the frontend regardless of existing auth sessions ([#596](https://github.com/edgehog-device-manager/edgehog/pull/596)).
Expand Down
5 changes: 5 additions & 0 deletions backend/lib/edgehog/triggers/trigger_payload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ defmodule Edgehog.Triggers.TriggerPayload do
public? true
allow_nil? false
end

# This is sent in Astarte >= 1.2.0, so it can be nil
attribute :trigger_name, :string do
public? true
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,33 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do
} = device
end

test "accepts trigger payload without `trigger_name` key (Astarte < 1.2.0)", ctx do
%{
conn: conn,
path: path,
realm: realm,
tenant: tenant
} = ctx

%Device{device_id: device_id} =
device_fixture(
tenant: tenant,
realm_id: realm.id,
online: false,
last_connection: DateTime.add(utc_now_second(), -50, :minute),
last_disconnection: DateTime.add(utc_now_second(), -10, :minute)
)

timestamp = utc_now_second()

astarte_pre_1_2_0_event =
device_id
|> connection_trigger(timestamp)
|> Map.delete(:trigger_name)

assert conn |> post(path, astarte_pre_1_2_0_event) |> response(200)
end

test "disconnection events update an existing device, not calling Astarte", ctx do
%{
conn: conn,
Expand Down Expand Up @@ -395,6 +422,7 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do
path = Routes.astarte_trigger_path(conn, :process_event, tenant.slug)

ota_event = %{
trigger_name: "edgehog-ota-event",
device_id: device.device_id,
event: %{
type: "incoming_data",
Expand Down Expand Up @@ -479,6 +507,7 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do
path = Routes.astarte_trigger_path(conn, :process_event, tenant.slug)

ota_event = %{
trigger_name: "edgehog-ota-event",
device_id: device.device_id,
event: %{
type: "incoming_data",
Expand Down Expand Up @@ -516,6 +545,7 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do

defp connection_trigger(device_id, timestamp) do
%{
trigger_name: "edgehog-connection",
device_id: device_id,
event: %{
type: "device_connected",
Expand All @@ -527,6 +557,7 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do

defp disconnection_trigger(device_id, timestamp) do
%{
trigger_name: "edgehog-disconnection",
device_id: device_id,
event: %{
type: "device_disconnected"
Expand All @@ -539,6 +570,7 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do

defp part_number_trigger(device_id, part_number) do
%{
trigger_name: "edgehog-system-info",
device_id: device_id,
event: %{
type: "incoming_data",
Expand All @@ -552,6 +584,7 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do

defp serial_number_trigger(device_id, serial_number) do
%{
trigger_name: "edgehog-system-info",
device_id: device_id,
event: %{
type: "incoming_data",
Expand All @@ -565,6 +598,7 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do

defp unknown_trigger(device_id) do
%{
trigger_name: "other-trigger",
device_id: device_id,
event: %{
type: "incoming_data",
Expand Down

0 comments on commit a71412f

Please sign in to comment.