Skip to content
Draft
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
4 changes: 4 additions & 0 deletions lib/waffle_ecto/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ defmodule Waffle.Ecto.Schema do
upload = %{upload | path: path}
if path_allowed?(path, options), do: [{field, {upload, scope}} | fields], else: fields

{field, upload = %{filename: filename, stream: stream}}, fields
when is_binary(filename) and is_map(stream) ->
[{field, {upload, scope}} | fields]

# If casting a binary (path), ensure we've explicitly allowed paths
{field, path}, fields when is_binary(path) ->
path = String.trim(path)
Expand Down
15 changes: 15 additions & 0 deletions test/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,19 @@ defmodule WaffleTest.Ecto.Schema do
)
)
end

test_with_mock "casting stream data", DummyDefinition,
store: fn {%{filename: "/path/to/my/file.png", stream: %Stream{}}, %TestUser{}} ->
{:ok, "file.png"}
end do
TestUser.changeset(%TestUser{}, %{
"avatar" => %{filename: "/path/to/my/file.png", stream: %Stream{}}
})

assert called(
DummyDefinition.store(
{%{filename: "/path/to/my/file.png", stream: %Stream{}}, %TestUser{}}
)
)
end
end