Skip to content

Commit

Permalink
Merge pull request #90 from joebew42/allow_urls-feature
Browse files Browse the repository at this point in the history
fixes #89: add the allow_urls feature
  • Loading branch information
stavro committed May 19, 2019
2 parents 25b0ca8 + 77ee7b7 commit 8dea5cb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/arc_ecto/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ defmodule Arc.Ecto.Schema do

# If casting a binary (path), ensure we've explicitly allowed paths
{field, path}, fields when is_binary(path) ->
if Keyword.get(options, :allow_paths, false) do
[{field, {path, scope}} | fields]
else
fields
cond do
Keyword.get(options, :allow_urls, false) and Regex.match?( ~r/^https?:\/\// , path) -> [{field, {path, scope}} | fields]
Keyword.get(options, :allow_paths, false) -> [{field, {path, scope}} | fields]
true -> fields
end
end)
|> Enum.into(%{})
Expand Down
17 changes: 17 additions & 0 deletions test/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ defmodule ArcTest.Ecto.Schema do
|> validate_required(:avatar)
end

def url_changeset(user, params \\ :invalid) do
user
|> cast(params, ~w(first_name)a)
|> cast_attachments(params, ~w(avatar)a, allow_urls: true)
|> validate_required(:avatar)
end

def changeset2(user, params \\ :invalid) do
user
|> cast(params, ~w(first_name)a)
Expand Down Expand Up @@ -96,4 +103,14 @@ defmodule ArcTest.Ecto.Schema do
_changeset = TestUser.path_changeset(%TestUser{}, %{"avatar" => "/path/to/my/file.png"})
assert called DummyDefinition.store({"/path/to/my/file.png", %TestUser{}})
end

test_with_mock "allow_urls => true", DummyDefinition, [store: fn({"http://external.url/file.png", %TestUser{}}) -> {:ok, "file.png"} end] do
changeset = TestUser.url_changeset(%TestUser{}, %{"avatar" => "http://external.url/file.png"})
assert called DummyDefinition.store({"http://external.url/file.png", %TestUser{}})
end

test_with_mock "allow_urls => true with an invalid URL", DummyDefinition, [store: fn({"/path/to/my/file.png", %TestUser{}}) -> {:ok, "file.png"} end] do
changeset = TestUser.url_changeset(%TestUser{}, %{"avatar" => "/path/to/my/file.png"})
assert not called DummyDefinition.store({"/path/to/my/file.png", %TestUser{}})
end
end

0 comments on commit 8dea5cb

Please sign in to comment.