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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
|
require "spec_helper"
RSpec.describe "Rollout" do
before do
@rollout = Rollout.new(Redis.current)
end
describe "when a group is activated" do
before do
@rollout.define_group(:fivesonly) { |user| user.id == 5 }
@rollout.activate_group(:chat, :fivesonly)
end
it "the feature is active for users for which the block evaluates to true" do
expect(@rollout).to be_active(:chat, double(id: 5))
end
it "is not active for users for which the block evaluates to false" do
expect(@rollout).not_to be_active(:chat, double(id: 1))
end
it "is not active if a group is found in Redis but not defined in Rollout" do
@rollout.activate_group(:chat, :fake)
expect(@rollout).not_to be_active(:chat, double(id: 1))
end
end
describe "the default all group" do
before do
@rollout.activate_group(:chat, :all)
end
it "evaluates to true no matter what" do
expect(@rollout).to be_active(:chat, double(id: 0))
end
end
describe "deactivating a group" do
before do
@rollout.define_group(:fivesonly) { |user| user.id == 5 }
@rollout.activate_group(:chat, :all)
@rollout.activate_group(:chat, :some)
@rollout.activate_group(:chat, :fivesonly)
@rollout.deactivate_group(:chat, :all)
@rollout.deactivate_group(:chat, "some")
end
it "deactivates the rules for that group" do
expect(@rollout).not_to be_active(:chat, double(id: 10))
end
it "leaves the other groups active" do
expect(@rollout.get(:chat).groups).to eq [:fivesonly]
end
it "leaves the other groups active using sets" do
@options = @rollout.instance_variable_get("@options")
@options[:use_sets] = true
expect(@rollout.get(:chat).groups).to eq [:fivesonly].to_set
end
end
describe "deactivating a feature completely" do
before do
@rollout.define_group(:fivesonly) { |user| user.id == 5 }
@rollout.activate_group(:chat, :all)
@rollout.activate_group(:chat, :fivesonly)
@rollout.activate_user(:chat, double(id: 51))
@rollout.activate_percentage(:chat, 100)
@rollout.activate(:chat)
@rollout.deactivate(:chat)
end
it "removes all of the groups" do
expect(@rollout).not_to be_active(:chat, double(id: 0))
end
it "removes all of the users" do
expect(@rollout).not_to be_active(:chat, double(id: 51))
end
it "removes the percentage" do
expect(@rollout).not_to be_active(:chat, double(id: 24))
end
it "removes globally" do
expect(@rollout).not_to be_active(:chat)
end
end
describe "activating a specific user" do
before do
@rollout.activate_user(:chat, double(id: 42))
end
it "is active for that user" do
expect(@rollout).to be_active(:chat, double(id: 42))
end
it "remains inactive for other users" do
expect(@rollout).not_to be_active(:chat, double(id: 24))
end
end
describe "activating a specific user by ID" do
before do
@rollout.activate_user(:chat, 42)
end
it "is active for that user" do
expect(@rollout).to be_active(:chat, double(id: 42))
end
it "remains inactive for other users" do
expect(@rollout).not_to be_active(:chat, double(id: 24))
end
end
describe "activating a specific user with a string id" do
before do
@rollout.activate_user(:chat, double(id: "user-72"))
end
it "is active for that user" do
expect(@rollout).to be_active(:chat, double(id: "user-72"))
end
it "remains inactive for other users" do
expect(@rollout).not_to be_active(:chat, double(id: "user-12"))
end
end
describe "activating a group of users" do
context "specified by user objects" do
let(:users) { [double(id: 1), double(id: 2), double(id: 3)] }
before { @rollout.activate_users(:chat, users) }
it "is active for the given users" do
users.each { |user| expect(@rollout).to be_active(:chat, user) }
end
it "remains inactive for other users" do
expect(@rollout).not_to be_active(:chat, double(id: 4))
end
end
context "specified by user ids" do
let(:users) { [1, 2, 3] }
before { @rollout.activate_users(:chat, users) }
it "is active for the given users" do
users.each { |user| expect(@rollout).to be_active(:chat, user) }
end
it "remains inactive for other users" do
expect(@rollout).not_to be_active(:chat, 4)
end
end
end
describe "deactivating a specific user" do
before do
@rollout.activate_user(:chat, double(id: 42))
@rollout.activate_user(:chat, double(id: 4242))
@rollout.activate_user(:chat, double(id: 24))
@rollout.deactivate_user(:chat, double(id: 42))
@rollout.deactivate_user(:chat, double(id: "4242"))
end
it "that user should no longer be active" do
expect(@rollout).not_to be_active(:chat, double(id: 42))
end
it "remains active for other active users" do
@options = @rollout.instance_variable_get("@options")
@options[:use_sets] = false
expect(@rollout.get(:chat).users).to eq %w(24)
end
it "remains active for other active users using sets" do
@options = @rollout.instance_variable_get("@options")
@options[:use_sets] = true
expect(@rollout.get(:chat).users).to eq %w(24).to_set
end
end
describe "deactivating a group of users" do
context "specified by user objects" do
let(:active_users) { [double(id: 1), double(id: 2)] }
let(:inactive_users) { [double(id: 3), double(id: 4)] }
before do
@rollout.activate_users(:chat, active_users + inactive_users)
@rollout.deactivate_users(:chat, inactive_users)
end
it "is active for the active users" do
active_users.each { |user| expect(@rollout).to be_active(:chat, user) }
end
it "is not active for inactive users" do
inactive_users.each { |user| expect(@rollout).not_to be_active(:chat, user) }
end
end
context "specified by user ids" do
let(:active_users) { [1, 2] }
let(:inactive_users) { [3, 4] }
before do
@rollout.activate_users(:chat, active_users + inactive_users)
@rollout.deactivate_users(:chat, inactive_users)
end
it "is active for the active users" do
active_users.each { |user| expect(@rollout).to be_active(:chat, user) }
end
it "is not active for inactive users" do
inactive_users.each { |user| expect(@rollout).not_to be_active(:chat, user) }
end
end
end
describe 'set a group of users' do
it 'should replace the users with the given array' do
users = %w(1 2 3 4)
@rollout.activate_users(:chat, %w(10 20 30))
@rollout.set_users(:chat, users)
expect(@rollout.get(:chat).users).to eq(users)
end
end
describe "activating a feature globally" do
before do
@rollout.activate(:chat)
end
it "activates the feature" do
expect(@rollout).to be_active(:chat)
end
it "sets @data to empty hash" do
expect(@rollout.get(:chat).data).to eq({})
end
end
describe "activating a feature for a percentage of users" do
before do
@rollout.activate_percentage(:chat, 20)
end
it "activates the feature for that percentage of the users" do
expect((1..100).select { |id| @rollout.active?(:chat, double(id: id)) }.length).to be_within(2).of(20)
end
end
describe "activating a feature for a percentage of users" do
before do
@rollout.activate_percentage(:chat, 20)
end
it "activates the feature for that percentage of the users" do
expect((1..200).select { |id| @rollout.active?(:chat, double(id: id)) }.length).to be_within(4).of(40)
end
end
describe "activating a feature for a percentage of users" do
before do
@rollout.activate_percentage(:chat, 5)
end
it "activates the feature for that percentage of the users" do
expect((1..100).select { |id| @rollout.active?(:chat, double(id: id)) }.length).to be_within(2).of(5)
end
end
describe "activating a feature for a percentage of users" do
before do
@rollout.activate_percentage(:chat, 0.1)
end
it "activates the feature for that percentage of the users" do
expect((1..10_000).to_set.select { |id| @rollout.active?(:chat, double(id: id)) }.length).to be_within(2).of(10)
end
end
describe "activating a feature for a percentage of users" do
before do
@rollout.activate_percentage(:chat, 20)
@rollout.activate_percentage(:beta, 20)
@options = @rollout.instance_variable_get("@options")
end
it "activates the feature for a random set of users when opt is set" do
@options[:randomize_percentage] = true
chat_users = (1..100).select { |id| @rollout.active?(:chat, double(id: id)) }
beta_users = (1..100).select { |id| @rollout.active?(:beta, double(id: id)) }
expect(chat_users).not_to eq beta_users
end
it "activates the feature for the same set of users when opt is not set" do
@options[:randomize_percentage] = false
chat_users = (1..100).select { |id| @rollout.active?(:chat, double(id: id)) }
beta_users = (1..100).select { |id| @rollout.active?(:beta, double(id: id)) }
expect(chat_users).to eq beta_users
end
end
describe "activating a feature for a group as a string" do
before do
@rollout.define_group(:admins) { |user| user.id == 5 }
@rollout.activate_group(:chat, "admins")
end
it "the feature is active for users for which the block evaluates to true" do
expect(@rollout).to be_active(:chat, double(id: 5))
end
it "is not active for users for which the block evaluates to false" do
expect(@rollout).not_to be_active(:chat, double(id: 1))
end
end
describe "deactivating the percentage of users" do
before do
@rollout.activate_percentage(:chat, 100)
@rollout.deactivate_percentage(:chat)
end
it "becomes inactivate for all users" do
expect(@rollout).not_to be_active(:chat, double(id: 24))
end
end
describe "deactivating the feature globally" do
before do
@rollout.activate(:chat)
@rollout.deactivate(:chat)
end
it "becomes inactivate" do
expect(@rollout).not_to be_active(:chat)
end
end
describe "setting a feature on" do
before do
@rollout.set(:chat, true)
end
it "becomes activated" do
expect(@rollout).to be_active(:chat)
end
end
describe "setting a feature off" do
before do
@rollout.set(:chat, false)
end
it "becomes inactivated" do
expect(@rollout).not_to be_active(:chat)
end
end
describe "deleting a feature" do
before do
@rollout.set(:chat, true)
end
context "when feature was passed as string" do
it "should be removed from features list" do
expect(@rollout.features.size).to eq 1
@rollout.delete('chat')
expect(@rollout.features.size).to eq 0
end
end
it "should be removed from features list" do
expect(@rollout.features.size).to eq 1
@rollout.delete(:chat)
expect(@rollout.features.size).to eq 0
end
it "should have metadata cleared" do
expect(@rollout.get(:chat).percentage).to eq 100
@rollout.delete(:chat)
expect(@rollout.get(:chat).percentage).to eq 0
end
end
describe "keeps a list of features" do
it "saves the feature" do
@rollout.activate(:chat)
expect(@rollout.features).to be_include(:chat)
end
it "does not contain doubles" do
@rollout.activate(:chat)
@rollout.activate(:chat)
expect(@rollout.features.size).to eq(1)
end
it "does not contain doubles when using string" do
@rollout.activate(:chat)
@rollout.activate("chat")
expect(@rollout.features.size).to eq(1)
end
end
describe "#get" do
before do
@rollout.activate_percentage(:chat, 10)
@rollout.activate_group(:chat, :caretakers)
@rollout.activate_group(:chat, :greeters)
@rollout.activate(:signup)
@rollout.activate_user(:chat, double(id: 42))
end
it "returns the feature object" do
feature = @rollout.get(:chat)
expect(feature.groups).to eq [:caretakers, :greeters]
expect(feature.percentage).to eq 10
expect(feature.users).to eq %w(42)
expect(feature.to_hash).to eq(
groups: [:caretakers, :greeters],
percentage: 10,
users: %w(42),
data: {},
)
feature = @rollout.get(:signup)
expect(feature.groups).to be_empty
expect(feature.users).to be_empty
expect(feature.percentage).to eq(100)
end
it "returns the feature objects using sets" do
@options = @rollout.instance_variable_get("@options")
@options[:use_sets] = true
feature = @rollout.get(:chat)
expect(feature.groups).to eq [:caretakers, :greeters].to_set
expect(feature.percentage).to eq 10
expect(feature.users).to eq %w(42).to_set
expect(feature.to_hash).to eq(
groups: [:caretakers, :greeters].to_set,
percentage: 10,
users: %w(42).to_set,
data: {},
)
feature = @rollout.get(:signup)
expect(feature.groups).to be_empty
expect(feature.users).to be_empty
expect(feature.percentage).to eq(100)
end
end
describe "#clear" do
let(:features) { %w(signup beta alpha gm) }
before do
features.each { |f| @rollout.activate(f) }
@rollout.clear!
end
it "each feature is cleared" do
features.each do |feature|
expect(@rollout.get(feature).to_hash).to eq(
percentage: 0,
users: [],
groups: [],
data: {},
)
end
end
it "each feature is cleared with sets" do
@options = @rollout.instance_variable_get("@options")
@options[:use_sets] = true
features.each do |feature|
expect(@rollout.get(feature).to_hash).to eq(
percentage: 0,
users: Set.new,
groups: Set.new,
data: {},
)
end
end
it "removes all features" do
expect(@rollout.features).to be_empty
end
end
describe "#feature_states" do
let(:user_double) { double(id: 7) }
before do
@rollout.activate(:chat)
@rollout.activate_user(:video, user_double)
@rollout.deactivate(:vr)
end
it "returns a hash" do
expect(@rollout.feature_states).to be_a(Hash)
end
context "with user argument" do
it "maps active feature as true" do
state = @rollout.feature_states(user_double)[:video]
expect(state).to eq(true)
end
it "maps inactive feature as false" do
state = @rollout.feature_states[:vr]
expect(state).to eq(false)
end
end
context "with no argument" do
it "maps active feature as true" do
state = @rollout.feature_states[:chat]
expect(state).to eq(true)
end
it "maps inactive feature as false" do
state = @rollout.feature_states[:video]
expect(state).to eq(false)
end
end
end
describe "#active_features" do
let(:user_double) { double(id: 19) }
before do
@rollout.activate(:chat)
@rollout.activate_user(:video, user_double)
@rollout.deactivate(:vr)
end
it "returns an array" do
expect(@rollout.active_features).to be_a(Array)
end
context "with user argument" do
it "includes active feature" do
features = @rollout.active_features(user_double)
expect(features).to include(:video)
expect(features).to include(:chat)
end
it "excludes inactive feature" do
features = @rollout.active_features(user_double)
expect(features).to_not include(:vr)
end
end
context "with no argument" do
it "includes active feature" do
features = @rollout.active_features
expect(features).to include(:chat)
end
it "excludes inactive feature" do
features = @rollout.active_features
expect(features).to_not include(:video)
end
end
end
describe "#user_in_active_users?" do
it "returns true if activated for user" do
@rollout.activate_user(:chat, double(id: 5))
expect(@rollout.user_in_active_users?(:chat, "5")).to eq(true)
end
it "returns false if activated for group" do
@rollout.activate_group(:chat, :all)
expect(@rollout.user_in_active_users?(:chat, "5")).to eq(false)
end
end
describe "#multi_get" do
before do
@rollout.activate_percentage(:chat, 10)
@rollout.activate_group(:chat, :caretakers)
@rollout.activate_group(:videos, :greeters)
@rollout.activate(:signup)
@rollout.activate_user(:photos, double(id: 42))
end
it "returns an array of features" do
features = @rollout.multi_get(:chat, :videos, :signup)
expect(features[0].name).to eq :chat
expect(features[0].groups).to eq [:caretakers]
expect(features[0].percentage).to eq 10
expect(features[1].name).to eq :videos
expect(features[1].groups).to eq [:greeters]
expect(features[2].name).to eq :signup
expect(features[2].percentage).to eq 100
expect(features.size).to eq 3
end
describe 'when given feature keys is empty' do
it 'returns empty array' do
expect(@rollout.multi_get(*[])).to match_array([])
end
end
end
describe "#set_feature_data" do
before do
@rollout.set_feature_data(:chat, description: 'foo', release_date: 'bar')
end
it 'sets the data attribute on feature' do
expect(@rollout.get(:chat).data).to include('description' => 'foo', 'release_date' => 'bar')
end
it 'updates a data attribute' do
@rollout.set_feature_data(:chat, description: 'baz')
expect(@rollout.get(:chat).data).to include('description' => 'baz', 'release_date' => 'bar')
end
it 'only sets data on specified feature' do
@rollout.set_feature_data(:talk, image_url: 'kittens.png')
expect(@rollout.get(:chat).data).not_to include('image_url' => 'kittens.png')
expect(@rollout.get(:chat).data).to include('description' => 'foo', 'release_date' => 'bar')
end
it 'does not modify @data if param is nil' do
expect(@rollout.get(:chat).data).to include('description' => 'foo', 'release_date' => 'bar')
@rollout.set_feature_data(:chat, nil)
expect(@rollout.get(:chat).data).to include('description' => 'foo', 'release_date' => 'bar')
end
it 'does not modify @data if param is empty string' do
expect(@rollout.get(:chat).data).to include('description' => 'foo', 'release_date' => 'bar')
@rollout.set_feature_data(:chat, " ")
expect(@rollout.get(:chat).data).to include('description' => 'foo', 'release_date' => 'bar')
end
it 'properly parses data when it contains a |' do
user = double("User", id: 8)
@rollout.activate_user(:chat, user)
@rollout.set_feature_data(:chat, "|call||text|" => "a|bunch|of|stuff")
expect(@rollout.get(:chat).data).to include("|call||text|" => "a|bunch|of|stuff")
expect(@rollout.active?(:chat, user)).to be true
end
end
describe "#clear_feature_data" do
it 'resets data to empty string' do
@rollout.set_feature_data(:chat, description: 'foo')
expect(@rollout.get(:chat).data).to include('description' => 'foo')
@rollout.clear_feature_data(:chat)
expect(@rollout.get(:chat).data).to eq({})
end
end
describe 'Check if feature exists' do
it 'it should return true if the feature is exist' do
@rollout.activate_percentage(:chat, 1)
expect(@rollout.exists?(:chat)).to be true
end
it 'it should return false if the feature is not exist' do
expect(@rollout.exists?(:chat)).to be false
end
end
end
describe "Rollout::Feature" do
describe "#add_user" do
it "ids a user using id_user_by" do
user = double("User", email: "test@test.com")
feature = Rollout::Feature.new(:chat, nil, id_user_by: :email)
feature.add_user(user)
expect(user).to have_received :email
end
end
describe "#initialize" do
describe "when string does not exist" do
it 'clears feature attributes when string is not given' do
feature = Rollout::Feature.new(:chat)
expect(feature.groups).to be_empty
expect(feature.users).to be_empty
expect(feature.percentage).to eq 0
expect(feature.data).to eq({})
end
it 'clears feature attributes when string is nil' do
feature = Rollout::Feature.new(:chat, nil)
expect(feature.groups).to be_empty
expect(feature.users).to be_empty
expect(feature.percentage).to eq 0
expect(feature.data).to eq({})
end
it 'clears feature attributes when string is empty string' do
feature = Rollout::Feature.new(:chat, "")
expect(feature.groups).to be_empty
expect(feature.users).to be_empty
expect(feature.percentage).to eq 0
expect(feature.data).to eq({})
end
describe "when there is no data" do
it 'sets @data to empty hash' do
feature = Rollout::Feature.new(:chat, "0||")
expect(feature.data).to eq({})
end
it 'sets @data to empty hash' do
feature = Rollout::Feature.new(:chat, "||| ")
expect(feature.data).to eq({})
end
end
end
end
end
|