|
3 | 3 | RSpec.describe Api::V1::SplitConfigsController, type: :controller do |
4 | 4 | let(:default_app) { FactoryGirl.create :app, name: "default_app", auth_secret: "6Sd6T7T6Q8hKcoo0t8CTzV0IdN1EEHqXB2Ig4raZsOU" } |
5 | 5 |
|
6 | | - before do |
7 | | - http_authenticate username: default_app.name, auth_secret: default_app.auth_secret |
8 | | - end |
9 | | - |
10 | 6 | describe '#create' do |
11 | | - it "creates a new split with desired weightings" do |
| 7 | + it "doesn't create when unauthenticated" do |
12 | 8 | post :create, name: 'foobar', weighting_registry: { foo: 10, bar: 90 } |
13 | 9 |
|
14 | | - expect(response).to have_http_status(:no_content) |
15 | | - split = Split.find_by(name: 'foobar', owner_app: default_app) |
16 | | - expect(split).to be_truthy |
17 | | - expect(split.registry).to eq 'foo' => 10, 'bar' => 90 |
| 10 | + expect(response).to have_http_status(:unauthorized) |
| 11 | + expect(Split.where(name: 'foobar')).to be_empty |
18 | 12 | end |
| 13 | + end |
19 | 14 |
|
20 | | - it 'returns errors when invalid' do |
21 | | - post :create, name: 'fooBar', weighting_registry: { foo: 10, bar: 90 } |
22 | | - expect(response).to have_http_status(:unprocessable_entity) |
23 | | - expect(response_json['errors']['name'].first).to include("snake_case") |
| 15 | + describe "while authenticated" do |
| 16 | + before do |
| 17 | + http_authenticate username: default_app.name, auth_secret: default_app.auth_secret |
24 | 18 | end |
25 | | - end |
26 | 19 |
|
27 | | - describe '#destroy' do |
28 | | - it "sets the finished_at time on the split" do |
29 | | - split = FactoryGirl.create(:split, name: "old_split", owner_app: default_app, finished_at: nil) |
| 20 | + describe '#create' do |
| 21 | + it "creates a new split with desired weightings" do |
| 22 | + post :create, name: 'foobar', weighting_registry: { foo: 10, bar: 90 } |
30 | 23 |
|
31 | | - delete :destroy, id: "old_split" |
| 24 | + expect(response).to have_http_status(:no_content) |
| 25 | + split = Split.find_by(name: 'foobar', owner_app: default_app) |
| 26 | + expect(split).to be_truthy |
| 27 | + expect(split.registry).to eq 'foo' => 10, 'bar' => 90 |
| 28 | + end |
32 | 29 |
|
33 | | - expect(response).to have_http_status(:no_content) |
34 | | - expect(split.reload).to be_finished |
| 30 | + it 'returns errors when invalid' do |
| 31 | + post :create, name: 'fooBar', weighting_registry: { foo: 10, bar: 90 } |
| 32 | + expect(response).to have_http_status(:unprocessable_entity) |
| 33 | + expect(response_json['errors']['name'].first).to include("snake_case") |
| 34 | + end |
35 | 35 | end |
36 | 36 |
|
37 | | - it "is idempotent" do |
38 | | - split = FactoryGirl.create(:split, name: "old_split", owner_app: default_app, finished_at: nil) |
| 37 | + describe '#destroy' do |
| 38 | + it "sets the finished_at time on the split" do |
| 39 | + split = FactoryGirl.create(:split, name: "old_split", owner_app: default_app, finished_at: nil) |
39 | 40 |
|
40 | | - Timecop.freeze(Time.zone.parse('2011-01-01')) do |
41 | 41 | delete :destroy, id: "old_split" |
| 42 | + |
| 43 | + expect(response).to have_http_status(:no_content) |
| 44 | + expect(split.reload).to be_finished |
42 | 45 | end |
43 | 46 |
|
44 | | - expect(response).to have_http_status(:no_content) |
45 | | - expect(split.reload.finished_at).to eq Time.zone.parse('2011-01-01') |
| 47 | + it "can't delete another app's split" do |
| 48 | + other_app = FactoryGirl.create :app, name: "other_app" |
| 49 | + split = FactoryGirl.create(:split, name: "other_split", owner_app: other_app, finished_at: nil) |
46 | 50 |
|
47 | | - Timecop.freeze(Time.zone.parse('2011-01-02')) do |
48 | | - delete :destroy, id: "old_split" |
| 51 | + expect { delete :destroy, id: "other_split" }.to raise_error(ActiveRecord::RecordNotFound) |
| 52 | + |
| 53 | + expect(split.reload).not_to be_finished |
49 | 54 | end |
50 | 55 |
|
51 | | - expect(response).to have_http_status(:no_content) |
52 | | - expect(split.reload.finished_at).to eq Time.zone.parse('2011-01-01') |
| 56 | + it "is idempotent" do |
| 57 | + split = FactoryGirl.create(:split, name: "old_split", owner_app: default_app, finished_at: nil) |
| 58 | + |
| 59 | + Timecop.freeze(Time.zone.parse('2011-01-01')) do |
| 60 | + delete :destroy, id: "old_split" |
| 61 | + end |
| 62 | + |
| 63 | + expect(response).to have_http_status(:no_content) |
| 64 | + expect(split.reload.finished_at).to eq Time.zone.parse('2011-01-01') |
| 65 | + |
| 66 | + Timecop.freeze(Time.zone.parse('2011-01-02')) do |
| 67 | + delete :destroy, id: "old_split" |
| 68 | + end |
| 69 | + |
| 70 | + expect(response).to have_http_status(:no_content) |
| 71 | + expect(split.reload.finished_at).to eq Time.zone.parse('2011-01-01') |
| 72 | + end |
53 | 73 | end |
54 | 74 | end |
55 | 75 | end |
0 commit comments