Skip to content

A platform for community discussion. Free, open, simple.

15.8 KB
Raw Blame
# frozen_string_literal: true

require "csv"

RSpec.describe Admin::WatchedWordsController do
  fab!(:admin)
  fab!(:user)

  describe "#index" do
    context "when logged in as non-staff user" do
      before { sign_in(user) }

      it "does not return watched words" do
        get "/admin/customize/watched_words.json"

        expect(response.status).to eq(404)
      end
    end

    context "when logged in as a staff user" do
      fab!(:word1) { Fabricate(:watched_word, action: WatchedWord.actions[:block]) }
      fab!(:word2) { Fabricate(:watched_word, action: WatchedWord.actions[:block]) }
      fab!(:word3) { Fabricate(:watched_word, action: WatchedWord.actions[:censor]) }
      fab!(:word4) { Fabricate(:watched_word, action: WatchedWord.actions[:censor]) }

      before { sign_in(admin) }

      it "returns all watched words" do
        get "/admin/customize/watched_words.json"

        expect(response.status).to eq(200)

        watched_words = response.parsed_body

        expect(watched_words["actions"]).to match_array(WatchedWord.actions.keys.map(&:to_s))
        expect(watched_words["words"].length).to eq(4)
        expect(watched_words["words"]).to include(
          hash_including(
            "id" => word1.id,
            "word" => word1.word,
            "regexp" => WordWatcher.word_to_regexp(word1.word),
            "case_sensitive" => false,
            "action" => "block",
          ),
          hash_including(
            "id" => word4.id,
            "word" => word4.word,
            "regexp" => WordWatcher.word_to_regexp(word4.word),
            "case_sensitive" => false,
            "action" => "censor",
          ),
        )
        expect(watched_words["compiled_regular_expressions"]["block"].first).to eq(
          WordWatcher.serialized_regexps_for_action(:block).first.deep_stringify_keys,
        )
      end
    end
  end

  describe "#destroy" do
    fab!(:watched_word)

    context "when logged in as a non-staff user" do
      before { sign_in(user) }

      it "can't delete a watched word" do
        delete "/admin/customize/watched_words/#{watched_word.id}.json"

        expect(response.status).to eq(404)
      end
    end

    context "when logged in as staff user" do
      before { sign_in(admin) }

      it "rejects an invalid watched word ID" do
        delete "/admin/customize/watched_words/9999.json"

        expect(response.status).to eq(400)
      end

      it "deletes the watched word" do
        delete "/admin/customize/watched_words/#{watched_word.id}.json"

        expect(response.status).to eq(200)
        expect(WatchedWord.find_by(id: watched_word.id)).to eq(nil)
        expect(UserHistory.where(action: UserHistory.actions[:watched_word_destroy]).count).to eq(1)
      end

      it "deletes the watched word group after its last word is removed" do
        watched_word_group = Fabricate(:watched_word_group)
        watched_word = watched_word_group.watched_words.first

        delete "/admin/customize/watched_words/#{watched_word.id}.json"

        expect(response.status).to eq(200)
        expect(WatchedWordGroup.exists?(id: watched_word_group.id)).to be_falsey
        expect(WatchedWord.exists?(id: watched_word.id)).to be_falsey
        expect(
          UserHistory.where(action: UserHistory.actions[:delete_watched_word_group]).count,
        ).to eq(1)
      end
    end
  end

  describe "#create" do
    context "when logged in as a non-staff user" do
      before { sign_in(user) }

      it "can't create a watched word" do
        post "/admin/customize/watched_words.json", params: { action_key: "flag", word: "Fr33" }

        expect(response.status).to eq(404)
      end
    end

    context "when logged in as a staff user" do
      before { sign_in(admin) }

      it "creates a word with default case sensitivity" do
        expect {
          post "/admin/customize/watched_words.json",
               params: {
                 action_key: "flag",
                 words: %w[Deals Offer],
               }
        }.to change { WatchedWord.count }.by(2)

        expect(response.status).to eq(200)
        expect(WatchedWord.take.word).to eq("Deals")
        expect(WatchedWord.last.word).to eq("Offer")
        expect(
          UserHistory.where(action: UserHistory.actions[:create_watched_word_group]).count,
        ).to eq(1)
      end

      it "creates a word with the given case sensitivity" do
        post "/admin/customize/watched_words.json",
             params: {
               action_key: "flag",
               words: ["PNG"],
               case_sensitive: true,
             }

        expect(response.status).to eq(200)
        expect(WatchedWord.take.case_sensitive?).to eq(true)
        expect(WatchedWord.take.word).to eq("PNG")
      end

      it "creates a tag watched word with existing tags by id" do
        tag = Fabricate(:tag, name: "greeting")

        post "/admin/customize/watched_words.json",
             params: {
               action_key: "tag",
               words: ["hello"],
               replacement_tags: [{ id: tag.id, name: tag.name }],
             }

        expect(response.status).to eq(200)
        expect(WatchedWord.last.replacement).to eq("greeting")
      end

      it "creates a tag watched word and creates new tags" do
        post "/admin/customize/watched_words.json",
             params: {
               action_key: "tag",
               words: ["hello"],
               replacement_tags: [{ name: "brandnewtag" }],
             }

        expect(response.status).to eq(200)
        expect(Tag.find_by(name: "brandnewtag")).to be_present
        expect(WatchedWord.last.replacement).to eq("brandnewtag")
      end

      it "creates a tag watched word with a mix of existing and new tags" do
        tag = Fabricate(:tag, name: "existing")

        post "/admin/customize/watched_words.json",
             params: {
               action_key: "tag",
               words: ["hello"],
               replacement_tags: [{ id: tag.id, name: tag.name }, { name: "newone" }],
             }

        expect(response.status).to eq(200)
        expect(Tag.find_by(name: "newone")).to be_present
        expect(WatchedWord.last.replacement).to eq("existing,newone")
      end

      it "creates a tag watched word via the legacy replacement param" do
        Fabricate(:tag, name: "greeting")

        post "/admin/customize/watched_words.json",
             params: {
               action_key: "tag",
               words: ["hello"],
               replacement: "greeting",
             }

        expect(response.status).to eq(200)
        expect(WatchedWord.last.replacement).to eq("greeting")
      end
    end
  end

  describe "#upload" do
    context "when logged in as a non-staff user" do
      before { sign_in(user) }

      it "can't create watched words via file upload" do
        post "/admin/customize/watched_words/upload.json",
             params: {
               action_key: "flag",
               file: Rack::Test::UploadedFile.new(file_from_fixtures("words.csv", "csv")),
             }

        expect(response.status).to eq(404)
      end
    end

    context "when logged in as admin" do
      before do
        sign_in(admin)
        Fabricate(:tag, name: "tag1")
        Fabricate(:tag, name: "tag2")
        Fabricate(:tag, name: "tag3")
      end

      it "imports flagged words from the CSV file" do
        post "/admin/customize/watched_words/upload.json",
             params: {
               action_key: "flag",
               file: Rack::Test::UploadedFile.new(file_from_fixtures("words.csv", "csv")),
             }

        expect(response.status).to eq(200)
        expect(WatchedWord.count).to eq(6)

        expect(WatchedWord.pluck(:word)).to contain_exactly(
          "thread",
          "线",
          "धागा",
          "실",
          "tråd",
          "нить",
        )

        expect(WatchedWord.pluck(:action).uniq).to eq([WatchedWord.actions[:flag]])
        expect(UserHistory.where(action: UserHistory.actions[:watched_word_create]).count).to eq(6)
      end

      it "imports tagged words and replacements from the CSV file" do
        post "/admin/customize/watched_words/upload.json",
             params: {
               action_key: "tag",
               file: Rack::Test::UploadedFile.new(file_from_fixtures("words_tag.csv", "csv")),
             }

        expect(response.status).to eq(200)
        expect(WatchedWord.count).to eq(2)

        expect(WatchedWord.pluck(:word, :replacement)).to contain_exactly(
          %w[hello tag1,tag2],
          %w[world tag2,tag3],
        )

        expect(WatchedWord.pluck(:action).uniq).to eq([WatchedWord.actions[:tag]])
        expect(UserHistory.where(action: UserHistory.actions[:watched_word_create]).count).to eq(2)
      end

      it "creates case-sensitive words from the file" do
        post "/admin/customize/watched_words/upload.json",
             params: {
               action_key: "flag",
               file:
                 Rack::Test::UploadedFile.new(
                   file_from_fixtures("words_case_sensitive.csv", "csv"),
                 ),
             }

        expect(response.status).to eq(200)
        expect(WatchedWord.pluck(:word, :case_sensitive)).to contain_exactly(
          ["hello", true],
          ["UN", true],
          ["world", false],
          ["test", false],
        )
      end

      it "reads file content before deferring work so tempfile cleanup does not cause errors" do
        upload_tempfile = nil

        allow(ActionDispatch::Http::UploadedFile).to receive(
          :new,
        ).and_wrap_original do |original, hash|
          upload_tempfile ||= hash[:tempfile]
          original.call(hash)
        end

        allow(Scheduler::Defer).to receive(
          :later,
        ).and_wrap_original do |original, *args, **kwargs, &block|
          # Simulate Rack::TempfileReaper closing the multipart tempfile after the request completes,
          # before the deferred block runs. With the fix, File.read has already happened so the
          # block does not need the tempfile anymore.
          upload_tempfile&.close! if args.first == "Upload watched words"
          block.call
        end

        post "/admin/customize/watched_words/upload.json",
             params: {
               action_key: "flag",
               file: Rack::Test::UploadedFile.new(file_from_fixtures("words.csv", "csv")),
             }

        expect(upload_tempfile).to be_a(Tempfile)
        expect(response.status).to eq(200)
        expect(WatchedWord.count).to eq(6)
      end

      it "rejects a CSV with more entries than the maximum number of words" do
        stub_const(WatchedWord, "MAX_WORDS_PER_ACTION", 2) do
          post "/admin/customize/watched_words/upload.json",
               params: {
                 action_key: "flag",
                 file:
                   Rack::Test::UploadedFile.new(file_from_contents("w1\nw2\nw3\n", "words.csv")),
               }
        end

        expect(response.status).to eq(422)
        expect(response.parsed_body["errors"]).to eq(
          [I18n.t("watched_words.upload_too_many_csv_entries", count: 2)],
        )
        expect(WatchedWord.count).to eq(0)
      end

      it "preserves non-ASCII words in a UTF-8 file without a BOM" do
        content = (1..30).map { |i| "badword#{i}" }.join("\n") + "\nCafé"

        post "/admin/customize/watched_words/upload.json",
             params: {
               action_key: "flag",
               file: Rack::Test::UploadedFile.new(file_from_contents(content, "words.csv")),
             }

        expect(response.status).to eq(200)
        expect(WatchedWord.pluck(:word)).to include("Café")
      end
    end
  end

  describe "#download" do
    context "when not logged in as admin" do
      it "doesn't allow performing #download" do
        get "/admin/customize/watched_words/action/block/download"
        expect(response.status).to eq(404)
      end
    end

    context "when logged in as admin" do
      before do
        sign_in(admin)
        Fabricate(:tag, name: "tag1")
        Fabricate(:tag, name: "tag2")
        Fabricate(:tag, name: "tag3")
      end

      it "words of different actions are downloaded separately" do
        block_word_1 = Fabricate(:watched_word, action: WatchedWord.actions[:block])
        block_word_2 = Fabricate(:watched_word, action: WatchedWord.actions[:block])
        censor_word_1 = Fabricate(:watched_word, action: WatchedWord.actions[:censor])
        autotag_1 =
          Fabricate(:watched_word, action: WatchedWord.actions[:tag], replacement: "tag1,tag2")
        autotag_2 =
          Fabricate(:watched_word, action: WatchedWord.actions[:tag], replacement: "tag3,tag2")

        get "/admin/customize/watched_words/action/block/download"
        expect(response.status).to eq(200)
        expect(response.body).to start_with(Encodings::BOM)
        block_words = response.body.delete_prefix(Encodings::BOM).split("\n").map(&:parse_csv)
        expect(block_words).to contain_exactly(
          [block_word_1.word, nil, "false"],
          [block_word_2.word, nil, "false"],
        )

        get "/admin/customize/watched_words/action/censor/download"
        expect(response.status).to eq(200)
        censor_words = response.body.delete_prefix(Encodings::BOM).split("\n").map(&:parse_csv)
        expect(censor_words).to contain_exactly([censor_word_1.word, nil, "false"])

        get "/admin/customize/watched_words/action/tag/download"
        expect(response.status).to eq(200)
        tag_words = response.body.delete_prefix(Encodings::BOM).split("\n").map(&:parse_csv)
        expect(tag_words).to contain_exactly(
          [autotag_1.word, autotag_1.replacement, "false"],
          [autotag_2.word, autotag_2.replacement, "false"],
        )
      end
    end
  end

  describe "#download then #upload" do
    before { sign_in(admin) }

    it "round-trips delimiters and case sensitivity" do
      Fabricate(
        :watched_word,
        action: WatchedWord.actions[:block],
        word: 'MiXeD,"word"',
        case_sensitive: true,
      )

      get "/admin/customize/watched_words/action/block/download"
      expect(response.status).to eq(200)
      exported = response.body

      WatchedWord.delete_all

      post "/admin/customize/watched_words/upload.json",
           params: {
             action_key: "block",
             file: Rack::Test::UploadedFile.new(file_from_contents(exported, "words.csv")),
           }

      expect(response.status).to eq(200)
      expect(WatchedWord.pluck(:word, :case_sensitive)).to eq([['MiXeD,"word"', true]])
    end
  end

  describe "#clear_all" do
    context "with non admins" do
      it "doesn't allow them to perform #clear_all" do
        word = Fabricate(:watched_word, action: WatchedWord.actions[:block])
        delete "/admin/customize/watched_words/action/block"
        expect(response.status).to eq(404)
        expect(WatchedWord.pluck(:word)).to include(word.word)
      end
    end

    context "with admins" do
      before { sign_in(admin) }

      it "allows them to perform #clear_all" do
        word = Fabricate(:watched_word, action: WatchedWord.actions[:block])
        delete "/admin/customize/watched_words/action/block.json"
        expect(response.status).to eq(200)
        expect(WatchedWord.pluck(:word)).not_to include(word.word)
        expect(UserHistory.where(action: UserHistory.actions[:watched_word_destroy]).count).to eq(1)
      end

      it "doesn't delete words of multiple actions in one call" do
        block_word = Fabricate(:watched_word, action: WatchedWord.actions[:block])
        flag_word = Fabricate(:watched_word, action: WatchedWord.actions[:flag])

        delete "/admin/customize/watched_words/action/flag.json"
        expect(response.status).to eq(200)
        all_words = WatchedWord.pluck(:word)
        expect(all_words).to include(block_word.word)
        expect(all_words).not_to include(flag_word.word)
        expect(UserHistory.where(action: UserHistory.actions[:watched_word_destroy]).count).to eq(1)
      end
    end
  end
end