Aucune description http://j1x-huginn.herokuapp.com

service_spec.rb 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. require 'spec_helper'
  2. describe Service do
  3. before(:each) do
  4. @user = users(:bob)
  5. end
  6. it "should toggle the global flag" do
  7. @service = services(:generic)
  8. @service.global.should == false
  9. @service.toggle_availability!
  10. @service.global.should == true
  11. @service.toggle_availability!
  12. @service.global.should == false
  13. end
  14. it "disables all agents before beeing destroyed" do
  15. agent = agents(:bob_basecamp_agent)
  16. service = agent.service
  17. service.destroy
  18. agent.reload
  19. agent.service_id.should be_nil
  20. agent.disabled.should be true
  21. end
  22. describe "preparing for a request" do
  23. before(:each) do
  24. @service = services(:generic)
  25. end
  26. it "should not update the token if the token never expires" do
  27. @service.expires_at = nil
  28. @service.prepare_request.should == nil
  29. end
  30. it "should not update the token if the token is still valid" do
  31. @service.expires_at = Time.now + 1.hour
  32. @service.prepare_request.should == nil
  33. end
  34. it "should call refresh_token! if the token expired" do
  35. stub(@service).refresh_token! { @service }
  36. @service.expires_at = Time.now - 1.hour
  37. @service.prepare_request.should == @service
  38. end
  39. end
  40. describe "updating the access token" do
  41. before(:each) do
  42. @service = services(:generic)
  43. end
  44. it "should return the correct endpoint" do
  45. @service.provider = '37signals'
  46. @service.send(:endpoint).to_s.should == "https://launchpad.37signals.com/authorization/token"
  47. end
  48. it "should update the token" do
  49. stub_request(:post, "https://launchpad.37signals.com/authorization/token?client_id=TESTKEY&client_secret=TESTSECRET&refresh_token=refreshtokentest&type=refresh").
  50. to_return(:status => 200, :body => '{"expires_in":1209600,"access_token": "NEWTOKEN"}', :headers => {})
  51. @service.provider = '37signals'
  52. @service.refresh_token = 'refreshtokentest'
  53. @service.refresh_token!
  54. @service.token.should == 'NEWTOKEN'
  55. end
  56. end
  57. describe "creating services via omniauth" do
  58. it "should work with twitter services" do
  59. twitter = JSON.parse(File.read(Rails.root.join('spec/data_fixtures/services/twitter.json')))
  60. expect {
  61. service = @user.services.initialize_or_update_via_omniauth(twitter)
  62. service.save!
  63. }.to change { @user.services.count }.by(1)
  64. service = @user.services.first
  65. service.name.should == 'johnqpublic'
  66. service.provider.should == 'twitter'
  67. service.token.should == 'a1b2c3d4...'
  68. service.secret.should == 'abcdef1234'
  69. end
  70. it "should work with 37signals services" do
  71. signals = JSON.parse(File.read(Rails.root.join('spec/data_fixtures/services/37signals.json')))
  72. expect {
  73. service = @user.services.initialize_or_update_via_omniauth(signals)
  74. service.save!
  75. }.to change { @user.services.count }.by(1)
  76. service = @user.services.first
  77. service.provider.should == '37signals'
  78. service.name.should == 'Dominik Sander'
  79. service.token.should == 'abcde'
  80. service.refresh_token.should == 'fghrefresh'
  81. service.options[:user_id].should == 12345
  82. service.expires_at = Time.at(1401554352)
  83. end
  84. it "should work with github services" do
  85. signals = JSON.parse(File.read(Rails.root.join('spec/data_fixtures/services/github.json')))
  86. expect {
  87. service = @user.services.initialize_or_update_via_omniauth(signals)
  88. service.save!
  89. }.to change { @user.services.count }.by(1)
  90. service = @user.services.first
  91. service.provider.should == 'github'
  92. service.name.should == 'dsander'
  93. service.token.should == 'agithubtoken'
  94. end
  95. end
  96. end