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

Handle nil urls for skipped versions #6

Merged
merged 2 commits into from
Sep 14, 2019
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: 5 additions & 3 deletions lib/waffle_ecto/definition.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ defmodule Waffle.Ecto.Definition do
if options[:signed] do
url
else
case updated_at do
%NaiveDateTime{} ->
case {url, updated_at} do
{nil, _} -> nil

{_, %NaiveDateTime{}} ->
version_url(updated_at, url)

string when is_bitstring(updated_at) ->
{_, string} when is_bitstring(updated_at) ->
version_url(NaiveDateTime.from_iso8601!(string), url)

_ ->
Expand Down
6 changes: 6 additions & 0 deletions test/definition_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ defmodule WaffleTest.Ecto.Definition do
url = DummyDefinitionTwo.url({%{file_name: "test.png", updated_at: nil}, :scope}, :original, [])
assert url == "fallback"
end

test "url is nil if version is skipped" do
updated_at = NaiveDateTime.from_erl!({{2015, 1, 1}, {1, 1, 1}})
url = DummyDefinitionTwo.url({%{file_name: "test.png", updated_at: updated_at}, :scope}, :skipped, [])
assert is_nil(url)
end
end
6 changes: 3 additions & 3 deletions test/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ defmodule WaffleTest.Ecto.Schema do
end

test_with_mock "allow_paths => true", DummyDefinition, [store: fn({"/path/to/my/file.png", %TestUser{}}) -> {:ok, "file.png"} end] do
_changeset = TestUser.path_changeset(%TestUser{}, %{"avatar" => "/path/to/my/file.png"})
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"})
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"})
TestUser.url_changeset(%TestUser{}, %{"avatar" => "/path/to/my/file.png"})
assert not called DummyDefinition.store({"/path/to/my/file.png", %TestUser{}})
end
end
1 change: 1 addition & 0 deletions test/support/dummy_definition_two.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule DummyDefinitionTwo do
def url(_, :skipped, _), do: nil
def url(_, :original, _), do: "fallback"
def url(_, :signed, _), do: "fallback?a=1&b=2"
def store({file, _}), do: {:ok, file}
Expand Down