diff --git a/spec/controllers/discouraged_articles_controller_spec.rb b/spec/controllers/discouraged_articles_controller_spec.rb new file mode 100644 index 0000000000..b73c0a2242 --- /dev/null +++ b/spec/controllers/discouraged_articles_controller_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe DiscouragedArticlesController, type: :controller do + describe 'GET #category_member?' do + it 'returns true when a category member exists' do + FactoryBot.create(:wikipedia_category_member, category_member: 'Example Article') + get :category_member?, params: { article_title: 'Example Article' } + expect(response).to have_http_status(:success) + expect(JSON.parse(response.body)['is_category_member']).to be_truthy + end + + it 'returns false when a category member does not exist' do + get :category_member?, params: { article_title: 'Nonexistent Article' } + expect(response).to have_http_status(:success) + expect(JSON.parse(response.body)['is_category_member']).to be_falsey + end + end +end diff --git a/spec/factories/wikipedia_category_members.rb b/spec/factories/wikipedia_category_members.rb index 4c76f9d268..6b9e5b5e10 100644 --- a/spec/factories/wikipedia_category_members.rb +++ b/spec/factories/wikipedia_category_members.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + FactoryBot.define do factory :wikipedia_category_member do - category_members { "MyText" } + category_member { 'Example Category Member' } end end diff --git a/spec/models/wikipedia_category_member_spec.rb b/spec/models/wikipedia_category_member_spec.rb index ed30875283..2ad3d31c45 100644 --- a/spec/models/wikipedia_category_member_spec.rb +++ b/spec/models/wikipedia_category_member_spec.rb @@ -1,5 +1,35 @@ +# frozen_string_literal: true + require 'rails_helper' RSpec.describe WikipediaCategoryMember, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + describe '#fetch_category_members' do + it 'fetches and saves category members' do + response = instance_double('response', status: 200, + data: { 'categorymembers' => [{ 'title' => 'Member 1' }, { 'title' => 'Member 2' }] }) + + wiki_api = instance_double('wiki_api', query: response) + allow(WikiApi).to receive(:new).and_return(wiki_api) + + described_class.create(category_member: 'Member 2') + described_class.create(category_member: 'Member 3') + + subject.fetch_category_members + + expect(described_class.pluck(:category_member)).to contain_exactly('Member 1', 'Member 2') + + expect(described_class.find_by(category_member: 'Member 3')).to be_nil + end + + it 'logs a warning on API failure' do + response = instance_double('response', status: 500) + + wiki_api = instance_double('wiki_api', query: response) + allow(WikiApi).to receive(:new).and_return(wiki_api) + + expect(Rails.logger).to receive(:warn).with('Failed to fetch categorymembers data') + + subject.fetch_category_members + end + end end diff --git a/spec/workers/update_wikipedia_category_worker_spec.rb b/spec/workers/update_wikipedia_category_worker_spec.rb new file mode 100644 index 0000000000..3f42c277c0 --- /dev/null +++ b/spec/workers/update_wikipedia_category_worker_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'sidekiq/testing' + +Sidekiq::Testing.fake! + +RSpec.describe UpdateWikipediaCategoryWorker, type: :worker do + it 'fetches and saves Wikipedia category members' do + allow_any_instance_of(WikipediaCategoryMember).to receive(:fetch_category_members) + + expect do + described_class.perform_async + end.to change(described_class.jobs, :size).by(1) + + described_class.drain + end +end