|
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +RSpec.describe "NoteMentions", type: :request do |
| 4 | + def sign_in(email:, password: "secret") |
| 5 | + post session_path, params: { email: email, password: password } |
| 6 | + expect(response).to redirect_to(root_path) |
| 7 | + end |
| 8 | + |
| 9 | + def attach_verified_alias(user, email:, primary: true) |
| 10 | + al = create(:alias, user: user, email: email) |
| 11 | + if primary && user.person&.default_alias_id.nil? |
| 12 | + user.person.update!(default_alias_id: al.id) |
| 13 | + end |
| 14 | + Alias.by_email(email).update_all(verified_at: Time.current) |
| 15 | + al |
| 16 | + end |
| 17 | + |
| 18 | + let!(:topic) { create(:topic) } |
| 19 | + let!(:author) { create(:user, password: "secret", password_confirmation: "secret") } |
| 20 | + let!(:mentioned_user) { create(:user, password: "secret", password_confirmation: "secret") } |
| 21 | + let!(:other_user) { create(:user, password: "secret", password_confirmation: "secret") } |
| 22 | + let!(:team) { create(:team) } |
| 23 | + let!(:team_admin) { create(:user, password: "secret", password_confirmation: "secret") } |
| 24 | + let!(:team_member) { create(:user, password: "secret", password_confirmation: "secret") } |
| 25 | + |
| 26 | + let!(:note) { Note.create!(topic: topic, author: author, body: "A note") } |
| 27 | + let!(:user_mention) { NoteMention.create!(note: note, mentionable: mentioned_user) } |
| 28 | + let!(:team_mention) { NoteMention.create!(note: note, mentionable: team) } |
| 29 | + |
| 30 | + before do |
| 31 | + create(:team_member, team: team, user: team_admin, role: "admin") |
| 32 | + create(:team_member, team: team, user: team_member, role: "member") |
| 33 | + end |
| 34 | + |
| 35 | + describe "DELETE /note_mentions/:id" do |
| 36 | + context "when user removes their own mention" do |
| 37 | + before do |
| 38 | + attach_verified_alias(mentioned_user, email: "mentioned@example.com") |
| 39 | + sign_in(email: "mentioned@example.com") |
| 40 | + end |
| 41 | + |
| 42 | + it "allows the mentioned user to remove their own mention" do |
| 43 | + expect { |
| 44 | + delete note_mention_path(user_mention) |
| 45 | + }.to change(NoteMention, :count).by(-1) |
| 46 | + |
| 47 | + expect(response).to redirect_to(topic_path(topic)) |
| 48 | + expect(flash[:notice]).to eq("Mention removed") |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + context "when user tries to remove another user's mention" do |
| 53 | + before do |
| 54 | + attach_verified_alias(other_user, email: "other@example.com") |
| 55 | + sign_in(email: "other@example.com") |
| 56 | + end |
| 57 | + |
| 58 | + it "prevents removing another user's mention" do |
| 59 | + expect { |
| 60 | + delete note_mention_path(user_mention) |
| 61 | + }.not_to change(NoteMention, :count) |
| 62 | + |
| 63 | + expect(response).to redirect_to(topic_path(topic)) |
| 64 | + expect(flash[:alert]).to eq("You can only remove your own mentions") |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + context "when team admin removes team mention" do |
| 69 | + before do |
| 70 | + attach_verified_alias(team_admin, email: "teamadmin@example.com") |
| 71 | + sign_in(email: "teamadmin@example.com") |
| 72 | + end |
| 73 | + |
| 74 | + it "allows team admin to remove team mention" do |
| 75 | + expect { |
| 76 | + delete note_mention_path(team_mention) |
| 77 | + }.to change(NoteMention, :count).by(-1) |
| 78 | + |
| 79 | + expect(response).to redirect_to(topic_path(topic)) |
| 80 | + expect(flash[:notice]).to eq("Mention removed") |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + context "when team member (non-admin) tries to remove team mention" do |
| 85 | + before do |
| 86 | + attach_verified_alias(team_member, email: "teammember@example.com") |
| 87 | + sign_in(email: "teammember@example.com") |
| 88 | + end |
| 89 | + |
| 90 | + it "prevents non-admin team members from removing team mention" do |
| 91 | + expect { |
| 92 | + delete note_mention_path(team_mention) |
| 93 | + }.not_to change(NoteMention, :count) |
| 94 | + |
| 95 | + expect(response).to redirect_to(topic_path(topic)) |
| 96 | + expect(flash[:alert]).to eq("Only team admins can remove team mentions") |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + context "when non-team member tries to remove team mention" do |
| 101 | + before do |
| 102 | + attach_verified_alias(other_user, email: "other@example.com") |
| 103 | + sign_in(email: "other@example.com") |
| 104 | + end |
| 105 | + |
| 106 | + it "prevents non-members from removing team mention" do |
| 107 | + expect { |
| 108 | + delete note_mention_path(team_mention) |
| 109 | + }.not_to change(NoteMention, :count) |
| 110 | + |
| 111 | + expect(response).to redirect_to(topic_path(topic)) |
| 112 | + expect(flash[:alert]).to eq("Only team admins can remove team mentions") |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + context "when not signed in" do |
| 117 | + it "requires authentication" do |
| 118 | + delete note_mention_path(user_mention) |
| 119 | + |
| 120 | + expect(response).to redirect_to(new_session_path) |
| 121 | + end |
| 122 | + end |
| 123 | + end |
| 124 | +end |
0 commit comments