|
| 1 | +require_relative "../../spec_helper.rb" |
| 2 | + |
| 3 | +describe Openjournals::SetArchiveResponder do |
| 4 | + |
| 5 | + subject do |
| 6 | + described_class |
| 7 | + end |
| 8 | + |
| 9 | + describe "listening" do |
| 10 | + before { @responder = subject.new({env: {bot_github_user: "botsci"}}, {}) } |
| 11 | + |
| 12 | + it "should listen to new comments" do |
| 13 | + expect(@responder.event_action).to eq("issue_comment.created") |
| 14 | + end |
| 15 | + |
| 16 | + it "should define regex" do |
| 17 | + expect(@responder.event_regex).to match("@botsci set 10.5281/zenodo.6861996 as archive") |
| 18 | + expect(@responder.event_regex).to match("@botsci set 10.5281/zenodo.6861996 as archive.") |
| 19 | + expect(@responder.event_regex).to match("@botsci set 10.5281/zenodo.6861996 as archive \r\n") |
| 20 | + expect(@responder.event_regex).to match("@botsci set 10.5281/zenodo.6861996 as archive \r\n more") |
| 21 | + expect(@responder.event_regex).to_not match("@botsci set 10.5281/zenodo.6861996 as editor") |
| 22 | + expect(@responder.event_regex).to_not match("@botsci set 10.5281/zenodo.6861996 as archive now") |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + describe "#process_message" do |
| 27 | + before do |
| 28 | + @responder = subject.new({env: {bot_github_user: "botsci"}}, {}) |
| 29 | + disable_github_calls_for(@responder) |
| 30 | + |
| 31 | + issue_body = "...Archive: <!--archive-->Pending<!--end-archive--> ..." |
| 32 | + allow(@responder).to receive(:issue_body).and_return(issue_body) |
| 33 | + end |
| 34 | + |
| 35 | + describe "with a valid DOI" do |
| 36 | + before do |
| 37 | + @msg = "@botsci set 10.5281/zenodo.6861996 as archive" |
| 38 | + @responder.match_data = @responder.event_regex.match(@msg) |
| 39 | + expect(Faraday).to receive(:head).with("https://doi.org/10.5281/zenodo.6861996").and_return(double(status: 301)) |
| 40 | + end |
| 41 | + |
| 42 | + it "should update value in the body of the issue" do |
| 43 | + expected_new_body = "...Archive: <!--archive-->10.5281/zenodo.6861996<!--end-archive--> ..." |
| 44 | + expect(@responder).to receive(:update_issue).with({ body: expected_new_body }) |
| 45 | + @responder.process_message(@msg) |
| 46 | + end |
| 47 | + |
| 48 | + it "should respond to github" do |
| 49 | + expect(@responder).to receive(:respond).with("Done! archive is now [10.5281/zenodo.6861996](https://doi.org/10.5281/zenodo.6861996)") |
| 50 | + @responder.process_message(@msg) |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + describe "with invalid DOI values" do |
| 55 | + it "should clean doi.org URLs" do |
| 56 | + @msg = "@botsci set https://doi.org/10.5281/zenodo.6861996 as archive" |
| 57 | + @responder.match_data = @responder.event_regex.match(@msg) |
| 58 | + expect(Faraday).to receive(:head).with("https://doi.org/10.5281/zenodo.6861996").and_return(double(status: 301)) |
| 59 | + |
| 60 | + expected_new_body = "...Archive: <!--archive-->10.5281/zenodo.6861996<!--end-archive--> ..." |
| 61 | + expect(@responder).to receive(:update_issue).with({ body: expected_new_body }) |
| 62 | + expect(@responder).to receive(:respond).with("Done! archive is now [10.5281/zenodo.6861996](https://doi.org/10.5281/zenodo.6861996)") |
| 63 | + |
| 64 | + @responder.process_message(@msg) |
| 65 | + end |
| 66 | + |
| 67 | + it "should reply error if DOI doesn't resolve" do |
| 68 | + @msg = "@botsci set https://zenodo.org/invalid.6573452618CX as archive" |
| 69 | + @responder.match_data = @responder.event_regex.match(@msg) |
| 70 | + |
| 71 | + expect(Faraday).to receive(:head).with("https://zenodo.org/invalid.6573452618CX").and_return(double(status: 404)) |
| 72 | + expect(@responder).to_not receive(:update_issue) |
| 73 | + expect(@responder).to receive(:respond).with("That doesn't look like a valid DOI value") |
| 74 | + |
| 75 | + @responder.process_message(@msg) |
| 76 | + end |
| 77 | + |
| 78 | + it "should reply error if malformed URI" do |
| 79 | + @msg = "@botsci set ' OR 1=1 as archive" |
| 80 | + @responder.match_data = @responder.event_regex.match(@msg) |
| 81 | + |
| 82 | + expect(Faraday).to_not receive(:head) |
| 83 | + expect(@responder).to_not receive(:update_issue) |
| 84 | + expect(@responder).to receive(:respond).with("That doesn't look like a valid DOI value") |
| 85 | + |
| 86 | + @responder.process_message(@msg) |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | +end |
0 commit comments