dev vouch dev on at. thats about it atvouch.dev
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

add fake slingshot server to tests

authored by

Luna and committed by tangled.org 9bdbcfe8 c8a33be6

+93 -5
+6 -2
appview/lib/atvouch/slingshot.ex
··· 5 5 6 6 require Logger 7 7 8 - @slingshot_url "https://slingshot.microcosm.blue" 8 + @default_url "https://slingshot.microcosm.blue" 9 + 10 + defp slingshot_url do 11 + Application.get_env(:atvouch, :slingshot_url, @default_url) 12 + end 9 13 10 14 @doc """ 11 15 Resolve a single DID to a handle via slingshot. 12 16 Returns `{:ok, handle}` or `:error`. 13 17 """ 14 18 def resolve_did(did) do 15 - url = "#{@slingshot_url}/xrpc/com.bad-example.identity.resolveMiniDoc?identifier=#{URI.encode_www_form(did)}" 19 + url = "#{slingshot_url()}/xrpc/com.bad-example.identity.resolveMiniDoc?identifier=#{URI.encode_www_form(did)}" 16 20 17 21 request = %Tesla.Env{ 18 22 method: :get,
+23 -3
appview/test/atvouch/pull_handler_test.exs
··· 48 48 bot_password: "test-password" 49 49 ) 50 50 51 + # Start fake slingshot server with test identities 52 + {slingshot_pid, slingshot_port, _slingshot_agent} = 53 + Atvouch.Test.FakeSlingshotServer.start(%{ 54 + "did:plc:maintainer1" => "maintainer1.test", 55 + "did:plc:maintainer2" => "maintainer2.test", 56 + "did:plc:author" => "author.test", 57 + "did:plc:middle" => "middle.test", 58 + "did:plc:repoowner" => "repoowner.test", 59 + "did:plc:repoowner2" => "repoowner2.test" 60 + }) 61 + 62 + prev_slingshot_url = Application.get_env(:atvouch, :slingshot_url) 63 + Application.put_env(:atvouch, :slingshot_url, "http://127.0.0.1:#{slingshot_port}") 64 + 51 65 # Start fake TAP server 52 66 {tap_pid, tap_port} = Atvouch.Test.FakeTapServer.start(self()) 53 67 54 68 on_exit(fn -> 55 69 Application.put_env(:atvouch, :tangled, prev_tangled) 56 70 57 - for pid <- [pds_pid, tangled_pid, tap_pid] do 71 + if prev_slingshot_url do 72 + Application.put_env(:atvouch, :slingshot_url, prev_slingshot_url) 73 + else 74 + Application.delete_env(:atvouch, :slingshot_url) 75 + end 76 + 77 + for pid <- [pds_pid, tangled_pid, tap_pid, slingshot_pid] do 58 78 try do 59 79 Supervisor.stop(pid, :normal, 1_000) 60 80 catch ··· 216 236 assert comment.handle == "repoowner.test" 217 237 assert comment.rkey == "testrepo" 218 238 assert comment.number == 1 219 - assert comment.body =~ "atvouch routes for [@author.test](https://bsky.app/profile/did:plc:author)" 220 239 assert comment.body =~ "[@maintainer1.test](https://bsky.app/profile/did:plc:maintainer1)" 221 - assert comment.body =~ "direct vouch" 222 240 assert comment.body =~ "[@maintainer2.test](https://bsky.app/profile/did:plc:maintainer2)" 241 + assert comment.body =~ "[@author.test](https://bsky.app/profile/did:plc:author)" 242 + assert comment.body =~ "generated by [atvouch](https://atvouch.dev)" 223 243 224 244 # Verify dedup record was created 225 245 assert Atvouch.BotComment.exists?(repo_at_uri, 1)
+64
appview/test/support/fake_slingshot_server.ex
··· 1 + defmodule Atvouch.Test.FakeSlingshotServer do 2 + @moduledoc """ 3 + Fake slingshot identity service for testing DID-to-handle resolution. 4 + """ 5 + 6 + defmodule Router do 7 + use Plug.Router 8 + 9 + plug(:match) 10 + plug(:dispatch) 11 + 12 + get "/xrpc/com.bad-example.identity.resolveMiniDoc" do 13 + conn = Plug.Conn.fetch_query_params(conn) 14 + did = conn.query_params["identifier"] 15 + identities = conn.private[:identities] 16 + 17 + case Map.get(identities, did) do 18 + nil -> 19 + conn 20 + |> put_resp_content_type("application/json") 21 + |> send_resp(404, Jason.encode!(%{"error" => "not_found"})) 22 + 23 + handle -> 24 + conn 25 + |> put_resp_content_type("application/json") 26 + |> send_resp(200, Jason.encode!(%{"handle" => handle})) 27 + end 28 + end 29 + 30 + match _ do 31 + send_resp(conn, 404, "not found") 32 + end 33 + end 34 + 35 + defmodule PlugWithState do 36 + @behaviour Plug 37 + 38 + def init(opts), do: opts 39 + 40 + def call(conn, opts) do 41 + conn 42 + |> Plug.Conn.put_private(:identities, Agent.get(opts[:state_agent], & &1)) 43 + |> Router.call(Router.init([])) 44 + end 45 + end 46 + 47 + def start(identities \\ %{}) do 48 + {:ok, state_agent} = Agent.start_link(fn -> identities end) 49 + 50 + {:ok, server_pid} = 51 + Bandit.start_link( 52 + plug: {PlugWithState, state_agent: state_agent}, 53 + port: 0, 54 + ip: {127, 0, 0, 1} 55 + ) 56 + 57 + {:ok, {_ip, port}} = ThousandIsland.listener_info(server_pid) 58 + {server_pid, port, state_agent} 59 + end 60 + 61 + def set_identities(state_agent, identities) do 62 + Agent.update(state_agent, fn _ -> identities end) 63 + end 64 + end