castle-rb 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +2 -5
  2. data/README.md +1 -1
  3. data/lib/castle-rb.rb +2 -19
  4. data/lib/castle.rb +42 -0
  5. data/lib/castle/api.rb +48 -0
  6. data/lib/castle/client.rb +43 -0
  7. data/lib/castle/configuration.rb +39 -0
  8. data/lib/{castle-rb/support → castle}/cookie_store.rb +9 -5
  9. data/lib/castle/errors.rb +27 -0
  10. data/lib/castle/extractors/client_id.rb +28 -0
  11. data/lib/castle/extractors/headers.rb +36 -0
  12. data/lib/castle/extractors/ip.rb +16 -0
  13. data/lib/castle/headers.rb +39 -0
  14. data/lib/castle/request.rb +34 -0
  15. data/lib/castle/response.rb +43 -0
  16. data/lib/castle/support.rb +11 -0
  17. data/lib/{castle-rb → castle}/support/padrino.rb +1 -1
  18. data/lib/{castle-rb → castle}/support/rails.rb +2 -0
  19. data/lib/{castle-rb → castle}/support/sinatra.rb +1 -1
  20. data/lib/castle/system.rb +36 -0
  21. data/lib/castle/version.rb +5 -0
  22. data/spec/lib/castle/api_spec.rb +54 -0
  23. data/spec/lib/castle/client_spec.rb +64 -0
  24. data/spec/lib/castle/configuration_spec.rb +98 -0
  25. data/spec/lib/castle/extractors/client_id_spec.rb +39 -0
  26. data/spec/lib/castle/extractors/headers_spec.rb +25 -0
  27. data/spec/lib/castle/extractors/ip_spec.rb +20 -0
  28. data/spec/lib/castle/headers_spec.rb +82 -0
  29. data/spec/lib/castle/request_spec.rb +37 -0
  30. data/spec/lib/castle/response_spec.rb +71 -0
  31. data/spec/lib/castle/system_spec.rb +70 -0
  32. data/spec/lib/castle/version_spec.rb +9 -0
  33. data/spec/lib/castle_spec.rb +73 -0
  34. data/spec/spec_helper.rb +9 -3
  35. metadata +49 -76
  36. data/lib/castle-rb/api.rb +0 -94
  37. data/lib/castle-rb/client.rb +0 -67
  38. data/lib/castle-rb/configuration.rb +0 -48
  39. data/lib/castle-rb/errors.rb +0 -15
  40. data/lib/castle-rb/version.rb +0 -3
  41. data/spec/api_spec.rb +0 -31
  42. data/spec/client_spec.rb +0 -55
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Castle::Request do
6
+ subject do
7
+ described_class.new(headers)
8
+ end
9
+
10
+ let(:headers) { { 'SAMPLE-HEADER' => '1' } }
11
+ let(:api_secret) { Castle.config.api_secret }
12
+
13
+ describe 'build' do
14
+ let(:path) { 'endpoint' }
15
+ let(:params) { { user_id: 1 } }
16
+
17
+ context 'get' do
18
+ let(:request) { subject.build_query(path) }
19
+
20
+ it { expect(request.body).to be_nil }
21
+ it { expect(request.method).to eql('GET') }
22
+ it { expect(request.path).to eql('/v1/endpoint') }
23
+ it { expect(request.to_hash['sample-header']).to eql(['1']) }
24
+ it { expect(request.to_hash['authorization'][0]).to match(/Basic \w/) }
25
+ end
26
+
27
+ context 'post' do
28
+ let(:request) { subject.build(path, params, :post) }
29
+
30
+ it { expect(request.body).to be_eql('{"user_id":1}') }
31
+ it { expect(request.method).to eql('POST') }
32
+ it { expect(request.path).to eql('/v1/endpoint') }
33
+ it { expect(request.to_hash['sample-header']).to eql(['1']) }
34
+ it { expect(request.to_hash['authorization'][0]).to match(/Basic \w/) }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Castle::Response do
6
+ subject(:castle_response) do
7
+ described_class.new(response)
8
+ end
9
+
10
+ describe 'initialize' do
11
+ context 'without error when response is 2xx' do
12
+ let(:response) { OpenStruct.new(code: 200) }
13
+
14
+ it do
15
+ expect(castle_response).to be_truthy
16
+ end
17
+ end
18
+
19
+ shared_examples 'response_failed' do |code, error|
20
+ let(:response) { OpenStruct.new(code: code) }
21
+
22
+ it "fail when response is #{code}" do
23
+ expect do
24
+ castle_response
25
+ end.to raise_error(error)
26
+ end
27
+ end
28
+
29
+ it_behaves_like 'response_failed', 400, Castle::BadRequestError
30
+ it_behaves_like 'response_failed', 401, Castle::UnauthorizedError
31
+ it_behaves_like 'response_failed', 403, Castle::ForbiddenError
32
+ it_behaves_like 'response_failed', 404, Castle::NotFoundError
33
+ it_behaves_like 'response_failed', 419, Castle::UserUnauthorizedError
34
+ it_behaves_like 'response_failed', 422, Castle::InvalidParametersError
35
+ it_behaves_like 'response_failed', 500, Castle::ApiError
36
+ it_behaves_like 'response_failed', 499, Castle::ApiError
37
+ end
38
+
39
+ describe 'parse' do
40
+ context 'successfully' do
41
+ let(:response) { OpenStruct.new(body: '{"user":1}', code: 200) }
42
+
43
+ it do
44
+ expect(castle_response.parse).to eql(user: 1)
45
+ end
46
+ end
47
+ context 'return empty object when response empty' do
48
+ let(:response) { OpenStruct.new(body: '', code: 200) }
49
+
50
+ it do
51
+ expect(castle_response.parse).to eql({})
52
+ end
53
+ end
54
+ context 'return empty object when response nil' do
55
+ let(:response) { OpenStruct.new(code: 200) }
56
+
57
+ it do
58
+ expect(castle_response.parse).to eql({})
59
+ end
60
+ end
61
+ context 'fail when json is malformed' do
62
+ let(:response) { OpenStruct.new(body: '{a', code: 200) }
63
+
64
+ it do
65
+ expect do
66
+ castle_response.parse
67
+ end.to raise_error(Castle::ApiError)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Castle::System do
6
+ describe '.uname' do
7
+ subject(:uname) { described_class.uname }
8
+
9
+ before { allow(described_class).to receive(:platform) { platform } }
10
+
11
+ context 'darwin' do
12
+ let(:platform) { 'x86_64-apple-darwin16.6.0' }
13
+
14
+ it { expect { uname }.not_to raise_error }
15
+ it { expect(uname).to be_kind_of(String) }
16
+ end
17
+
18
+ context 'linux' do
19
+ let(:platform) { 'x86_64-pc-linux-gnu' }
20
+
21
+ it { expect { uname }.not_to raise_error }
22
+ it { expect(uname).to be_kind_of(String) }
23
+ end
24
+
25
+ context 'different' do
26
+ let(:platform) { 'different' }
27
+
28
+ it { expect { uname }.not_to raise_error }
29
+ it { expect(uname).to be_nil }
30
+ end
31
+
32
+ context 'Errno::ENOMEM' do
33
+ let(:platform) { 'x86_64-pc-linux-gnu' }
34
+
35
+ before do
36
+ allow(described_class).to receive(:`).with(
37
+ 'uname -a 2>/dev/null'
38
+ ).and_raise(Errno::ENOMEM)
39
+ end
40
+
41
+ it { expect { uname }.not_to raise_error }
42
+ it { expect(uname).to be_kind_of(String) }
43
+ it { expect(uname).to eql('uname lookup failed') }
44
+ end
45
+ end
46
+
47
+ describe '.platform' do
48
+ subject(:platform) { described_class.platform }
49
+
50
+ context 'rbconfig' do
51
+ it { expect(platform).to eq(RbConfig::CONFIG['host']) }
52
+ end
53
+
54
+ context 'RUBY_PLATFORM' do
55
+ before { stub_const('RbConfig::CONFIG', host: nil) }
56
+
57
+ it { expect(platform).to eq(RUBY_PLATFORM) }
58
+ end
59
+ end
60
+
61
+ describe '.ruby_version' do
62
+ subject(:ruby_version) { described_class.ruby_version }
63
+
64
+ let(:version) do
65
+ "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"
66
+ end
67
+
68
+ it { expect(ruby_version).to eq(version) }
69
+ end
70
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Castle do
6
+ it 'have version' do
7
+ expect(described_class::VERSION).to match(/\d\.\d\.\d/)
8
+ end
9
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Castle do
6
+ subject(:castle) { described_class }
7
+
8
+ describe 'config' do
9
+ it do
10
+ expect(castle.config).to be_kind_of(Castle::Configuration)
11
+ end
12
+ end
13
+
14
+ describe 'api_secret setter' do
15
+ let(:value) { 'new_secret' }
16
+
17
+ before do
18
+ castle.api_secret = value
19
+ end
20
+ it do
21
+ expect(castle.config.api_secret).to be_eql(value)
22
+ end
23
+ end
24
+
25
+ describe 'configure' do
26
+ let(:value) { 'new_secret' }
27
+ let(:timeout) { 60 }
28
+
29
+ shared_examples 'config_setup' do
30
+ it { expect(castle.config.api_secret).to be_eql(value) }
31
+ it { expect(castle.config.request_timeout).to be_eql(timeout) }
32
+ end
33
+
34
+ context 'by block' do
35
+ before do
36
+ castle.configure do |config|
37
+ config.api_secret = value
38
+ config.request_timeout = timeout
39
+ end
40
+ end
41
+ it_behaves_like 'config_setup'
42
+ end
43
+
44
+ context 'by options' do
45
+ before do
46
+ castle.configure(request_timeout: timeout,
47
+ api_secret: value)
48
+ end
49
+ it_behaves_like 'config_setup'
50
+ end
51
+
52
+ context 'by block and options' do
53
+ before do
54
+ castle.configure(request_timeout: timeout) do |config|
55
+ config.api_secret = value
56
+ end
57
+ end
58
+ it_behaves_like 'config_setup'
59
+ end
60
+ end
61
+
62
+ describe 'configure wrongly' do
63
+ let(:value) { 'new_secret' }
64
+
65
+ it do
66
+ expect do
67
+ castle.configure do |config|
68
+ config.wrong_config = value
69
+ end
70
+ end.to raise_error(Castle::ConfigurationError)
71
+ end
72
+ end
73
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rubygems'
2
4
  require 'bundler/setup'
3
5
  require 'rack'
4
6
  require 'webmock/rspec'
5
7
  require 'pry'
6
- require 'castle-rb'
8
+
9
+ require 'simplecov'
10
+ SimpleCov.start
11
+
12
+ require 'castle'
7
13
 
8
14
  Castle.configure do |config|
9
15
  config.api_secret = 'secret'
@@ -15,7 +21,7 @@ RSpec.configure do |config|
15
21
  config.before(:each) do
16
22
  Castle.config.api_endpoint = 'https://api.castle.io/v1'
17
23
  Castle.config.request_timeout = 30.0
18
- stub_request(:any, /api.castle.io/).
19
- to_return(status: 200, body: "{}", headers: {})
24
+ stub_request(:any, /api.castle.io/)
25
+ .to_return(status: 200, body: '{}', headers: {})
20
26
  end
21
27
  end
metadata CHANGED
@@ -1,90 +1,53 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: castle-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johan Brissmyr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-13 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: pry
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ~>
18
- - !ruby/object:Gem::Version
19
- version: 0.10.4
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ~>
25
- - !ruby/object:Gem::Version
26
- version: 0.10.4
27
- - !ruby/object:Gem::Dependency
28
- name: rack
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ~>
32
- - !ruby/object:Gem::Version
33
- version: '1.6'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ~>
39
- - !ruby/object:Gem::Version
40
- version: '1.6'
41
- - !ruby/object:Gem::Dependency
42
- name: rspec
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ~>
46
- - !ruby/object:Gem::Version
47
- version: '3.5'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ~>
53
- - !ruby/object:Gem::Version
54
- version: '3.5'
55
- - !ruby/object:Gem::Dependency
56
- name: webmock
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: '1.21'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ~>
67
- - !ruby/object:Gem::Version
68
- version: '1.21'
11
+ date: 2017-07-18 00:00:00.000000000 Z
12
+ dependencies: []
69
13
  description: The easiest way to protect your users
70
14
  email: johan@castle.io
71
15
  executables: []
72
16
  extensions: []
73
17
  extra_rdoc_files: []
74
18
  files:
75
- - lib/castle-rb/api.rb
76
- - lib/castle-rb/client.rb
77
- - lib/castle-rb/configuration.rb
78
- - lib/castle-rb/errors.rb
79
- - lib/castle-rb/support/cookie_store.rb
80
- - lib/castle-rb/support/padrino.rb
81
- - lib/castle-rb/support/rails.rb
82
- - lib/castle-rb/support/sinatra.rb
83
- - lib/castle-rb/version.rb
84
- - lib/castle-rb.rb
85
19
  - README.md
86
- - spec/api_spec.rb
87
- - spec/client_spec.rb
20
+ - lib/castle-rb.rb
21
+ - lib/castle.rb
22
+ - lib/castle/api.rb
23
+ - lib/castle/client.rb
24
+ - lib/castle/configuration.rb
25
+ - lib/castle/cookie_store.rb
26
+ - lib/castle/errors.rb
27
+ - lib/castle/extractors/client_id.rb
28
+ - lib/castle/extractors/headers.rb
29
+ - lib/castle/extractors/ip.rb
30
+ - lib/castle/headers.rb
31
+ - lib/castle/request.rb
32
+ - lib/castle/response.rb
33
+ - lib/castle/support.rb
34
+ - lib/castle/support/padrino.rb
35
+ - lib/castle/support/rails.rb
36
+ - lib/castle/support/sinatra.rb
37
+ - lib/castle/system.rb
38
+ - lib/castle/version.rb
39
+ - spec/lib/castle/api_spec.rb
40
+ - spec/lib/castle/client_spec.rb
41
+ - spec/lib/castle/configuration_spec.rb
42
+ - spec/lib/castle/extractors/client_id_spec.rb
43
+ - spec/lib/castle/extractors/headers_spec.rb
44
+ - spec/lib/castle/extractors/ip_spec.rb
45
+ - spec/lib/castle/headers_spec.rb
46
+ - spec/lib/castle/request_spec.rb
47
+ - spec/lib/castle/response_spec.rb
48
+ - spec/lib/castle/system_spec.rb
49
+ - spec/lib/castle/version_spec.rb
50
+ - spec/lib/castle_spec.rb
88
51
  - spec/spec_helper.rb
89
52
  homepage: https://castle.io
90
53
  licenses:
@@ -96,21 +59,31 @@ require_paths:
96
59
  - lib
97
60
  required_ruby_version: !ruby/object:Gem::Requirement
98
61
  requirements:
99
- - - '>='
62
+ - - ">="
100
63
  - !ruby/object:Gem::Version
101
64
  version: '0'
102
65
  required_rubygems_version: !ruby/object:Gem::Requirement
103
66
  requirements:
104
- - - '>='
67
+ - - ">="
105
68
  - !ruby/object:Gem::Version
106
69
  version: '0'
107
70
  requirements: []
108
71
  rubyforge_project:
109
- rubygems_version: 2.0.14.1
72
+ rubygems_version: 2.6.12
110
73
  signing_key:
111
74
  specification_version: 4
112
75
  summary: Castle
113
76
  test_files:
114
- - spec/api_spec.rb
115
- - spec/client_spec.rb
77
+ - spec/lib/castle/api_spec.rb
78
+ - spec/lib/castle/client_spec.rb
79
+ - spec/lib/castle/configuration_spec.rb
80
+ - spec/lib/castle/extractors/client_id_spec.rb
81
+ - spec/lib/castle/extractors/headers_spec.rb
82
+ - spec/lib/castle/extractors/ip_spec.rb
83
+ - spec/lib/castle/headers_spec.rb
84
+ - spec/lib/castle/request_spec.rb
85
+ - spec/lib/castle/response_spec.rb
86
+ - spec/lib/castle/system_spec.rb
87
+ - spec/lib/castle/version_spec.rb
88
+ - spec/lib/castle_spec.rb
116
89
  - spec/spec_helper.rb