docbrown/spec/models/webhook/endpoint_spec.rb
rhymes 10efa25c0a
Add missing presence validators to models (#10495)
* Add missing presence validator for Tag

* Add missing presence validators to Follow

* Add missing presence validators to ProfileField

* Add missing presence validators to Comment

* Add missing presence validators to Profile

* Add missing presence validators to Organization

* Add missing presence validators to Badge

* Add missing presence validators to Webhook::Endpoint

* Add missing presence validators to NotificationSubscription

* Add missing presence validators to PodcastEpisode

* Add missing presence validators to Sponsorship

* Add missing presence validators to PollOption

* Add missing presence validators to Poll

* Add missing presence validators to Article

* Add missing presence validators to EmailAuthorization

* Add missing presence validators to AuditLog

* Add missing presence validators to DataUpdateScript

* Add missing presence validators to User

* Add missing presence validators to SiteConfig

* Fix specs
2020-10-01 16:15:32 +02:00

60 lines
2 KiB
Ruby

require "rails_helper"
RSpec.describe Webhook::Endpoint, type: :model do
let!(:endpoint) do
create(
:webhook_endpoint, user: user, events: %w[article_created article_updated article_destroyed]
)
end
let(:user) { create(:user) }
describe "validations" do
it { is_expected.to belong_to(:user).inverse_of(:webhook_endpoints) }
it { is_expected.to belong_to(:oauth_application).inverse_of(:webhook_endpoints).optional }
it { is_expected.to validate_presence_of(:events) }
it { is_expected.to validate_presence_of(:source) }
it { is_expected.to validate_presence_of(:target_url) }
it { is_expected.to validate_presence_of(:user_id) }
it { is_expected.to validate_uniqueness_of(:target_url) }
it { is_expected.to allow_value("https://foo.com").for(:target_url) }
it { is_expected.not_to allow_value("http://foo.com").for(:target_url) }
end
it "is valid" do
expect(endpoint).to be_valid
end
it "sets events according to the list" do
webhook = create(
:webhook_endpoint, events: %w[article_updated other_updated cool article_created]
)
webhook.reload
expect(webhook.events.sort).to eq(%w[article_created article_updated])
end
context "when endpoints exist" do
let!(:epoint2) { create(:webhook_endpoint, events: %w[article_destroyed], user: user) }
let!(:epoint3) { create(:webhook_endpoint, events: %w[article_updated article_destroyed]) }
before do
create(:webhook_endpoint, events: %w[article_created])
end
it "finds for events" do
d_points = described_class.for_events("article_destroyed")
expect(d_points.ids.sort).to eq([endpoint, epoint2, epoint3].map(&:id).sort)
end
it "finds for_events array" do
endpoints = described_class.for_events(%w[article_created article_destroyed])
expect(endpoints.ids).to eq([endpoint.id])
end
it "belongs to user" do
expect(user.webhook_endpoints.ids.sort).to eq([endpoint, epoint2].map(&:id).sort)
end
end
end