zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

commit 8835586855f48f3f61fa4d1fe89250d4c99c0626
parent 22209af4c9be76fd8c5ffa1f5163bb4ba156f77e
Author: srfsh <dev@srf.sh>
Date:   Wed,  3 Aug 2022 10:32:33 +0300

ZenflowsTest: support image fields

Diffstat:
Mtest/help/factory.ex | 17+++++++++++------
Mtest/vf/agent/domain.test.exs | 4++--
Mtest/vf/agent/type.test.exs | 117+++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
Mtest/vf/economic_event/domain.test.exs | 24++++++++++++------------
Mtest/vf/economic_resource/domain.test.exs | 5++---
Mtest/vf/intent.test.exs | 2+-
Mtest/vf/organization/domain.test.exs | 4++--
Mtest/vf/organization/type.test.exs | 114++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mtest/vf/person/domain.test.exs | 4++--
Mtest/vf/person/type.test.exs | 242++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mtest/vf/recipe_resource/domain.test.exs | 4++--
Mtest/vf/recipe_resource/type.test.exs | 143++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mtest/vf/resource_specification/domain.test.exs | 4++--
Mtest/vf/resource_specification/type.test.exs | 129+++++++++++++++++++++++++++++++++++++------------------------------------------
Mtest/vf/validate.test.exs | 34++++++++++++++++++++++++++++++++++
15 files changed, 451 insertions(+), 396 deletions(-)

