|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative '../lib/bullet_train' |
| 4 | +require 'ostruct' |
| 5 | +require 'json' |
| 6 | + |
| 7 | +describe BulletTrain do |
| 8 | + let(:mock_faraday) { double(Faraday) } |
| 9 | + let(:mock_api_key) { 'ASDFIEVNQWEPARJ' } |
| 10 | + let(:mock_api_url) { 'http://mock.bullet-train.io/api/' } |
| 11 | + let(:user_id) { '[email protected]' } |
| 12 | + let(:api_flags_response) { File.read('spec/fixtures/GET_flags.json') } |
| 13 | + let(:api_identities_response) { File.read('spec/fixtures/GET_identities_user.json') } |
| 14 | + let(:flags_response) { OpenStruct.new(body: JSON.parse(api_flags_response)) } |
| 15 | + let(:identities_response) { OpenStruct.new(body: JSON.parse(api_identities_response)) } |
| 16 | + |
| 17 | + before do |
| 18 | + allow(Faraday).to receive(:new).with(url: mock_api_url).and_return(mock_faraday) |
| 19 | + allow(mock_faraday).to receive(:get).with('flags/').and_return(flags_response) |
| 20 | + allow(mock_faraday).to receive(:get).with("identities/?identifier=#{user_id}").and_return(identities_response) |
| 21 | + end |
| 22 | + subject { BulletTrain.new(api_key: mock_api_key, url: mock_api_url) } |
| 23 | + |
| 24 | + describe '#get_flags' do |
| 25 | + it 'should return all flags without a segment' do |
| 26 | + expect(flags_response.body.length).to eq(4) |
| 27 | + flags = subject.get_flags(nil) |
| 28 | + expect(flags.length).to eq(3) |
| 29 | + expect(flags['feature_three'][:enabled]).to eq(false) |
| 30 | + end |
| 31 | + |
| 32 | + context 'with user_id' do |
| 33 | + it 'should return all flags adjusted for segments' do |
| 34 | + flags = subject.get_flags(user_id) |
| 35 | + expect(flags.length).to eq(3) |
| 36 | + expect(flags['feature_three'][:enabled]).to eq(true) |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + describe '#feature_enabled?' do |
| 42 | + it 'checks a specific feature' do |
| 43 | + expect(subject.feature_enabled?(:feature_one)).to eq(false) |
| 44 | + expect(subject.feature_enabled?('feature_two')).to eq(true) |
| 45 | + expect(subject.feature_enabled?('Feature_THREE')).to eq(false) |
| 46 | + end |
| 47 | + |
| 48 | + context 'with user_id' do |
| 49 | + it 'checks a specific feature for a given user' do |
| 50 | + expect(subject.feature_enabled?(:feature_one, user_id)).to eq(false) |
| 51 | + expect(subject.feature_enabled?('feature_two', user_id)).to eq(true) |
| 52 | + expect(subject.feature_enabled?(:feature_three, user_id)).to eq(true) |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + describe '#get_value' do |
| 58 | + it 'returns a value from a key' do |
| 59 | + expect(subject.get_value(:feature_one)).to be_nil |
| 60 | + expect(subject.get_value(:feature_two)).to eq(42) |
| 61 | + expect(subject.get_value(:feature_three)).to be_nil |
| 62 | + end |
| 63 | + |
| 64 | + context 'with user_id' do |
| 65 | + it 'returns a value for that user' do |
| 66 | + expect(subject.get_value(:feature_one, user_id)).to be_nil |
| 67 | + expect(subject.get_value(:feature_two, user_id)).to eq(42) |
| 68 | + expect(subject.get_value(:feature_three, user_id)).to eq(7) |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + describe '#set_trait' do |
| 74 | + let(:trait_key) { 'foo' } |
| 75 | + let(:trait_value) { 'bar' } |
| 76 | + let(:post_body) do |
| 77 | + { |
| 78 | + identity: { identifier: user_id }, |
| 79 | + trait_key: subject.normalize_key(trait_key), |
| 80 | + trait_value: trait_value |
| 81 | + }.to_json |
| 82 | + end |
| 83 | + |
| 84 | + it 'sets a trait for a given user' do |
| 85 | + trait_response = OpenStruct.new(body: {}) |
| 86 | + expect(mock_faraday).to receive(:post).with('traits/', post_body).and_return(trait_response) |
| 87 | + subject.set_trait user_id, trait_key, trait_value |
| 88 | + end |
| 89 | + |
| 90 | + it 'errors if user_id.nil?' do |
| 91 | + expect { subject.set_trait nil, trait_key, trait_value }.to raise_error(StandardError) |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + describe '#get_traits' do |
| 96 | + it 'returns hash of traits for a given user' do |
| 97 | + traits = subject.get_traits(user_id) |
| 98 | + expect(traits['roles']).to eq(%w[admin staff].to_json) |
| 99 | + expect(traits.length).to eq(2) |
| 100 | + end |
| 101 | + it 'returns {} for user_id.nil?' do |
| 102 | + expect(subject.get_traits(nil)).to eq({}) |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + describe '#normalize_key' do |
| 107 | + it 'returns an empty string given nil' do |
| 108 | + expect(subject.normalize_key(nil)).to eq('') |
| 109 | + end |
| 110 | + |
| 111 | + it 'returns lower case string given a symbol' do |
| 112 | + expect(subject.normalize_key(:key_value)).to eq('key_value') |
| 113 | + end |
| 114 | + |
| 115 | + it 'returns lower case string given a mixed case string' do |
| 116 | + expect(subject.normalize_key('KEY_VaLuE')).to eq('key_value') |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + describe 'maintain backward compatibility with non-idiomatic ruby' do |
| 121 | + it 'aliases hasFeature' do |
| 122 | + expect(subject.method(:hasFeature)).to eq(subject.method(:feature_enabled?)) |
| 123 | + end |
| 124 | + |
| 125 | + it 'aliases getValue' do |
| 126 | + expect(subject.method(:getValue)).to eq(subject.method(:get_value)) |
| 127 | + end |
| 128 | + |
| 129 | + it 'aliases getFlags'do |
| 130 | + expect(subject.method(:getFlags)).to eq(subject.method(:get_flags)) |
| 131 | + end |
| 132 | + |
| 133 | + it 'aliases getFlagsForUser'do |
| 134 | + expect(subject.method(:getFlagsForUser)).to eq(subject.method(:get_flags)) |
| 135 | + end |
| 136 | + end |
| 137 | +end |
0 commit comments