service_spec.rb 4.3 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. @service.global.should == false
  10. @service.toggle_availability!
  11. @service.global.should == true
  12. @service.toggle_availability!
  13. @service.global.should == false
  14. end
  15. it "disconnects agents and disables them if the previously global service is made private again", focus: true do
  16. agent = agents(:bob_basecamp_agent)
  17. jane_agent = agents(:jane_basecamp_agent)
  18. service = agent.service
  19. service.toggle_availability!
  20. service.agents.length.should == 2
  21. service.toggle_availability!
  22. jane_agent.reload
  23. jane_agent.service_id.should be_nil
  24. jane_agent.disabled.should be true
  25. service.reload
  26. service.agents.length.should == 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. agent.service_id.should be_nil
  35. agent.disabled.should 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. @service.prepare_request.should == 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. @service.prepare_request.should == 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. @service.prepare_request.should == @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. @service.send(:endpoint).to_s.should == "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. @service.token.should == '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. service.name.should == 'johnqpublic'
  81. service.uid.should == '123456'
  82. service.provider.should == 'twitter'
  83. service.token.should == 'a1b2c3d4...'
  84. service.secret.should == '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. service.provider.should == '37signals'
  94. service.name.should == 'Dominik Sander'
  95. service.token.should == 'abcde'
  96. service.uid.should == '12345'
  97. service.refresh_token.should == 'fghrefresh'
  98. service.options[:user_id].should == 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. service.provider.should == 'github'
  109. service.name.should == 'dsander'
  110. service.uid.should == '12345'
  111. service.token.should == 'agithubtoken'
  112. end
  113. end
  114. end