diff --git a/test/help/factory.ex b/test/help/factory.ex @@ -91,6 +91,11 @@ def uri() do uniq("schema://user@host:port/path") end +@doc "Returns a unique base64 encoded string (mock-up of image data)" +def img() do + :rand.bytes(200) |> Base.encode64(padding: true) +end + @doc "Inserts a schema into the database with field overrides." @spec insert!(atom(), %{required(atom()) => term()}) :: struct() def insert!(name, attrs \\ %{}) do @@ -166,7 +171,7 @@ def build(:resource_specification) do name: uniq("some name"), resource_classified_as: uniq_list("some uri"), note: uniq("some note"), - image: uri(), + image: img(), default_unit_of_effort: build(:unit), default_unit_of_resource: build(:unit), } @@ -181,7 +186,7 @@ def build(:recipe_resource) do resource_conforms_to: build(:resource_specification), substitutable: bool(), note: uniq("some note"), - image: uri(), + image: img(), } end @@ -225,7 +230,7 @@ def build(:person) do %VF.Person{ type: :per, name: uniq("some name"), - image: uri(), + image: img(), note: uniq("some note"), primary_location: build(:spatial_thing), user: uniq("some user"), @@ -245,7 +250,7 @@ def build(:organization) do %VF.Organization{ type: :org, name: uniq("some name"), - image: uri(), + image: img(), classified_as: uniq_list("some uri"), note: uniq("some note"), primary_location: build(:spatial_thing), @@ -354,7 +359,7 @@ def build(:economic_resource) do %VF.EconomicResource{ name: uniq("some name"), note: uniq("some note"), - image: uri(), + image: img(), tracking_identifier: uniq("some tracking identifier"), classified_as: uniq_list("some uri"), conforms_to: build(:resource_specification), @@ -413,7 +418,7 @@ def build(:intent) do has_point_in_time: DateTime.utc_now(), due: DateTime.utc_now(), finished: bool(), - image: uri(), + image: img(), note: uniq("some note"), # in_scope_of: agreed_in: uniq("some uri"), diff --git a/test/vf/agent/domain.test.exs b/test/vf/agent/domain.test.exs @@ -36,7 +36,7 @@ describe "by_id/1" do assert agent.type == per.type and agent.type == :per assert agent.name == per.name assert agent.note == per.note - assert agent.image == nil # per.image + assert agent.image == per.image assert agent.primary_location_id == per.primary_location_id # person @@ -55,7 +55,7 @@ describe "by_id/1" do assert agent.type == org.type and agent.type == :org assert agent.name == org.name assert agent.note == org.note - assert agent.image == nil # org.image + assert agent.image == org.image assert agent.primary_location_id == org.primary_location_id # person diff --git a/test/vf/agent/type.test.exs b/test/vf/agent/type.test.exs @@ -25,16 +25,19 @@ setup do } end +@tag skip: "TODO: sign query" test "Query myAgent" do assert %{data: %{"myAgent" => data}} = - query!(""" - myAgent { - id - name - image - note + run!(""" + query { + myAgent { + id + name + image + note + } } - """) + """, auth?: true) assert {:ok, _} = Zenflows.DB.ID.cast(data["id"]) assert data["name"] == "hello" @@ -45,87 +48,105 @@ end describe "Query agent()" do test "as Agent with Person concrete type", %{per: per} do assert %{data: %{"agent" => data}} = - query!(""" - agent(id: "#{per.id}") { - id - name - note - image - primaryLocation { id } + run!(""" + query ($id: ID!) { + agent(id: $id) { + id + name + note + image + primaryLocation { id } + } } - """) + """, vars: %{"id" => per.id}) assert data["id"] == per.id assert data["name"] == per.name assert data["note"] == per.note - assert data["image"] == nil + assert data["image"] == per.image assert data["primaryLocation"]["id"] == per.primary_location_id end test "as Agent with Organization concrete type", %{org: org} do assert %{data: %{"agent" => data}} = - query!(""" - agent(id: "#{org.id}") { - id - name - note - image - primaryLocation { id } + run!(""" + query ($id: ID!) { + agent(id: $id) { + id + name + note + image + primaryLocation { id } + } } - """) + """, vars: %{"id" => org.id}) assert data["id"] == org.id assert data["name"] == org.name assert data["note"] == org.note - assert data["image"] == nil + assert data["image"] == org.image assert data["primaryLocation"]["id"] == org.primary_location_id end test "as Person", %{per: per} do assert %{data: %{"agent" => data}} = - query!(""" - agent(id: "#{per.id}") { - ... on Person { - id - name - note - image - primaryLocation { id } - user - email + run!(""" + query($id: ID!) { + agent(id: $id) { + ... on Person { + id + name + note + image + primaryLocation { id } + user + email + ecdhPublicKey + eddsaPublicKey + ethereumAddress + reflowPublicKey + schnorrPublicKey + } } } - """) + """, vars: %{"id" => per.id}) assert data["id"] == per.id assert data["name"] == per.name assert data["note"] == per.note - assert data["image"] == nil + assert data["image"] == per.image assert data["primaryLocation"]["id"] == per.primary_location_id assert data["user"] == per.user assert data["email"] == per.email + assert data["ecdhPublicKey"] == per.ecdh_public_key + assert data["eddsaPublicKey"] == per.eddsa_public_key + assert data["ethereumAddress"] == per.ethereum_address + assert data["reflowPublicKey"] == per.reflow_public_key + assert data["schnorrPublicKey"] == per.schnorr_public_key end test "as Organization", %{org: org} do assert %{data: %{"agent" => data}} = - query!(""" - agent(id: "#{org.id}") { - ... on Organization { - id - name - note - image - primaryLocation { id } - classifiedAs + run!(""" + query ($id: ID!) { + agent(id: $id) { + ... on Organization { + id + name + note + image + primaryLocation { id } + classifiedAs + } } } - """) + """, vars: %{"id" => org.id}) assert data["id"] == org.id assert data["name"] == org.name assert data["note"] == org.note - assert data["image"] == nil + assert data["image"] == org.image assert data["primaryLocation"]["id"] == org.primary_location_id assert data["classifiedAs"] == org.classified_as diff --git a/test/vf/economic_event/domain.test.exs b/test/vf/economic_event/domain.test.exs @@ -119,7 +119,7 @@ describe "`create/2` with raise:" do res_params = %{ name: Factory.str("name"), note: Factory.str("note"), - # image:, + image: Factory.img(), tracking_identifier: Factory.str("tracking identifier"), lot_id: Factory.insert!(:product_batch).id, } @@ -128,7 +128,7 @@ describe "`create/2` with raise:" do assert res.name == res_params.name assert res.note == res_params.note - #assert res.image == res_params.image + assert res.image == res_params.image assert res.tracking_identifier == res_params.tracking_identifier assert res.lot_id == res_params.lot_id @@ -219,7 +219,7 @@ describe "`create/2` with produce:" do res_params = %{ name: Factory.str("name"), note: Factory.str("note"), - # image:, + image: Factory.img(), tracking_identifier: Factory.str("tracking identifier"), lot_id: Factory.insert!(:product_batch).id, } @@ -228,7 +228,7 @@ describe "`create/2` with produce:" do assert res.name == res_params.name assert res.note == res_params.note - #assert res.image == res_params.image + assert res.image == res_params.image assert res.tracking_identifier == res_params.tracking_identifier assert res.lot_id == res_params.lot_id @@ -783,7 +783,7 @@ describe "`create/2` with transferCustody:" do res_params = %{ name: Factory.str("name"), note: Factory.str("note"), - # image:, + image: Factory.img(), tracking_identifier: Factory.str("tracking identifier"), lot_id: Factory.insert!(:product_batch).id, } @@ -797,7 +797,7 @@ describe "`create/2` with transferCustody:" do assert to_res.name == res_params.name assert to_res.note == res_params.note - #assert to_res.image == res_params.image + assert to_res.image == res_params.image assert to_res.tracking_identifier == res_params.tracking_identifier assert to_res.lot_id == res_params.lot_id @@ -1025,7 +1025,7 @@ describe "`create/2` with transferAllRights:" do res_params = %{ name: Factory.str("name"), note: Factory.str("note"), - # image:, + image: Factory.img(), tracking_identifier: Factory.str("tracking identifier"), lot_id: Factory.insert!(:product_batch).id, } @@ -1039,7 +1039,7 @@ describe "`create/2` with transferAllRights:" do assert to_res.name == res_params.name assert to_res.note == res_params.note - #assert to_res.image == res_params.image + assert to_res.image == res_params.image assert to_res.tracking_identifier == res_params.tracking_identifier assert to_res.lot_id == res_params.lot_id @@ -1266,7 +1266,7 @@ describe "`create/2` with transfer:" do res_params = %{ name: Factory.str("name"), note: Factory.str("note"), - # image:, + image: Factory.img(), tracking_identifier: Factory.str("tracking identifier"), lot_id: Factory.insert!(:product_batch).id, } @@ -1280,7 +1280,7 @@ describe "`create/2` with transfer:" do assert to_res.name == res_params.name assert to_res.note == res_params.note - #assert to_res.image == res_params.image + assert to_res.image == res_params.image assert to_res.tracking_identifier == res_params.tracking_identifier assert to_res.lot_id == res_params.lot_id @@ -1536,7 +1536,7 @@ describe "`create/2` with move:" do res_params = %{ name: Factory.str("name"), note: Factory.str("note"), - # image:, + image: Factory.img(), tracking_identifier: Factory.str("tracking identifier"), lot_id: Factory.insert!(:product_batch).id, } @@ -1550,7 +1550,7 @@ describe "`create/2` with move:" do assert to_res.name == res_params.name assert to_res.note == res_params.note - #assert to_res.image == res_params.image + assert to_res.image == res_params.image assert to_res.tracking_identifier == res_params.tracking_identifier assert to_res.lot_id == res_params.lot_id diff --git a/test/vf/economic_resource/domain.test.exs b/test/vf/economic_resource/domain.test.exs @@ -37,7 +37,7 @@ setup ctx do params = %{ name: Factory.uniq("name"), note: Factory.uniq("note"), - image: Factory.uri(), + image: Factory.img(), tracking_identifier: Factory.uniq("tracking identifier"), classified_as: Factory.uniq_list("uri"), conforms_to_id: Factory.insert!(:resource_specification).id, @@ -87,8 +87,7 @@ describe "update/2" do assert new.onhand_quantity_has_numerical_value == old.onhand_quantity_has_numerical_value assert new.current_location_id == old.current_location_id assert new.note == old.note - # virtual fields atm - # assert new.image == old.image + assert new.image == old.image assert new.unit_of_effort_id == old.unit_of_effort_id assert new.stage_id == old.stage_id assert new.state_id == old.state_id diff --git a/test/vf/intent.test.exs b/test/vf/intent.test.exs @@ -48,7 +48,7 @@ setup do has_point_in_time: DateTime.utc_now(), due: DateTime.utc_now(), finished: Factory.bool(), - image: Factory.uri(), + image: Factory.img(), note: Factory.uniq("note"), # in_scope_of_id: agreed_in: Factory.uniq("uri"), diff --git a/test/vf/organization/domain.test.exs b/test/vf/organization/domain.test.exs @@ -27,7 +27,7 @@ setup ctx do else params = %{ name: Factory.uniq("name"), - image: Factory.uri(), + image: Factory.img(), classified_as: Factory.uniq_list("uri"), note: Factory.uniq("note"), primary_location_id: Factory.insert!(:spatial_thing).id, @@ -96,7 +96,7 @@ describe "update/2" do assert new.name == old.name assert new.classified_as == old.classified_as assert new.note == old.note - assert new.image == nil # old.image + assert new.image == old.image assert new.primary_location_id == old.primary_location_id end end diff --git a/test/vf/organization/type.test.exs b/test/vf/organization/type.test.exs @@ -21,11 +21,11 @@ use ZenflowsTest.Help.AbsinCase, async: true setup do %{ params: %{ - name: Factory.uniq("name"), - # image - classified_as: Factory.uniq_list("uri"), - note: Factory.uniq("note"), - primary_location_id: Factory.insert!(:spatial_thing).id, + "name" => Factory.uniq("name"), + "image" => Factory.img(), + "classifiedAs" => Factory.uniq_list("uri"), + "note" => Factory.uniq("note"), + "primaryLocation" => Factory.insert!(:spatial_thing).id, }, org: Factory.insert!(:organization), } @@ -34,19 +34,23 @@ end describe "Query" do test "organization()", %{org: org} do assert %{data: %{"organization" => data}} = - query!(""" - organization(id: "#{org.id}") { - id - name - note - primaryLocation { id } - classifiedAs + run!(""" + query ($id: ID!) { + organization(id: $id) { + id + name + note + image + primaryLocation { id } + classifiedAs + } } - """) + """, vars: %{"id" => org.id}) assert data["id"] == org.id assert data["name"] == org.name assert data["note"] == org.note + assert data["image"] == org.image assert data["primaryLocation"]["id"] == org.primary_location_id assert data["classifiedAs"] == org.classified_as end @@ -55,62 +59,64 @@ end describe "Mutation" do test "createOrganization", %{params: params} do assert %{data: %{"createOrganization" => %{"agent" => data}}} = - mutation!(""" - createOrganization(organization: { - name: "#{params.name}" - note: "#{params.note}" - primaryLocation: "#{params.primary_location_id}" - classifiedAs: #{inspect(params.classified_as)} - }) { - agent { - id - name - note - primaryLocation { id } - classifiedAs + run!(""" + mutation ($organization: OrganizationCreateParams!) { + createOrganization(organization: $organization) { + agent { + id + name + note + image + primaryLocation { id } + classifiedAs + } } } - """) + """, vars: %{"organization" => params}) assert {:ok, _} = Zenflows.DB.ID.cast(data["id"]) - assert data["name"] == params.name - assert data["note"] == params.note - assert data["primaryLocation"]["id"] == params.primary_location_id - assert data["classifiedAs"] == params.classified_as + data = Map.delete(data, "id") + + assert data["primaryLocation"]["id"] == params["primaryLocation"] + data = Map.delete(data, "primaryLocation") + params = Map.delete(params, "primaryLocation") + + assert data == params end test "updateOrganization()", %{params: params, org: org} do assert %{data: %{"updateOrganization" => %{"agent" => data}}} = - mutation!(""" - updateOrganization(organization: { - id: "#{org.id}" - name: "#{params.name}" - note: "#{params.note}" - primaryLocation: "#{params.primary_location_id}" - classifiedAs: #{inspect(params.classified_as)} - }) { - agent { - id - name - note - primaryLocation { id } - classifiedAs + run!(""" + mutation ($organization: OrganizationUpdateParams!) { + updateOrganization(organization: $organization) { + agent { + id + name + note + image + primaryLocation { id } + classifiedAs + } } } - """) + """, vars: %{"organization" => + params + |> Map.take(~w[name note image primaryLocation classifiedAs]) + |> Map.put("id", org.id) + }) - assert data["id"] == org.id - assert data["name"] == params.name - assert data["note"] == params.note - assert data["primaryLocation"]["id"] == params.primary_location_id - assert data["classifiedAs"] == params.classified_as + keys = ~w[name image note classifiedAs] + assert Map.take(data, keys) == Map.take(params, keys) + assert data["primaryLocation"]["id"] == params["primaryLocation"] end test "deleteOrganization", %{org: org} do assert %{data: %{"deleteOrganization" => true}} = - mutation!(""" - deleteOrganization(id: "#{org.id}") - """) + run!(""" + mutation ($id: ID!) { + deleteOrganization(id: $id) + } + """, vars: %{"id" => org.id}) end end end diff --git a/test/vf/person/domain.test.exs b/test/vf/person/domain.test.exs @@ -24,7 +24,7 @@ alias Zenflows.VF.{Person, Person.Domain} setup ctx do params = %{ name: Factory.uniq("name"), - image: Factory.uri(), + image: Factory.img(), note: Factory.uniq("note"), primary_location_id: Factory.insert!(:spatial_thing).id, user: Factory.uniq("user"), @@ -114,7 +114,7 @@ describe "update/2" do assert new.name == old.name assert new.note == old.note - assert new.image == nil # old.image + assert new.image == old.image assert new.primary_location_id == old.primary_location_id assert new.user == old.user assert new.email == old.email diff --git a/test/vf/person/type.test.exs b/test/vf/person/type.test.exs @@ -21,17 +21,17 @@ use ZenflowsTest.Help.AbsinCase, async: true setup do %{ params: %{ - name: Factory.uniq("name"), - # image - note: Factory.uniq("note"), - primary_location_id: Factory.insert!(:spatial_thing).id, - user: Factory.uniq("user"), - email: "#{Factory.uniq("user")}@example.com", - ecdh_public_key: Base.encode64("ecdh_public_key"), - eddsa_public_key: Base.encode64("eddsa_public_key"), - ethereum_address: Base.encode64("ethereum_address"), - reflow_public_key: Base.encode64("reflow_public_key"), - schnorr_public_key: Base.encode64("schnorr_public_key"), + "name" => Factory.uniq("name"), + "image" => Factory.img(), + "note" => Factory.uniq("note"), + "primaryLocation" => Factory.insert!(:spatial_thing).id, + "user" => Factory.uniq("user"), + "email" => "#{Factory.uniq("user")}@example.com", + "ecdhPublicKey" => Base.encode64("ecdh_public_key"), + "eddsaPublicKey" => Base.encode64("eddsa_public_key"), + "ethereumAddress" => Base.encode64("ethereum_address"), + "reflowPublicKey" => Base.encode64("reflow_public_key"), + "schnorrPublicKey" => Base.encode64("schnorr_public_key"), }, per: Factory.insert!(:person), } @@ -40,135 +40,130 @@ end describe "Query" do test "person()", %{per: per} do assert %{data: %{"person" => data}} = - query!(""" - person(id: "#{per.id}") { - id - name - note - primaryLocation { id } - user - email + run!(""" + query ($id: ID!) { + person(id: $id) { + id + name + note + image + primaryLocation { id } + user + email + ecdhPublicKey + eddsaPublicKey + ethereumAddress + reflowPublicKey + schnorrPublicKey + } } - """) + """, variables: %{"id" => per.id}) assert data["id"] == per.id assert data["name"] == per.name assert data["note"] == per.note + assert data["image"] == per.image assert data["primaryLocation"]["id"] == per.primary_location_id assert data["user"] == per.user assert data["email"] == per.email + assert data["ecdhPublicKey"] == per.ecdh_public_key + assert data["eddsaPublicKey"] == per.eddsa_public_key + assert data["ethereumAddress"] == per.ethereum_address + assert data["reflowPublicKey"] == per.reflow_public_key + assert data["schnorrPublicKey"] == per.schnorr_public_key end end describe "Mutation" do test "createPerson() doesn't create a person without the admin key", %{params: params} do - assert %{data: nil, errors: [%{message: "you are not authorized", path: ["createPerson"]}]} = - mutation!(""" - createPerson(person: { - name: "#{params.name}" - note: "#{params.note}" - primaryLocation: "#{params.primary_location_id}" - user: "#{params.user}" - email: "#{params.email}" - ecdhPublicKey: "#{params.ecdh_public_key}" - eddsaPublicKey: "#{params.eddsa_public_key}" - ethereumAddress: "#{params.ethereum_address}" - reflowPublicKey: "#{params.reflow_public_key}" - schnorrPublicKey: "#{params.schnorr_public_key}" - }) { - agent { - id - name - note - primaryLocation { id } - user - email - ecdhPublicKey - eddsaPublicKey - ethereumAddress - reflowPublicKey - schnorrPublicKey + assert %{data: nil, errors: [%{message: "you are not an admin", path: ["createPerson"]}]} = + run!(""" + mutation ($person: PersonCreateParams!) { + createPerson(person: $person) { + agent { + id + name + note + primaryLocation { id } + user + email + ecdhPublicKey + eddsaPublicKey + ethereumAddress + reflowPublicKey + schnorrPublicKey + } } } - """) + """, auth?: true, vars: %{"person" => params}) end test "createPerson() creates a person with the admin key", %{params: params} do assert %{data: %{"createPerson" => %{"agent" => data}}} = - mutation!(""" - createPerson(person: { - name: "#{params.name}" - note: "#{params.note}" - primaryLocation: "#{params.primary_location_id}" - user: "#{params.user}" - email: "#{params.email}" - ecdhPublicKey: "#{params.ecdh_public_key}" - eddsaPublicKey: "#{params.eddsa_public_key}" - ethereumAddress: "#{params.ethereum_address}" - reflowPublicKey: "#{params.reflow_public_key}" - schnorrPublicKey: "#{params.schnorr_public_key}" - }) { - agent { - id - name - note - primaryLocation { id } - user - email - ecdhPublicKey - eddsaPublicKey - ethereumAddress - reflowPublicKey - schnorrPublicKey + run!(""" + mutation ($person: PersonCreateParams!) { + createPerson(person: $person) { + agent { + id + name + note + primaryLocation { id } + user + email + image + ecdhPublicKey + eddsaPublicKey + ethereumAddress + reflowPublicKey + schnorrPublicKey + } } } - """) + """, vars: %{"person" => params}) assert {:ok, _} = Zenflows.DB.ID.cast(data["id"]) - assert data["name"] == params.name - assert data["note"] == params.note - assert data["primaryLocation"]["id"] == params.primary_location_id - assert data["user"] == params.user - assert data["email"] == params.email - assert data["ecdhPublicKey"] == params.ecdh_public_key - assert data["eddsaPublicKey"] == params.eddsa_public_key - assert data["ethereumAddress"] == params.ethereum_address - assert data["reflowPublicKey"] == params.reflow_public_key - assert data["schnorrPublicKey"] == params.schnorr_public_key + data = Map.delete(data, "id") + + assert data["primaryLocation"]["id"] == params["primaryLocation"] + data = Map.delete(data, "primaryLocation") + params = Map.delete(params, "primaryLocation") + + assert data == params end test "updatePerson()", %{params: params, per: per} do assert %{data: %{"updatePerson" => %{"agent" => data}}} = - mutation!(""" - updatePerson(person: { - id: "#{per.id}" - name: "#{params.name}" - note: "#{params.note}" - primaryLocation: "#{params.primary_location_id}" - user: "#{params.user}" - }) { - agent { - id - name - note - primaryLocation { id } - user - email - ecdhPublicKey - eddsaPublicKey - ethereumAddress - reflowPublicKey - schnorrPublicKey + run!(""" + mutation ($person: PersonUpdateParams!) { + updatePerson(person: $person) { + agent { + id + name + note + primaryLocation { id } + user + email + image + ecdhPublicKey + eddsaPublicKey + ethereumAddress + reflowPublicKey + schnorrPublicKey + } } } - """) + """, vars: %{"person" => + params + |> Map.take(~w[user name image note primaryLocation]) + |> Map.put("id", per.id) + }) + + keys = ~w[user name image note] + assert Map.take(data, keys) == Map.take(params, keys) + assert data["primaryLocation"]["id"] == params["primaryLocation"] assert data["id"] == per.id - assert data["name"] == params.name - assert data["note"] == params.note - assert data["primaryLocation"]["id"] == params.primary_location_id - assert data["user"] == params.user assert data["email"] == per.email assert data["ecdhPublicKey"] == per.ecdh_public_key assert data["eddsaPublicKey"] == per.eddsa_public_key @@ -178,21 +173,30 @@ describe "Mutation" do end test "deletePerson() doesn't delete the person without the admin key", %{per: per} do - assert %{data: nil, errors: [%{message: "you are not authorized", path: ["deletePerson"]}]} = - mutation!(""" - deletePerson( - id: "#{per.id}" - ) - """) + assert %{data: nil, errors: [%{message: "you are not an admin", path: ["deletePerson"]}]} = + run!(""" + mutation ($id: ID!) { + deletePerson(id: $id) + } + """, auth?: true, vars: %{"id" => per.id}) end test "deletePerson() deletes the person with the admin key", %{per: per} do + key = + Application.fetch_env!(:zenflows, Zenflows.Admin)[:admin_key] + |> Base.encode16(case: :lower) + assert %{data: %{"deletePerson" => true}} = - mutation!(""" - deletePerson( - id: "#{per.id}" - ) - """) + run!( + """ + mutation ($id: ID!) { + deletePerson(id: $id) + } + """, + auth?: true, + vars: %{"id" => per.id}, + ctx: %{gql_admin: key} + ) end end end diff --git a/test/vf/recipe_resource/domain.test.exs b/test/vf/recipe_resource/domain.test.exs @@ -35,7 +35,7 @@ setup do resource_conforms_to_id: Factory.insert!(:resource_specification).id, substitutable: Factory.bool(), note: Factory.uniq("note"), - image: Factory.uri(), + image: Factory.img(), }, recipe_resource: Factory.insert!(:recipe_resource), } @@ -88,7 +88,7 @@ describe "update/2" do assert new.resource_conforms_to_id == old.resource_conforms_to_id assert new.substitutable == old.substitutable assert new.note == old.note - assert new.image == nil # old.image + assert new.image == old.image end end diff --git a/test/vf/recipe_resource/type.test.exs b/test/vf/recipe_resource/type.test.exs @@ -21,13 +21,14 @@ use ZenflowsTest.Help.AbsinCase, async: true setup do %{ params: %{ - name: Factory.uniq("name"), - resource_classified_as: Factory.uniq_list("uri"), - unit_of_effort_id: Factory.insert!(:unit).id, - unit_of_resource_id: Factory.insert!(:unit).id, - resource_conforms_to_id: Factory.insert!(:resource_specification).id, - substitutable: Factory.bool(), - note: Factory.uniq("note"), + "name" => Factory.uniq("name"), + "resourceClassifiedAs" => Factory.uniq_list("uri"), + "unitOfEffort" => Factory.insert!(:unit).id, + "unitOfResource" => Factory.insert!(:unit).id, + "resourceConformsTo" => Factory.insert!(:resource_specification).id, + "substitutable" => Factory.bool(), + "note" => Factory.uniq("note"), + "image" => Factory.img(), }, recipe_resource: Factory.insert!(:recipe_resource), } @@ -36,18 +37,21 @@ end describe "Query" do test "recipeResource()", %{recipe_resource: rec_res} do assert %{data: %{"recipeResource" => data}} = - query!(""" - recipeResource(id: "#{rec_res.id}") { - id - name - resourceClassifiedAs - unitOfResource { id } - unitOfEffort { id } - resourceConformsTo { id } - substitutable - note + run!(""" + query ($id: ID!) { + recipeResource(id: $id) { + id + name + resourceClassifiedAs + unitOfResource { id } + unitOfEffort { id } + resourceConformsTo { id } + substitutable + image + note + } } - """) + """, vars: %{"id" => rec_res.id}) assert data["id"] == rec_res.id assert data["name"] == rec_res.name @@ -56,84 +60,75 @@ describe "Query" do assert data["unitOfEffort"]["id"] == rec_res.unit_of_effort_id assert data["resourceConformsTo"]["id"] == rec_res.resource_conforms_to_id assert data["note"] == rec_res.note + assert data["substitutable"] == rec_res.substitutable + assert data["image"] == rec_res.image end end describe "Mutation" do test "createRecipeResource()", %{params: params} do assert %{data: %{"createRecipeResource" => %{"recipeResource" => data}}} = - mutation!(""" - createRecipeResource(recipeResource: { - name: "#{params.name}" - resourceClassifiedAs: #{inspect(params.resource_classified_as)} - unitOfResource: "#{params.unit_of_resource_id}" - unitOfEffort: "#{params.unit_of_effort_id}" - resourceConformsTo: "#{params.resource_conforms_to_id}" - substitutable: #{params.substitutable} - note: "#{params.note}" - }) { - recipeResource { - id - name - resourceClassifiedAs - unitOfResource { id } - unitOfEffort { id } - resourceConformsTo { id } - substitutable - note + run!(""" + mutation ($recipeResource: RecipeResourceCreateParams!) { + createRecipeResource(recipeResource: $recipeResource) { + recipeResource { + id + name + image + resourceClassifiedAs + unitOfResource { id } + unitOfEffort { id } + resourceConformsTo { id } + substitutable + note + } } } - """) + """, vars: %{"recipeResource" => params}) assert {:ok, _} = Zenflows.DB.ID.cast(data["id"]) - assert data["name"] == params.name - assert data["resourceClassifiedAs"] == params.resource_classified_as - assert data["unitOfResource"]["id"] == params.unit_of_resource_id - assert data["unitOfEffort"]["id"] == params.unit_of_effort_id - assert data["resourceConformsTo"]["id"] == params.resource_conforms_to_id - assert data["note"] == params.note + keys = ~w[name note image resourceClassifiedAs substitutable] + assert Map.take(data, keys) == Map.take(params, keys) + assert data["unitOfResource"]["id"] == params["unitOfResource"] + assert data["unitOfEffort"]["id"] == params["unitOfEffort"] + assert data["resourceConformsTo"]["id"] == params["resourceConformsTo"] end test "updateRecipeResource()", %{params: params, recipe_resource: rec_res} do assert %{data: %{"updateRecipeResource" => %{"recipeResource" => data}}} = - mutation!(""" - updateRecipeResource(recipeResource: { - id: "#{rec_res.id}" - name: "#{params.name}" - resourceClassifiedAs: #{inspect(params.resource_classified_as)} - unitOfResource: "#{params.unit_of_resource_id}" - unitOfEffort: "#{params.unit_of_effort_id}" - resourceConformsTo: "#{params.resource_conforms_to_id}" - substitutable: #{params.substitutable} - note: "#{params.note}" - }) { - recipeResource { - id - name - resourceClassifiedAs - unitOfResource { id } - unitOfEffort { id } - resourceConformsTo { id } - substitutable - note + run!(""" + mutation ($recipeResource: RecipeResourceUpdateParams!) { + updateRecipeResource(recipeResource: $recipeResource) { + recipeResource { + id + name + resourceClassifiedAs + unitOfResource { id } + unitOfEffort { id } + resourceConformsTo { id } + substitutable + note + image + } } } - """) + """, vars: %{"recipeResource" => Map.put(params, "id", rec_res.id)}) assert data["id"] == rec_res.id - assert data["name"] == params.name - assert data["resourceClassifiedAs"] == params.resource_classified_as - assert data["unitOfResource"]["id"] == params.unit_of_resource_id - assert data["unitOfEffort"]["id"] == params.unit_of_effort_id - assert data["resourceConformsTo"]["id"] == params.resource_conforms_to_id - assert data["note"] == params.note + keys = ~w[name note image resourceClassifiedAs substitutable] + assert Map.take(data, keys) == Map.take(params, keys) + assert data["unitOfResource"]["id"] == params["unitOfResource"] + assert data["unitOfEffort"]["id"] == params["unitOfEffort"] + assert data["resourceConformsTo"]["id"] == params["resourceConformsTo"] end test "deleteRecipeResource()", %{recipe_resource: %{id: id}} do assert %{data: %{"deleteRecipeResource" => true}} = - mutation!(""" - deleteRecipeResource(id: "#{id}") - """) + run!(""" + mutation ($id: ID!) { + deleteRecipeResource(id: $id) + } + """, vars: %{"id" => id}) end end end diff --git a/test/vf/resource_specification/domain.test.exs b/test/vf/resource_specification/domain.test.exs @@ -31,7 +31,7 @@ setup do name: Factory.uniq("name"), resource_classified_as: Factory.uniq_list("uri"), note: Factory.uniq("note"), - image: Factory.uri(), + image: Factory.img(), default_unit_of_effort_id: Factory.insert!(:unit).id, default_unit_of_resource_id: Factory.insert!(:unit).id, }, @@ -78,7 +78,7 @@ describe "update/2" do assert new.name == old.name assert new.resource_classified_as == old.resource_classified_as assert new.note == old.note - assert new.image == nil # old.image + assert new.image == old.image assert new.default_unit_of_resource_id == old.default_unit_of_resource_id assert new.default_unit_of_effort_id == old.default_unit_of_effort_id end diff --git a/test/vf/resource_specification/type.test.exs b/test/vf/resource_specification/type.test.exs @@ -21,12 +21,12 @@ use ZenflowsTest.Help.AbsinCase, async: true setup do %{ params: %{ - name: Factory.uniq("name"), - resource_classified_as: Factory.uniq_list("uri"), - note: Factory.uniq("note"), - image: Factory.uri(), - default_unit_of_effort_id: Factory.insert!(:unit).id, - default_unit_of_resource_id: Factory.insert!(:unit).id, + "name" => Factory.uniq("name"), + "resourceClassifiedAs" => Factory.uniq_list("uri"), + "note" => Factory.uniq("note"), + "image" => Factory.img(), + "defaultUnitOfEffort" => Factory.insert!(:unit).id, + "defaultUnitOfResource" => Factory.insert!(:unit).id, }, resource_specification: Factory.insert!(:resource_specification), } @@ -35,17 +35,19 @@ end describe "Query" do test "resourceSpecification()", %{resource_specification: res_spec} do assert %{data: %{"resourceSpecification" => data}} = - query!(""" - resourceSpecification(id: "#{res_spec.id}") { - id - name - resourceClassifiedAs - defaultUnitOfResource { id } - defaultUnitOfEffort { id } - note - image + run!(""" + query ($id: ID!) { + resourceSpecification(id: $id) { + id + name + resourceClassifiedAs + defaultUnitOfResource { id } + defaultUnitOfEffort { id } + note + image + } } - """) + """, vars: %{"id" => res_spec.id}) assert data["id"] == res_spec.id assert data["name"] == res_spec.name @@ -53,81 +55,70 @@ describe "Query" do assert data["defaultUnitOfResource"]["id"] == res_spec.default_unit_of_resource_id assert data["defaultUnitOfEffort"]["id"] == res_spec.default_unit_of_effort_id assert data["note"] == res_spec.note - assert data["image"] == nil + assert data["image"] == res_spec.image end end describe "Mutation" do test "createResourceSpecification()", %{params: params} do assert %{data: %{"createResourceSpecification" => %{"resourceSpecification" => data}}} = - mutation!(""" - createResourceSpecification(resourceSpecification: { - name: "#{params.name}" - resourceClassifiedAs: #{inspect(params.resource_classified_as)} - defaultUnitOfResource: "#{params.default_unit_of_resource_id}" - defaultUnitOfEffort: "#{params.default_unit_of_effort_id}" - note: "#{params.note}" - image: "#{params.image}" - }) { - resourceSpecification { - id - name - resourceClassifiedAs - defaultUnitOfResource { id } - defaultUnitOfEffort { id } - note - image + run!(""" + mutation ($resourceSpecification: ResourceSpecificationCreateParams!) { + createResourceSpecification(resourceSpecification: $resourceSpecification) { + resourceSpecification { + id + name + resourceClassifiedAs + defaultUnitOfResource { id } + defaultUnitOfEffort { id } + note + image + } } } - """) + """, vars: %{"resourceSpecification" => params}) assert {:ok, _} = Zenflows.DB.ID.cast(data["id"]) - assert data["name"] == params.name - assert data["resourceClassifiedAs"] == params.resource_classified_as - assert data["defaultUnitOfResource"]["id"] == params.default_unit_of_resource_id - assert data["defaultUnitOfEffort"]["id"] == params.default_unit_of_effort_id - assert data["note"] == params.note - assert data["image"] == params.image + keys = ~w[name note resourceClassifiedAs note image] + assert Map.take(data, keys) == Map.take(params, keys) + assert data["defaultUnitOfResource"]["id"] == params["defaultUnitOfResource"] + assert data["defaultUnitOfEffort"]["id"] == params["defaultUnitOfEffort"] end test "updateResourceSpecification()", %{params: params, resource_specification: res_spec} do assert %{data: %{"updateResourceSpecification" => %{"resourceSpecification" => data}}} = - mutation!(""" - updateResourceSpecification(resourceSpecification: { - id: "#{res_spec.id}" - name: "#{params.name}" - resourceClassifiedAs: #{inspect(params.resource_classified_as)} - defaultUnitOfResource: "#{params.default_unit_of_resource_id}" - defaultUnitOfEffort: "#{params.default_unit_of_effort_id}" - note: "#{params.note}" - image: "#{params.image}" - }) { - resourceSpecification { - id - name - resourceClassifiedAs - defaultUnitOfResource { id } - defaultUnitOfEffort { id } - note - image + run!(""" + mutation ($resourceSpecification: ResourceSpecificationUpdateParams!) { + updateResourceSpecification(resourceSpecification: $resourceSpecification) { + resourceSpecification { + id + name + resourceClassifiedAs + defaultUnitOfResource { id } + defaultUnitOfEffort { id } + note + image + } } } - """) + """, vars: %{"resourceSpecification" => + Map.put(params, "id", res_spec.id), + }) assert data["id"] == res_spec.id - assert data["name"] == params.name - assert data["resourceClassifiedAs"] == params.resource_classified_as - assert data["defaultUnitOfResource"]["id"] == params.default_unit_of_resource_id - assert data["defaultUnitOfEffort"]["id"] == params.default_unit_of_effort_id - assert data["note"] == params.note - assert data["image"] == params.image + keys = ~w[name note resourceClassifiedAs note image] + assert Map.take(data, keys) == Map.take(params, keys) + assert data["defaultUnitOfResource"]["id"] == params["defaultUnitOfResource"] + assert data["defaultUnitOfEffort"]["id"] == params["defaultUnitOfEffort"] end test "deleteResourceSpecification()", %{resource_specification: %{id: id}} do assert %{data: %{"deleteResourceSpecification" => true}} = - mutation!(""" - deleteResourceSpecification(id: "#{id}") - """) + run!(""" + mutation ($id: ID!) { + deleteResourceSpecification(id: $id) + } + """, vars: %{"id" => id}) end end end diff --git a/test/vf/validate.test.exs b/test/vf/validate.test.exs @@ -36,6 +36,11 @@ defp note_chgset(changes) do Changeset.change({%{}, %{note: :string}}, changes) end +@spec img_chgset(map()) :: Changeset.t() +defp img_chgset(changes) do + Changeset.change({%{}, %{img: :string}}, changes) +end + @spec uri_chgset(map()) :: Changeset.t() defp uri_chgset(changes) do Changeset.change({%{}, %{uri: :string}}, changes) @@ -133,6 +138,35 @@ describe "note/2" do end end +describe "img/2" do + test "with too short param" do + assert %Changeset{errors: errs} = + %{img: ""} + |> img_chgset() + |> Validate.img(:img) + + assert {:ok, _} = Keyword.fetch(errs, :img) + end + + test "with too long param" do + assert %Changeset{errors: errs} = + %{img: String.duplicate("a", 25 * 1024**2 + 1)} + |> img_chgset() + |> Validate.img(:img) + + assert {:ok, _} = Keyword.fetch(errs, :img) + end + + test "with the right size param" do + assert %Changeset{errors: errs} = + %{img: String.duplicate("a", 1024)} + |> img_chgset() + |> Validate.img(:img) + + assert :error = Keyword.fetch(errs, :img) + end +end + describe "uri/2" do test "with too short param" do assert %Changeset{errors: errs} =