Don't generate file fixtures anymore

When we want to test without rails, we can't use the controllers to
generate them.

Also fixes the problem where there is still an old file fixture that
doesn't match the users in the database.
This commit is contained in:
Benjamin Neff 2017-04-04 01:22:54 +02:00
parent 184954e09c
commit 7fe7a5da97
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
12 changed files with 127 additions and 84 deletions

View file

@ -204,7 +204,7 @@ Performance/RegexpMatch:
# for rails 4 and ruby < 2.2.2 # for rails 4 and ruby < 2.2.2
Rails/HttpPositionalArguments: Rails/HttpPositionalArguments:
Exclude: Exclude:
- "spec/controllers/diaspora_federation/fixtures_rails4_spec.rb" - "spec/controllers/diaspora_federation/rails4_spec.rb"
# remove with next rubocop update, see https://github.com/bbatsov/rubocop/issues/4172 # remove with next rubocop update, see https://github.com/bbatsov/rubocop/issues/4172
Style/MixinGrouping: Style/MixinGrouping:

View file

@ -1,15 +1,6 @@
if defined?(RSpec) if defined?(RSpec)
namespace :rails4 do namespace :rails4 do
desc "Run all specs that generate fixtures for rspec with rails 4" RSpec::Core::RakeTask.new(:spec)
RSpec::Core::RakeTask.new(:generate_fixtures) do |t| task test: %w(spec:prepare_db spec)
t.rspec_opts = ["--tag fixture4"]
end
desc "Run all specs in spec directory (exluding controller specs)"
RSpec::Core::RakeTask.new(:spec) do |task|
task.pattern = FileList["spec/**/*_spec.rb"].exclude("spec/controllers/**/*_spec.rb")
end
task test: %w(spec:prepare_db generate_fixtures spec)
end end
end end

View file

@ -1,18 +1,8 @@
if defined?(RSpec) if defined?(RSpec)
namespace :spec do namespace :spec do
task prepare_db: %w(db:create db:test:load) task prepare_db: %w(db:create db:test:load)
task :prepare_fixtures do
ENV["NO_COVERAGE"] = "true"
Rake::Task["spec:generate_fixtures"].invoke
ENV["NO_COVERAGE"] = "false"
end
desc "Prepare for rspec" desc "Prepare for rspec"
task prepare: %w(db:environment:set prepare_db prepare_fixtures) task prepare: %w(db:environment:set prepare_db)
desc "Run all specs that generate fixtures for rspec"
RSpec::Core::RakeTask.new(:generate_fixtures) do |t|
t.rspec_opts = ["--tag fixture"]
end
end end
end end

View file

@ -1,5 +1,5 @@
module DiasporaFederation module DiasporaFederation
describe FetchController, type: :controller do describe FetchController, type: :controller, rails: 5 do
routes { DiasporaFederation::Engine.routes } routes { DiasporaFederation::Engine.routes }
let(:guid) { "12345678901234567890" } let(:guid) { "12345678901234567890" }

View file

@ -1,21 +0,0 @@
module DiasporaFederation
describe WebfingerController, type: :controller do
routes { DiasporaFederation::Engine.routes }
it "generates webfinger fixture", fixture4: true, rails4: true do
get :legacy_webfinger, q: "alice@localhost:3000"
expect(response).to be_success
save_fixture(response.body, "legacy-webfinger")
end
end
describe HCardController, type: :controller do
routes { DiasporaFederation::Engine.routes }
it "generates hcard fixture", fixture4: true, rails4: true do
get :hcard, guid: alice.guid
expect(response).to be_success
save_fixture(response.body, "hcard")
end
end
end

View file

@ -1,12 +1,11 @@
module DiasporaFederation module DiasporaFederation
describe HCardController, type: :controller do describe HCardController, type: :controller, rails: 5 do
routes { DiasporaFederation::Engine.routes } routes { DiasporaFederation::Engine.routes }
describe "GET #hcard" do describe "GET #hcard" do
it "succeeds when the person exists", fixture: true do it "succeeds when the person exists" do
get :hcard, params: {guid: alice.guid} get :hcard, params: {guid: alice.guid}
expect(response).to be_success expect(response).to be_success
save_fixture(response.body, "hcard")
end end
it "contains the guid" do it "contains the guid" do

View file

