Merge branch 'fix/debian-install-libmagic-typo' into 'develop'
[akkoma] / test / pleroma / web / fed_sockets / socket_info_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.FedSockets.SocketInfoTest do
6 use ExUnit.Case
7
8 alias Pleroma.Web.FedSockets
9 alias Pleroma.Web.FedSockets.SocketInfo
10
11 describe "uri_for_origin" do
12 test "provides the fed_socket URL given the origin information" do
13 endpoint = "example.com:4000"
14 assert FedSockets.uri_for_origin(endpoint) =~ "ws://"
15 assert FedSockets.uri_for_origin(endpoint) =~ endpoint
16 end
17 end
18
19 describe "origin" do
20 test "will provide the origin field given a url" do
21 endpoint = "example.com:4000"
22 assert SocketInfo.origin("ws://#{endpoint}") == endpoint
23 assert SocketInfo.origin("http://#{endpoint}") == endpoint
24 assert SocketInfo.origin("https://#{endpoint}") == endpoint
25 end
26
27 test "will proide the origin field given a uri" do
28 endpoint = "example.com:4000"
29 uri = URI.parse("http://#{endpoint}")
30
31 assert SocketInfo.origin(uri) == endpoint
32 end
33 end
34
35 describe "touch" do
36 test "will update the TTL" do
37 endpoint = "example.com:4000"
38 socket = SocketInfo.build("ws://#{endpoint}")
39 Process.sleep(2)
40 touched_socket = SocketInfo.touch(socket)
41
42 assert socket.connected_until < touched_socket.connected_until
43 end
44 end
45
46 describe "expired?" do
47 setup do
48 start_supervised(
49 {Pleroma.Web.FedSockets.Supervisor,
50 [
51 ping_interval: 8,
52 connection_duration: 5,
53 rejection_duration: 5,
54 fed_socket_rejections: [lazy: true]
55 ]}
56 )
57
58 :ok
59 end
60
61 test "tests if the TTL is exceeded" do
62 endpoint = "example.com:4000"
63 socket = SocketInfo.build("ws://#{endpoint}")
64 refute SocketInfo.expired?(socket)
65 Process.sleep(10)
66
67 assert SocketInfo.expired?(socket)
68 end
69 end
70
71 describe "creating outgoing connection records" do
72 test "can be passed a string" do
73 assert %{conn_pid: :pid, origin: _origin} = SocketInfo.build("example.com:4000", :pid)
74 end
75
76 test "can be passed a URI" do
77 uri = URI.parse("http://example.com:4000")
78 assert %{conn_pid: :pid, origin: origin} = SocketInfo.build(uri, :pid)
79 assert origin =~ "example.com:4000"
80 end
81
82 test "will include the port number" do
83 assert %{conn_pid: :pid, origin: origin} = SocketInfo.build("http://example.com:4000", :pid)
84
85 assert origin =~ ":4000"
86 end
87
88 test "will provide the port if missing" do
89 assert %{conn_pid: :pid, origin: "example.com:80"} =
90 SocketInfo.build("http://example.com", :pid)
91
92 assert %{conn_pid: :pid, origin: "example.com:443"} =
93 SocketInfo.build("https://example.com", :pid)
94 end
95 end
96
97 describe "creating incoming connection records" do
98 test "can be passed a string" do
99 assert %{pid: _, origin: _origin} = SocketInfo.build("example.com:4000")
100 end
101
102 test "can be passed a URI" do
103 uri = URI.parse("example.com:4000")
104 assert %{pid: _, origin: _origin} = SocketInfo.build(uri)
105 end
106
107 test "will include the port number" do
108 assert %{pid: _, origin: origin} = SocketInfo.build("http://example.com:4000")
109
110 assert origin =~ ":4000"
111 end
112
113 test "will provide the port if missing" do
114 assert %{pid: _, origin: "example.com:80"} = SocketInfo.build("http://example.com")
115 assert %{pid: _, origin: "example.com:443"} = SocketInfo.build("https://example.com")
116 end
117 end
118 end