Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 333 don't allow sites starting with beacon #385

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/beacon/types/site.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ defmodule Beacon.Types.Site do

def valid?(_site), do: false

def valid_name?(site) when is_atom(site) do
valid_name?(Atom.to_string(site))
end

def valid_name?(site) when is_binary(site) do
not String.starts_with?(site, "beacon_")
end

@doc false
def safe_to_atom(site) when is_atom(site), do: site
def safe_to_atom(site) when is_binary(site), do: String.to_existing_atom(site)
Expand Down
1 change: 1 addition & 0 deletions lib/mix/tasks/install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ defmodule Mix.Tasks.Beacon.Install do

cond do
!Beacon.Types.Site.valid?(site) -> raise_with_help!("Invalid site name. It should not contain special characters.")
!Beacon.Types.Site.valid_name?(site) -> raise_with_help!("Invalid site name. The site name can't start with \"beacon_\".")
:default -> options
end
end
Expand Down
10 changes: 10 additions & 0 deletions test/beacon/types/site_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ defmodule Beacon.Types.SiteTest do
assert Site.load("site") == {:ok, :site}
assert Site.load(0) == :error
end

describe "valid_name?/1" do
test "SUCCESS: Return TRUE if it is a valid name" do
assert Site.valid_name?("some_name")
end

test "SUCCESS: Return FALSE if it is an invalid name" do
refute Site.valid_name?("beacon_some_name")
end
end
end
5 changes: 5 additions & 0 deletions test/mix/tasks/install_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ defmodule Mix.Tasks.Beacon.InstallTest do
Install.run(["--site", "my@site!"])
end

# Invalid site name
assert_raise Mix.Error, fn ->
Install.run(["--site", "beacon_"])
end

# Invalid option
assert_raise OptionParser.ParseError, ~r/1 error found!\n--invalid-argument : Unknown option/, fn ->
Install.run(["--invalid-argument", "invalid"])
Expand Down