@ -0,0 +1,81 @@
# only some basic controller tests for rails 4
module DiasporaFederation
describe WebfingerController, type: :controller, rails: 4 do
routes { DiasporaFederation::Engine.routes }
it "contains the webfinger result" do
webfinger_xrd = DiasporaFederation::Discovery::WebFinger.new(
acct_uri: "acct:#{alice.diaspora_id}",
alias_url: alice.alias_url,
hcard_url: alice.hcard_url,
seed_url: alice.url,
profile_url: alice.profile_url,
atom_url: alice.atom_url,
salmon_url: alice.salmon_url,
subscribe_url: alice.subscribe_url,
guid: alice.guid,
public_key: alice.serialized_public_key
).to_xml
get :legacy_webfinger, q: "alice@localhost:3000"
expect(response).to be_success
expect(response.body).to eq(webfinger_xrd)
end
it "404s when the person does not exist" do
get :legacy_webfinger, q: "me@mydiaspora.pod.com"
expect(response).to be_not_found
end
end
describe HCardController, type: :controller, rails: 4 do
routes { DiasporaFederation::Engine.routes }
it "contains the hcard result" do
hcard_html = DiasporaFederation::Discovery::HCard.new(
guid: alice.guid,
nickname: alice.nickname,
full_name: alice.full_name,
url: alice.url,
photo_large_url: alice.photo_default_url,
photo_medium_url: alice.photo_default_url,
photo_small_url: alice.photo_default_url,
public_key: alice.serialized_public_key,
searchable: alice.searchable,
first_name: alice.first_name,
last_name: alice.last_name
).to_html
get :hcard, guid: alice.guid
expect(response).to be_success
expect(response.body).to eq(hcard_html)
end
it "404s when the person does not exist" do
get :hcard, guid: "unknown_guid"
expect(response).to be_not_found
end
end
describe ReceiveController, type: :controller, rails: 4 do
routes { DiasporaFederation::Engine.routes }
describe "POST #public" do
it "returns a 202 if queued correctly" do
expect_callback(:queue_public_receive, "<diaspora/>", true)
post :public, xml: "<diaspora/>"
expect(response.code).to eq("202")
end
end
describe "POST #private" do
it "returns a 202 if the callback returned true" do
expect_callback(:queue_private_receive, "any-guid", "<diaspora/>", true).and_return(true)
post :private, guid: "any-guid", xml: "<diaspora/>"
expect(response.code).to eq("202")
end
end
end
end

View file

@ -15,21 +15,21 @@ module DiasporaFederation
expect(response.code).to eq("422") expect(response.code).to eq("422")
end end
it "returns a 202 if queued correctly" do it "returns a 202 if queued correctly", rails: 5 do
expect_callback(:queue_public_receive, "<diaspora/>", true) expect_callback(:queue_public_receive, "<diaspora/>", true)
post :public, params: {xml: "<diaspora/>"} post :public, params: {xml: "<diaspora/>"}
expect(response.code).to eq("202") expect(response.code).to eq("202")
end end
it "unescapes the xml before sending it to the callback" do it "unescapes the xml before sending it to the callback", rails: 5 do
expect_callback(:queue_public_receive, "<diaspora/>", true) expect_callback(:queue_public_receive, "<diaspora/>", true)
post :public, params: {xml: CGI.escape("<diaspora/>")} post :public, params: {xml: CGI.escape("<diaspora/>")}
end end
end end
context "magic envelope" do context "magic envelope", rails: 5 do
before do before do
Mime::Type.register("application/magic-envelope+xml", :magic_envelope) Mime::Type.register("application/magic-envelope+xml", :magic_envelope)
@request.env["CONTENT_TYPE"] = "application/magic-envelope+xml" @request.env["CONTENT_TYPE"] = "application/magic-envelope+xml"
@ -44,7 +44,7 @@ module DiasporaFederation
end end
end end
describe "POST #private" do describe "POST #private", rails: 5 do
context "legacy salmon slap" do context "legacy salmon slap" do
it "return a 404 if not queued successfully (unknown user guid)" do it "return a 404 if not queued successfully (unknown user guid)" do
expect_callback(:queue_private_receive, "any-guid", "<diaspora/>", true).and_return(false) expect_callback(:queue_private_receive, "any-guid", "<diaspora/>", true).and_return(false)

View file

