Skip to content

Commit

Permalink
Merge a63bf97 into 8af0eca
Browse files Browse the repository at this point in the history
  • Loading branch information
dsalahutdinov committed Oct 3, 2018
2 parents 8af0eca + a63bf97 commit 0ae2fa1
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 60 deletions.
10 changes: 10 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
inherit_from: .rubocop_todo.yml

require: rubocop-rspec

AllCops:
Exclude:
- 'spec/dummy/db/**/*.rb'
Expand All @@ -19,3 +21,11 @@ Metrics/AbcSize:
Metrics/MethodLength:
Exclude:
- 'app/controllers/logux_controller.rb'

RSpec/DescribeClass:
Exclude:
- 'spec/logux/tasks/*.rb'
- 'spec/requests/*.rb'

RSpec/ExpectChange:
EnforcedStyle: block
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ GEM
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
rubocop-rspec (1.27.0)
rubocop (>= 0.56.0)
ruby-progressbar (1.9.0)
safe_yaml (1.0.4)
simplecov (0.16.1)
Expand Down Expand Up @@ -229,6 +231,7 @@ DEPENDENCIES
rspec-live_controllers
rspec-rails
rubocop
rubocop-rspec
simplecov
sqlite3
timecop
Expand Down
1 change: 1 addition & 0 deletions logux_rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rspec-live_controllers'
spec.add_development_dependency 'rspec-rails'
spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'rubocop-rspec'
spec.add_development_dependency 'simplecov'
spec.add_development_dependency 'sqlite3'
spec.add_development_dependency 'timecop'
Expand Down
8 changes: 4 additions & 4 deletions spec/logux/action_caller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
let(:meta) { create(:logux_meta) }

describe '#call!' do
subject { action_caller.call! }

it 'raise error' do
expect { subject }.to raise_error(Logux::NoActionError)
expect { action_caller.call! }.to raise_error(Logux::NoActionError)
end

context 'when action defined' do
Expand All @@ -25,14 +23,16 @@ def add
end
end

let(:result) { action_caller.call! }

after do
Actions::User.send :undef_method, :add
Actions.send :remove_const, :User
Actions.send :const_set, :User, Class.new
end

it 'return ok' do
expect(subject.status).to eq(:ok)
expect(result.status).to eq(:ok)
end
end
end
Expand Down
21 changes: 12 additions & 9 deletions spec/logux/action_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
let(:meta) { Logux::Meta.new }

describe '#respond' do
subject { action_controller.respond(:processed) }
let(:response) { action_controller.respond(:processed) }

it 'returns logux response' do
expect(subject.status).to eq(:processed)
expect(subject.action).to eq(action)
expect(subject.meta).to have_key(:time)
expect(subject.custom_data).to be_nil
expect(response).to have_attributes(
status: :processed, action: action, custom_data: nil
)
end

it 'sets the meta with time' do
expect(response.meta).to have_key(:time)
end
end

describe '.verify_authorized!' do
subject { described_class.verify_authorized! }
let(:verify_authorized!) { described_class.verify_authorized! }

around do |example|
Logux.configuration.verify_authorized = false
Expand All @@ -29,20 +32,20 @@
end

it 'sets to true' do
expect { subject }
expect { verify_authorized! }
.to change { Logux.configuration.verify_authorized }
.from(false)
.to(true)
end
end

describe '.unverify_authorized!' do
subject { described_class.unverify_authorized! }
let(:unverify_authorized!) { described_class.unverify_authorized! }

before { Logux.configuration.verify_authorized = true }

it 'sets to false' do
expect { subject }
expect { unverify_authorized! }
.to change { Logux.configuration.verify_authorized }
.from(true)
.to(false)
Expand Down
8 changes: 4 additions & 4 deletions spec/logux/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
describe '#action_type' do
subject { actions.action_type }

it { should eq 'add' }
it { is_expected.to eq 'add' }
end

describe '#action_name' do
subject { actions.action_name }

it { should eq 'user' }
it { is_expected.to eq 'user' }
end

describe '#channel_name' do
subject { actions.channel_name }

it { should eq 'project' }
it { is_expected.to eq 'project' }
end

describe '#channel_id' do
subject { actions.channel_id }

it { should eq '123' }
it { is_expected.to eq '123' }
end
end
4 changes: 1 addition & 3 deletions spec/logux/add_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
let(:request) { described_class.new }

describe '#call' do
subject { request.call(data, meta: meta) }

let(:data) { [{ id: 1 }, { id: 2 }] }
let(:meta) { create(:logux_meta) }
let(:logux_request) do
Expand All @@ -19,7 +17,7 @@
end

it 'return processed' do
expect { subject }.to send_to_logux(logux_request)
expect { request.call(data, meta: meta) }.to send_to_logux(logux_request)
end
end
end
4 changes: 2 additions & 2 deletions spec/logux/channel_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
let(:meta) { Logux::Meta.new }

describe '#subscribe' do
subject { channel_controller.subscribe }
let(:subscribe) { channel_controller.subscribe }

context 'when ActiveRecord defined' do
it 'tries to find record by chanel data' do
expect { subject }.to send_to_logux
expect { subscribe }.to send_to_logux
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/logux/class_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
let(:finder) { described_class.new(params) }

