Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix org_certificate_controller :edit cannot render #847

Merged
merged 1 commit into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

<%= inputs_for f, :jitp, fn fp -> %>
<label for="jitp_toggle"> Enable Just In Time Provisioning </label>
<input type="checkbox" name="jitp_toggle" id="jitp_toggle" checked={f.data.jitp}>
<input type="checkbox" name="jitp_toggle" id="jitp_toggle" checked={not is_nil(f.data.jitp)}>
<%= hidden_input fp, :delete, id: "jitp-delete" %>
<div hidden={!f.data.jitp} id="jitp_form">
<div hidden={is_nil(f.data.jitp)} id="jitp_form">
Comment on lines -12 to +14
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f.data.jitp is not boolean, is nil or struct.

<div class="form-group">
<label for="description_input" class="tooltip-label h3 mb-1">
<span>JITP Description</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,28 @@ defmodule NervesHubWWWWeb.OrgCertificateControllerTest do
test "renders form", %{conn: conn, org: org} do
%{db_cert: ca} = Fixtures.ca_certificate_fixture(org)
conn = get(conn, Routes.org_certificate_path(conn, :edit, org.name, ca.serial))
assert html_response(conn, 200) =~ "Edit Certificate Authority"

html = html_response(conn, 200)
assert html =~ "Edit Certificate Authority"

assert html
|> Floki.parse_document!()
|> Floki.find("#jitp_form")
|> Floki.attribute("hidden") == ["hidden"]
end

test "renders form, jitp enabled", %{conn: conn, org: org, user: user} do
product = Fixtures.product_fixture(user, org)
%{db_cert: ca} = Fixtures.ca_certificate_jitp_enabled_fixture(org, product)
conn = get(conn, Routes.org_certificate_path(conn, :edit, org.name, ca.serial))

html = html_response(conn, 200)
assert html =~ "Edit Certificate Authority"

assert html
|> Floki.parse_document!()
|> Floki.find("#jitp_form")
|> Floki.attribute("hidden") == []
end

test "redirects to index when not found", %{conn: conn, org: org} do
Expand Down
29 changes: 23 additions & 6 deletions test/support/fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,24 +124,41 @@ defmodule NervesHubWebCore.Fixtures do
org_key
end

def ca_certificate_fixture(%Accounts.Org{} = org, opts \\ []) do
opts = Keyword.merge([template: :root_ca], opts)
ca_key = X509.PrivateKey.new_ec(:secp256r1)
ca = X509.Certificate.self_signed(ca_key, "CN=#{org.name}", opts)

defp to_ca_certificate_params(ca) do
{not_before, not_after} = NervesHubWebCore.Certificate.get_validity(ca)

serial = NervesHubWebCore.Certificate.get_serial_number(ca)
aki = NervesHubWebCore.Certificate.get_aki(ca)

params = %{
%{
serial: serial,
aki: aki,
ski: NervesHubWebCore.Certificate.get_ski(ca),
not_before: not_before,
not_after: not_after,
der: X509.Certificate.to_der(ca)
}
end

def ca_certificate_fixture(%Accounts.Org{} = org, opts \\ []) do
opts = Keyword.merge([template: :root_ca], opts)
ca_key = X509.PrivateKey.new_ec(:secp256r1)
ca = X509.Certificate.self_signed(ca_key, "CN=#{org.name}", opts)

params = to_ca_certificate_params(ca)

{:ok, db_cert} = Devices.create_ca_certificate(org, params)
%{cert: ca, key: ca_key, db_cert: db_cert}
end

def ca_certificate_jitp_enabled_fixture(%Accounts.Org{} = org, %Product{} = product, opts \\ []) do
opts = Keyword.merge([template: :root_ca], opts)
ca_key = X509.PrivateKey.new_ec(:secp256r1)
ca = X509.Certificate.self_signed(ca_key, "CN=#{org.name}", opts)

params =
to_ca_certificate_params(ca)
|> Map.put(:jitp, %{"description" => "jitp", "product_id" => product.id, "tags" => ["prod"]})

{:ok, db_cert} = Devices.create_ca_certificate(org, params)
%{cert: ca, key: ca_key, db_cert: db_cert}
Expand Down