Initial commit

This commit is contained in:
2023-01-27 22:55:09 +11:00
commit 833f3ea8b2
130 changed files with 5637 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
RSpec.describe Adamantium::MicropubRequestParser do
subject { described_class.new }
context "json request" do
context "HTML post" do
let(:params) {
{
type: ["h-entry"],
properties: {
name: ["title"],
content: [
"Hello world"
]
},
category: ["ruby", "rspec"]
}
}
it "parses the params in to the expected shape" do
Timecop.freeze do
result = subject.call(params: params)
expect(result).to be_a Adamantium::Entities::PostRequest
end
end
end
end
context "form request" do
let(:params) {
{
h: "entry",
name: "title",
content: "Hello world",
category: ["ruby", "rspec"]
}
}
it "parses the params in to the expected shape" do
Timecop.freeze do
result = subject.call(params: params)
expect(result).to be_a Adamantium::Entities::PostRequest
end
end
end
end

View File

@@ -0,0 +1,23 @@
# frozen_string_literal: true
RSpec.describe Adamantium::SlugCreator do
describe "#call" do
subject { described_class.new }
let(:checker) { ->(_input) { false } }
let(:uuid_regex) { /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/ }
it "creates a slugified string" do
expect(subject.call(text: "my string", checker: checker)).to eq "my-string"
end
it "can handle no input" do
expect(subject.call(text: nil, checker: checker)).to match(uuid_regex)
end
it "adds a number to the end of the slug if the checker finds an existing slug" do
text = "my-existing-slug"
checker = ->(input) { true if input == text }
expect(subject.call(text: text, checker: checker)).to eq "my-existing-slug-1"
end
end
end

View File

@@ -0,0 +1,28 @@
# frozen_string_literal: true
require "dry/monads"
RSpec.describe Adamantium::Commands::Media::Upload do
subject { described_class.new }
it "saves a file and returns its URL" do
file = {
filename: "foo.txt",
tempfile: Tempfile.new
}
result = subject.call(file: file)
expected_path = "media/#{Time.now.strftime("%m-%Y")}/foo.txt"
expect(result).to be_success
expect(result.value!).to eq "http://localhost/#{expected_path}"
File.read("public/#{expected_path}")
File.delete("public/#{expected_path}")
end
it "returns a Failure if the file couldn't be saved" do
file = {filename: "file.txt", tempfile: ""}
result = subject.call(file: file)
expect(result).to be_failure
end
end

View File

@@ -0,0 +1,61 @@
# frozen_string_literal: true
RSpec.describe "Post creation", type: [:request, :db] do
let(:post_repo) { Adamantium::Repos::PostRepo.new }
context "posts" do
it "is successful" do
params = {
type: ["h-entry"],
properties: {
category: ["ruby", "rspec"],
name: ["Test"],
content: [
"<p>Hello world!</p>"
]
}
}
post "/micropub", params
expect(last_response).to be_successful
expect(post_repo.post_listing.count).to eq 1
expect(post_repo.post_listing.first.tags.map(&:label)).to eq ["ruby", "rspec"]
end
it "is successful" do
params = {
type: ["h-entry"],
properties: {
category: ["ruby", "rspec"],
name: [],
content: [
"<p>Hello world!</p>"
]
}
}
post "/micropub", params
expect(last_response).to be_successful
expect(post_repo.post_listing.count).to eq 1
expect(post_repo.post_listing.first.tags.map(&:label)).to eq ["ruby", "rspec"]
end
end
context "bookmarks" do
it "is successful" do
params = {
h: "entry",
"bookmark-of": "http://example.com",
name: "Name",
content: "Content of theh post"
}
post "/micropub", params
expect(last_response).to be_successful
expect(post_repo.bookmark_listing.count).to eq 1
end
end
end

View File

@@ -0,0 +1,9 @@
# frozen_string_literal: true
RSpec.describe "Root", type: :request do
it "is successful" do
get "/"
expect(last_response).to be_successful
end
end

13
spec/spec_helper.rb Normal file
View File

@@ -0,0 +1,13 @@
# frozen_string_literal: true
require "pathname"
SPEC_ROOT = Pathname(__dir__).realpath.freeze
ENV["HANAMI_ENV"] ||= "test"
require "hanami/prepare"
require "timecop"
require_relative "support/rspec"
require_relative "support/requests"
require_relative "support/db"

15
spec/support/db.rb Normal file
View File

@@ -0,0 +1,15 @@
# require_with_metadata: true
# frozen_string_literal: true
require_relative "db/helpers"
require_relative "db/database_cleaner"
RSpec.configure do |config|
config.before :suite do
Hanami.app.start :persistence
end
config.include Test::DB::Helpers, :db
# config.include(Test::DB::FactoryHelper.new, factory: nil)
end

View File

@@ -0,0 +1,18 @@
require "sequel"
require "database_cleaner/sequel"
require_relative "helpers"
DatabaseCleaner[:sequel].strategy = :transaction
RSpec.configure do |config|
config.prepend_before :each, type: :db do |example|
strategy = example.metadata[:js] ? :truncation : :transaction
DatabaseCleaner[:sequel].strategy = strategy
DatabaseCleaner[:sequel].start
end
config.append_after :each, type: :db do
DatabaseCleaner[:sequel].clean
end
end

View File

@@ -0,0 +1,19 @@
module Test
module DB
module Helpers
module_function
def relations
rom.relations
end
def rom
Hanami.app["persistence.rom"]
end
def db
Hanami.app["persistence.db"]
end
end
end
end

12
spec/support/requests.rb Normal file
View File

@@ -0,0 +1,12 @@
# frozen_string_literal: true
require "rack/test"
RSpec.shared_context "Hanami app" do
let(:app) { Hanami.app }
end
RSpec.configure do |config|
config.include Rack::Test::Methods, type: :request
config.include_context "Hanami app", type: :request
end

27
spec/support/rspec.rb Normal file
View File

@@ -0,0 +1,27 @@
# frozen_string_literal: true
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
config.filter_run_when_matching :focus
config.disable_monkey_patching!
config.warnings = true
if config.files_to_run.one?
config.default_formatter = "doc"
end
config.profile_examples = 10
config.order = :random
Kernel.srand config.seed
end