From e60799a86b3fad35ec74d9cf33d3793859d564cd Mon Sep 17 00:00:00 2001 From: Riccardo Binetti Date: Mon, 28 Oct 2024 15:31:57 +0100 Subject: [PATCH] trigger_payload: accept trigger_name key 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. --- CHANGELOG.md | 4 +++ .../lib/edgehog/triggers/trigger_payload.ex | 5 ++++ .../astarte_trigger_controller_test.exs | 28 +++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e644d3bb..46bf14183 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)). diff --git a/backend/lib/edgehog/triggers/trigger_payload.ex b/backend/lib/edgehog/triggers/trigger_payload.ex index 76c485a3b..58df57386 100644 --- a/backend/lib/edgehog/triggers/trigger_payload.ex +++ b/backend/lib/edgehog/triggers/trigger_payload.ex @@ -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 diff --git a/backend/test/edgehog_web/controllers/astarte_trigger_controller_test.exs b/backend/test/edgehog_web/controllers/astarte_trigger_controller_test.exs index 4d8352fe8..84ca7f469 100644 --- a/backend/test/edgehog_web/controllers/astarte_trigger_controller_test.exs +++ b/backend/test/edgehog_web/controllers/astarte_trigger_controller_test.exs @@ -168,6 +168,34 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do } = device end + test "accepts `trigger_name` key in trigger payload (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_1_2_0_event = + device_id + |> connection_trigger(timestamp) + |> Map.put(:trigger_name, "connection_trigger") + + expect(DeviceStatusMock, :get, 0, fn _client, _device_id -> flunk() end) + assert conn |> post(path, astarte_1_2_0_event) |> response(200) + end + test "disconnection events update an existing device, not calling Astarte", ctx do %{ conn: conn,