Skip to content

Commit

Permalink
fix tests, use image fixture instead of tmp file
Browse files Browse the repository at this point in the history
  • Loading branch information
matthutchinson committed Sep 28, 2024
1 parent 8d10672 commit 86588f0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 91 deletions.
1 change: 1 addition & 0 deletions lib/lolcommits/plugin/uploldz.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def valid_configuration?
#
def run_capture_ready
debug "Posting capture to #{configuration[:endpoint]}"

RestClient.post(
configuration[:endpoint],
{
Expand Down
Binary file added test/images/lolcommit.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
178 changes: 87 additions & 91 deletions test/lolcommits/plugin/uploldz_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,118 +4,114 @@
require 'webmock/minitest'

describe Lolcommits::Plugin::Uploldz do

include Lolcommits::TestHelpers::GitRepo
include Lolcommits::TestHelpers::FakeIO

describe "with a runner" do
def runner
# a simple lolcommits runner with an empty configuration Hash
@runner ||= Lolcommits::Runner.new(
lolcommit_path: Tempfile.new('lolcommit.jpg').path
)
end
def valid_enabled_config
{
enabled: true,
endpoint: "https://uploldz.com/uplol",
optional_http_auth_username: 'joe',
optional_http_auth_password: '1234'
}
end

def plugin
@plugin ||= Lolcommits::Plugin::Uploldz.new(runner: runner)
describe "initalizing" do
it "assigns runner and all plugin options" do
_(plugin.runner).wont_be_nil
_(plugin.options).must_equal [
:enabled,
:endpoint,
:optional_key,
:optional_http_auth_username,
:optional_http_auth_password
]
end
end

def valid_enabled_config
{
enabled: true,
endpoint: "https://uploldz.com/uplol",
optional_http_auth_username: 'joe',
optional_http_auth_password: '1234'
}
describe "#enabled?" do
it "is false by default" do
_(plugin.enabled?).must_equal false
end

describe "initalizing" do
it "assigns runner and all plugin options" do
_(plugin.runner).must_equal runner
_(plugin.options).must_equal [
:enabled,
:endpoint,
:optional_key,
:optional_http_auth_username,
:optional_http_auth_password
]
end
it "is true when configured" do
plugin.configuration = valid_enabled_config
_(plugin.enabled?).must_equal true
end
end

describe "#enabled?" do
it "is false by default" do
_(plugin.enabled?).must_equal false
end
describe "run_capture_ready" do
before { commit_repo_with_message("first commit!") }
after { teardown_repo }

it "is true when configured" do
plugin.configuration = valid_enabled_config
_(plugin.enabled?).must_equal true
end
end
it "syncs lolcommits" do
captured_img_path = File.expand_path("./test/images/lolcommit.jpg")

describe "run_capture_ready" do
before { commit_repo_with_message("first commit!") }
after { teardown_repo }

it "syncs lolcommits" do
in_repo do
plugin.configuration = valid_enabled_config

stub_request(:post, "https://uploldz.com/uplol").to_return(status: 200)

plugin.run_capture_ready

assert_requested :post, "https://uploldz.com/uplol", times: 1,
headers: {'Content-Type' => /multipart\/form-data/ } do |req|
_(req.body).must_match(/Content-Disposition: form-data;.+name="file"; filename="lolcommit.jpg.+"/)
_(req.body).must_match 'name="repo"'
_(req.body).must_match 'name="author_name"'
_(req.body).must_match 'name="author_email"'
_(req.body).must_match 'name="sha"'
_(req.body).must_match 'name="key"'
_(req.body).must_match "plugin-test-repo"
_(req.body).must_match "first commit!"
end
in_repo do
plugin.configuration = valid_enabled_config
plugin.runner.lolcommit_path = captured_img_path

stub_request(:post, "https://uploldz.com/uplol").to_return(status: 200)

plugin.run_capture_ready

assert_requested :post, "https://uploldz.com/uplol", times: 1,
headers: {'Content-Type' => /multipart\/form-data/ } do |req|
_(req.body).must_match(/Content-Disposition: form-data;.+name="file"; filename="lolcommit.jpg"/)
_(req.body).must_match 'name="repo"'
_(req.body).must_match 'name="author_name"'
_(req.body).must_match 'name="author_email"'
_(req.body).must_match 'name="sha"'
_(req.body).must_match 'name="key"'
_(req.body).must_match "plugin-test-repo"
_(req.body).must_match "first commit!"
end
end
end
end

describe "configuration" do
it "allows plugin options to be configured" do
# enabled, endpoint, key, user, password
inputs = %w(
true
https://my-server.com/uplol
key-123
joe
1337pass
)
configured_plugin_options = {}

fake_io_capture(inputs: inputs) do
configured_plugin_options = plugin.configure_options!
end
describe "configuration" do
it "allows plugin options to be configured" do
# enabled, endpoint, key, user, password
inputs = %w(
true
https://my-server.com/uplol
key-123
joe
1337pass
)
configured_plugin_options = {}

_(configured_plugin_options).must_equal({
enabled: true,
endpoint: "https://my-server.com/uplol",
optional_key: "key-123",
optional_http_auth_username: "joe",
optional_http_auth_password: "1337pass"
})
fake_io_capture(inputs: inputs) do
configured_plugin_options = plugin.configure_options!
end

describe "#valid_configuration?" do
it "returns false for an invalid configuration" do
plugin.configuration = { endpoint: "gibberish" }
_(plugin.valid_configuration?).must_equal false
end
_(configured_plugin_options).must_equal({
enabled: true,
endpoint: "https://my-server.com/uplol",
optional_key: "key-123",
optional_http_auth_username: "joe",
optional_http_auth_password: "1337pass"
})
end

it "returns true with a valid configuration" do
plugin.configuration = valid_enabled_config
_(plugin.valid_configuration?).must_equal true
end
describe "#valid_configuration?" do
it "returns false for an invalid configuration" do
plugin.configuration = { endpoint: "gibberish" }
_(plugin.valid_configuration?).must_equal false
end

it "returns true with a valid configuration" do
plugin.configuration = valid_enabled_config
_(plugin.valid_configuration?).must_equal true
end
end
end

private
def plugin
@plugin ||= Lolcommits::Plugin::Uploldz.new(
runner: Lolcommits::Runner.new
)
end
end

0 comments on commit 86588f0

Please sign in to comment.