Skip to content

Commit

Permalink
update tests to use EmailHash field, #29
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonLab committed Dec 3, 2019
1 parent 1b4550c commit 027bc20
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
12 changes: 6 additions & 6 deletions lib/app/ctx/person.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule App.Ctx.Person do

schema "people" do
field :email, Fields.EmailEncrypted
field :email_hash, :binary
field :email_hash, Fields.EmailHash
field :familyName, :binary
field :givenName, :binary
field :locale, :string
Expand All @@ -22,12 +22,12 @@ defmodule App.Ctx.Person do
end

@doc false
def changeset(person, attrs) do
def changeset(%{email: email} = person, attrs) do
{:ok, emailHash } = Fields.EmailHash.dump(email)
person
|> cast(attrs, [:username, :username_hash, :email, :email_hash, :givenName, :familyName, :password_hash, :key_id, :locale, :picture])
|> validate_required([:username, :username_hash, :email, :givenName, :familyName, :password_hash, :key_id])
# |> Map.put(:email_hash, Fields.EmailHash.dump(person["email"]))
# |> IO.inspect(label: "person")
|> cast(attrs, [:username, :email, :givenName, :familyName, :password_hash, :key_id, :locale, :picture])
|> validate_required([:username, :email, :givenName, :familyName, :password_hash, :key_id])
|> put_change(:email_hash, emailHash )
end

def google_changeset(profile, attrs) do
Expand Down
4 changes: 0 additions & 4 deletions lib/app_web/templates/person/form.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
<%= text_input f, :email %>
<%= error_tag f, :email %>

<%= label f, :email_hash %>
<%= text_input f, :email_hash %>
<%= error_tag f, :email_hash %>

<%= label f, :givenName %>
<%= text_input f, :givenName %>
<%= error_tag f, :givenName %>
Expand Down
22 changes: 9 additions & 13 deletions test/app/ctx_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,27 @@ defmodule App.CtxTest do
end

test "list_people/0 returns all people" do
person = person_fixture()
assert Ctx.list_people() == [person]
person_fixture()
assert Enum.count(Ctx.list_people()) >= 1
end

test "get_person!/1 returns the person with given id" do
person = person_fixture()
# IO.inspect person, label: "person"
# res = Ctx.get_person!(person.id)
# IO.inspect res, label: "Ctx.get_person!(person.id)"
assert Ctx.get_person!(person.id) == person
person_data = person_fixture()
person = Ctx.get_person!(person_data.id)

assert person_data.email == person.email
end

test "create_person/1 with valid data creates a person" do
assert {:ok, %Person{} = person} = Ctx.create_person(@valid_attrs)
assert person.email == "[email protected]"
# <<116, 223, 252, 249, 57, 13, 89, 186, 199, 10, 177, 236, 117, 117, 76, 147,
# 109, 87, 187, 126, 168, 1, 63, 236, 134, 67, 92, 136, 136, 224, 45, 65>>
assert person.email_hash == "some email_hash"
assert person.familyName == "some familyName"
assert person.givenName == "some givenName"
assert person.key_id == 42
assert person.password_hash == "some password_hash"
assert person.username == "some username"
assert person.username_hash == "some username_hash"
end

test "create_person/1 with invalid data returns error changeset" do
Expand All @@ -173,19 +170,18 @@ defmodule App.CtxTest do
person = person_fixture()
assert {:ok, %Person{} = person} = Ctx.update_person(person, @update_attrs)
assert person.email == "[email protected]"
assert person.email_hash == "some updated email_hash"
assert person.familyName == "some updated familyName"
assert person.givenName == "some updated givenName"
assert person.key_id == 43
assert person.password_hash == "some updated password_hash"
assert person.username == "updated username"
assert person.username_hash == "updated username_hash"
end

test "update_person/2 with invalid data returns error changeset" do
person = person_fixture()
assert {:error, %Ecto.Changeset{}} = Ctx.update_person(person, @invalid_attrs)
assert person == Ctx.get_person!(person.id)
person_data = Ctx.get_person!(person.id)

assert person.id == person_data.id
end

test "delete_person/1 deletes the person" do
Expand Down

0 comments on commit 027bc20

Please sign in to comment.