deepl_spec.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe TranslationService::DeepL do
  4. subject(:service) { described_class.new(plan, 'my-api-key') }
  5. let(:plan) { 'advanced' }
  6. before do
  7. %w(api-free.deepl.com api.deepl.com).each do |host|
  8. stub_request(:get, "https://#{host}/v2/languages?type=source").to_return(
  9. body: '[{"language":"EN","name":"English"},{"language":"UK","name":"Ukrainian"}]'
  10. )
  11. stub_request(:get, "https://#{host}/v2/languages?type=target").to_return(
  12. body: '[{"language":"EN-GB","name":"English (British)"},{"language":"ZH","name":"Chinese"}]'
  13. )
  14. end
  15. end
  16. describe '#translate' do
  17. it 'returns translation with specified source language' do
  18. stub_request(:post, 'https://api.deepl.com/v2/translate')
  19. .with(body: 'text=Hasta+la+vista&source_lang=ES&target_lang=en&tag_handling=html')
  20. .to_return(body: '{"translations":[{"detected_source_language":"ES","text":"See you soon"}]}')
  21. translations = service.translate(['Hasta la vista'], 'es', 'en')
  22. expect(translations.size).to eq 1
  23. translation = translations.first
  24. expect(translation.detected_source_language).to eq 'es'
  25. expect(translation.provider).to eq 'DeepL.com'
  26. expect(translation.text).to eq 'See you soon'
  27. end
  28. it 'returns translation with auto-detected source language' do
  29. stub_request(:post, 'https://api.deepl.com/v2/translate')
  30. .with(body: 'text=Guten+Tag&source_lang&target_lang=en&tag_handling=html')
  31. .to_return(body: '{"translations":[{"detected_source_language":"DE","text":"Good morning"}]}')
  32. translations = service.translate(['Guten Tag'], nil, 'en')
  33. expect(translations.size).to eq 1
  34. translation = translations.first
  35. expect(translation.detected_source_language).to eq 'de'
  36. expect(translation.provider).to eq 'DeepL.com'
  37. expect(translation.text).to eq 'Good morning'
  38. end
  39. it 'returns translation of multiple texts' do
  40. stub_request(:post, 'https://api.deepl.com/v2/translate')
  41. .with(body: 'text=Guten+Morgen&text=Gute+Nacht&source_lang=DE&target_lang=en&tag_handling=html')
  42. .to_return(body: '{"translations":[{"detected_source_language":"DE","text":"Good morning"},{"detected_source_language":"DE","text":"Good night"}]}')
  43. translations = service.translate(['Guten Morgen', 'Gute Nacht'], 'de', 'en')
  44. expect(translations.size).to eq 2
  45. expect(translations.first.text).to eq 'Good morning'
  46. expect(translations.last.text).to eq 'Good night'
  47. end
  48. end
  49. describe '#languages' do
  50. it 'returns source languages' do
  51. expect(service.languages.keys).to eq [nil, 'en', 'uk']
  52. end
  53. it 'returns target languages for each source language' do
  54. expect(service.languages['en']).to eq %w(pt en-GB zh)
  55. expect(service.languages['uk']).to eq %w(en pt en-GB zh)
  56. end
  57. it 'returns target languages for auto-detection' do
  58. expect(service.languages[nil]).to eq %w(en pt en-GB zh)
  59. end
  60. end
  61. describe 'the paid and free plan api hostnames' do
  62. before do
  63. service.languages
  64. end
  65. context 'without a plan set' do
  66. it 'uses paid plan base URL and sends an API key' do
  67. expect(a_request(:get, 'https://api.deepl.com/v2/languages?type=source').with(headers: { Authorization: 'DeepL-Auth-Key my-api-key' })).to have_been_made.once
  68. expect(a_request(:get, 'https://api.deepl.com/v2/languages?type=target').with(headers: { Authorization: 'DeepL-Auth-Key my-api-key' })).to have_been_made.once
  69. end
  70. end
  71. context 'with the free plan' do
  72. let(:plan) { 'free' }
  73. it 'uses free plan base URL and sends an API key' do
  74. expect(a_request(:get, 'https://api-free.deepl.com/v2/languages?type=source').with(headers: { Authorization: 'DeepL-Auth-Key my-api-key' })).to have_been_made.once
  75. expect(a_request(:get, 'https://api-free.deepl.com/v2/languages?type=target').with(headers: { Authorization: 'DeepL-Auth-Key my-api-key' })).to have_been_made.once
  76. end
  77. end
  78. end
  79. end