|
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +RSpec.describe "Notes", 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!(:note) { Note.create!(topic: topic, author: author, body: "Original note") } |
| 21 | + let!(:other_user) { create(:user, password: "secret", password_confirmation: "secret") } |
| 22 | + |
| 23 | + before do |
| 24 | + attach_verified_alias(other_user, email: "other@example.com") |
| 25 | + sign_in(email: "other@example.com") |
| 26 | + end |
| 27 | + |
| 28 | + describe "PATCH /notes/:id" do |
| 29 | + it "prevents non-authors from updating notes" do |
| 30 | + patch note_path(note), params: { note: { body: "Changed note" } } |
| 31 | + |
| 32 | + expect(response).to redirect_to(topic_path(note.topic)) |
| 33 | + expect(note.reload.body).to eq("Original note") |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + describe "DELETE /notes/:id" do |
| 38 | + it "prevents non-authors from deleting notes" do |
| 39 | + delete note_path(note) |
| 40 | + |
| 41 | + expect(response).to redirect_to(topic_path(note.topic)) |
| 42 | + expect(note.reload.deleted_at).to be_nil |
| 43 | + end |
| 44 | + end |
| 45 | +end |
0 commit comments