|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | +require "sentry-ruby" |
| 5 | +require "pliny/error_reporters/sentry" |
| 6 | + |
| 7 | +describe Pliny::ErrorReporters::Sentry do |
| 8 | + subject(:reporter) { described_class.new } |
| 9 | + |
| 10 | + describe "#notify" do |
| 11 | + let(:exception) { StandardError.new("Something went wrong") } |
| 12 | + let(:context) { { step: :foo } } |
| 13 | + let(:rack_env) { { "rack.input" => StringIO.new } } |
| 14 | + |
| 15 | + subject(:notify) do |
| 16 | + reporter.notify(exception, context: context, rack_env: rack_env) |
| 17 | + end |
| 18 | + |
| 19 | + before do |
| 20 | + allow(::Sentry).to receive(:with_scope).and_yield(scope) |
| 21 | + allow(::Sentry).to receive(:capture_exception) |
| 22 | + end |
| 23 | + |
| 24 | + let(:scope) { instance_double("Sentry::Scope") } |
| 25 | + |
| 26 | + before do |
| 27 | + allow(scope).to receive(:set_context) |
| 28 | + allow(scope).to receive(:set_user) |
| 29 | + end |
| 30 | + |
| 31 | + it "creates a sentry scope" do |
| 32 | + notify |
| 33 | + expect(::Sentry).to have_received(:with_scope).once |
| 34 | + end |
| 35 | + |
| 36 | + it "sets custom context" do |
| 37 | + notify |
| 38 | + expect(scope).to have_received(:set_context).with("custom", { step: :foo }) |
| 39 | + end |
| 40 | + |
| 41 | + it "captures the exception" do |
| 42 | + notify |
| 43 | + expect(::Sentry).to have_received(:capture_exception).with(exception) |
| 44 | + end |
| 45 | + |
| 46 | + context "given a rack_env with sentry.person_data" do |
| 47 | + let(:rack_env) { { "sentry.person_data" => { id: 123, email: "test@example.com", username: "testuser" }, "rack.input" => StringIO.new } } |
| 48 | + |
| 49 | + it "sets user context from sentry.person_data" do |
| 50 | + notify |
| 51 | + expect(scope).to have_received(:set_user).with(id: 123, email: "test@example.com", username: "testuser") |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context "given a rack_env with empty sentry.person_data" do |
| 56 | + let(:rack_env) { { "sentry.person_data" => {}, "rack.input" => StringIO.new } } |
| 57 | + |
| 58 | + it "does not set user context" do |
| 59 | + notify |
| 60 | + expect(scope).not_to have_received(:set_user) |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + context "given an empty rack_env" do |
| 65 | + let(:rack_env) { {} } |
| 66 | + |
| 67 | + it "expects rack_env to be a hash" do |
| 68 | + assert_kind_of(Hash, rack_env) |
| 69 | + end |
| 70 | + |
| 71 | + it "sets only custom context" do |
| 72 | + notify |
| 73 | + expect(scope).to have_received(:set_context).once.with("custom", { step: :foo }) |
| 74 | + expect(scope).not_to have_received(:set_user) |
| 75 | + end |
| 76 | + |
| 77 | + it "captures the exception" do |
| 78 | + notify |
| 79 | + expect(::Sentry).to have_received(:capture_exception).with(exception) |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | +end |
0 commit comments