DEV: Minor tweaks to Admin::WatchedWordsController.

This commit is contained in:
Guo Xiang Tan 2019-07-15 10:22:46 +08:00
parent ce8e099639
commit a4234e9be0
3 changed files with 55 additions and 23 deletions

View File

@ -16,8 +16,9 @@ class Admin::WatchedWordsController < Admin::AdminController
end end
def destroy def destroy
watched_word = WatchedWord.find(params[:id]) watched_word = WatchedWord.find_by(id: params[:id])
watched_word.destroy raise Discourse::InvalidParameters.new(:id) unless watched_word
watched_word.destroy!
render json: success_json render json: success_json
end end

View File

@ -181,25 +181,4 @@ describe WatchedWord do
}.to_not change { PostAction.count } }.to_not change { PostAction.count }
end end
end end
describe 'upload' do
context 'logged in as admin' do
before do
sign_in(admin)
end
it 'creates the words from the file' do
post '/admin/logs/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]])
end
end
end
end end

View File

@ -0,0 +1,52 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Admin::WatchedWordsController do
fab!(:admin) { Fabricate(:admin) }
describe '#destroy' do
fab!(:watched_word) { Fabricate(:watched_word) }
before do
sign_in(admin)
end
it 'should return the right response when given an invalid id param' do
delete '/admin/logs/watched_words/9999.json'
expect(response.status).to eq(400)
end
it 'should be able to delete a watched word' do
delete "/admin/logs/watched_words/#{watched_word.id}.json"
expect(response.status).to eq(200)
expect(WatchedWord.find_by(id: watched_word.id)).to eq(nil)
end
end
describe '#upload' do
context 'logged in as admin' do
before do
sign_in(admin)
end
it 'creates the words from the file' do
post '/admin/logs/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]])
end
end
end
end