-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed - using exandra -- waiting for database project - implemented jwt + public/private key - wip group tests - squashed all failure - polishing - removed one_of_constant -> member_of - splitted tests - WIP device generator - completed device generator...but using opaque data (TODO change it) - basic datetime generator - WIP device generator - using Keyword.validate! - WIP insert into db, IPNET error - completed device WIP id doesn't match - completed base tests device - polishing - renamed realm -> keyspace - completed group generator + setup + case - wip group test (group_name "!" doesn't work) - added encoded_id field to device test - unlinked Conn <-> Database cases - fixed realm name - fixed valid group name test - added already exists - polished - removed common.ex Signed-off-by: Gabriele Ghio <gabriele.ghio@secomind.com>
- Loading branch information
1 parent
20fa298
commit ef71037
Showing
26 changed files
with
2,126 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
apps/astarte_appengine_api/test/astarte_appengine_api/v2_device_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# | ||
# This file is part of Astarte. | ||
# | ||
# Copyright 2024 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. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
defmodule Astarte.AppEngine.API.V2DeviceTest do | ||
use ExUnit.Case, async: true | ||
use ExUnitProperties | ||
use Astarte.Test.Cases.Device | ||
|
||
alias Ecto.Changeset | ||
alias StreamData | ||
alias Astarte.Core.Mapping | ||
alias Astarte.Core.Interface | ||
alias Astarte.AppEngine.API.Stats | ||
alias Astarte.AppEngine.API.Stats.DevicesStats | ||
alias Astarte.Test.Setups.Database, as: DatabaseSetup | ||
alias Astarte.Test.Setups.Interface, as: InterfaceSetup | ||
alias Astarte.Test.Generators.String, as: StringGenerator | ||
alias Astarte.Test.Generators.Interface, as: InterfaceGenerator | ||
alias Astarte.Test.Generators.Mapping, as: MappingGenerator | ||
alias Astarte.Test.Generators.Device, as: DeviceGenerator | ||
alias Astarte.Test.Helpers.Database, as: DatabaseHelper | ||
|
||
@moduletag :v2 | ||
@moduletag :device | ||
@moduletag interface_count: 10 | ||
@moduletag device_count: 100 | ||
|
||
describe "device generator testing" do | ||
property "validate device with pre-generated interfaces", %{interfaces: interfaces} do | ||
check all device <- DeviceGenerator.device(interfaces: interfaces) do | ||
:ok | ||
end | ||
end | ||
end | ||
|
||
describe "devices fixtures testing" do | ||
test "validate inserted devices", %{ | ||
cluster: cluster, | ||
keyspace: keyspace, | ||
devices: devices | ||
} do | ||
list = DatabaseHelper.select!(:device, cluster, keyspace, devices) | ||
fields = [:device_id, :encoded_id] | ||
|
||
for field <- fields do | ||
f = fn l -> Enum.map(l, fn d -> d[field] end) end | ||
devices_ids_a = f.(devices) | ||
devices_ids_b = f.(list) | ||
assert [] === devices_ids_a -- devices_ids_b | ||
assert [] === devices_ids_b -- devices_ids_a | ||
end | ||
end | ||
end | ||
end |
147 changes: 147 additions & 0 deletions
147
apps/astarte_appengine_api/test/astarte_appengine_api/v2_group_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
# | ||
# This file is part of Astarte. | ||
# | ||
# Copyright 2024 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. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
defmodule Astarte.AppEngine.API.V2GroupTest do | ||
use ExUnit.Case, async: true | ||
use ExUnitProperties | ||
use Astarte.Test.Cases.Group | ||
use Astarte.Test.Cases.Conn | ||
|
||
alias Astarte.AppEngine.API.Device | ||
alias Astarte.AppEngine.API.Device.DevicesList | ||
alias Astarte.AppEngine.API.Device.DeviceStatus | ||
alias Astarte.Test.Generators.Group, as: GroupGenerator | ||
alias Astarte.Test.Generators.Device, as: DeviceGenerator | ||
|
||
@moduletag :v2 | ||
@moduletag :group | ||
@moduletag interface_count: 10 | ||
@moduletag device_count: 100 | ||
@moduletag group_count: 2 | ||
|
||
describe "create" do | ||
@tag :unit | ||
property "fails when group name is not valid", %{auth_conn: auth_conn, keyspace: keyspace} do | ||
check all {{group_name, error}, devices} <- | ||
tuple({ | ||
bind(GroupGenerator.name(), fn name -> | ||
bind(integer(0..2), fn num -> | ||
constant( | ||
case num do | ||
0 -> {"", "can't be blank"} | ||
1 -> {"~" <> name, "is not valid"} | ||
2 -> {"@" <> name, "is not valid"} | ||
end | ||
) | ||
end) | ||
end), | ||
list_of(DeviceGenerator.encoded_id(), min_length: 0, max_length: 1) | ||
}) do | ||
params = %{ | ||
"group_name" => group_name, | ||
"devices" => devices | ||
} | ||
|
||
response = post(auth_conn, groups_path(auth_conn, :create, keyspace), data: params) | ||
assert [error] === json_response(response, 422)["errors"]["group_name"] | ||
end | ||
end | ||
|
||
property "fails when devices list empty", %{auth_conn: auth_conn, keyspace: keyspace} do | ||
@tag :unit | ||
check all group_name <- GroupGenerator.name() do | ||
params = %{ | ||
"group_name" => group_name, | ||
"devices" => [] | ||
} | ||
|
||
response = post(auth_conn, groups_path(auth_conn, :create, keyspace), data: params) | ||
|
||
assert ["should have at least 1 item(s)"] === | ||
json_response(response, 422)["errors"]["devices"] | ||
end | ||
end | ||
|
||
property "fails when device does not exist", %{auth_conn: auth_conn, keyspace: keyspace} do | ||
check all group_name <- GroupGenerator.name(), | ||
devices <- list_of(DeviceGenerator.encoded_id(), min_length: 1) do | ||
params = %{ | ||
"group_name" => group_name, | ||
"devices" => devices | ||
} | ||
|
||
response = post(auth_conn, groups_path(auth_conn, :create, keyspace), data: params) | ||
|
||
assert ["must exist (#{Enum.at(devices, 0)} not found)"] === | ||
json_response(response, 422)["errors"]["devices"] | ||
end | ||
end | ||
|
||
test "fails when the group already exists", %{ | ||
auth_conn: auth_conn, | ||
cluster: cluster, | ||
keyspace: keyspace, | ||
interfaces: interfaces, | ||
devices: devices, | ||
groups: groups | ||
} do | ||
device_ids = Enum.map(devices, & &1.encoded_id) | ||
existing_group_name = Enum.at(groups, 0).name | ||
|
||
params = %{ | ||
"group_name" => existing_group_name, | ||
"devices" => device_ids | ||
} | ||
|
||
response = post(auth_conn, groups_path(auth_conn, :create, keyspace), data: params) | ||
assert "Group already exists" === json_response(response, 409)["errors"]["detail"] | ||
end | ||
|
||
property "success creates groups with valid parameters", %{ | ||
auth_conn: auth_conn, | ||
cluster: cluster, | ||
keyspace: keyspace, | ||
interfaces: interfaces, | ||
devices: devices, | ||
groups: groups | ||
} do | ||
device_ids = Enum.map(devices, & &1.encoded_id) | ||
old_group_names = Enum.map(groups, & &1.name) | ||
|
||
check all group_name <- | ||
filter(GroupGenerator.name(), fn name -> name not in old_group_names end) do | ||
params = %{ | ||
"group_name" => group_name, | ||
"devices" => device_ids | ||
} | ||
|
||
response = post(auth_conn, groups_path(auth_conn, :create, keyspace), data: params) | ||
assert params === json_response(response, 201)["data"] | ||
response = get(auth_conn, groups_path(auth_conn, :show, keyspace, group_name)) | ||
assert group_name === json_response(response, 200)["data"]["group_name"] | ||
|
||
for device <- device_ids do | ||
{:ok, %DeviceStatus{groups: groups}} = Device.get_device_status!(keyspace, device) | ||
assert group_name in groups | ||
# TODO right way | ||
# assert [group_name] === groups -- old_group_names | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.