jira_agent_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. expect(@checker).to receive(:get).with(expected_url, expected_headers).and_return(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. expect(@checker).to receive(:get).with(expected_url_1, expected_headers).and_return(reply)
  67. @checker.check
  68. expect(@checker).to receive(:get).with(/\d+-\d+-\d+\+\d+%3A\d+/, expected_headers).and_return(reply)
  69. @checker.check
  70. end
  71. end
  72. describe "#check" do
  73. it "should be able to retrieve issues" do
  74. reply = JSON.parse(File.read(Rails.root.join("spec/data_fixtures/jira.json")))
  75. expect(@checker).to receive(:get).and_return(reply)
  76. expect { @checker.check }.to change { Event.count }.by(50)
  77. end
  78. end
  79. describe "#working?" do
  80. it "it is working when at least one event was emited" do
  81. expect(@checker).not_to be_working
  82. @checker.check
  83. expect(@checker.reload).to be_working
  84. end
  85. end
  86. end