service_spec.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. require 'rails_helper'
  2. describe Service do
  3. before(:each) do
  4. @user = users(:bob)
  5. end
  6. describe "#toggle_availability!" do
  7. it "should toggle the global flag" do
  8. @service = services(:generic)
  9. expect(@service.global).to eq(false)
  10. @service.toggle_availability!
  11. expect(@service.global).to eq(true)
  12. @service.toggle_availability!
  13. expect(@service.global).to eq(false)
  14. end
  15. it "disconnects agents and disables them if the previously global service is made private again" do
  16. agent = agents(:bob_twitter_user_agent)
  17. jane_agent = agents(:jane_twitter_user_agent)
  18. service = agent.service
  19. service.toggle_availability!
  20. expect(service.agents.length).to eq(2)
  21. service.toggle_availability!
  22. jane_agent.reload
  23. expect(jane_agent.service_id).to be_nil
  24. expect(jane_agent.disabled).to be true
  25. service.reload
  26. expect(service.agents.length).to eq(1)
  27. end
  28. end
  29. it "disables all agents before beeing destroyed" do
  30. agent = agents(:bob_twitter_user_agent)
  31. service = agent.service
  32. service.destroy
  33. agent.reload
  34. expect(agent.service_id).to be_nil
  35. expect(agent.disabled).to be true
  36. end
  37. describe "preparing for a request" do
  38. before(:each) do
  39. @service = services(:generic)
  40. end
  41. it "should not update the token if the token never expires" do
  42. @service.expires_at = nil
  43. expect(@service.prepare_request).to eq(nil)
  44. end
  45. it "should not update the token if the token is still valid" do
  46. @service.expires_at = Time.now + 1.hour
  47. expect(@service.prepare_request).to eq(nil)
  48. end
  49. it "should call refresh_token! if the token expired" do
  50. allow(@service).to receive(:refresh_token!) { @service }
  51. @service.expires_at = Time.now - 1.hour
  52. expect(@service.prepare_request).to eq(@service)
  53. end
  54. end
  55. describe "updating the access token" do
  56. before(:each) do
  57. @service = services(:generic)
  58. end
  59. it "should return the correct endpoint" do
  60. @service.provider = 'google'
  61. expect(@service.send(:endpoint).to_s).to eq("https://oauth2.googleapis.com/token")
  62. end
  63. it "should update the token" do
  64. stub_request(:post, "https://oauth2.googleapis.com/token?client_id=googleclientid&client_secret=googleclientsecret&grant_type=refresh_token&refresh_token=refreshtokentest").
  65. to_return(:status => 200, :body => '{"expires_in":1209600,"access_token": "NEWTOKEN"}', :headers => {})
  66. @service.provider = 'google'
  67. @service.refresh_token = 'refreshtokentest'
  68. @service.refresh_token!
  69. expect(@service.token).to eq('NEWTOKEN')
  70. end
  71. end
  72. describe "creating services via omniauth" do
  73. it "should work with twitter services" do
  74. twitter = JSON.parse(File.read(Rails.root.join('spec/data_fixtures/services/twitter.json')))
  75. expect {
  76. service = @user.services.initialize_or_update_via_omniauth(twitter)
  77. service.save!
  78. }.to change { @user.services.count }.by(1)
  79. service = @user.services.first
  80. expect(service.name).to eq('johnqpublic')
  81. expect(service.uid).to eq('123456')
  82. expect(service.provider).to eq('twitter')
  83. expect(service.token).to eq('a1b2c3d4...')
  84. expect(service.secret).to eq('abcdef1234')
  85. end
  86. it "should work with github services" do
  87. signals = JSON.parse(File.read(Rails.root.join('spec/data_fixtures/services/github.json')))
  88. expect {
  89. service = @user.services.initialize_or_update_via_omniauth(signals)
  90. service.save!
  91. }.to change { @user.services.count }.by(1)
  92. service = @user.services.first
  93. expect(service.provider).to eq('github')
  94. expect(service.name).to eq('dsander')
  95. expect(service.uid).to eq('12345')
  96. expect(service.token).to eq('agithubtoken')
  97. end
  98. end
  99. describe 'omniauth options provider registry for non-conforming omniauth responses' do
  100. describe '.register_options_provider' do
  101. before do
  102. Service.register_options_provider('test-omniauth-provider') do |omniauth|
  103. { name: omniauth['special_field'] }
  104. end
  105. end
  106. after do
  107. Service.option_providers.delete('test-omniauth-provider')
  108. end
  109. it 'allows gem developers to add their own options provider to the registry' do
  110. actual_options = Service.get_options({
  111. 'provider' => 'test-omniauth-provider',
  112. 'special_field' => 'A Great Name'
  113. })
  114. expect(actual_options[:name]).to eq('A Great Name')
  115. end
  116. end
  117. end
  118. end