@ -8,10 +8,9 @@ module DiasporaFederation
WebfingerController.instance_variable_set(:@host_meta_xml, nil) # clear cache WebfingerController.instance_variable_set(:@host_meta_xml, nil) # clear cache
end end
it "succeeds", fixture: true, fixture4: true do it "succeeds" do
get :host_meta get :host_meta
expect(response).to be_success expect(response).to be_success
save_fixture(response.body, "host-meta")
end end
it "contains the webfinger-template" do it "contains the webfinger-template" do
@ -36,11 +35,10 @@ module DiasporaFederation
end end
end end
describe "GET #legacy_webfinger" do describe "GET #legacy_webfinger", rails: 5 do
it "succeeds when the person exists", fixture: true do it "succeeds when the person exists" do
get :legacy_webfinger, params: {q: "alice@localhost:3000"} get :legacy_webfinger, params: {q: "alice@localhost:3000"}
expect(response).to be_success expect(response).to be_success
save_fixture(response.body, "legacy-webfinger")
end end
it "succeeds with 'acct:' in the query when the person exists" do it "succeeds with 'acct:' in the query when the person exists" do

View file

@ -1,8 +1,35 @@
module DiasporaFederation module DiasporaFederation
describe Discovery::Discovery do describe Discovery::Discovery do
let(:host_meta_xrd) { FixtureGeneration.load_fixture("host-meta") } let(:host_meta_xrd) { Discovery::HostMeta.from_base_url("http://localhost:3000/").to_xml }
let(:webfinger_xrd) { FixtureGeneration.load_fixture("legacy-webfinger") } let(:webfinger_xrd) {
let(:hcard_html) { FixtureGeneration.load_fixture("hcard") } DiasporaFederation::Discovery::WebFinger.new(
acct_uri: "acct:#{alice.diaspora_id}",
alias_url: alice.alias_url,
hcard_url: alice.hcard_url,
seed_url: alice.url,
profile_url: alice.profile_url,
atom_url: alice.atom_url,
salmon_url: alice.salmon_url,
subscribe_url: alice.subscribe_url,
guid: alice.guid,
public_key: alice.serialized_public_key
).to_xml
}
let(:hcard_html) {
DiasporaFederation::Discovery::HCard.new(
guid: alice.guid,
nickname: alice.nickname,
full_name: alice.full_name,
url: alice.url,
photo_large_url: alice.photo_default_url,
photo_medium_url: alice.photo_default_url,
photo_small_url: alice.photo_default_url,
public_key: alice.serialized_public_key,
searchable: alice.searchable,
first_name: alice.first_name,
last_name: alice.last_name
).to_html
}
let(:account) { alice.diaspora_id } let(:account) { alice.diaspora_id }
let(:default_image) { "http://localhost:3000/assets/user/default.png" } let(:default_image) { "http://localhost:3000/assets/user/default.png" }
@ -179,7 +206,7 @@ module DiasporaFederation
</div> </div>
</div> </div>
</div> </div>
HTML HTML
stub_request(:get, "https://localhost:3000/.well-known/host-meta") stub_request(:get, "https://localhost:3000/.well-known/host-meta")
.to_return(status: 200, body: host_meta_xrd) .to_return(status: 200, body: host_meta_xrd)

View file

@ -80,7 +80,7 @@ RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/test/fixtures" config.fixture_path = "#{::Rails.root}/test/fixtures"
config.global_fixtures = :all config.global_fixtures = :all
config.filter_run_excluding rails4: true if Rails::VERSION::MAJOR == 5 config.filter_run_excluding rails: (Rails::VERSION::MAJOR == 5 ? 4 : 5)
# whitelist codeclimate.com so test coverage can be reported # whitelist codeclimate.com so test coverage can be reported
config.after(:suite) do config.after(:suite) do

View file

@ -1,22 +0,0 @@
module FixtureGeneration
# Saves the markup to a fixture file using the given name
def save_fixture(markup, name, fixture_path=nil)
fixture_path = Rails.root.join("tmp", "fixtures") unless fixture_path
Dir.mkdir(fixture_path) unless File.exist?(fixture_path)
fixture_file = fixture_path.join("#{name}.fixture.html")
File.open(fixture_file, "w") do |file|
file.puts(markup)
end
end
def self.load_fixture(name, fixture_path=nil)
fixture_path = Rails.root.join("tmp", "fixtures") unless fixture_path
fixture_file = fixture_path.join("#{name}.fixture.html")
File.open(fixture_file).read
end
end
RSpec::Rails::ControllerExampleGroup.class_eval do
include FixtureGeneration
end