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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
require 'flipper/cloud/configuration'
require 'flipper/adapters/instrumented'
RSpec.describe Flipper::Cloud::Configuration do
let(:required_options) do
{ token: "asdf" }
end
it "can set token" do
instance = described_class.new(required_options)
expect(instance.token).to eq(required_options[:token])
end
it "can set token from ENV var" do
ENV["FLIPPER_CLOUD_TOKEN"] = "from_env"
instance = described_class.new(required_options.reject { |k, v| k == :token })
expect(instance.token).to eq("from_env")
end
it "can set instrumenter" do
instrumenter = Object.new
instance = described_class.new(required_options.merge(instrumenter: instrumenter))
expect(instance.instrumenter).to be(instrumenter)
end
it "can set read_timeout" do
instance = described_class.new(required_options.merge(read_timeout: 5))
expect(instance.read_timeout).to eq(5)
end
it "can set read_timeout from ENV var" do
ENV["FLIPPER_CLOUD_READ_TIMEOUT"] = "9"
instance = described_class.new(required_options.reject { |k, v| k == :read_timeout })
expect(instance.read_timeout).to eq(9)
end
it "can set open_timeout" do
instance = described_class.new(required_options.merge(open_timeout: 5))
expect(instance.open_timeout).to eq(5)
end
it "can set open_timeout from ENV var" do
ENV["FLIPPER_CLOUD_OPEN_TIMEOUT"] = "9"
instance = described_class.new(required_options.reject { |k, v| k == :open_timeout })
expect(instance.open_timeout).to eq(9)
end
it "can set write_timeout" do
instance = described_class.new(required_options.merge(write_timeout: 5))
expect(instance.write_timeout).to eq(5)
end
it "can set write_timeout from ENV var" do
ENV["FLIPPER_CLOUD_WRITE_TIMEOUT"] = "9"
instance = described_class.new(required_options.reject { |k, v| k == :write_timeout })
expect(instance.write_timeout).to eq(9)
end
it "can set sync_interval" do
instance = described_class.new(required_options.merge(sync_interval: 1))
expect(instance.sync_interval).to eq(1)
end
it "can set sync_interval from ENV var" do
ENV["FLIPPER_CLOUD_SYNC_INTERVAL"] = "5"
instance = described_class.new(required_options.reject { |k, v| k == :sync_interval })
expect(instance.sync_interval).to eq(5)
end
it "passes sync_interval into sync adapter" do
# The initial sync of http to local invokes this web request.
stub_request(:get, /flippercloud\.io/).to_return(status: 200, body: "{}")
instance = described_class.new(required_options.merge(sync_interval: 1))
poller = instance.send(:poller)
expect(poller.interval).to eq(1)
end
it "can set debug_output" do
instance = described_class.new(required_options.merge(debug_output: STDOUT))
expect(instance.debug_output).to eq(STDOUT)
end
it "defaults adapter block" do
# The initial sync of http to local invokes this web request.
stub_request(:get, /flippercloud\.io/).to_return(status: 200, body: "{}")
instance = described_class.new(required_options)
expect(instance.adapter).to be_instance_of(Flipper::Adapters::Poll)
end
it "can override adapter block" do
# The initial sync of http to local invokes this web request.
stub_request(:get, /flippercloud\.io/).to_return(status: 200, body: "{}")
instance = described_class.new(required_options)
instance.adapter do |adapter|
Flipper::Adapters::Instrumented.new(adapter)
end
expect(instance.adapter).to be_instance_of(Flipper::Adapters::Instrumented)
end
it "defaults url" do
instance = described_class.new(required_options.reject { |k, v| k == :url })
expect(instance.url).to eq("https://www.flippercloud.io/adapter")
end
it "can override url using options" do
options = required_options.merge(url: "http://localhost:5000/adapter")
instance = described_class.new(options)
expect(instance.url).to eq("http://localhost:5000/adapter")
instance = described_class.new(required_options)
instance.url = "http://localhost:5000/adapter"
expect(instance.url).to eq("http://localhost:5000/adapter")
end
it "can override URL using ENV var" do
ENV["FLIPPER_CLOUD_URL"] = "https://example.com"
instance = described_class.new(required_options.reject { |k, v| k == :url })
expect(instance.url).to eq("https://example.com")
end
it "defaults sync_method to :poll" do
instance = described_class.new(required_options)
expect(instance.sync_method).to eq(:poll)
end
it "sets sync_method to :webhook if sync_secret provided" do
instance = described_class.new(required_options.merge({
sync_secret: "secret",
}))
expect(instance.sync_method).to eq(:webhook)
expect(instance.adapter).to be_instance_of(Flipper::Adapters::DualWrite)
end
it "sets sync_method to :webhook if FLIPPER_CLOUD_SYNC_SECRET set" do
ENV["FLIPPER_CLOUD_SYNC_SECRET"] = "abc"
instance = described_class.new(required_options)
expect(instance.sync_method).to eq(:webhook)
expect(instance.adapter).to be_instance_of(Flipper::Adapters::DualWrite)
end
it "can set sync_secret" do
instance = described_class.new(required_options.merge(sync_secret: "from_config"))
expect(instance.sync_secret).to eq("from_config")
end
it "can override sync_secret using ENV var" do
ENV["FLIPPER_CLOUD_SYNC_SECRET"] = "from_env"
instance = described_class.new(required_options.reject { |k, v| k == :sync_secret })
expect(instance.sync_secret).to eq("from_env")
end
it "can sync with cloud" do
body = JSON.generate({
"features": [
{
"key": "search",
"state": "on",
"gates": [
{
"key": "boolean",
"name": "boolean",
"value": true
},
{
"key": "groups",
"name": "group",
"value": []
},
{
"key": "actors",
"name": "actor",
"value": []
},
{
"key": "percentage_of_actors",
"name": "percentage_of_actors",
"value": 0
},
{
"key": "percentage_of_time",
"name": "percentage_of_time",
"value": 0
}
]
},
{
"key": "history",
"state": "off",
"gates": [
{
"key": "boolean",
"name": "boolean",
"value": false
},
{
"key": "groups",
"name": "group",
"value": []
},
{
"key": "actors",
"name": "actor",
"value": []
},
{
"key": "percentage_of_actors",
"name": "percentage_of_actors",
"value": 0
},
{
"key": "percentage_of_time",
"name": "percentage_of_time",
"value": 0
}
]
}
]
})
stub = stub_request(:get, "https://www.flippercloud.io/adapter/features").
with({
headers: {
'Flipper-Cloud-Token'=>'asdf',
},
}).to_return(status: 200, body: body, headers: {})
instance = described_class.new(required_options)
instance.sync
# Check that remote was fetched.
expect(stub).to have_been_requested
# Check that local adapter really did sync.
local_adapter = instance.local_adapter
all = local_adapter.get_all
expect(all.keys).to eq(["search", "history"])
expect(all["search"][:boolean]).to eq("true")
expect(all["history"][:boolean]).to eq(nil)
end
it "can setup brow to report events to cloud" do
# skip logging brow
Brow.logger = Logger.new(File::NULL)
brow = described_class.new(required_options).brow
stub = stub_request(:post, "https://www.flippercloud.io/adapter/events")
.with { |request|
data = JSON.parse(request.body)
data.keys == ["uuid", "messages"] && data["messages"] == [{"n" => 1}]
}
.to_return(status: 201, body: "{}", headers: {})
brow.push({"n" => 1})
brow.worker.stop
expect(stub).to have_been_requested.times(1)
end
end
|