summaryrefslogtreecommitdiff
path: root/spec/migrations/migrate_ops_feature_flags_scopes_target_user_ids_spec.rb
blob: fc5d814a2de5f9aa5a76d13277bdbb282576e8b1 (plain)
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
# frozen_string_literal: true

require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20191118211629_migrate_ops_feature_flags_scopes_target_user_ids.rb')

describe MigrateOpsFeatureFlagsScopesTargetUserIds do
  let(:namespaces) { table(:namespaces) }
  let(:projects)   { table(:projects) }
  let(:flags)      { table(:operations_feature_flags) }
  let(:scopes)     { table(:operations_feature_flag_scopes) }

  def setup
    namespace = namespaces.create!(name: 'foo', path: 'foo')
    project = projects.create!(namespace_id: namespace.id)
    flag = flags.create!(project_id: project.id, active: true, name: 'test_flag')

    flag
  end

  it 'migrates successfully when there are no scopes in the database' do
    setup

    disable_migrations_output { migrate! }

    expect(scopes.count).to eq(0)
  end

  it 'migrates a disabled scope with gradualRolloutUserId and userWithId strategies' do
    flag = setup
    scope = scopes.create!(feature_flag_id: flag.id, active: false, strategies: [
      { name: 'gradualRolloutUserId', parameters: { groupId: 'default', percentage: '50' } },
      { name: 'userWithId', parameters: { userIds: '5' } }
    ])

    disable_migrations_output { migrate! }

    scope.reload
    expect(scope.active).to eq(true)
    expect(scope.strategies).to eq([{ 'name' => 'userWithId', 'parameters' => { 'userIds' => '5' } }])
  end

  it 'migrates a disabled scope with default and userWithId strategies' do
    flag = setup
    scope = scopes.create!(feature_flag_id: flag.id, active: false, strategies: [
      { name: 'default', parameters: {} },
      { name: 'userWithId', parameters: { userIds: 'amy@gmail.com,karen@gmail.com' } }
    ])

    disable_migrations_output { migrate! }

    scope.reload
    expect(scope.active).to eq(true)
    expect(scope.strategies).to eq([{ 'name' => 'userWithId', 'parameters' => { 'userIds' => 'amy@gmail.com,karen@gmail.com' } }])
  end

  it 'migrates an enabled scope with default and userWithId strategies' do
    flag = setup
    scope = scopes.create!(feature_flag_id: flag.id, active: true, strategies: [
      { name: 'default', parameters: {} },
      { name: 'userWithId', parameters: { userIds: 'tim' } }
    ])

    disable_migrations_output { migrate! }

    scope.reload
    expect(scope.active).to eq(true)
    expect(scope.strategies).to eq([{ 'name' => 'default', 'parameters' => {} }])
  end

  it 'does not alter an enabled scope with gradualRolloutUserId and userWithId strategies' do
    flag = setup
    scope = scopes.create!(feature_flag_id: flag.id, active: true, strategies: [
      { name: 'gradualRolloutUserId', parameters: { groupId: 'default', percentage: '50' } },
      { name: 'userWithId', parameters: { userIds: '5' } }
    ])

    disable_migrations_output { migrate! }

    scope.reload
    expect(scope.active).to eq(true)
    expect(scope.strategies).to eq([
      { 'name' => 'gradualRolloutUserId', 'parameters' => { 'groupId' => 'default', 'percentage' => '50' } },
      { 'name' => 'userWithId', 'parameters' => { 'userIds' => '5' } }
    ])
  end

  it 'does not alter a disabled scope without a userWithId strategy' do
    flag = setup
    scope = scopes.create!(feature_flag_id: flag.id, active: false, strategies: [
      { name: 'gradualRolloutUserId', parameters: { percentage: '60' } }
    ])

    disable_migrations_output { migrate! }

    scope.reload
    expect(scope.active).to eq(false)
    expect(scope.strategies).to eq([
      { 'name' => 'gradualRolloutUserId', 'parameters' => { 'percentage' => '60' } }
    ])
  end

  it 'does not alter an enabled scope without a userWithId strategy' do
    flag = setup
    scope = scopes.create!(feature_flag_id: flag.id, active: true, strategies: [
      { name: 'default', parameters: {} }
    ])

    disable_migrations_output { migrate! }

    scope.reload
    expect(scope.active).to eq(true)
    expect(scope.strategies).to eq([
      { 'name' => 'default', 'parameters' => {} }
    ])
  end

  it 'migrates multiple scopes' do
    flag = setup
    scope_a = scopes.create!(feature_flag_id: flag.id, active: false, strategies: [
      { name: 'gradualRolloutUserId', parameters: { groupId: 'default', percentage: '50' } },
      { name: 'userWithId', parameters: { userIds: '5,6,7' } }
    ])
    scope_b = scopes.create!(feature_flag_id: flag.id, active: false, environment_scope: 'production', strategies: [
      { name: 'default', parameters: {} },
      { name: 'userWithId', parameters: { userIds: 'lisa,carol' } }
    ])

    disable_migrations_output { migrate! }

    scope_a.reload
    scope_b.reload
    expect(scope_a.active).to eq(true)
    expect(scope_a.strategies).to eq([{ 'name' => 'userWithId', 'parameters' => { 'userIds' => '5,6,7' } }])
    expect(scope_b.active).to eq(true)
    expect(scope_b.strategies).to eq([{ 'name' => 'userWithId', 'parameters' => { 'userIds' => 'lisa,carol' } }])
  end
end