ubicloud/spec/routes/web/ssh_public_key_spec.rb
Jeremy Evans 996931d35b Show link to SSH public key management page on VM create page
If the user has not already registered an SSH public key, and they
have permission to do so, show a link to the page. This makes it
more likely users will be aware of this feature.

If the project has a registered SSH public key, we don't need to
explain the benefit of an SSH public key, so hide the description
in that case.
2025-10-04 01:36:33 +09:00

102 lines
3.8 KiB
Ruby

# frozen_string_literal: true
require_relative "spec_helper"
RSpec.describe Clover do
let(:user) { create_account }
let(:project) { user.projects.first }
before do
login(user.email)
visit "#{project.path}/ssh-public-key"
end
it "can navigate to management page from project page" do
visit project.path
click_link "Manage"
expect(page.title).to eq "Ubicloud - SSH Public Keys"
end
it "can navigate to management page from vm create page" do
visit "#{project.path}/vm/create"
click_link "register SSH keys for this project"
expect(page.title).to eq "Ubicloud - SSH Public Keys"
end
it "does not show link on vm create page if user lacks permissions" do
visit "#{project.path}/vm/create"
expect(page.title).to eq "Ubicloud - Create Virtual Machine"
expect(page).to have_content("register SSH keys for this project")
AccessControlEntry.dataset.destroy
AccessControlEntry.create(project_id: project.id, subject_id: user.id, action_id: ActionType::NAME_MAP["Vm:create"])
page.refresh
expect(page.title).to eq "Ubicloud - Create Virtual Machine"
expect(page).to have_no_content("register SSH keys for this project")
AccessControlEntry.create(project_id: project.id, subject_id: user.id, action_id: ActionType::NAME_MAP["Project:edit"])
page.refresh
expect(page.title).to eq "Ubicloud - Create Virtual Machine"
expect(page).to have_content("register SSH keys for this project")
end
it "does not allow access to SSH public keys in other projects" do
click_link "Register SSH Public Key"
fill_in "Name", with: "a"
fill_in "Public Key", with: "a a"
click_button "Register"
pj = Project.create(name: "Other")
SshPublicKey.first.update(project_id: pj.id)
click_link "a"
expect(page.status_code).to eq 404
end
it "support creating, updating, and deleting SSH public keys" do
expect(page.title).to eq "Ubicloud - SSH Public Keys"
click_link "Register SSH Public Key"
expect(page.title).to eq "Ubicloud - Register SSH Public Key"
click_button "Register"
expect(page).to have_flash_error("Error registering SSH public key")
expect(page).to have_content("is not present")
fill_in "Name", with: "A A"
fill_in "Public Key", with: "a"
click_button "Register"
expect(page).to have_flash_error("Error registering SSH public key")
expect(page).to have_content("must only contain lowercase letters, numbers, and hyphens and have max length 63.")
expect(page).to have_content("invalid SSH public key format")
fill_in "Name", with: "a"
fill_in "Public Key", with: "a a"
expect(project.ssh_public_keys_dataset.all).to eq []
click_button "Register"
expect(page).to have_flash_notice("SSH public key with name a registered")
expect(project.ssh_public_keys_dataset.select_order_map([:name, :public_key])).to eq [["a", "a a"]]
expect(page.all("td a").map(&:text)).to eq ["a"]
click_link "a"
expect(page.title).to eq "Ubicloud - Update SSH Public Key"
fill_in "Name", with: "A A"
fill_in "Public Key", with: "a"
click_button "Update"
expect(page).to have_flash_error("Error updating SSH public key")
expect(page).to have_content("must only contain lowercase letters, numbers, and hyphens and have max length 63.")
expect(page).to have_content("invalid SSH public key format")
fill_in "Name", with: "b"
fill_in "Public Key", with: "b b"
click_button "Update"
expect(page).to have_flash_notice("SSH public key with name b updated")
expect(project.ssh_public_keys_dataset.select_order_map([:name, :public_key])).to eq [["b", "b b"]]
expect(page.all("td a").map(&:text)).to eq ["b"]
click_link "b"
btn = find ".delete-btn"
page.driver.delete btn["data-url"], {_csrf: btn["data-csrf"]}
expect(project.ssh_public_keys_dataset.all).to eq []
end
end