Skip to content

Commit d13c94a

Browse files
authored
Merge pull request #749 from DougReeder/login-email-test
Login emails: Adds tests for auth_email/2
2 parents 16a12f4 + ce7a9ca commit d13c94a

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

test/ret_web/email_test.exs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
defmodule RetWeb.EmailTest do
2+
use RetWeb.ConnCase
3+
alias RetWeb.Email
4+
alias Ret.AppConfig
5+
6+
setup do
7+
Application.put_env(:ret, RetWeb.Email, from: "noreply")
8+
:ok
9+
end
10+
11+
@to_address "test@example.com"
12+
@signin_args %{"token" => "test-token", "auth_foo" => "42"}
13+
14+
describe "auth_email/2" do
15+
# When testing, the reticulum endpoint listens on localhost port 4001.
16+
# When no app-name is set in translations, the domain name is used.
17+
# So, when testing, the default app-name is "localhost"
18+
test "returns an email with default subject and body" do
19+
email = Email.auth_email(@to_address, @signin_args)
20+
21+
assert email.from == {"localhost", "noreply"}
22+
assert email.to == @to_address
23+
assert email.subject == "Your localhost Sign-In Link"
24+
assert email.text_body =~ "To sign-in to localhost, please visit the link below."
25+
assert email.text_body =~ "http://localhost:4001/?auth_foo=42&token=test-token"
26+
end
27+
28+
test "returns an email with custom app name" do
29+
AppConfig.set_config_value("translations|en|app-name", "My Hubs Instance")
30+
31+
email = Email.auth_email(@to_address, @signin_args)
32+
33+
assert email.subject == "Your My Hubs Instance Sign-In Link"
34+
assert email.text_body =~ "To sign-in to My Hubs Instance, please visit the link below."
35+
36+
AppConfig.set_config_value("translations|en|app-name", nil)
37+
end
38+
39+
test "returns an email with custom subject" do
40+
AppConfig.set_config_value("auth|login_subject", "Custom Login Subject")
41+
42+
email = Email.auth_email(@to_address, @signin_args)
43+
44+
assert email.subject == "Custom Login Subject"
45+
46+
AppConfig.set_config_value("auth|login_subject", nil)
47+
end
48+
49+
test "returns an email with custom body and {{ link }} replacement" do
50+
AppConfig.set_config_value("auth|login_body", "Surf to: {{ link }} to log in.")
51+
52+
email = Email.auth_email(@to_address, @signin_args)
53+
54+
assert email.text_body =~
55+
"Surf to: http://localhost:4001/?auth_foo=42&token=test-token to log in."
56+
57+
AppConfig.set_config_value("auth|login_body", nil)
58+
end
59+
60+
test "returns an email with custom body appending link if {{ link }} is missing" do
61+
AppConfig.set_config_value("auth|login_body", "Custom body without placeholder.")
62+
63+
email = Email.auth_email(@to_address, @signin_args)
64+
65+
assert email.text_body =~
66+
"Custom body without placeholder.\n\nhttp://localhost:4001/?auth_foo=42&token=test-token"
67+
68+
AppConfig.set_config_value("auth|login_body", nil)
69+
end
70+
71+
test "includes Return-Path header if admin_email is set" do
72+
# admin_email is set to "admin@hubsfoundation.org" in config/test.exs
73+
email = Email.auth_email(@to_address, @signin_args)
74+
assert email.headers["Return-Path"] == "admin@hubsfoundation.org"
75+
end
76+
77+
test "omits Return-Path header if admin_email is set and TURKEY_MODE enabled" do
78+
# admin_email is set to "admin@hubsfoundation.org" in config/test.exs
79+
System.put_env("TURKEY_MODE", "1")
80+
81+
on_exit(fn ->
82+
System.delete_env("TURKEY_MODE")
83+
end)
84+
85+
email = Email.auth_email(@to_address, @signin_args)
86+
refute Map.has_key?(email.headers, "Return-Path")
87+
end
88+
89+
test "omits Return-Path header if admin_email is not set" do
90+
# admin_email is set to "admin@hubsfoundation.org" in config/test.exs
91+
admin_email = Application.get_env(:ret, Ret.Account)[:admin_email]
92+
Application.put_env(:ret, Ret.Account, admin_email: nil)
93+
94+
on_exit(fn ->
95+
Application.put_env(:ret, Ret.Account, admin_email: admin_email)
96+
end)
97+
98+
email = Email.auth_email(@to_address, @signin_args)
99+
refute Map.has_key?(email.headers, "Return-Path")
100+
end
101+
end
102+
end

0 commit comments

Comments
 (0)