-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
astarte_appengine_api_web: add tests to Plug.GroupNameDecoder
use of the ExUnitProperties/StreamData (https://hexdocs.pm/stream_data/ExUnitProperties.html) library to parameterize test writing - use of generators (gen all) for creating cases to evaluate and checks - execution of tests via prop tests (check all) - added functional e2e test to "group creates the group with / in group name" - setup test.exs and ci.exs max runs Signed-off-by: Gabriele Ghio <[email protected]>
- Loading branch information
1 parent
01e0fff
commit 998f385
Showing
11 changed files
with
141 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[ | ||
import_deps: [:phoenix, :ecto, :skogsra], | ||
import_deps: [:phoenix, :ecto, :skogsra, :stream_data], | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,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 |
---|---|---|
@@ -1 +1,4 @@ | ||
import Config | ||
|
||
config :stream_data, | ||
max_runs: 1_000 |
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
21 changes: 19 additions & 2 deletions
21
apps/astarte_appengine_api/lib/astarte_appengine_api_web/plug/group_name_decoder.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
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
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
49 changes: 49 additions & 0 deletions
49
apps/astarte_appengine_api/test/astarte_appengine_api_web/plug/group_name_decoder_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,49 @@ | ||
# | ||
# 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.APIWeb.Plug.GroupNameDecoderTest do | ||
use Astarte.AppEngine.APIWeb.ConnCase | ||
use ExUnitProperties | ||
alias Astarte.AppEngine.APIWeb.Plug.GroupNameDecoder | ||
alias Astarte.AppEngine.API.GroupTestGenerator | ||
|
||
@max_subpath_count 10 | ||
|
||
@tag issue: 904 | ||
property "call/2 decode path" do | ||
check all raw <- GroupTestGenerator.group_name() do | ||
conn = build_conn() | ||
# Normally, phx does encoding | ||
encoded = | ||
raw | ||
|> URI.encode() | ||
|
||
request = | ||
put_in( | ||
conn.path_params["group_name"], | ||
encoded | ||
) | ||
|
||
%{path_params: %{"group_name" => check}} = | ||
request | ||
|> GroupNameDecoder.call(nil) | ||
|
||
assert check == raw, | ||
"The group name #{encoded} cannot be decoded back to its original value" | ||
end | ||
end | ||
end |
43 changes: 43 additions & 0 deletions
43
apps/astarte_appengine_api/test/support/generators/group.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,43 @@ | ||
# | ||
# 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.GroupTestGenerator do | ||
@moduledoc """ | ||
Helpers group's generators | ||
""" | ||
use ExUnitProperties | ||
|
||
@max_subpath_count 10 | ||
@max_subpath_length 20 | ||
|
||
@doc """ | ||
Generate a random group name | ||
Es. | ||
world/europe/italy | ||
""" | ||
def group_name do | ||
string(:ascii, min_length: 1, max_length: @max_subpath_length) | ||
|> uniq_list_of( | ||
min_length: 1, | ||
max_length: @max_subpath_count | ||
) | ||
|> filter(fn [<<first, _::binary>> | _] -> | ||
first not in [?@, ?~, ?\s] | ||
end) | ||
|> map(&Enum.join(&1, "/")) | ||
end | ||
end |