2019-07-14 22:22:46 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-03-22 16:32:18 -04:00
|
|
|
require 'csv'
|
2022-07-27 12:14:14 -04:00
|
|
|
|
2019-07-14 22:22:46 -04:00
|
|
|
RSpec.describe Admin::WatchedWordsController do
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2022-10-31 08:02:26 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
|
2019-07-14 22:22:46 -04:00
|
|
|
describe '#destroy' do
|
|
|
|
fab!(:watched_word) { Fabricate(:watched_word) }
|
|
|
|
|
2022-10-31 08:02:26 -04:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2019-07-14 22:22:46 -04:00
|
|
|
|
2022-10-31 08:02:26 -04:00
|
|
|
it "can't delete a watched word" do
|
|
|
|
delete "/admin/customize/watched_words/#{watched_word.id}.json"
|
2019-07-14 22:22:46 -04:00
|
|
|
|
2022-10-31 08:02:26 -04:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
2019-07-14 22:22:46 -04:00
|
|
|
end
|
|
|
|
|
2022-10-31 08:02:26 -04:00
|
|
|
context "when logged in as staff user" do
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right response when given an invalid id param' do
|
|
|
|
delete "/admin/customize/watched_words/9999.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
2019-07-14 22:22:46 -04:00
|
|
|
|
2022-10-31 08:02:26 -04:00
|
|
|
it "should be able to delete a 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
|
2019-07-14 22:22:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-02 04:06:03 -04:00
|
|
|
describe '#create' do
|
2022-10-31 08:02:26 -04:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2022-08-02 04:06:03 -04:00
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a word with default case sensitivity' do
|
|
|
|
post '/admin/customize/watched_words.json', params: {
|
|
|
|
action_key: 'flag',
|
|
|
|
word: 'Deals'
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(WatchedWord.take.word).to eq('Deals')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a word with the given case sensitivity' do
|
|
|
|
post '/admin/customize/watched_words.json', params: {
|
|
|
|
action_key: 'flag',
|
|
|
|
word: '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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-14 22:22:46 -04:00
|
|
|
describe '#upload' do
|
2022-10-31 08:02:26 -04:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when logged in as admin' do
|
2019-07-14 22:22:46 -04:00
|
|
|
before do
|
|
|
|
sign_in(admin)
|
2022-06-27 15:16:33 -04:00
|
|
|
Fabricate(:tag, name: 'tag1')
|
|
|
|
Fabricate(:tag, name: 'tag2')
|
|
|
|
Fabricate(:tag, name: 'tag3')
|
2019-07-14 22:22:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates the words from the file' do
|
2021-04-01 02:14:17 -04:00
|
|
|
post '/admin/customize/watched_words/upload.json', params: {
|
2019-07-14 22:22:46 -04:00
|
|
|
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]])
|
2021-06-30 01:52:46 -04:00
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:watched_word_create]).count).to eq(6)
|
2019-07-14 22:22:46 -04:00
|
|
|
end
|
2021-03-22 16:32:18 -04:00
|
|
|
|
|
|
|
it 'creates the words from the file' do
|
2021-04-01 02:14:17 -04:00
|
|
|
post '/admin/customize/watched_words/upload.json', params: {
|
2021-03-22 16:32:18 -04:00
|
|
|
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(
|
|
|
|
['hello', 'tag1,tag2'],
|
|
|
|
['world', 'tag2,tag3']
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(WatchedWord.pluck(:action).uniq).to eq([WatchedWord.actions[:tag]])
|
2021-06-30 01:52:46 -04:00
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:watched_word_create]).count).to eq(2)
|
2021-03-22 16:32:18 -04:00
|
|
|
end
|
2022-08-02 04:06:03 -04:00
|
|
|
|
|
|
|
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
|
2019-07-14 22:22:46 -04:00
|
|
|
end
|
|
|
|
end
|
2019-07-22 07:59:56 -04:00
|
|
|
|
|
|
|
describe '#download' do
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when not logged in as admin' do
|
2019-07-22 07:59:56 -04:00
|
|
|
it "doesn't allow performing #download" do
|
2021-04-01 02:14:17 -04:00
|
|
|
get "/admin/customize/watched_words/action/block/download"
|
2019-07-22 07:59:56 -04:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when logged in as admin' do
|
2019-07-22 07:59:56 -04:00
|
|
|
before do
|
|
|
|
sign_in(admin)
|
2022-06-27 15:16:33 -04:00
|
|
|
Fabricate(:tag, name: 'tag1')
|
|
|
|
Fabricate(:tag, name: 'tag2')
|
|
|
|
Fabricate(:tag, name: 'tag3')
|
2019-07-22 07:59:56 -04:00
|
|
|
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])
|
2021-03-22 16:32:18 -04:00
|
|
|
autotag_1 = Fabricate(:watched_word, action: WatchedWord.actions[:tag], replacement: "tag1,tag2")
|
|
|
|
autotag_2 = Fabricate(:watched_word, action: WatchedWord.actions[:tag], replacement: "tag3,tag2")
|
2019-07-22 07:59:56 -04:00
|
|
|
|
2021-04-01 02:14:17 -04:00
|
|
|
get "/admin/customize/watched_words/action/block/download"
|
2019-07-22 07:59:56 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
block_words = response.body.split("\n")
|
|
|
|
expect(block_words).to contain_exactly(block_word_1.word, block_word_2.word)
|
|
|
|
|
2021-04-01 02:14:17 -04:00
|
|
|
get "/admin/customize/watched_words/action/censor/download"
|
2019-07-22 07:59:56 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
censor_words = response.body.split("\n")
|
2021-03-22 16:32:18 -04:00
|
|
|
expect(censor_words).to contain_exactly(censor_word_1.word)
|
|
|
|
|
2021-04-01 02:14:17 -04:00
|
|
|
get "/admin/customize/watched_words/action/tag/download"
|
2021-03-22 16:32:18 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
tag_words = response.body.split("\n").map(&:parse_csv)
|
|
|
|
expect(tag_words).to contain_exactly(
|
|
|
|
[autotag_1.word, autotag_1.replacement],
|
|
|
|
[autotag_2.word, autotag_2.replacement]
|
|
|
|
)
|
2019-07-22 07:59:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe '#clear_all' do
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with non admins' do
|
2019-07-22 07:59:56 -04:00
|
|
|
it "doesn't allow them to perform #clear_all" do
|
|
|
|
word = Fabricate(:watched_word, action: WatchedWord.actions[:block])
|
2021-04-01 02:14:17 -04:00
|
|
|
delete "/admin/customize/watched_words/action/block"
|
2019-07-22 07:59:56 -04:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(WatchedWord.pluck(:word)).to include(word.word)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with admins' do
|
2019-07-22 07:59:56 -04:00
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows them to perform #clear_all" do
|
|
|
|
word = Fabricate(:watched_word, action: WatchedWord.actions[:block])
|
2021-04-01 02:14:17 -04:00
|
|
|
delete "/admin/customize/watched_words/action/block.json"
|
2019-07-22 07:59:56 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(WatchedWord.pluck(:word)).not_to include(word.word)
|
2021-06-30 01:52:46 -04:00
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:watched_word_destroy]).count).to eq(1)
|
2019-07-22 07:59:56 -04:00
|
|
|
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])
|
|
|
|
|
2021-04-01 02:14:17 -04:00
|
|
|
delete "/admin/customize/watched_words/action/flag.json"
|
2019-07-22 07:59:56 -04:00
|
|
|
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)
|
2021-06-30 01:52:46 -04:00
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:watched_word_destroy]).count).to eq(1)
|
2019-07-22 07:59:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-07-14 22:22:46 -04:00
|
|
|
end
|