diff --git a/.iex.exs b/.iex.exs index 8ed42b0..eba3e69 100644 --- a/.iex.exs +++ b/.iex.exs @@ -1,19 +1,16 @@ -best_block_height_getter = - File.read!("examples/best_block_height_getter.ex") -endpoint_provider = - File.read!("examples/endpoint_provider.ex") +best_block_height_getter = File.read!("examples/best_block_height_getter.ex") +endpoint_provider = File.read!("examples/endpoint_provider.ex") -block_to_ascii_emoji_translator = - File.read!("examples/block_to_game_map_translator.ex") +block_to_ascii_emoji_translator = File.read!("examples/block_to_game_map_translator.ex") alias FunctionServerBasedOnArweave.DataToChain read_conn = Components.Ipfs.Connection.conn(:read) dao_info_exp = %{ - "description" => "it's just a cool DAO.", - "logo" => "https://tva1.sinaimg.cn/large/e6c9d24egy1h2rb87iw5ij20je0j8t9t.jpg", - "homepage" => "https://google.com", - "name" => "coolDAO", - "template_gist_id" => "xxxx", - "verified_erc721_contract" => "xxxx" - } + "description" => "it's just a cool DAO.", + "logo" => "https://tva1.sinaimg.cn/large/e6c9d24egy1h2rb87iw5ij20je0j8t9t.jpg", + "homepage" => "https://google.com", + "name" => "coolDAO", + "template_gist_id" => "xxxx", + "verified_erc721_contract" => "xxxx" +} diff --git a/mix.exs b/mix.exs index ebfe9ad..429097f 100644 --- a/mix.exs +++ b/mix.exs @@ -73,10 +73,9 @@ defmodule FunctionServerBasedOnArweave.MixProject do {:pow, "~> 1.0.26"}, # trace {:recon, "~> 2.5"}, - {:cors_plug, "~> 2.0"}, - - {:tentacat, "~> 2.2"} + {:tentacat, "~> 2.2"}, + {:credo, "~> 1.6", only: [:dev, :test], runtime: false} ] end diff --git a/mix.lock b/mix.lock index 725c710..1bc62e4 100644 --- a/mix.lock +++ b/mix.lock @@ -1,6 +1,7 @@ %{ "arweave_sdk_ex": {:hex, :arweave_sdk_ex, "0.1.10", "92cc993569ad896b6c6608bf584f1b46cf9d861870071446f7ba9d8f72265b21", [:mix], [{:ex_struct_translator, "~> 0.1.1", [hex: :ex_struct_translator, repo: "hexpm", optional: false]}, {:export, "~> 0.1.0", [hex: :export, repo: "hexpm", optional: false]}, {:httpoison, "~> 1.8", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}, {:jose, "~> 1.11", [hex: :jose, repo: "hexpm", optional: false]}], "hexpm", "3ccf179eb85cdfa113b890a122ca10d2cb678ac5c70edbac7b3ccb30f581a84d"}, "binary": {:hex, :binary, "0.0.5", "20d816f7274ea34f1b673b4cff2fdb9ebec9391a7a68c349070d515c66b1b2cf", [:mix], [], "hexpm", "ee1e9ebcab703a4e24db554957fbb540642fe9327eb9e295cb3f07dd7c11ddb2"}, + "bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"}, "castore": {:hex, :castore, "0.1.17", "ba672681de4e51ed8ec1f74ed624d104c0db72742ea1a5e74edbc770c815182f", [:mix], [], "hexpm", "d9844227ed52d26e7519224525cb6868650c272d4a3d327ce3ca5570c12163f9"}, "certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"}, "connection": {:hex, :connection, "1.1.0", "ff2a49c4b75b6fb3e674bfc5536451607270aac754ffd1bdfe175abe4a6d7a68", [:mix], [], "hexpm", "722c1eb0a418fbe91ba7bd59a47e28008a189d47e37e0e7bb85585a016b2869c"}, @@ -8,6 +9,7 @@ "cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"}, "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"}, "cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"}, + "credo": {:hex, :credo, "1.6.7", "323f5734350fd23a456f2688b9430e7d517afb313fbd38671b8a4449798a7854", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "41e110bfb007f7eda7f897c10bf019ceab9a0b269ce79f015d54b0dcf4fc7dd3"}, "db_connection": {:hex, :db_connection, "2.4.2", "f92e79aff2375299a16bcb069a14ee8615c3414863a6fef93156aee8e86c2ff3", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4fe53ca91b99f55ea249693a0229356a08f4d1a7931d8ffa79289b145fe83668"}, "decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, "earmark": {:hex, :earmark, "1.4.26", "f0e3c3d5c278a6d448ad8c27ab0ecdec9c57a7710553138c56af220a6330a4fd", [:mix], [{:earmark_parser, "~> 1.4.26", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "e1231882b56bece0692af33f0959f06c9cd580c2dc2ecb1dc9f16f2750fa78c5"}, diff --git a/test/code_on_chain/data_handler.exs b/test/code_on_chain/data_handler.exs new file mode 100644 index 0000000..7696150 --- /dev/null +++ b/test/code_on_chain/data_handler.exs @@ -0,0 +1,105 @@ +defmodule CodeOnChain.SoulCard.DataHandlerTest do + use ExUnit.Case + + alias CodesOnChain.SoulCard.DataHandler + alias Components.KVHandler + + Ecto.Adapters.SQL.Sandbox.mode(FunctionServerBasedOnArweave.Repo, :manual) + + setup do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(FunctionServerBasedOnArweave.Repo) + end + + test "analyze github" do + DataHandler.init_github_repos_white_map() + + payload = %{ + WeLightProject: %{ + if_in_owner: true, + repo_list: [ + %{if_in: true, name: "tai_shang_micro_faas_system"}, + %{if_in: true, name: "Tai-Shang-Soul-Card"} + ] + } + } + + assert payload == DataHandler.analyze_github("0x00000001", "leeduckgo") + # assert payload == KVHandler.get("0x00000001", "SoulCard.DataHandler") + end + + def data_user() do + %{ + "awesome_things" => [ + %{"link" => "www.baidu.com", "title" => "Design for the transport"} + ], + "basic_info" => %{ + "avatar" => "test", + "location" => "California", + "name" => "Robert Fox", + "skills" => [ + "Javascript", + "C++", + "Python", + "HTML", + "Node", + "C#", + "Java", + "Javascript", + "C++", + "Python", + "HTML", + "Node", + "C#", + "Java" + ], + "slogan" => "Have more than 6 years of Digital Product Design experience.", + "social_links" => %{ + "discord " => "hitchhacker@3691", + "github_link " => "https://github.com/WeLightProject", + "mirror_link " => "https://mirror.xyz/apecoder.eth", + "twitter" => "https://twitter.com/Web3dAppCamp", + "wechat " => "197626581" + } + }, + "daos_joined" => ["0x73c7448760517E3E6e416b2c130E3c6dB2026A1d"] + } + end + + def data_dao() do + %{ + "awesome_things" => [%{"link" => "www.google.com", "title" => "Ho"}], + "basic_info" => %{ + "avatar" => "https://leeduckgo.com/assets/images/ava.jpeg", + "contract_addresses" => [%{"addr" => "0x0", "alias" => "BYAC NFT"}], + "homepage" => "https://noncegeek.com", + "location" => "California", + "name" => "NonceGeekDAO", + "slogan" => "sth_sth", + "social_links" => %{ + "discord" => "hitchhacker@3691", + "github_link" => "https://github.com/WeLightProject", + "mirror_link" => "https://mirror.xyz/apecoder.eth", + "twitter" => "https://twitter.com/Web3dAppCamp", + "wechat" => "197626581" + } + }, + "core_members" => [], + "members" => [], + "partners" => [ + "0x01234567", + %{"avatar" => "xxx", "link" => "https://google.com", "name" => "谷歌"} + ], + "sub_daos" => [] + } + end + + test "data format" do + test_user_return = + {:ok, "all check is passed!"} == DataHandler.check_format(data_user(), "user") + + test_dao_return = {:ok, "all check is passed!"} == DataHandler.check_format(data_dao(), "dao") + + assert test_user_return + assert test_dao_return + end +end diff --git a/test/code_on_chain/ipfs_interactor.exs b/test/code_on_chain/ipfs_interactor.exs new file mode 100644 index 0000000..038891a --- /dev/null +++ b/test/code_on_chain/ipfs_interactor.exs @@ -0,0 +1,17 @@ +defmodule CodeOnChain.SoulCard.IpfsInteractorTest do + use ExUnit.Case + + alias CodesOnChain.SoulCard.IpfsInteractor + + test "test get data" do + # Base 64 of "hello, world" + test_read_return = + {:ok, "aGVsbG8sIHdvcmxk"} == + IpfsInteractor.get_data("QmbJtyu82TQSHU52AzRMXBENZGQKYqPsmao9dPuTeorPui") + + %{ + test_read_return: test_read_return + } + |> IO.inspect() + end +end diff --git a/test/code_on_chain/user_manager.exs b/test/code_on_chain/user_manager.exs new file mode 100644 index 0000000..461944b --- /dev/null +++ b/test/code_on_chain/user_manager.exs @@ -0,0 +1,90 @@ +defmodule CodeOnChain.SoulCard.UserManagerTest do + use ExUnit.Case + + def data_user() do + %{ + "awesome_things" => [ + %{"link" => "www.baidu.com", "title" => "Design for the transport"} + ], + "basic_info" => %{ + "avatar" => "test", + "location" => "California", + "name" => "Robert Fox", + "skills" => [ + "Javascript", + "C++", + "Python", + "HTML", + "Node", + "C#", + "Java", + "Javascript", + "C++", + "Python", + "HTML", + "Node", + "C#", + "Java" + ], + "slogan" => "Have more than 6 years of Digital Product Design experience.", + "social_links" => %{ + "discord " => "hitchhacker@3691", + "github_link " => "https://github.com/WeLightProject", + "mirror_link " => "https://mirror.xyz/apecoder.eth", + "twitter" => "https://twitter.com/Web3dAppCamp", + "wechat " => "197626581" + } + }, + "daos_joined" => ["0x73c7448760517E3E6e416b2c130E3c6dB2026A1d"] + } + end + + def data_dao() do + %{ + "awesome_things" => [%{"link" => "www.google.com", "title" => "Ho"}], + "basic_info" => %{ + "avatar" => "https://leeduckgo.com/assets/images/ava.jpeg", + "contract_addresses" => [%{"addr" => "0x0", "alias" => "BYAC NFT"}], + "homepage" => "https://noncegeek.com", + "location" => "California", + "name" => "NonceGeekDAO", + "slogan" => "sth_sth", + "social_links" => %{ + "discord" => "hitchhacker@3691", + "github_link" => "https://github.com/WeLightProject", + "mirror_link" => "https://mirror.xyz/apecoder.eth", + "twitter" => "https://twitter.com/Web3dAppCamp", + "wechat" => "197626581" + } + }, + "core_members" => [], + "members" => [], + "partners" => [ + "0x01234567", + %{"avatar" => "xxx", "link" => "https://google.com", "name" => "谷歌"} + ], + "sub_daos" => [] + } + end + + test "create user" do + {%{addr: addr}, msg, %{sig: signature}} = gen_rand_acct_msg_and_sig() + UserManager.create_user(data_user(), "user", addr, msg, signature) + end + + test "create user dao" do + {%{addr: addr}, msg, %{sig: signature}} = gen_rand_acct_msg_and_sig() + UserManager.create_user(data_dao(), "dao", addr, msg, signature) + end + + def gen_rand_acct_msg_and_sig() do + %{priv: priv} = acct = EthWallet.generate_keys() + msg = DataHandler.rand_msg() + sig_full = sign(msg, priv) + {acct, msg, sig_full} + end + + def sign(msg, priv) do + EthWallet.sign_compact(msg, priv) + end +end diff --git a/test/function_server_base_on_arweave/code_fetchers/nft_test.exs b/test/function_server_base_on_arweave/code_fetchers/nft_test.exs index 315687a..031b1cc 100644 --- a/test/function_server_base_on_arweave/code_fetchers/nft_test.exs +++ b/test/function_server_base_on_arweave/code_fetchers/nft_test.exs @@ -2,6 +2,7 @@ defmodule FunctionServerBasedOnArweave.CodeFetchers.NFTTest do use ExUnit.Case alias FunctionServerBasedOnArweave.CodeFetchers.NFT + test "get from nft" do {r, _} = NFT.get_from_nft("1") assert r == :ok diff --git a/test/function_server_base_on_arweave_web/views/error_view_test.exs b/test/function_server_base_on_arweave_web/views/error_view_test.exs index 72d4cbf..a64036f 100644 --- a/test/function_server_base_on_arweave_web/views/error_view_test.exs +++ b/test/function_server_base_on_arweave_web/views/error_view_test.exs @@ -5,10 +5,12 @@ defmodule FunctionServerBasedOnArweaveWeb.ErrorViewTest do import Phoenix.View test "renders 404.html" do - assert render_to_string(FunctionServerBasedOnArweaveWeb.ErrorView, "404.html", []) == "Not Found" + assert render_to_string(FunctionServerBasedOnArweaveWeb.ErrorView, "404.html", []) == + "Not Found" end test "renders 500.html" do - assert render_to_string(FunctionServerBasedOnArweaveWeb.ErrorView, "500.html", []) == "Internal Server Error" + assert render_to_string(FunctionServerBasedOnArweaveWeb.ErrorView, "500.html", []) == + "Internal Server Error" end end diff --git a/test/support/channel_case.ex b/test/support/channel_case.ex index 89dd1ea..9224325 100644 --- a/test/support/channel_case.ex +++ b/test/support/channel_case.ex @@ -2,11 +2,11 @@ defmodule FunctionServerBasedOnArweaveWeb.ChannelCase do @moduledoc """ This module defines the test case to be used by channel tests. - + Such tests rely on `Phoenix.ChannelTest` and also import other functionality to make it easier to build common data structures and query the data layer. - + Finally, if the test case interacts with the database, we enable the SQL sandbox, so changes done to the database are reverted at the end of every test. If you are using @@ -29,7 +29,11 @@ defmodule FunctionServerBasedOnArweaveWeb.ChannelCase do end setup tags do - pid = Ecto.Adapters.SQL.Sandbox.start_owner!(FunctionServerBasedOnArweave.Repo, shared: not tags[:async]) + pid = + Ecto.Adapters.SQL.Sandbox.start_owner!(FunctionServerBasedOnArweave.Repo, + shared: not tags[:async] + ) + on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) :ok end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index e7b2c7a..d0fdffb 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -2,11 +2,11 @@ defmodule FunctionServerBasedOnArweaveWeb.ConnCase do @moduledoc """ This module defines the test case to be used by tests that require setting up a connection. - + Such tests rely on `Phoenix.ConnTest` and also import other functionality to make it easier to build common data structures and query the data layer. - + Finally, if the test case interacts with the database, we enable the SQL sandbox, so changes done to the database are reverted at the end of every test. If you are using @@ -32,7 +32,11 @@ defmodule FunctionServerBasedOnArweaveWeb.ConnCase do end setup tags do - pid = Ecto.Adapters.SQL.Sandbox.start_owner!(FunctionServerBasedOnArweave.Repo, shared: not tags[:async]) + pid = + Ecto.Adapters.SQL.Sandbox.start_owner!(FunctionServerBasedOnArweave.Repo, + shared: not tags[:async] + ) + on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) {:ok, conn: Phoenix.ConnTest.build_conn()} end diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 2a46d5b..031eeb0 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -2,10 +2,10 @@ defmodule FunctionServerBasedOnArweave.DataCase do @moduledoc """ This module defines the setup for tests requiring access to the application's data layer. - + You may define functions here to be used as helpers in your tests. - + Finally, if the test case interacts with the database, we enable the SQL sandbox, so changes done to the database are reverted at the end of every test. If you are using @@ -28,18 +28,22 @@ defmodule FunctionServerBasedOnArweave.DataCase do end setup tags do - pid = Ecto.Adapters.SQL.Sandbox.start_owner!(FunctionServerBasedOnArweave.Repo, shared: not tags[:async]) + pid = + Ecto.Adapters.SQL.Sandbox.start_owner!(FunctionServerBasedOnArweave.Repo, + shared: not tags[:async] + ) + on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) :ok end @doc """ A helper that transforms changeset errors into a map of messages. - + assert {:error, changeset} = Accounts.create_user(%{password: "short"}) assert "password is too short" in errors_on(changeset).password assert %{password: ["password is too short"]} = errors_on(changeset) - + """ def errors_on(changeset) do Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->