BBS: Tests and formatting.

This commit is contained in:
lain 2019-03-31 20:35:10 +02:00
parent 3fc4ea45df
commit 10fdc080a0
3 changed files with 55 additions and 18 deletions

View File

@ -378,6 +378,9 @@
base: System.get_env("LDAP_BASE") || "dc=example,dc=com", base: System.get_env("LDAP_BASE") || "dc=example,dc=com",
uid: System.get_env("LDAP_UID") || "cn" uid: System.get_env("LDAP_UID") || "cn"
config :esshd,
enabled: false
# Import environment specific config. This must remain at the bottom # Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above. # of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs" import_config "#{Mix.env()}.exs"

View File

@ -8,34 +8,36 @@ defmodule Pleroma.BBS.Handler do
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
def on_shell(username, _pubkey, _ip, _port) do def on_shell(username, _pubkey, _ip, _port) do
:ok = IO.puts "Welcome to #{Pleroma.Config.get([:instance, :name])}!" :ok = IO.puts("Welcome to #{Pleroma.Config.get([:instance, :name])}!")
user = Pleroma.User.get_by_nickname(to_string(username)) user = Pleroma.User.get_by_nickname(to_string(username))
Logger.debug("#{inspect user}") Logger.debug("#{inspect(user)}")
loop(run_state([user: user])) loop(run_state(user: user))
end end
def on_connect(username, ip, port, method) do def on_connect(username, ip, port, method) do
Logger.debug fn -> Logger.debug(fn ->
""" """
Incoming SSH shell #{inspect self()} requested for #{username} from #{inspect ip}:#{inspect port} using #{inspect method} Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{
inspect(port)
} using #{inspect(method)}
""" """
end end)
end end
def on_disconnect(username, ip, port) do def on_disconnect(username, ip, port) do
Logger.debug fn -> Logger.debug(fn ->
"Disconnecting SSH shell for #{username} from #{inspect ip}:#{inspect port}" "Disconnecting SSH shell for #{username} from #{inspect(ip)}:#{inspect(port)}"
end end)
end end
defp loop(state) do defp loop(state) do
self_pid = self() self_pid = self()
counter = state.counter counter = state.counter
prefix = state.prefix prefix = state.prefix
user = state.user user = state.user
input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end) input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end)
wait_input state, input wait_input(state, input)
end end
def puts_activity(activity) do def puts_activity(activity) do
@ -63,11 +65,13 @@ def handle_command(%{user: user} = state, "p " <> text) do
else else
_e -> IO.puts("Could not post...") _e -> IO.puts("Could not post...")
end end
state state
end end
def handle_command(state, "home") do def handle_command(state, "home") do
user = state.user user = state.user
params = params =
%{} %{}
|> Map.put("type", ["Create", "Announce"]) |> Map.put("type", ["Create", "Announce"])
@ -81,7 +85,7 @@ def handle_command(state, "home") do
|> ActivityPub.contain_timeline(user) |> ActivityPub.contain_timeline(user)
|> Enum.reverse() |> Enum.reverse()
Enum.each(activities, fn (activity) -> Enum.each(activities, fn activity ->
puts_activity(activity) puts_activity(activity)
end) end)
@ -95,7 +99,7 @@ def handle_command(_state, command) do
defp wait_input(state, input) do defp wait_input(state, input) do
receive do receive do
{:input, ^input, "quit\n"} -> {:input, ^input, "quit\n"} ->
IO.puts "Exiting..." IO.puts("Exiting...")
{:input, ^input, code} when is_binary(code) -> {:input, ^input, code} when is_binary(code) ->
code = String.trim(code) code = String.trim(code)
@ -105,11 +109,11 @@ defp wait_input(state, input) do
loop(%{state | counter: state.counter + 1}) loop(%{state | counter: state.counter + 1})
{:error, :interrupted} -> {:error, :interrupted} ->
IO.puts "Caught Ctrl+C..." IO.puts("Caught Ctrl+C...")
loop(%{state | counter: state.counter + 1}) loop(%{state | counter: state.counter + 1})
{:input, ^input, msg} -> {:input, ^input, msg} ->
:ok = Logger.warn "received unknown message: #{inspect msg}" :ok = Logger.warn("received unknown message: #{inspect(msg)}")
loop(%{state | counter: state.counter + 1}) loop(%{state | counter: state.counter + 1})
end end
end end
@ -120,7 +124,7 @@ defp run_state(opts) do
defp io_get(pid, prefix, counter, username) do defp io_get(pid, prefix, counter, username) do
prompt = prompt(prefix, counter, username) prompt = prompt(prefix, counter, username)
send pid, {:input, self(), IO.gets(:stdio, prompt)} send(pid, {:input, self(), IO.gets(:stdio, prompt)})
end end
defp prompt(prefix, counter, username) do defp prompt(prefix, counter, username) do

30
test/bbs/handler_test.exs Normal file
View File

@ -0,0 +1,30 @@
defmodule Pleroma.BBS.HandlerTest do
use Pleroma.DataCase
alias Pleroma.BBS.Handler
alias Pleroma.Web.CommonAPI
alias Pleroma.User
import ExUnit.CaptureIO
import Pleroma.Factory
test "getting the home timeline" do
user = insert(:user)
followed = insert(:user)
{:ok, user} = User.follow(user, followed)
{:ok, _first} = CommonAPI.post(user, %{"status" => "hey"})
{:ok, _second} = CommonAPI.post(followed, %{"status" => "hello"})
output =
capture_io(fn ->
Handler.handle_command(%{user: user}, "home")
end)
assert output =~ user.nickname
assert output =~ followed.nickname
assert output =~ "hey"
assert output =~ "hello"
end
end