diff --git a/app/models/tweet.rb b/app/models/tweet.rb index f5724e431..17c41dfa2 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -48,6 +48,8 @@ class Tweet < ApplicationRecord retrieve_and_save_tweet(status_id) rescue TwitterClient::Errors::NotFound => e raise e, I18n.t("models.tweet.tweet_not_found") + rescue TwitterClient::Errors::BadRequest => e + raise e, I18n.t("models.tweet.authentication_error") end def retrieve_and_save_tweet(status_id) diff --git a/app/services/twitter_client/errors.rb b/app/services/twitter_client/errors.rb index a81081ad3..333b51881 100644 --- a/app/services/twitter_client/errors.rb +++ b/app/services/twitter_client/errors.rb @@ -11,5 +11,8 @@ module TwitterClient class NotFound < ClientError end + + class BadRequest < ClientError + end end end diff --git a/config/locales/models/en.yml b/config/locales/models/en.yml index 47e01aec1..24c04184b 100644 --- a/config/locales/models/en.yml +++ b/config/locales/models/en.yml @@ -27,9 +27,9 @@ en: broadcast: single_active: You can only have one active announcement broadcast comment: - deleted: "[deleted]" - has_been_deleted: "%{type} has been deleted." - hidden: "[hidden by post author]" + deleted: '[deleted]' + has_been_deleted: '%{type} has been deleted.' + hidden: '[hidden by post author]' is_not_valid: is not valid. locked: the discussion is locked on this Post type: @@ -41,7 +41,7 @@ en: one: You cannot mention more than %{count} users in a comment! other: You cannot mention more than %{count} users in a comment! feedback_message: - reported: "(you) previously reported this URL." + reported: '(you) previously reported this URL.' github_issue: issue_comment_not_found: Issue comment %{issue_id} not found issue_not_found: Issue %{issue_id} not found @@ -58,7 +58,7 @@ en: is_not_valid: is not valid. organization: integer_only: Integer only. No sign allowed. - reserved_word: "%s is a reserved word. Contact site admins for help registering your organization." + reserved_word: '%s is a reserved word. Contact site admins for help registering your organization.' page: body_must_exist: must exist if body_html or body_json doesn't exist. podcast: @@ -110,11 +110,12 @@ en: unique: can't be an already adjusted tag tweet: tweet_not_found: Tweet not found + authentication_error: Authentication error; please contact your Forem admin about possible missing Twitter keys user: could_not_send: confirmation could not be sent. %{e_message} password_not_matched: doesn't match password confirmation has_been_banished: has been banished. - is_taken: "%{username} is taken." + is_taken: '%{username} is taken.' user_could_not_be_saved: User could not be saved. %{e_message} username_is_reserved: username is reserved user_block: @@ -125,7 +126,7 @@ en: not_enabled: User subscriptions are not enabled for the source. users: deleted_user: - name: "[Deleted User]" + name: '[Deleted User]' setting: invalid_hex: is not a valid hex color invalid_rss: is not a valid RSS/Atom feed diff --git a/config/locales/models/fr.yml b/config/locales/models/fr.yml index b058d8344..504b40ccc 100644 --- a/config/locales/models/fr.yml +++ b/config/locales/models/fr.yml @@ -27,9 +27,9 @@ fr: broadcast: single_active: You can only have one active announcement broadcast comment: - deleted: "[deleted]" - has_been_deleted: "%{type} has been deleted." - hidden: "[hidden by post author]" + deleted: '[deleted]' + has_been_deleted: '%{type} has been deleted.' + hidden: '[hidden by post author]' is_not_valid: is not valid. locked: the discussion is locked on this Post type: @@ -41,7 +41,7 @@ fr: one: You cannot mention more than %{count} users in a comment! other: You cannot mention more than %{count} users in a comment! feedback_message: - reported: "(you) previously reported this URL." + reported: '(you) previously reported this URL.' github_issue: issue_comment_not_found: Issue comment %{issue_id} not found issue_not_found: Issue %{issue_id} not found @@ -58,7 +58,7 @@ fr: is_not_valid: is not valid. organization: integer_only: Integer only. No sign allowed. - reserved_word: "%s is a reserved word. Contact site admins for help registering your organization." + reserved_word: '%s is a reserved word. Contact site admins for help registering your organization.' page: body_must_exist: must exist if body_html or body_json doesn't exist. podcast: @@ -110,11 +110,12 @@ fr: unique: can't be an already adjusted tag tweet: tweet_not_found: Tweet not found + authentication_error: Authentication error; please contact your Forem admin about possible missing Twitter keys user: could_not_send: confirmation could not be sent. %{e_message} password_not_matched: doesn't match password confirmation has_been_banished: has been banished. - is_taken: "%{username} is taken." + is_taken: '%{username} is taken.' user_could_not_be_saved: User could not be saved. %{e_message} username_is_reserved: username is reserved user_block: @@ -125,7 +126,7 @@ fr: not_enabled: User subscriptions are not enabled for the source. users: deleted_user: - name: "[Deleted User]" + name: '[Deleted User]' setting: invalid_hex: is not a valid hex color invalid_rss: is not a valid RSS/Atom feed diff --git a/spec/liquid_tags/unified_embed/registry_spec.rb b/spec/liquid_tags/unified_embed/registry_spec.rb index 7d23adc15..50e090a39 100644 --- a/spec/liquid_tags/unified_embed/registry_spec.rb +++ b/spec/liquid_tags/unified_embed/registry_spec.rb @@ -316,7 +316,7 @@ RSpec.describe UnifiedEmbed::Registry do end end - it "returns WikipediaTag for a twitter timeline url" do + it "returns WikipediaTag for a wikipedia url" do expect(described_class.find_liquid_tag_for(link: "https://en.wikipedia.org/wiki/Steve_Jobs")) .to eq(WikipediaTag) end diff --git a/spec/models/tweet_spec.rb b/spec/models/tweet_spec.rb index b1cb53587..924029534 100644 --- a/spec/models/tweet_spec.rb +++ b/spec/models/tweet_spec.rb @@ -88,6 +88,17 @@ RSpec.describe Tweet, type: :model, vcr: true do tweet = described_class.find_or_fetch(tweet_id) expect(tweet.user_id).to eq(user.id) end + + it "raises an error when Twitter key or secret are missing" do + allow(TwitterClient::Client) + .to receive(:status) + .and_raise(TwitterClient::Errors::BadRequest, "Bad Authentication data.") + + expect do + described_class.find_or_fetch(tweet_id) + end.to raise_error(TwitterClient::Errors::BadRequest, + "Authentication error; please contact your Forem admin about possible missing Twitter keys") + end end context "when retrieving non existent tweet", vcr: { cassette_name: "twitter_client_status_not_found_extended" } do