-
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 Signed-off-by: Gabriele Ghio <[email protected]>
- Loading branch information
1 parent
20fa298
commit a046b19
Showing
26 changed files
with
1,551 additions
and
5 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
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
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" 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 db" do | ||
test "validate insered 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 |
129 changes: 129 additions & 0 deletions
129
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,129 @@ | ||
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.Setups.Database, as: DatabaseSetup | ||
alias Astarte.Test.Setups.Conn, as: ConnSetup | ||
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 | ||
property "wrong group name", %{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 "devices list empty", %{auth_conn: auth_conn, keyspace: keyspace} do | ||
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 "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 | ||
|
||
property "rejects an already existing group", %{ | ||
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 "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 |
50 changes: 50 additions & 0 deletions
50
apps/astarte_appengine_api/test/astarte_appengine_api/v2_interface_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,50 @@ | ||
defmodule Astarte.AppEngine.API.V2InterfaceTest do | ||
use ExUnit.Case, async: true | ||
use ExUnitProperties | ||
use Astarte.Test.Cases.Interface | ||
|
||
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 :interface | ||
@moduletag interface_count: 10 | ||
|
||
describe "interface generator" do | ||
@tag timeout: :infinity | ||
property "validate interface" do | ||
check all interface <- InterfaceGenerator.interface() do | ||
%Changeset{valid?: valid, errors: errors} = Interface.changeset(interface) | ||
|
||
assert valid, "Invalid interface: " <> (errors |> Enum.join(", ")) | ||
end | ||
end | ||
end | ||
|
||
describe "interfaces db" do | ||
test "validate insered interfaces names", %{ | ||
cluster: cluster, | ||
keyspace: keyspace, | ||
interfaces: interfaces | ||
} do | ||
list = DatabaseHelper.select!(:interface, cluster, keyspace, interfaces) | ||
f = fn l -> Enum.map(l, fn i -> i.name end) end | ||
interfaces_names_a = f.(interfaces) | ||
interfaces_names_b = f.(list) | ||
|
||
assert [] === interfaces_names_a -- interfaces_names_b | ||
assert [] === interfaces_names_b -- interfaces_names_a | ||
end | ||
end | ||
end |
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,22 @@ | ||
defmodule Astarte.Test.Cases.Conn do | ||
use ExUnit.CaseTemplate | ||
|
||
using do | ||
quote do | ||
import Plug.Conn | ||
import Phoenix.ConnTest | ||
import Astarte.AppEngine.APIWeb.Router.Helpers | ||
|
||
# The default endpoint for testing | ||
@endpoint Astarte.AppEngine.APIWeb.Endpoint | ||
end | ||
end | ||
|
||
alias Astarte.Test.Setups.Conn, as: ConnSetup | ||
|
||
setup_all [ | ||
{ConnSetup, :create_conn}, | ||
{ConnSetup, :jwt}, | ||
{ConnSetup, :auth_conn} | ||
] | ||
end |
18 changes: 18 additions & 0 deletions
18
apps/astarte_appengine_api/test/support_v2/cases/database.ex
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,18 @@ | ||
defmodule Astarte.Test.Cases.Database do | ||
use ExUnit.CaseTemplate | ||
|
||
using do | ||
quote do | ||
# Do nothing for now | ||
end | ||
end | ||
|
||
alias Astarte.Test.Setups.Database, as: DatabaseSetup | ||
|
||
setup_all [ | ||
{DatabaseSetup, :connect}, | ||
{DatabaseSetup, :keyspace}, | ||
{DatabaseSetup, :setup}, | ||
{DatabaseSetup, :setup_auth} | ||
] | ||
end |
17 changes: 17 additions & 0 deletions
17
apps/astarte_appengine_api/test/support_v2/cases/device.ex
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,17 @@ | ||
defmodule Astarte.Test.Cases.Device do | ||
use ExUnit.CaseTemplate | ||
use Astarte.Test.Cases.Interface | ||
|
||
using do | ||
quote do | ||
# Do nothing for now | ||
end | ||
end | ||
|
||
alias Astarte.Test.Setups.Device, as: DeviceSetup | ||
|
||
setup_all [ | ||
{DeviceSetup, :init}, | ||
{DeviceSetup, :setup} | ||
] | ||
end |
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,17 @@ | ||
defmodule Astarte.Test.Cases.Group do | ||
use ExUnit.CaseTemplate | ||
use Astarte.Test.Cases.Device | ||
|
||
using do | ||
quote do | ||
# Do nothing for now | ||
end | ||
end | ||
|
||
alias Astarte.Test.Setups.Group, as: GroupSetup | ||
|
||
setup_all [ | ||
{GroupSetup, :init}, | ||
{GroupSetup, :setup} | ||
] | ||
end |
Oops, something went wrong.