Skip to content

Commit

Permalink
Add testing for {:array, type} fields in Ecto with encrypted members
Browse files Browse the repository at this point in the history
  • Loading branch information
ACBullen committed Sep 18, 2020
1 parent b7bdea2 commit 3dd87cb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
14 changes: 14 additions & 0 deletions test/cloak_ecto/migrator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,20 @@ defmodule Cloak.Ecto.MigratorTest do
end

@post_title "Test Title"
@post_tag "test-tag"

describe ".migrate/2 with binary ids" do
setup do
now = DateTime.utc_now()
encrypted_title = Cloak.Ecto.TestVault.encrypt!(@post_title, :secondary)
encrypted_tag = Cloak.Ecto.TestVault.encrypt!(@post_tag, :secondary)

posts =
for _ <- 1..500 do
%{
title: encrypted_title,
comments: [%{author: "Daniel", body: "Comment"}],
tags: [encrypted_tag],
inserted_at: now,
updated_at: now
}
Expand All @@ -105,8 +108,19 @@ defmodule Cloak.Ecto.MigratorTest do
|> Enum.map(&decrypt(&1.title, :default))
|> Enum.uniq()

tags =
"posts"
|> select([:tags])
|> Repo.all()
|> Enum.map(fn post ->
[tag] = post.tags
decrypt(tag, :default)
end)
|> 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!"
assert tags == [{:ok, @post_tag}]
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule ScannerData.Repo.Migrations.GivePostsArrayField do
use Ecto.Migration

def change do
alter table(:posts) do
add(:tags, {:array, :binary}, default: [])
end
end
end
1 change: 1 addition & 0 deletions test/support/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Cloak.Ecto.TestPost do
schema "posts" do
field(:title, Cloak.Ecto.Encrypted.Binary)
embeds_many(:comments, Cloak.Ecto.TestComment)
field(:tags, {:array, Cloak.Ecto.Encrypted.Binary}, default: [])
timestamps(type: :utc_datetime)
end

Expand Down

0 comments on commit 3dd87cb

Please sign in to comment.