diff --git a/app/controllers/profile_preview_card_controller.rb b/app/controllers/profile_preview_card_controller.rb deleted file mode 100644 index 29f2d326f..000000000 --- a/app/controllers/profile_preview_card_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -class ProfilePreviewCardController < ApplicationController - layout false - - def show - @actor = User.find_by(id: params[:id]) - end -end diff --git a/app/controllers/profile_preview_cards_controller.rb b/app/controllers/profile_preview_cards_controller.rb new file mode 100644 index 000000000..774ba7ecd --- /dev/null +++ b/app/controllers/profile_preview_cards_controller.rb @@ -0,0 +1,7 @@ +class ProfilePreviewCardsController < ApplicationController + layout false + + def show + @user = User.includes(:profile, :setting).find(params[:id]) + end +end diff --git a/app/javascript/packs/commentDropdowns.js b/app/javascript/packs/commentDropdowns.js index 979878a14..107748e08 100644 --- a/app/javascript/packs/commentDropdowns.js +++ b/app/javascript/packs/commentDropdowns.js @@ -77,7 +77,9 @@ const fetchMissingProfilePreviewCard = async (placeholderElement) => { jsCommentUserId: commentUserId, jsDropdownContentId: dropdownContentId, } = placeholderElement.dataset; - const response = await window.fetch(`/profile_preview_card/${commentUserId}`); + const response = await window.fetch( + `/profile_preview_cards/${commentUserId}`, + ); const htmlContent = await response.text(); const generatedElement = document.createElement('div'); diff --git a/app/views/profile_preview_card/show.html.erb b/app/views/profile_preview_card/show.html.erb deleted file mode 100644 index 7a172a59f..000000000 --- a/app/views/profile_preview_card/show.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= render "shared/profile_preview_card", actor: @actor, id: nil %> diff --git a/app/views/profile_preview_cards/show.html.erb b/app/views/profile_preview_cards/show.html.erb new file mode 100644 index 000000000..b62285a5c --- /dev/null +++ b/app/views/profile_preview_cards/show.html.erb @@ -0,0 +1 @@ +<%= render "shared/profile_preview_card", actor: @user, id: nil %> diff --git a/app/views/profile_preview_cards/show.json.jbuilder b/app/views/profile_preview_cards/show.json.jbuilder new file mode 100644 index 000000000..f3b1ac9f4 --- /dev/null +++ b/app/views/profile_preview_cards/show.json.jbuilder @@ -0,0 +1,12 @@ +json.extract!( + @user.profile, + :summary, :employment_title, :employer_name, :employer_url, :location, :education +) + +json.card_color( + Color::CompareHex.new([user_colors(@user)[:bg], user_colors(@user)[:text]]).brightness(0.88), +) + +json.email @user.email if @user.setting.display_email_on_profile + +json.created_at utc_iso_timestamp(@user.created_at) diff --git a/config/routes.rb b/config/routes.rb index 5a20eb3a5..a0a940750 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -189,7 +189,7 @@ Rails.application.routes.draw do resources :article_approvals, only: %i[create] resources :video_chats, only: %i[show] resources :sidebars, only: %i[show] - resources :profile_preview_card, only: %i[show] + resources :profile_preview_cards, only: %i[show] resources :user_subscriptions, only: %i[create] do collection do get "/subscribed", action: "subscribed" diff --git a/spec/requests/profile_preview_cards_spec.rb b/spec/requests/profile_preview_cards_spec.rb new file mode 100644 index 000000000..f450dfe8c --- /dev/null +++ b/spec/requests/profile_preview_cards_spec.rb @@ -0,0 +1,155 @@ +require "rails_helper" + +RSpec.describe "ProfilePreviewCards", type: :request do + let(:user) { create(:profile).user } + + describe "GET /:id" do + context "when signed out" do + it "does not find an unknown user id" do + expect { get profile_preview_card_path(9999) }.to raise_error(ActiveRecord::RecordNotFound) + end + + it "is a successful response" do + get profile_preview_card_path(user) + + expect(response).to have_http_status(:ok) + end + + it "returns the data" do + get profile_preview_card_path(user) + + expect(response.body).to include("profile-preview-card__content") + end + end + + context "when signed in" do + before { sign_in(user) } + + it "does not find an unknown user id" do + expect { get profile_preview_card_path(9999) }.to raise_error(ActiveRecord::RecordNotFound) + end + + it "is a successful response" do + get profile_preview_card_path(user) + + expect(response).to have_http_status(:ok) + end + + it "returns the data" do + get profile_preview_card_path(user) + + expect(response.body).to include("profile-preview-card__content") + end + end + end + + describe "GET /:id as JSON" do + let(:profile) { user.profile } + + context "when signed out" do + it "does not find an unknown user id" do + expect { get profile_preview_card_path(9999), as: :json }.to raise_error(ActiveRecord::RecordNotFound) + end + + it "is a successful response" do + get profile_preview_card_path(user), as: :json + + expect(response).to have_http_status(:ok) + end + + it "returns the data", :aggregate_failures do + get profile_preview_card_path(user), as: :json + + preview_card = response.parsed_body + expect(preview_card["summary"]).to eq(profile.summary) + expect(preview_card["employment_title"]).to eq(profile.employment_title) + expect(preview_card["employer_name"]).to eq(profile.employer_name) + expect(preview_card["employer_url"]).to eq(profile.employer_url) + expect(preview_card["location"]).to eq(profile.location) + expect(preview_card["education"]).to eq(profile.education) + expect(preview_card["created_at"]).to eq(profile.created_at.utc.iso8601) + end + + it "has the correct card color" do + user.setting.update(brand_color1: Faker::Color.hex_color) + + get profile_preview_card_path(user), as: :json + + expected_card_color = Color::CompareHex.new([user_colors(user)[:bg], user_colors(user)[:text]]).brightness(0.88) + expect(response.parsed_body["card_color"]).to eq(expected_card_color) + end + + it "does not return the email if the user has asked not to" do + user.setting.update_columns(display_email_on_profile: false) + + get profile_preview_card_path(user), as: :json + + preview_card = response.parsed_body + expect(preview_card.key?("email")).to be(false) + end + + it "returns the email if the user wants to" do + user.setting.update_columns(display_email_on_profile: true) + + get profile_preview_card_path(user), as: :json + + preview_card = response.parsed_body + expect(preview_card["email"]).to eq(user.email) + end + end + + context "when signed in" do + before { sign_in(user) } + + it "does not find an unknown user id" do + expect { get profile_preview_card_path(9999), as: :json }.to raise_error(ActiveRecord::RecordNotFound) + end + + it "is a successful response" do + get profile_preview_card_path(user), as: :json + + expect(response).to have_http_status(:ok) + end + + it "returns the data", :aggregate_failures do + get profile_preview_card_path(user), as: :json + + preview_card = response.parsed_body + expect(preview_card["summary"]).to eq(profile.summary) + expect(preview_card["employment_title"]).to eq(profile.employment_title) + expect(preview_card["employer_name"]).to eq(profile.employer_name) + expect(preview_card["employer_url"]).to eq(profile.employer_url) + expect(preview_card["location"]).to eq(profile.location) + expect(preview_card["education"]).to eq(profile.education) + expect(preview_card["created_at"]).to eq(profile.created_at.utc.iso8601) + end + + it "has the correct card color" do + user.setting.update(brand_color1: Faker::Color.hex_color) + + get profile_preview_card_path(user), as: :json + + expected_card_color = Color::CompareHex.new([user_colors(user)[:bg], user_colors(user)[:text]]).brightness(0.88) + expect(response.parsed_body["card_color"]).to eq(expected_card_color) + end + + it "does not return the email if the user has asked not to" do + user.setting.update_columns(display_email_on_profile: false) + + get profile_preview_card_path(user), as: :json + + preview_card = response.parsed_body + expect(preview_card.key?("email")).to be(false) + end + + it "returns the email if the user wants to" do + user.setting.update_columns(display_email_on_profile: true) + + get profile_preview_card_path(user), as: :json + + preview_card = response.parsed_body + expect(preview_card["email"]).to eq(user.email) + end + end + end +end