describe '#class_name' do
subject { finder.class_name }
let(:class_name) { finder.class_name }

let(:params) { create(:logux_actions_add, type: 'test/test/name/try/user/add') }

it 'returns nested classes' do
expect(subject).to eq('Test::Test::Name::Try::User')
expect(class_name).to eq('Test::Test::Name::Try::User')
end
end

describe '#find_policy_class' do
subject { finder.find_policy_class }
let(:policy_class) { finder.find_policy_class }

let(:params) { create(:logux_actions_add, type: 'test/test/name/try/user/add') }

it 'raise an error' do
expect { subject }.to raise_error(Logux::NoPolicyError)
expect { policy_class }.to raise_error(Logux::NoPolicyError)
end
end
end
4 changes: 1 addition & 3 deletions spec/logux/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
let(:params) { create(:logux_actions_add) }

describe '#post' do
subject { client.post(params) }

it 'performs request' do
expect { subject }.to send_to_logux(params)
expect { client.post(params) }.to send_to_logux(params)
end
end
end
12 changes: 6 additions & 6 deletions spec/logux/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
let(:node) { described_class.instance }

describe '#generate_action_id' do
subject { node.generate_action_id }
let(:action_id) { node.generate_action_id }

it 'returns correct id' do
expect(subject).to match(/^[0-9]{13} server:.{8} 0$/)
expect(action_id).to match(/^[0-9]{13} server:.{8} 0$/)
end

context 'with action at the same time', timecop: true do
Expand All @@ -20,20 +20,20 @@
end

it 'returns 1 in sequence' do
expect(subject).to match(/^[0-9]{13} server:.{8} 1$/)
expect(action_id).to match(/^[0-9]{13} server:.{8} 1$/)
end
end
end

describe '#node_id' do
subject { node.node_id }
let(:node_id) { node.node_id }

it 'generates nanoid' do
expect(subject).not_to be_empty
expect(node_id).not_to be_empty
end

it "doesn't change from call to call" do
expect(subject).to eq(node.node_id)
expect(node_id).to eq(node.node_id)
end
end
end
16 changes: 11 additions & 5 deletions spec/logux/policy_caller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
let(:policy_caller) { described_class.new(action: action, meta: meta) }

describe '.call!' do
subject { policy_caller.call! }
let(:call!) { policy_caller.call! }

let(:action) { Logux::Actions.new(type: 'test/test') }
let(:meta) { {} }

it 'doesn\'t raise an error' do
expect(Logux.logger).to receive(:warn).once
subject
context 'when request is not verified' do
before do
allow(Logux.logger).to receive(:warn)
call!
end

it 'doesn\'t raise an error' do
expect(Logux.logger).to have_received(:warn).once
end
end

context 'when verify_authorized' do
Expand All @@ -24,7 +30,7 @@
end

it 'raises an error' do
expect { subject }.to raise_error(Logux::NoPolicyError)
expect { call! }.to raise_error(Logux::NoPolicyError)
end
end
end
Expand Down
7 changes: 3 additions & 4 deletions spec/logux_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
end

describe '.add' do
subject { described_class.add(type) }
before { described_class.add(type) }

let(:type) { [] }

it 'makes request' do
subject
expect(WebMock).to have_requested(:post, Logux.configuration.logux_host)
end
end

describe '.generate_action_id' do
subject { described_class.generate_action_id }
let(:action_id) { described_class.generate_action_id }

it 'returns correct action id' do
expect(subject).not_to be_empty
expect(action_id).not_to be_empty
end
end
end
32 changes: 17 additions & 15 deletions spec/requests/request_from_logux_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
require 'rails_helper'

describe 'Logux response' do
subject do
let(:request_logux) do
post('/logux',
params: logux_params,
as: :json)
end

let(:password) { Logux.configuration.password }

let(:logux_params) do
Expand All @@ -23,21 +24,22 @@
] }
end
let(:params) { Logux.configuration.password }
let(:logux_response) do
[
['processed', '219_856_768 clientid 0'],
['processed', '219_856_768 clientid 0']
]
end

it 'does return correct body' do
subject
expect(response.stream).to have_chunk(['approved', '219_856_768 clientid 0'])
expect(response.stream).to have_chunk(logux_response[0])
expect(response.stream).to have_chunk(logux_response[1])
context 'when authorized' do
before { request_logux }

it 'returns approved chunk' do
expect(response.stream).to have_chunk(['approved', '219_856_768 clientid 0'])
end

it 'returns processed chunk' do
expect(response.stream).to have_chunk(['processed', '219_856_768 clientid 0'])
end
end

context 'when no authorized' do
before { request_logux }

let(:logux_params) do
{ version: 0,
password: password,
Expand All @@ -52,16 +54,16 @@
end

it 'does return correct body' do
subject
expect(response.stream).to have_chunk(logux_response)
end
end

context 'when password wrong' do
before { request_logux }

let(:password) { '12345' }

it 'does return error' do
subject
expect(response.stream).to start_from_chunk([:error])
end
end
Expand All @@ -82,7 +84,7 @@
end

it 'returns correct chunk' do
expect { subject }.to change { logux_store.size }.by(1)
expect { request_logux }.to change { logux_store.size }.by(1)
end
end
end
Loading

0 comments on commit 0ae2fa1

Please sign in to comment.