Files
akkounts/spec/components/settings/nostr_edit_profile_component_spec.rb
raucao 3e3d52ad8b
CI / Test (pull_request) Successful in 1m19s
Release Drafter / Update release notes draft (pull_request) Successful in 2s
Upload the account avatar to Blossom when it fills in for the picture
When a profile has no usable picture, the account avatar is now cropped and uploaded to the configured Blossom server on save, and the returned URL is written to the profile, instead of publishing the account's own avatar URL. A failed upload fails the save.
2026-11-28 21:23:45 +04:00

420 lines
18 KiB
Ruby
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# frozen_string_literal: true
require "rails_helper"
RSpec.describe Settings::NostrEditProfileComponent, type: :component do
let(:user) { build(:user, cn: "alice") }
before do
allow(user).to receive(:address).and_return("alice@kosmos.org")
allow(user).to receive(:display_name).and_return("Alice Wonderland")
end
it "wires the form to the profile editor" do
rendered = render_inline(described_class.new(user: user, profile_event: nil))
form = rendered.css("form").first
expect(form["data-controller"]).to eq("nostr-profile-editor")
expect(form["data-action"]).to include("submit->nostr-profile-editor#save")
expect(form["data-nostr-profile-editor-handle-value"]).to eq("alice")
expect(JSON.parse(form["data-nostr-profile-editor-profile-value"])).to eq({})
expect(rendered.css("[data-nostr-profile-editor-target='error']")).to be_present
expect(rendered.css("[data-nostr-profile-editor-target='submit']")).to be_present
end
it "uses the standard editor width and a responsive banner ratio" do
rendered = render_inline(described_class.new(user: user, profile_event: nil))
root = rendered.css("[data-controller='nostr-image-upload']").first
expect(root["class"]).to include("md:w-[600px]")
expect(root["class"]).not_to include("lg:w-[760px]")
header = rendered.css("header").first
expect(header["class"]).to include("aspect-[11/4]", "sm:aspect-[4/1]")
expect(header["class"]).not_to include("aspect-[3/1]", "h-36", "h-44")
avatar = header.css(".absolute").find { |node| node["class"].to_s.include?("size-28") }
expect(avatar["class"]).to include("-bottom-14", "left-4")
main = rendered.css("main").first
expect(main["class"]).to include("pt-20", "sm:pt-24")
expect(main["class"]).not_to include("mt-44")
end
it "passes the display-name key list to the editor" do
rendered = render_inline(described_class.new(user: user, profile_event: nil))
form = rendered.css("form").first
keys = JSON.parse(form["data-nostr-profile-editor-display-name-keys-value"])
expect(keys).to eq(%w[display_name displayName name username])
end
describe "display name resolution" do
def rendered_display_name(profile_event)
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
rendered.css("input[name='user[display_name]']").first["value"]
end
def profile_event(content)
{ "content" => content.to_json }
end
it "reads display_name" do
expect(rendered_display_name(profile_event("display_name" => "Alice D"))).to eq("Alice D")
end
it "falls back to displayName" do
expect(rendered_display_name(profile_event("displayName" => "Alice Camel"))).to eq("Alice Camel")
end
it "falls back to name" do
expect(rendered_display_name(profile_event("name" => "alice"))).to eq("alice")
end
it "falls back to username" do
expect(rendered_display_name(profile_event("username" => "alice_legacy"))).to eq("alice_legacy")
end
it "prefers display_name over name" do
profile = profile_event("display_name" => "Alice D", "name" => "alice")
expect(rendered_display_name(profile)).to eq("Alice D")
end
it "skips blank values" do
profile = profile_event("display_name" => "", "name" => "alice")
expect(rendered_display_name(profile)).to eq("alice")
end
it "falls back to the account display name" do
expect(rendered_display_name(profile_event("about" => "hi"))).to eq("Alice Wonderland")
end
it "uses the account display name without a profile event" do
expect(rendered_display_name(nil)).to eq("Alice Wonderland")
end
end
it "passes the existing profile to the editor so unknown keys survive" do
profile_event = {
"content" => { "picture" => "https://example.com/p.png", "nip05" => "alice@kosmos.org" }.to_json
}
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
form = rendered.css("form").first
profile = JSON.parse(form["data-nostr-profile-editor-profile-value"])
expect(profile).to eq("picture" => "https://example.com/p.png", "nip05" => "alice@kosmos.org")
end
describe "about and website fields" do
it "prefills them from the profile" do
profile_event = {
"content" => {
"about" => "Hello there",
"website" => "https://alice.example.com"
}.to_json
}
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
expect(rendered.css("textarea[name='user[about]']").first.text).to eq("Hello there")
expect(rendered.css("input[name='user[website]']").first["value"])
.to eq("https://alice.example.com")
end
it "renders them blank when absent" do
rendered = render_inline(described_class.new(user: user, profile_event: nil))
expect(rendered.css("textarea[name='user[about]']").first.text).to be_blank
expect(rendered.css("input[name='user[website]']").first["value"]).to be_blank
end
it "ignores non-string values" do
profile_event = {
"content" => { "about" => 42, "website" => ["https://x.example.com"] }.to_json
}
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
expect(rendered.css("textarea[name='user[about]']").first.text).to be_blank
expect(rendered.css("input[name='user[website]']").first["value"]).to be_blank
end
end
describe "image URL sanitization" do
def preview_img(rendered)
rendered.css("img[data-nostr-profile-editor-target='picturePreview']").first
end
it "ignores non-http(s) banner and picture URLs" do
profile_event = {
"content" => {
"banner" => "javascript:alert(1)",
"picture" => "ftp://example.com/x.png"
}.to_json
}
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
expect(preview_img(rendered)["class"]).to include("hidden")
expect(rendered.css("header").first["style"]).to be_blank
end
it "renders http(s) banner and picture URLs" do
profile_event = {
"content" => {
"banner" => "https://example.com/b.png",
"picture" => "https://example.com/p.png"
}.to_json
}
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
expect(preview_img(rendered)["src"]).to eq("https://example.com/p.png")
expect(preview_img(rendered)["class"]).not_to include("hidden")
expect(rendered.css("header").first["style"]).to include("https://example.com/b.png")
end
end
it "seeds the banner value from the profile" do
profile_event = { "content" => { "banner" => "https://example.com/b.png" }.to_json }
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
form = rendered.css("form").first
expect(form["data-nostr-profile-editor-banner-value"]).to eq("https://example.com/b.png")
end
describe "image upload affordances" do
it "shows the edit buttons and panel when a Blossom server is configured" do
allow(Setting).to receive(:nostr_blossom_server_url).and_return("https://blossom.example.com")
rendered = render_inline(described_class.new(user: user, profile_event: nil))
expect(rendered.css("button[data-nostr-image-upload-field-param='picture']")).to be_present
expect(rendered.css("button[data-nostr-image-upload-field-param='banner']")).to be_present
expect(rendered.css("[data-nostr-image-upload-target='panel']")).to be_present
root = rendered.css("[data-controller='nostr-image-upload']").first
expect(root["data-nostr-image-upload-store-url-value"]).to eq("/settings/nostr_profile_images")
expect(root["data-nostr-image-upload-nostr-profile-editor-outlet"]).to eq("[data-controller~='nostr-profile-editor']")
expect(root["data-nostr-image-upload-picture-max-bytes-value"]).to eq("1048576")
expect(root["data-nostr-image-upload-banner-max-bytes-value"]).to eq("2097152")
descriptions = JSON.parse(root["data-nostr-image-upload-descriptions-value"])
expect(descriptions["picture"]).to include("400×400", "1 MB")
expect(descriptions["banner"]).to include("1500×500", "2 MB")
form = rendered.css("form").first
expect(form["data-nostr-profile-editor-flush-url-value"]).to eq("/settings/nostr_media")
end
it "hides the edit buttons when no Blossom server is configured" do
allow(Setting).to receive(:nostr_blossom_server_url).and_return(nil)
rendered = render_inline(described_class.new(user: user, profile_event: nil))
expect(rendered.css("button[data-nostr-image-upload-field-param='picture']")).to be_empty
expect(rendered.css("button[data-nostr-image-upload-field-param='banner']")).to be_empty
end
end
describe "account avatar fallback" do
context "with an account avatar" do
let(:user) { create(:user, cn: "alice") }
let(:fixture_path) { Rails.root.join("spec/fixtures/files/taipei.jpg") }
let(:img_hash) { Digest::SHA256.hexdigest(File.read(fixture_path)) }
let(:avatar_src) { %r{users/alice/avatars/#{img_hash}\.jpg} }
before do
allow(Setting).to receive(:nostr_blossom_server_url).and_return("https://blossom.example.com")
ActiveStorage::Blob.create_and_upload!(
io: File.open(fixture_path),
filename: "#{img_hash}.jpg",
content_type: "image/jpeg"
).tap { |blob| user.avatar.attach(blob) }
end
it "seeds the picture with the account avatar when the profile has none" do
rendered = render_inline(described_class.new(user: user, profile_event: nil))
img = rendered.css("img[data-nostr-profile-editor-target='picturePreview']").first
expect(img["src"]).to match(avatar_src)
form = rendered.css("form").first
expect(form["data-nostr-profile-editor-picture-value"]).to match(avatar_src)
end
it "prefers the profile's picture over the account avatar" do
profile_event = { "content" => { "picture" => "https://example.com/p.png" }.to_json }
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
form = rendered.css("form").first
expect(form["data-nostr-profile-editor-picture-value"]).to eq("https://example.com/p.png")
end
it "treats a non-URL picture as absent" do
profile_event = { "content" => { "picture" => "ftp://example.com/x.png" }.to_json }
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
form = rendered.css("form").first
expect(form["data-nostr-profile-editor-picture-value"]).to match(avatar_src)
end
it "flags the account avatar for upload to Blossom" do
rendered = render_inline(described_class.new(user: user, profile_event: nil))
form = rendered.css("form").first
expect(form["data-nostr-profile-editor-picture-fallback-value"]).to eq("true")
expect(form["data-nostr-profile-editor-store-url-value"]).to eq("/settings/nostr_profile_images")
end
it "does not flag the fallback when the profile already has a picture" do
profile_event = { "content" => { "picture" => "https://example.com/p.png" }.to_json }
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
form = rendered.css("form").first
expect(form["data-nostr-profile-editor-picture-fallback-value"]).to eq("false")
end
end
context "without an account avatar" do
before { allow(Setting).to receive(:nostr_blossom_server_url).and_return("https://blossom.example.com") }
it "renders the placeholder and seeds an empty picture value" do
rendered = render_inline(described_class.new(user: user, profile_event: nil))
img = rendered.css("img[data-nostr-profile-editor-target='picturePreview']").first
expect(img["class"]).to include("hidden")
placeholder = rendered.css("[data-nostr-profile-editor-target='picturePlaceholder']").first
expect(placeholder["class"].split).not_to include("hidden")
form = rendered.css("form").first
expect(form["data-nostr-profile-editor-picture-value"]).to eq("")
end
it "does not flag the account avatar fallback" do
rendered = render_inline(described_class.new(user: user, profile_event: nil))
form = rendered.css("form").first
expect(form["data-nostr-profile-editor-picture-fallback-value"]).to eq("false")
end
end
context "with an account avatar but no Blossom server configured" do
let(:user) { create(:user, cn: "alice") }
let(:fixture_path) { Rails.root.join("spec/fixtures/files/taipei.jpg") }
let(:img_hash) { Digest::SHA256.hexdigest(File.read(fixture_path)) }
before do
allow(Setting).to receive(:nostr_blossom_server_url).and_return(nil)
ActiveStorage::Blob.create_and_upload!(
io: File.open(fixture_path),
filename: "#{img_hash}.jpg",
content_type: "image/jpeg"
).tap { |blob| user.avatar.attach(blob) }
end
it "does not flag the account avatar fallback" do
rendered = render_inline(described_class.new(user: user, profile_event: nil))
form = rendered.css("form").first
expect(form["data-nostr-profile-editor-picture-fallback-value"]).to eq("false")
end
end
end
describe "when profile event is missing or fields are empty" do
it "prefills the display name, NIP-05 and Lightning addresses and marks them as added" do
rendered = render_inline(described_class.new(user: user, profile_event: nil))
display_name_input = rendered.css("input[name='user[display_name]']").first
expect(display_name_input["value"]).to eq("Alice Wonderland")
expect(display_name_input["class"]).to include("border-emerald-500")
expect(display_name_input["data-action"]).to include("input->field-suggestion#checkAdded")
nip05_input = rendered.css("input[name='user[nip05_address]']").first
expect(nip05_input["value"]).to eq("alice@kosmos.org")
expect(nip05_input["class"]).to include("border-emerald-500")
expect(nip05_input["data-action"]).to include("input->field-suggestion#checkAdded")
lud16_input = rendered.css("input[name='user[lud16_address]']").first
expect(lud16_input["value"]).to eq("alice@kosmos.org")
expect(lud16_input["class"]).to include("border-emerald-500")
expect(lud16_input["data-action"]).to include("input->field-suggestion#checkAdded")
badges = rendered.css("span.rounded-full").select { |el| el.text.strip == "added" }
expect(badges.size).to eq(3)
end
context "without an account display name" do
before { allow(user).to receive(:display_name).and_return(nil) }
it "does not mark the display name as added" do
rendered = render_inline(described_class.new(user: user, profile_event: nil))
display_name_input = rendered.css("input[name='user[display_name]']").first
expect(display_name_input["value"]).to be_blank
expect(display_name_input["class"]).not_to include("border-emerald-500")
badges = rendered.css("span.rounded-full").select { |el| el.text.strip == "added" }
expect(badges.size).to eq(2)
end
end
end
describe "when profile has different NIP-05 and Lightning addresses" do
let(:profile_event) do
{
"content" => {
"nip05" => "alice@other.org",
"lud16" => "alice@getalby.com",
"display_name" => "Alice External"
}.to_json
}
end
it "shows current values with different badges and suggestion strips" do
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
display_name_input = rendered.css("input[name='user[display_name]']").first
expect(display_name_input["value"]).to eq("Alice External")
expect(display_name_input["class"]).not_to include("border-emerald-500")
expect(display_name_input["class"]).not_to include("border-amber-400")
nip05_input = rendered.css("input[name='user[nip05_address]']").first
expect(nip05_input["value"]).to eq("alice@other.org")
expect(nip05_input["class"]).to include("border-amber-400")
lud16_input = rendered.css("input[name='user[lud16_address]']").first
expect(lud16_input["value"]).to eq("alice@getalby.com")
expect(lud16_input["class"]).to include("border-amber-400")
different_badges = rendered.css("span.rounded-full").select { |el| el.text.strip == "different" }
expect(different_badges.size).to eq(2)
suggestion_buttons = rendered.css("button[data-action='click->field-suggestion#apply']")
expect(suggestion_buttons.size).to eq(2)
expect(suggestion_buttons.first["data-field-suggestion-value-param"]).to eq("alice@kosmos.org")
end
end
describe "when profile addresses match user address" do
let(:profile_event) do
{
"content" => {
"nip05" => "alice@kosmos.org",
"lud16" => "alice@kosmos.org",
"display_name" => "Alice Wonderland"
}.to_json
}
end
it "renders standard inputs without added or different badges" do
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
nip05_input = rendered.css("input[name='user[nip05_address]']").first
expect(nip05_input["value"]).to eq("alice@kosmos.org")
expect(nip05_input["class"]).not_to include("border-emerald-500")
expect(nip05_input["class"]).not_to include("border-amber-400")
badges = rendered.css("span.rounded-full").select { |el| %w[added different].include?(el.text.strip) }
expect(badges).to be_empty
end
end
end