# Changing the email address, by a link mailed to the new one.
setup do
get "/session/new"
end
# The path of the link in the last mail sent.
def mailed_link -> String do
text = glacier::testing::delivered_mail().last().unwrap().text.clone().unwrap()
link = text.split_whitespace().find(fn word -> word.contains("/email_changes/") end).unwrap().to_owned()
link[link.find("/email_changes/").unwrap()..].to_owned()
end
test "changes the address once the new one is confirmed" do
user = insert(User, email_address: "old@example.com")
post "/session", [email_address: "old@example.com", password: "password"]
get "/email_change/new"
res = post "/email_change", [email_address: "New@Example.com"]
assert res.location == "/"
mail = glacier::testing::delivered_mail().last().unwrap().clone()
assert mail.to == ["new@example.com"]
# Not yet: only following the link changes it.
assert User.find(user.id)!.email_address == "old@example.com"
get mailed_link()
user = User.find(user.id)!
assert user.email_address == "new@example.com"
assert user.unconfirmed_email.is_none()
# The old address hears about it.
assert glacier::testing::delivered_mail().last().unwrap().to == ["old@example.com"]
end
test "mails nothing for an address someone has, and says the same" do
insert(User, email_address: "old@example.com")
insert(User, email_address: "taken@example.com")
post "/session", [email_address: "old@example.com", password: "password"]
get "/email_change/new"
before = glacier::testing::delivered_mail().len()
res = post "/email_change", [email_address: "taken@example.com"]
assert get(res.location).body =~ "Check taken@example.com for a link"
assert glacier::testing::delivered_mail().len() == before
end
test "a link for an address since replaced doesn't work" do
user = insert(User, email_address: "old@example.com")
post "/session", [email_address: "old@example.com", password: "password"]
get "/email_change/new"
post "/email_change", [email_address: "first@example.com"]
first = mailed_link()
post "/email_change", [email_address: "second@example.com"]
get &first
assert User.find(user.id)!.email_address == "old@example.com"
end