Skip to content

Commit

Permalink
Merge 9d2131c into 0ff12df
Browse files Browse the repository at this point in the history
  • Loading branch information
danielberkompas committed Jun 17, 2022
2 parents 0ff12df + 9d2131c commit da2897a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/cloak_ecto/migrator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ defmodule Cloak.Ecto.Migrator do
false
end

defp cloak_field?({field, {kind, inner_type}}) when kind in [:array, :map] do
cloak_field?({field, inner_type})
end

defp cloak_field?({_field, type}) do
Code.ensure_loaded?(type) && function_exported?(type, :__cloak__, 0)
end
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ defmodule Cloak.Ecto.MixProject do

defp aliases do
[
test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"]
test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"],
"ecto.reset": ["ecto.drop", "ecto.create", "ecto.migrate"]
]
end
end
22 changes: 20 additions & 2 deletions test/cloak_ecto/migrator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,19 @@ defmodule Cloak.Ecto.MigratorTest do
end

@post_title "Test Title"
@post_tags ["test", "encryption"]

describe ".migrate/2 with binary ids" do
setup do
now = DateTime.utc_now()
encrypted_title = Cloak.Ecto.TestVault.encrypt!(@post_title, :secondary)
encrypted_title = Vault.encrypt!(@post_title, :secondary)
encrypted_tags = Enum.map(@post_tags, &Vault.encrypt!(&1, :secondary))

posts =
for _ <- 1..500 do
%{
title: encrypted_title,
tags: encrypted_tags,
comments: [%{author: "Daniel", body: "Comment"}],
inserted_at: now,
updated_at: now
Expand All @@ -98,15 +101,30 @@ defmodule Cloak.Ecto.MigratorTest do
Migrator.migrate(Repo, Cloak.Ecto.TestPost)
end)

assert io =~ "__cloak_cursor_fields__", "Did not call __cloak_cursor_fields__ on schema!"

titles =
"posts"
|> select([:title])
|> Repo.all()
|> Enum.map(&decrypt(&1.title, :default))
|> Enum.uniq()

assert io =~ "__cloak_cursor_fields__", "Did not call __cloak_cursor_fields__ on schema!"
assert titles == [{:ok, @post_title}], "Not all titles were migrated!"

tags =
"posts"
|> select([:tags])
|> Repo.all()
|> Enum.map(fn %{tags: tags} ->
tags
|> Enum.map(&decrypt(&1, :default))
|> Enum.map(&elem(&1, 1))
end)
|> Enum.uniq()
|> List.flatten()

assert tags == @post_tags, "Not all tags were migrated!"
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/support/migrations/20180915035851_create_posts.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule Cloak.Ecto.TestRepo.Migrations.CreatePosts do
create table(:posts, primary_key: false) do
add(:id, :uuid, primary_key: true, default: fragment("uuid_generate_v4()"))
add(:title, :binary)
add(:tags, {:array, :binary})
add(:metadata, {:map, :string})
add(:comments, {:array, :map})
timestamps(type: :utc_datetime)
end
Expand Down
2 changes: 2 additions & 0 deletions test/support/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule Cloak.Ecto.TestPost do

schema "posts" do
field(:title, Cloak.Ecto.Encrypted.Binary)
field(:tags, {:array, Cloak.Ecto.Encrypted.Binary})
field(:metadata, {:map, :string})
embeds_many(:comments, Cloak.Ecto.TestComment)
timestamps(type: :utc_datetime)
end
Expand Down

0 comments on commit da2897a

Please sign in to comment.