gmail_spec.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. require 'rails_helper'
  2. RSpec.describe 'Gmail XOAUTH2' do # rubocop:disable RSpec/DescribeClass
  3. let(:channel) do
  4. create(:channel,
  5. area: 'Google::Account',
  6. options: {
  7. 'inbound' => {
  8. 'adapter' => 'imap',
  9. 'options' => {
  10. 'auth_type' => 'XOAUTH2',
  11. 'host' => 'imap.gmail.com',
  12. 'ssl' => true,
  13. 'user' => ENV['GMAIL_USER'],
  14. 'folder' => '',
  15. 'keep_on_server' => false,
  16. }
  17. },
  18. 'outbound' => {
  19. 'adapter' => 'smtp',
  20. 'options' => {
  21. 'host' => 'smtp.gmail.com',
  22. 'domain' => 'gmail.com',
  23. 'port' => 465,
  24. 'ssl' => true,
  25. 'user' => ENV['GMAIL_USER'],
  26. 'authentication' => 'xoauth2',
  27. }
  28. },
  29. 'auth' => {
  30. 'type' => 'XOAUTH2',
  31. 'provider' => 'google',
  32. 'access_token' => 'xxx',
  33. 'expires_in' => 3599,
  34. 'refresh_token' => ENV['GMAIL_REFRESH_TOKEN'],
  35. 'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://mail.google.com/ openid',
  36. 'token_type' => 'Bearer',
  37. 'id_token' => 'xxx',
  38. 'created_at' => 30.days.ago,
  39. 'client_id' => ENV['GMAIL_CLIENT_ID'],
  40. 'client_secret' => ENV['GMAIL_CLIENT_SECRET'],
  41. }
  42. })
  43. end
  44. before do
  45. required_envs = %w[GMAIL_REFRESH_TOKEN GMAIL_CLIENT_ID GMAIL_CLIENT_SECRET GMAIL_USER]
  46. required_envs.each do |key|
  47. skip("NOTICE: Missing environment variable #{key} for test! (Please fill up: #{required_envs.join(' && ')})") if ENV[key].blank?
  48. end
  49. end
  50. context 'inbound' do
  51. it 'succeeds' do
  52. result = EmailHelper::Probe.inbound(channel.options[:inbound])
  53. expect(result[:result]).to eq('ok')
  54. end
  55. end
  56. context 'outbound' do
  57. it 'succeeds' do
  58. result = EmailHelper::Probe.outbound(channel.options[:outbound], ENV['GMAIL_USER'], "test gmail oauth unittest #{Random.new_seed}")
  59. expect(result[:result]).to eq('ok')
  60. end
  61. end
  62. end