1
0

jira_agent_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. require 'rails_helper'
  2. describe Agents::JiraAgent do
  3. before(:each) do
  4. stub_request(:get, /atlassian.com/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/jira.json")), :status => 200, :headers => {"Content-Type" => "text/json"})
  5. @valid_params = {
  6. :username => "user",
  7. :password => "pass",
  8. :jira_url => 'https://jira.atlassian.com',
  9. :jql => 'resolution = unresolved',
  10. :expected_update_period_in_days => '7',
  11. :timeout => '1'
  12. }
  13. @checker = Agents::JiraAgent.new(:name => "jira-agent", :options => @valid_params)
  14. @checker.user = users(:jane)
  15. @checker.save!
  16. end
  17. describe "validating" do
  18. before do
  19. expect(@checker).to be_valid
  20. end
  21. it "should work without username" do
  22. @checker.options['username'] = nil
  23. expect(@checker).to be_valid
  24. end
  25. it "should require the jira password if username is specified" do
  26. @checker.options['username'] = 'user'
  27. @checker.options['password'] = nil
  28. expect(@checker).not_to be_valid
  29. end
  30. it "should require the jira url" do
  31. @checker.options['jira_url'] = nil
  32. expect(@checker).not_to be_valid
  33. end
  34. it "should work without jql" do
  35. @checker.options['jql'] = nil
  36. expect(@checker).to be_valid
  37. end
  38. it "should require the expected_update_period_in_days" do
  39. @checker.options['expected_update_period_in_days'] = nil
  40. expect(@checker).not_to be_valid
  41. end
  42. it "should require timeout" do
  43. @checker.options['timeout'] = nil
  44. expect(@checker).not_to be_valid
  45. end
  46. end
  47. describe "helpers" do
  48. it "should generate a correct request options hash" do
  49. expect(@checker.send(:request_options)).to eq({basic_auth: {username: "user", password: "pass"}, headers: {"User-Agent" => "Huginn - https://github.com/huginn/huginn"}})
  50. end
  51. it "should generate a correct request url" do
  52. expect(@checker.send(:request_url, 'foo=bar', 10)).to eq("https://jira.atlassian.com/rest/api/2/search?jql=foo%3Dbar&fields=*all&startAt=10")
  53. end
  54. it "should not set the 'since' time on the first run" do
  55. expected_url = "https://jira.atlassian.com/rest/api/2/search?jql=resolution+%3D+unresolved&fields=*all&startAt=0"
  56. expected_headers = {headers: {"User-Agent"=>"Huginn - https://github.com/huginn/huginn"}, basic_auth: {username: "user", password: "pass"}}
  57. reply = JSON.parse(File.read(Rails.root.join("spec/data_fixtures/jira.json")))
  58. mock(@checker).get(expected_url, expected_headers).returns(reply)
  59. @checker.check
  60. end
  61. it "should provide set the 'since' time after the first run" do
  62. expected_url_1 = "https://jira.atlassian.com/rest/api/2/search?jql=resolution+%3D+unresolved&fields=*all&startAt=0"
  63. expected_url_2 = "https://jira.atlassian.com/rest/api/2/search?jql=resolution+%3D+unresolved&fields=*all&startAt=0"
  64. expected_headers = {headers: {"User-Agent"=>"Huginn - https://github.com/huginn/huginn"}, basic_auth: {username: "user", password: "pass"}}
  65. reply = JSON.parse(File.read(Rails.root.join("spec/data_fixtures/jira.json")))
  66. mock(@checker) do
  67. get(expected_url_1, expected_headers).returns(reply)
  68. # time specification
  69. get(/\d+-\d+-\d+\+\d+%3A\d+/, expected_headers).returns(reply)
  70. end
  71. @checker.check
  72. @checker.check
  73. end
  74. end
  75. describe "#check" do
  76. it "should be able to retrieve issues" do
  77. reply = JSON.parse(File.read(Rails.root.join("spec/data_fixtures/jira.json")))
  78. mock(@checker).get(anything,anything).returns(reply)
  79. expect { @checker.check }.to change { Event.count }.by(50)
  80. end
  81. end
  82. describe "#working?" do
  83. it "it is working when at least one event was emited" do
  84. expect(@checker).not_to be_working
  85. @checker.check
  86. expect(@checker.reload).to be_working
  87. end
  88. end
  89. end