Validate bookmark URL uniqueness
This commit is contained in:
@@ -53,6 +53,12 @@ module Micropub
|
|||||||
.one
|
.one
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def url_exists?(url)
|
||||||
|
!!posts
|
||||||
|
.where(url: url)
|
||||||
|
.one
|
||||||
|
end
|
||||||
|
|
||||||
def find!(id)
|
def find!(id)
|
||||||
posts
|
posts
|
||||||
.by_pk(id)
|
.by_pk(id)
|
||||||
|
@@ -2,6 +2,9 @@ module Micropub
|
|||||||
module Validation
|
module Validation
|
||||||
module Posts
|
module Posts
|
||||||
class BookmarkContract < Dry::Validation::Contract
|
class BookmarkContract < Dry::Validation::Contract
|
||||||
|
|
||||||
|
include Deps["repos.post_repo"]
|
||||||
|
|
||||||
params do
|
params do
|
||||||
required(:name).filled(:string)
|
required(:name).filled(:string)
|
||||||
required(:content).maybe(:string)
|
required(:content).maybe(:string)
|
||||||
@@ -14,6 +17,10 @@ module Micropub
|
|||||||
required(:photos).array(:hash)
|
required(:photos).array(:hash)
|
||||||
required(:cache).filled(:bool)
|
required(:cache).filled(:bool)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
rule(:url) do
|
||||||
|
key.failure("URL already bookmarked") if post_repo.url_exists?(values[:url])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@@ -176,6 +176,26 @@ RSpec.describe "Post creation", :db, :requests do
|
|||||||
expect(post_repo.bookmark_listing.count).to eq 1
|
expect(post_repo.bookmark_listing.count).to eq 1
|
||||||
expect(post_repo.bookmark_listing.last.cached_content).to_not eq nil
|
expect(post_repo.bookmark_listing.last.cached_content).to_not eq nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "validates an already bookmarked URL" do
|
||||||
|
params = {
|
||||||
|
h: "entry",
|
||||||
|
"bookmark-of": "http://example.com",
|
||||||
|
name: "Name",
|
||||||
|
content: "Content of the post",
|
||||||
|
cache: "true"
|
||||||
|
}
|
||||||
|
|
||||||
|
post "/micropub", params
|
||||||
|
|
||||||
|
expect(last_response).to be_successful
|
||||||
|
expect(post_repo.bookmark_listing.count).to eq 1
|
||||||
|
expect(post_repo.bookmark_listing.last.cached_content).to_not eq nil
|
||||||
|
|
||||||
|
post "/micropub", params
|
||||||
|
expect(last_response).to_not be_successful
|
||||||
|
expect(post_repo.bookmark_listing.count).to eq 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "checkins" do
|
context "checkins" do
|
||||||
|
Reference in New Issue
Block a user