service_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. require 'spec_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_basecamp_agent)
  17. jane_agent = agents(:jane_basecamp_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_basecamp_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. stub(@service).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 = '37signals'
  61. expect(@service.send(:endpoint).to_s).to eq("https://launchpad.37signals.com/authorization/token")
  62. end
  63. it "should update the token" do
  64. stub_request(:post, "https://launchpad.37signals.com/authorization/token?client_id=TESTKEY&client_secret=TESTSECRET&refresh_token=refreshtokentest&type=refresh").
  65. to_return(:status => 200, :body => '{"expires_in":1209600,"access_token": "NEWTOKEN"}', :headers => {})
  66. @service.provider = '37signals'
  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 37signals services" do
  87. signals = JSON.parse(File.read(Rails.root.join('spec/data_fixtures/services/37signals.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('37signals')
  94. expect(service.name).to eq('Dominik Sander')
  95. expect(service.token).to eq('abcde')
  96. expect(service.uid).to eq('12345')
  97. expect(service.refresh_token).to eq('fghrefresh')
  98. expect(service.options[:user_id]).to eq(12345)
  99. service.expires_at = Time.at(1401554352)
  100. end
  101. it "should work with github services" do
  102. signals = JSON.parse(File.read(Rails.root.join('spec/data_fixtures/services/github.json')))
  103. expect {
  104. service = @user.services.initialize_or_update_via_omniauth(signals)
  105. service.save!
  106. }.to change { @user.services.count }.by(1)
  107. service = @user.services.first
  108. expect(service.provider).to eq('github')
  109. expect(service.name).to eq('dsander')
  110. expect(service.uid).to eq('12345')
  111. expect(service.token).to eq('agithubtoken')
  112. end
  113. end
  114. end