1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
require 'rails'
require 'flipper/cloud'
RSpec.describe Flipper::Cloud::Engine do
let(:env) do
{ "FLIPPER_CLOUD_TOKEN" => "test-token" }
end
let(:application) do
Class.new(Rails::Application) do
config.eager_load = false
config.logger = ActiveSupport::Logger.new($stdout)
end
end
# App for Rack::Test
let(:app) { application.routes }
before do
Rails.application = nil
ActiveSupport::Dependencies.autoload_paths = ActiveSupport::Dependencies.autoload_paths.dup
ActiveSupport::Dependencies.autoload_once_paths = ActiveSupport::Dependencies.autoload_once_paths.dup
# Force loading of flipper to configure itself
load 'flipper/cloud.rb'
end
it "initializes cloud configuration" do
stub_request(:get, /flippercloud\.io/).to_return(status: 200, body: "{}")
ENV.update(env)
application.initialize!
expect(Flipper.instance).to be_a(Flipper::Cloud::DSL)
expect(Flipper.instance.instrumenter).to be(ActiveSupport::Notifications)
end
context "with CLOUD_SYNC_SECRET" do
before do
env.update "FLIPPER_CLOUD_SYNC_SECRET" => "test-secret"
end
let(:request_body) do
JSON.generate({
"environment_id" => 1,
"webhook_id" => 1,
"delivery_id" => SecureRandom.uuid,
"action" => "sync",
})
end
let(:timestamp) { Time.now }
let(:signature) {
Flipper::Cloud::MessageVerifier.new(secret: env["FLIPPER_CLOUD_SYNC_SECRET"]).generate(request_body, timestamp)
}
let(:signature_header_value) {
Flipper::Cloud::MessageVerifier.new(secret: "").header(signature, timestamp)
}
it "configures webhook app" do
ENV.update(env)
application.initialize!
stub = stub_request(:get, "https://www.flippercloud.io/adapter/features").with({
headers: { "Flipper-Cloud-Token" => ENV["FLIPPER_CLOUD_TOKEN"] },
}).to_return(status: 200, body: JSON.generate({ features: {} }), headers: {})
post "/_flipper", request_body, { "HTTP_FLIPPER_CLOUD_SIGNATURE" => signature_header_value }
expect(last_response.status).to eq(200)
expect(stub).to have_been_requested
end
end
context "without CLOUD_SYNC_SECRET" do
it "does not configure webhook app" do
ENV.update(env)
application.initialize!
post "/_flipper"
expect(last_response.status).to eq(404)
end
end
context "without FLIPPER_CLOUD_TOKEN" do
it "gracefully skips configuring webhook app" do
ENV["FLIPPER_CLOUD_TOKEN"] = nil
application.initialize!
expect(silence { Flipper.instance }).to match(/Missing FLIPPER_CLOUD_TOKEN/)
expect(Flipper.instance).to be_a(Flipper::DSL)
post "/_flipper"
expect(last_response.status).to eq(404)
end
end
end
|