Skip to content

Commit

Permalink
chore(update_channels): allow empty targets
Browse files Browse the repository at this point in the history
Update channels can have zero target device groups.
Closes #399 and Closes #518.

Signed-off-by: Luca Zaninotto <[email protected]>
  • Loading branch information
lusergit committed Jan 7, 2025
1 parent 38d7e23 commit 09aa76c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2025 SECO Mind Srl
# Copyright 2024-2025 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ defmodule Edgehog.UpdateCampaigns.UpdateChannel do
description """
The IDs of the target groups that are targeted by this update channel.
"""

allow_nil? false
constraints min_length: 1
end

change Changes.RelateTargetGroups do
Expand All @@ -78,16 +75,13 @@ defmodule Edgehog.UpdateCampaigns.UpdateChannel do
description """
The IDs of the target groups that are targeted by this update channel.
"""

constraints min_length: 1
end

# Needed because manage_relationship is not atomic
require_atomic? false

change Changes.UnrelateCurrentTargetGroups do
where present(:target_group_ids)
end
# Always unrelate target groups
change Changes.UnrelateCurrentTargetGroups

change Changes.RelateTargetGroups do
where present(:target_group_ids)
Expand Down
2 changes: 1 addition & 1 deletion backend/mix.lock.license
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SPDX-FileCopyrightText: 2021-2024 SECO Mind Srl
SPDX-FileCopyrightText: 2021-2025 SECO Mind Srl
SPDX-License-Identifier: CC0-1.0
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ defmodule EdgehogWeb.Schema.Mutation.CreateUpdateChannelTest do
assert target_group_data["handle"] == target_group.handle
end

test "creates update_channel with nil target_group_ids", %{tenant: tenant} do
name = unique_update_channel_name()
handle = unique_update_channel_handle()

result =
[target_group_ids: nil, tenant: tenant, name: name, handle: handle]
|> create_update_channel_mutation()
|> extract_result!()

assert result["name"] == name
assert result["handle"] == handle
assert result["targetGroups"] == []
end

test "creates update_channel with empty target_group_ids", %{tenant: tenant} do
name = unique_update_channel_name()
handle = unique_update_channel_handle()

result =
[target_group_ids: [], tenant: tenant, name: name, handle: handle]
|> create_update_channel_mutation()
|> extract_result!()

assert result["name"] == name
assert result["handle"] == handle
assert result["targetGroups"] == []
end

test "fails with missing name", %{tenant: tenant} do
error =
[name: nil, tenant: tenant]
Expand Down Expand Up @@ -143,30 +171,6 @@ defmodule EdgehogWeb.Schema.Mutation.CreateUpdateChannelTest do
} = error
end

test "fails with missing target_group_ids", %{tenant: tenant} do
error =
[target_group_ids: nil, tenant: tenant]
|> create_update_channel_mutation()
|> extract_error!()

assert %{message: message} = error
assert message =~ ~s<In field "targetGroupIds": Expected type "[ID!]!", found null.>
end

test "fails with empty target_group_ids", %{tenant: tenant} do
error =
[target_group_ids: [], tenant: tenant]
|> create_update_channel_mutation()
|> extract_error!()

assert %{
path: ["createUpdateChannel"],
fields: [:target_group_ids],
message: "must have 1 or more items",
code: "invalid_argument"
} = error
end

test "fails when trying to use a non-existing target group", %{tenant: tenant} do
target_group_id = non_existing_device_group_id(tenant)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,13 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateUpdateChannelTest do
} = error
end

test "fails with empty target_group_ids", %{tenant: tenant, id: id} do
error =
test "updates update_channel with empty target_group_ids", %{tenant: tenant, id: id} do
result =
[id: id, target_group_ids: [], tenant: tenant]
|> update_update_channel_mutation()
|> extract_error!()
|> extract_result!()

assert %{
path: ["updateUpdateChannel"],
fields: [:target_group_ids],
code: "invalid_argument",
message: "must have 1 or more items"
} = error
assert result["id"] == id
end

test "fails when trying to use a non-existing target group id", %{tenant: tenant, id: id} do
Expand Down

0 comments on commit 09aa76c

Please sign in to comment.