|
73 | 73 |
|
74 | 74 | describe ".translate_supported?" do |
75 | 75 | let(:topic) { Fabricate(:topic, title: "This title is in english") } |
76 | | - it "should equate source language to target" do |
| 76 | + |
| 77 | + it "equates source language to target" do |
77 | 78 | source = "en" |
78 | 79 | target = "fr" |
79 | | - Excon.expects(:post).returns( |
80 | | - mock_response.new(200, %{ { "data": { "languages": [ { "language": "#{source}" }] } } }), |
| 80 | + stub_request(:post, DiscourseTranslator::Google::SUPPORT_URI).to_return( |
| 81 | + status: 200, |
| 82 | + body: %{ { "data": { "languages": [ { "language": "#{source}" }] } } }, |
81 | 83 | ) |
82 | 84 | expect(described_class.translate_supported?(source, target)).to be true |
83 | 85 | end |
84 | 86 |
|
85 | | - it "should pass through strings already in target language" do |
86 | | - lang = I18n.locale |
87 | | - topic.set_detected_locale(lang) |
88 | | - expect(described_class.translate(topic)).to eq(["en", "This title is in english"]) |
| 87 | + it "checks again without -* when the source language is not supported" do |
| 88 | + source = "en" |
| 89 | + target = "fr" |
| 90 | + stub_request(:post, DiscourseTranslator::Google::SUPPORT_URI).to_return( |
| 91 | + status: 200, |
| 92 | + body: %{ { "data": { "languages": [ { "language": "#{source}" }] } } }, |
| 93 | + ) |
| 94 | + |
| 95 | + expect(described_class.translate_supported?("en-GB", target)).to be true |
89 | 96 | end |
90 | 97 | end |
91 | 98 |
|
92 | | - describe ".translate" do |
| 99 | + describe ".translate!" do |
93 | 100 | let(:post) { Fabricate(:post) } |
94 | 101 |
|
95 | | - it "raise an error and warns admin on failure" do |
| 102 | + it "raises an error and warns admin on failure" do |
96 | 103 | described_class.expects(:access_token).returns(api_key) |
97 | 104 | described_class.expects(:detect).returns("__") |
98 | 105 |
|
99 | | - Excon.expects(:post).returns( |
100 | | - mock_response.new( |
101 | | - 400, |
102 | | - { |
103 | | - error: { |
104 | | - code: "400", |
105 | | - message: "API key not valid. Please pass a valid API key.", |
106 | | - }, |
107 | | - }.to_json, |
108 | | - ), |
| 106 | + stub_request(:post, DiscourseTranslator::Google::SUPPORT_URI).to_return( |
| 107 | + status: 400, |
| 108 | + body: { |
| 109 | + error: { |
| 110 | + code: "400", |
| 111 | + message: "API key not valid. Please pass a valid API key.", |
| 112 | + }, |
| 113 | + }.to_json, |
109 | 114 | ) |
110 | 115 |
|
111 | 116 | ProblemCheckTracker[:translator_error].no_problem! |
|
153 | 158 | post.set_detected_locale("de") |
154 | 159 | body = { |
155 | 160 | q: post.cooked.truncate(SiteSetting.max_characters_per_translation, omission: nil), |
156 | | - source: "de", |
157 | 161 | target: "en", |
158 | 162 | key: api_key, |
159 | 163 | } |
160 | 164 |
|
161 | 165 | translated_text = "hur dur hur dur" |
162 | | - Excon |
163 | | - .expects(:post) |
164 | | - .with( |
165 | | - DiscourseTranslator::Google::TRANSLATE_URI, |
166 | | - body: URI.encode_www_form(body), |
167 | | - headers: { |
168 | | - "Content-Type" => "application/x-www-form-urlencoded", |
169 | | - "Referer" => "http://test.localhost", |
170 | | - }, |
171 | | - ) |
172 | | - .returns( |
173 | | - mock_response.new( |
174 | | - 200, |
175 | | - %{ { "data": { "translations": [ { "translatedText": "#{translated_text}" } ] } } }, |
176 | | - ), |
177 | | - ) |
178 | | - .once |
179 | | - Excon.expects(:post).returns( |
180 | | - mock_response.new(200, %{ { "data": { "languages": [ { "language": "de" }] } } }), |
| 166 | + stub_request(:post, DiscourseTranslator::Google::SUPPORT_URI).to_return( |
| 167 | + status: 200, |
| 168 | + body: %{ { "data": { "languages": [ { "language": "de" }] } } }, |
| 169 | + ) |
| 170 | + stub_request(:post, DiscourseTranslator::Google::TRANSLATE_URI).with( |
| 171 | + body: URI.encode_www_form(body), |
| 172 | + headers: { |
| 173 | + "Content-Type" => "application/x-www-form-urlencoded", |
| 174 | + "Referer" => "http://test.localhost", |
| 175 | + }, |
| 176 | + ).to_return( |
| 177 | + status: 200, |
| 178 | + body: %{ { "data": { "translations": [ { "translatedText": "#{translated_text}" } ] } } }, |
181 | 179 | ) |
182 | 180 |
|
183 | | - expect(described_class.translate(post)).to eq(["de", translated_text]) |
| 181 | + expect(described_class.translate!(post)).to eq(translated_text) |
184 | 182 | end |
185 | 183 | end |
186 | 184 | end |
0 commit comments