|
| 1 | +require "rails_helper" |
| 2 | +require "cgi" |
| 3 | + |
| 4 | +RSpec.describe "Read status imports", type: :request do |
| 5 | + def sign_in(email:, password: "secret") |
| 6 | + post session_path, params: { email: email, password: password } |
| 7 | + expect(response).to redirect_to(root_path) |
| 8 | + end |
| 9 | + |
| 10 | + def attach_verified_alias(user, email:, primary: true) |
| 11 | + al = create(:alias, user: user, email: email) |
| 12 | + if primary && user.person&.default_alias_id.nil? |
| 13 | + user.person.update!(default_alias_id: al.id) |
| 14 | + end |
| 15 | + Alias.by_email(email).update_all(verified_at: Time.current) |
| 16 | + al |
| 17 | + end |
| 18 | + |
| 19 | + def upload_csv(content) |
| 20 | + file = Tempfile.new(["read-status", ".csv"]) |
| 21 | + file.write(content) |
| 22 | + file.rewind |
| 23 | + Rack::Test::UploadedFile.new(file.path, "text/csv") |
| 24 | + ensure |
| 25 | + file.close |
| 26 | + end |
| 27 | + |
| 28 | + it "requires authentication" do |
| 29 | + get new_read_status_import_path |
| 30 | + expect(response).to redirect_to(new_session_path) |
| 31 | + end |
| 32 | + |
| 33 | + it "imports read status and notes with warnings and skips" do |
| 34 | + user = create(:user, password: "secret", password_confirmation: "secret") |
| 35 | + attach_verified_alias(user, email: "[email protected]") |
| 36 | + sign_in(email: "[email protected]") |
| 37 | + |
| 38 | + message1 = create(:message) |
| 39 | + message2 = create(:message) |
| 40 | + |
| 41 | + csv = <<~CSV |
| 42 | + message_id,notemode,note |
| 43 | + #{message1.message_id},message,Imported #tag |
| 44 | + #{message2.message_id},message, |
| 45 | + <[email protected]>,message,Missing message |
| 46 | + CSV |
| 47 | + |
| 48 | + post read_status_imports_path, params: { import_file: upload_csv(csv) } |
| 49 | + |
| 50 | + note = Note.active.find_by(author: user, message_id: message1.id) |
| 51 | + expect(note).to be_present |
| 52 | + expect(note.body).to start_with("!autoimport") |
| 53 | + expect(note.note_tags.pluck(:tag)).to include("tag") |
| 54 | + |
| 55 | + expect(Note.active.find_by(author: user, message_id: message2.id)).to be_nil |
| 56 | + |
| 57 | + expect(MessageReadRange.covering?(user: user, topic: message1.topic, message_id: message1.id)).to be(true) |
| 58 | + expect(MessageReadRange.covering?(user: user, topic: message2.topic, message_id: message2.id)).to be(true) |
| 59 | + expect(ThreadAwareness.covering?(user: user, topic: message1.topic, message_id: message1.id)).to be(true) |
| 60 | + expect(ThreadAwareness.covering?(user: user, topic: message2.topic, message_id: message2.id)).to be(true) |
| 61 | + |
| 62 | + expect(response.body).to include("New notes: 1") |
| 63 | + expect(response.body).to include("Replaced notes: 0") |
| 64 | + expect(response.body).to include("Message IDs marked as read: 2") |
| 65 | + expect(response.body).to include("Skipped message IDs: 1") |
| 66 | + escaped_message_id = CGI.escapeHTML(message2.message_id) |
| 67 | + expect(response.body).to include("Message #{escaped_message_id}: note text missing; skipped note import.") |
| 68 | + expect(response.body).to include(CGI.escapeHTML("<[email protected]>")) |
| 69 | + end |
| 70 | + |
| 71 | + it "replaces existing imported notes" do |
| 72 | + user = create(:user, password: "secret", password_confirmation: "secret") |
| 73 | + attach_verified_alias(user, email: "[email protected]") |
| 74 | + sign_in(email: "[email protected]") |
| 75 | + |
| 76 | + message = create(:message) |
| 77 | + Note.create!(topic: message.topic, message: message, author: user, body: "!autoimport old text") |
| 78 | + |
| 79 | + csv = <<~CSV |
| 80 | + #{message.message_id},message,New text |
| 81 | + CSV |
| 82 | + |
| 83 | + post read_status_imports_path, params: { import_file: upload_csv(csv) } |
| 84 | + |
| 85 | + note = Note.active.find_by(author: user, message_id: message.id) |
| 86 | + expect(note.body).to eq("!autoimport New text") |
| 87 | + expect(response.body).to include("New notes: 0") |
| 88 | + expect(response.body).to include("Replaced notes: 1") |
| 89 | + end |
| 90 | + |
| 91 | + it "imports topic-level notes for a message's topic" do |
| 92 | + user = create(:user, password: "secret", password_confirmation: "secret") |
| 93 | + attach_verified_alias(user, email: "[email protected]") |
| 94 | + sign_in(email: "[email protected]") |
| 95 | + |
| 96 | + message = create(:message) |
| 97 | + |
| 98 | + csv = <<~CSV |
| 99 | + #{message.message_id},topic,Thread note #tag |
| 100 | + CSV |
| 101 | + |
| 102 | + post read_status_imports_path, params: { import_file: upload_csv(csv) } |
| 103 | + |
| 104 | + note = Note.active.find_by(author: user, topic_id: message.topic_id, message_id: nil) |
| 105 | + expect(note).to be_present |
| 106 | + expect(note.body).to eq("!autoimport Thread note #tag") |
| 107 | + expect(note.note_tags.pluck(:tag)).to include("tag") |
| 108 | + end |
| 109 | +end |
0 commit comments