2020-06-02 13:37:38 +03:00
|
|
|
defmodule Plausible.Workers.SslCertificatesTest do
|
2020-02-26 11:54:21 +03:00
|
|
|
use Plausible.DataCase
|
2020-06-02 13:37:38 +03:00
|
|
|
alias Plausible.Workers.ProvisionSslCertificates
|
2020-02-26 11:54:21 +03:00
|
|
|
import Double
|
|
|
|
|
|
|
|
test "makes ssh call to certbot" do
|
|
|
|
site = insert(:site)
|
|
|
|
insert(:custom_domain, site: site, domain: "custom-site.com")
|
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
ssh_stub =
|
|
|
|
stub(SSHEx, :connect, fn _cmd -> {:ok, nil} end)
|
2021-04-14 15:31:36 +03:00
|
|
|
|> stub(:run, fn _conn, _cmd, _opts -> {:ok, "", 0} end)
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
ProvisionSslCertificates.perform(nil, ssh_stub)
|
2020-02-26 11:54:21 +03:00
|
|
|
|
2021-04-14 15:08:44 +03:00
|
|
|
assert_receive(
|
|
|
|
{SSHEx, :run,
|
2021-04-14 15:31:36 +03:00
|
|
|
[nil, 'sudo certbot certonly --webroot -w /root/webroot -n -d "custom-site.com"', _]}
|
2021-04-14 15:08:44 +03:00
|
|
|
)
|
2020-02-26 11:54:21 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "sets has_ssl_certficate=true if the ssh command is succesful" do
|
|
|
|
site = insert(:site)
|
|
|
|
insert(:custom_domain, site: site, domain: "custom-site.com")
|
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
ssh_stub =
|
|
|
|
stub(SSHEx, :connect, fn _cmd -> {:ok, nil} end)
|
2021-04-14 15:31:36 +03:00
|
|
|
|> stub(:run, fn _conn, _cmd, _opts -> {:ok, "", 0} end)
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
ProvisionSslCertificates.perform(nil, ssh_stub)
|
2020-02-26 11:54:21 +03:00
|
|
|
|
|
|
|
domain = Repo.get_by(Plausible.Site.CustomDomain, site_id: site.id)
|
|
|
|
assert domain.has_ssl_certificate
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not set has_ssl_certficate=true if the ssh command fails" do
|
|
|
|
site = insert(:site)
|
|
|
|
insert(:custom_domain, site: site, domain: "custom-site.com")
|
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
ssh_stub =
|
|
|
|
stub(SSHEx, :connect, fn _cmd -> {:ok, nil} end)
|
2021-04-14 15:31:36 +03:00
|
|
|
|> stub(:run, fn _conn, _cmd, _opts -> {:ok, "", 1} end)
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
ProvisionSslCertificates.perform(nil, ssh_stub)
|
2020-02-26 11:54:21 +03:00
|
|
|
|
|
|
|
domain = Repo.get_by(Plausible.Site.CustomDomain, site_id: site.id)
|
|
|
|
refute domain.has_ssl_certificate
|
|
|
|
end
|
2021-04-28 15:25:30 +03:00
|
|
|
|
|
|
|
test "does not set has_ssl_certficate=true if the ssh command errors completely" do
|
|
|
|
site = insert(:site)
|
|
|
|
insert(:custom_domain, site: site, domain: "custom-site.com")
|
|
|
|
|
|
|
|
ssh_stub =
|
|
|
|
stub(SSHEx, :connect, fn _cmd -> {:ok, nil} end)
|
|
|
|
|> stub(:run, fn _conn, _cmd, _opts -> {:error, "msg"} end)
|
|
|
|
|
|
|
|
ProvisionSslCertificates.perform(nil, ssh_stub)
|
|
|
|
|
|
|
|
domain = Repo.get_by(Plausible.Site.CustomDomain, site_id: site.id)
|
|
|
|
refute domain.has_ssl_certificate
|
|
|
|
end
|
2020-02-26 11:54:21 +03:00
|
|
|
end
|