zf

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

type.test.exs (5153B)


      1 # Zenflows is designed to implement the Valueflows vocabulary,
      2 # written and maintained by srfsh <info@dyne.org>.
      3 # Copyright (C) 2021-2023 Dyne.org foundation <foundation@dyne.org>.
      4 #
      5 # This program is free software: you can redistribute it and/or modify
      6 # it under the terms of the GNU Affero General Public License as published by
      7 # the Free Software Foundation, either version 3 of the License, or
      8 # (at your option) any later version.
      9 #
     10 # This program is distributed in the hope that it will be useful,
     11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 # GNU Affero General Public License for more details.
     14 #
     15 # You should have received a copy of the GNU Affero General Public License
     16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
     17 
     18 defmodule ZenflowsTest.VF.Person.Type do
     19 use ZenflowsTest.Help.AbsinCase, async: true
     20 
     21 setup do
     22 	%{
     23 		params: %{
     24 			"name" => Factory.str("name"),
     25 			"note" => Factory.str("note"),
     26 			"primaryLocation" => Factory.insert!(:spatial_thing).id,
     27 			"user" => Factory.str("user"),
     28 			"email" => "#{Factory.str("user")}@example.com",
     29 			"ecdhPublicKey" => Base.encode64("ecdh_public_key"),
     30 			"eddsaPublicKey" => Base.encode64("eddsa_public_key"),
     31 			"ethereumAddress" => Base.encode64("ethereum_address"),
     32 			"reflowPublicKey" => Base.encode64("reflow_public_key"),
     33 			"bitcoinPublicKey" => Base.encode64("bitcoin_public_key"),
     34 		},
     35 		inserted: Factory.insert!(:person),
     36 	}
     37 end
     38 
     39 @frag """
     40 fragment person on Person {
     41 	id
     42 	name
     43 	note
     44 	primaryLocation { id }
     45 	user
     46 	email
     47 	ecdhPublicKey
     48 	eddsaPublicKey
     49 	ethereumAddress
     50 	reflowPublicKey
     51 	bitcoinPublicKey
     52 }
     53 """
     54 
     55 describe "Query" do
     56 	test "person", %{inserted: new} do
     57 		assert %{data: %{"person" => data}} =
     58 			run!("""
     59 				#{@frag}
     60 				query ($id: ID!) {
     61 					person(id: $id) {...person}
     62 				}
     63 			""", variables: %{"id" => new.id})
     64 
     65 		assert data["id"] == new.id
     66 		assert data["name"] == new.name
     67 		assert data["note"] == new.note
     68 		assert data["primaryLocation"]["id"] == new.primary_location_id
     69 
     70 		assert data["user"] == new.user
     71 		assert data["email"] == new.email
     72 		assert data["ecdhPublicKey"] == new.ecdh_public_key
     73 		assert data["eddsaPublicKey"] == new.eddsa_public_key
     74 		assert data["ethereumAddress"] == new.ethereum_address
     75 		assert data["reflowPublicKey"] == new.reflow_public_key
     76 		assert data["bitcoinPublicKey"] == new.bitcoin_public_key
     77 	end
     78 end
     79 
     80 describe "Mutation" do
     81 	test "createPerson: doesn't create a person without the admin key", %{params: params} do
     82 		assert %{data: nil, errors: [%{message: "you are not an admin", path: ["createPerson"]}]} =
     83 			run!("""
     84 				#{@frag}
     85 				mutation ($person: PersonCreateParams!) {
     86 					createPerson(person: $person) {
     87 						agent {...person}
     88 					}
     89 				}
     90 			""", auth?: true, vars: %{"person" => params})
     91 	end
     92 
     93 	test "createPerson: creates a person with the admin key", %{params: params} do
     94 		assert %{data: %{"createPerson" => %{"agent" => data}}} =
     95 			run!("""
     96 				#{@frag}
     97 				mutation ($person: PersonCreateParams!) {
     98 					createPerson(person: $person) {
     99 						agent {...person}
    100 					}
    101 				}
    102 			""",
    103 			auth?: true,
    104 			ctx: %{gql_admin: admin_key()},
    105 			vars: %{"person" => params})
    106 
    107 		assert {:ok, _} = Zenflows.DB.ID.cast(data["id"])
    108 		data = Map.delete(data, "id")
    109 
    110 		assert data["primaryLocation"]["id"] == params["primaryLocation"]
    111 		data = Map.delete(data, "primaryLocation")
    112 		params = Map.delete(params, "primaryLocation")
    113 
    114 		assert data == params
    115 	end
    116 
    117 	test "updatePerson", %{params: params, inserted: old} do
    118 		assert %{data: %{"updatePerson" => %{"agent" => data}}} =
    119 			run!("""
    120 				#{@frag}
    121 				mutation ($person: PersonUpdateParams!) {
    122 					updatePerson(person: $person) {
    123 						agent {...person}
    124 					}
    125 				}
    126 			""", vars: %{"person" =>
    127 				params
    128 				|> Map.take(~w[user name note primaryLocation])
    129 				|> Map.put("id", old.id)
    130 			})
    131 
    132 		keys = ~w[user name note]
    133 		assert Map.take(data, keys) == Map.take(params, keys)
    134 		assert data["primaryLocation"]["id"] == params["primaryLocation"]
    135 
    136 		assert data["id"] == old.id
    137 		assert data["email"] == old.email
    138 		assert data["ecdhPublicKey"] == old.ecdh_public_key
    139 		assert data["eddsaPublicKey"] == old.eddsa_public_key
    140 		assert data["ethereumAddress"] == old.ethereum_address
    141 		assert data["reflowPublicKey"] == old.reflow_public_key
    142 		assert data["bitcoinPublicKey"] == old.bitcoin_public_key
    143 	end
    144 
    145 	test "deletePerson: doesn't delete the person without the admin key", %{inserted: new} do
    146 		assert %{data: nil, errors: [%{message: "you are not an admin", path: ["deletePerson"]}]} =
    147 			run!("""
    148 				mutation ($id: ID!) {
    149 					deletePerson(id: $id)
    150 				}
    151 			""", auth?: true, vars: %{"id" => new.id})
    152 	end
    153 
    154 	test "deletePerson: deletes the person with the admin key", %{inserted: new} do
    155 		assert %{data: %{"deletePerson" => true}} =
    156 			run!(
    157 				"""
    158 					mutation ($id: ID!) {
    159 						deletePerson(id: $id)
    160 					}
    161 				""",
    162 				auth?: true,
    163 				ctx: %{gql_admin: admin_key()},
    164 				vars: %{"id" => new.id})
    165 	end
    166 
    167 	defp admin_key() do
    168 		Application.fetch_env!(:zenflows, Zenflows.Admin)[:admin_key]
    169 		|> Base.encode16(case: :lower)
    170 	end
    171 end
    172 end