Skip to content

Instantly share code, notes, and snippets.

View cybrox's full-sized avatar
☠️
Tinkering

Sven Gehring cybrox

☠️
Tinkering
View GitHub Profile
@cybrox
cybrox / testing-protected-phoenix-controllers-2.ex
Created July 15, 2018 13:32
Blog: Testing Protected Phoenix Controllers - Snippet 2
if @env == :test do
defp send_unauthorized_if_not_faked(conn) do
# Implement testing backdoor
conn
end
else
defp send_unauthorized_if_not_faked(conn) do
send_unauthorized_response(conn)
end
end
@cybrox
cybrox / testing-protected-phoenix-controllers-3.ex
Created July 15, 2018 13:33
testing-protected-phoenix-controllers-3.ex
@spec send_unauthorized_if_not_faked(Plug.Conn.t) :: Plug.Conn.t
if @env == :test do
defp send_unauthorized_if_not_faked(conn) do
conn |> append_testing_backdoor()
end
else
defp send_unauthorized_if_not_faked(conn) do
conn |> send_unauthorized_response()
end
end
@cybrox
cybrox / testing-protected-phoenix-controllers-4.ex
Created July 15, 2018 13:34
testing-protected-phoenix-controllers-4.ex
test "accessing a protected route" do
user = insert(:user, [username: "Sven"])
conn = conn |> get(user_path(conn, :show, "self") <> with_user(user))
# assert results
end
defmodule Rayman do
@moduledoc """
Rayman is a pure Elixir implementation of MQTT for educational purposes.
"""
def decode_packet(packet) do
IO.inspect packet
end
def encode_packet(data) do
defmodule RaymanPlayground.Application do
@moduledoc false
use Application
def start(_type, _args) do
{:ok, pid} = :gen_tcp.listen(1883, [
:binary,
active: false,
reuseaddr: true
defp deps do
[
# NOTE: You would have to adjust this to your local path
{:rayman, path: "/Users/sgehring/development/blog/rayman"}
]
end
@cybrox
cybrox / phx-ete1.ex
Created March 20, 2019 15:33
phx-ete1.ex
config :myapp, MyApp.Endpoint,
server: true
@cybrox
cybrox / phx-ete2.ex
Last active March 20, 2019 15:59
phx-ete2.ex
if @env == :test do
forward("end-to-end", MyApp.Plug.TestEndToEnd)
end
@cybrox
cybrox / phx-ete3.ex
Created March 20, 2019 15:34
phx-ete3.ex
defmodule MyApp.Plug.TestEndToEnd do
use Plug.Router
plug :match
plug :dispatch
match _, do: send_resp(conn, 404, "not found")
end
@cybrox
cybrox / phx-ete4.ex
Created March 20, 2019 15:34
phx-ete4.ex
defp checkout_shared_db_conn do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo, ownership_timeout: :infinity)
:ok = Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()})
end
defp checkin_shared_db_conn(_) do
:ok = Ecto.Adapters.SQL.Sandbox.checkin(Repo)
end