lws 6.1.0.beta4 → 6.1.0.beta5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b58d4f19b39cd31f8eb39c19252248889bbcdc1
4
- data.tar.gz: fe5430ca9566e47fce0585eb7dc5e9f3e06d1406
3
+ metadata.gz: 1572c59ecae295dace557e5554bbc20aa1a3d48e
4
+ data.tar.gz: 7ad14b5a843814bec826628a8a76a1c36827a389
5
5
  SHA512:
6
- metadata.gz: a9b8964fe328533bd15bcc54aaa2e9f44239bafd204f00b56fbfc9e8ecc0050fecf14a1d6c4363f432d06074267e80689f4f40465ffa5be8ffdd8040a355cc39
7
- data.tar.gz: 4f8664f672916043a6aac86cb7175f7e001c4df14537e0b2c345bd6f03306610184519afe7dc5ab5ca4f309eee8aca62c3f6a55b068f6c465e46b9063fde0009
6
+ metadata.gz: 8c18b43430a42d12ab04b42af7bd32ad4081e694aabc73206ffc20a4c7dcbafe1ed0377056500345cd142ea8be5691d1dc75b2a28a05ae148257d8adf7874863
7
+ data.tar.gz: b9b647419cbab122f209c8efada301d3e14aede20ce3a14cfa784b5138569b8421d29a0a31966338bc045c47c6a93bbbdb5f8fd6ce9d0bfdc5d336455b6f242b
data/bin/lwsconsole CHANGED
@@ -82,7 +82,7 @@ begin
82
82
  puts "#{PROG_NAME}: Cannot find app `#{@options[:app]}', ignoring it!" unless @app_module
83
83
  end
84
84
  rescue => e
85
- puts "#{PROG_NAME}: Problem during LWS setup: #{e}"
85
+ puts "#{PROG_NAME}: Problem during LWS setup: #{e} (#{e.class})"
86
86
  exit 3
87
87
  end
88
88
 
data/lib/lws/apps/auth.rb CHANGED
@@ -299,6 +299,7 @@ module LWS::Auth
299
299
  # It cannot be accessed directly.
300
300
  class Device < LWS::Generic::Model
301
301
  use_api LWS::Auth.api
302
+ uri "accounts/:account_id/devices/(:id)"
302
303
 
303
304
  # @!attribute id [r]
304
305
  # @return [Fixnum] the (unique) ID of the device
@@ -497,6 +498,7 @@ module LWS::Auth
497
498
  # It cannot be accessed directly.
498
499
  class User < LWS::Generic::Model
499
500
  use_api LWS::Auth.api
501
+ uri "accounts/:account_id/users/(:id)"
500
502
 
501
503
  # @!attribute id [r]
502
504
  # @return [Fixnum] the (unique) ID of the user
@@ -20,6 +20,25 @@ module LWS::Generic
20
20
  class Model < Spyke::Base
21
21
  include_root_in_json true
22
22
 
23
+ # @private
24
+ # @!visibility private
25
+ #
26
+ # This method wraps Spyke's .find method so that we can raise
27
+ # an LWS-specific exception.
28
+ #
29
+ # @param [Fixnum, String] id the ID of the resource to find and
30
+ # retrieve
31
+ # @raise [LWS::Errors::ResourceNotFound] if not all of the resources
32
+ # could be found
33
+ # @return [LWS::Generic::Model] the found resource
34
+ def self.find(id)
35
+ super
36
+ rescue Spyke::ResourceNotFound => exception
37
+ msg = "resource not found at " +
38
+ "URI #{self.uri} where :#{primary_key} is #{id.inspect}"
39
+ raise LWS::Errors::ResourceNotFound, msg
40
+ end
41
+
23
42
  # @private
24
43
  # @!visibility private
25
44
  #
@@ -158,7 +158,9 @@ module LWS::Ticket
158
158
  attribute :message
159
159
 
160
160
  # @!attribute status
161
- # @return [Fixnum] the new ticket status (ID) set by the message
161
+ # @return ["success", "open", "closed", "in_progress", 'solved",
162
+ # "feedback", "priority", "reassign"] the new ticket status set by
163
+ # the message
162
164
  attribute :status
163
165
 
164
166
  # @!attribute ticket
@@ -293,11 +295,12 @@ module LWS::Ticket
293
295
  attribute :owner_id
294
296
 
295
297
  # @!attribute priority
296
- # @return [Fixnum] the priority (ID) of the ticket
298
+ # @return ["low", "medium", "high"] the priority of the ticket
297
299
  attribute :priority
298
300
 
299
301
  # @!attribute status
300
- # @return [Fixnum] the current ticket status (ID)
302
+ # @return ["success", "open", "closed", "in_progress", 'solved",
303
+ # "feedback", "priority", "reassign"] the current ticket status
301
304
  attribute :status
302
305
 
303
306
  # @!attribute tags
data/lib/lws/errors.rb ADDED
@@ -0,0 +1,31 @@
1
+ #
2
+ # Copyright © 2016 LeftClick B.V.
3
+ #
4
+ # This software is property of LeftClick B.V. and cannot be redistributed
5
+ # and/or modified without permission. The software or any of its parts
6
+ # cannot be used for any other purposes than the LeftClick services and
7
+ # only during a valid license subscription. For more information, please
8
+ # contact LeftClick B.V. at: Geldropseweg 8B, 5731 SG Mierlo, The
9
+ # Netherlands, info@leftclick.eu, +31492-782120.
10
+
11
+
12
+ module LWS
13
+
14
+ # = The errors module
15
+ #
16
+ # This module contains the exceptions that can be thrown.
17
+ module Errors
18
+ # Exception thrown if there are issues with the LWS configuration.
19
+ class ConfigError < StandardError; end
20
+
21
+ # Exception thrown if an invalid response is returned by LWS.
22
+ class InvalidResponse < StandardError; end
23
+
24
+ # Exception thrown if the resource could not be found in LWS.
25
+ class ResourceNotFound < StandardError; end
26
+
27
+ # Exception thrown if there is an issue with the stubbing setup.
28
+ class StubError < StandardError; end
29
+ end
30
+
31
+ end
@@ -18,7 +18,10 @@ module LWS
18
18
  class HTTPLogger < Faraday::Response::Middleware
19
19
 
20
20
  def initialize(app, logger, show_headers)
21
- raise "cannot log HTTP requests without a logger" if logger.nil?
21
+ if logger.nil?
22
+ raise LWS::Errors::ConfigError,
23
+ "cannot log HTTP requests without a logger"
24
+ end
22
25
  @logger = logger
23
26
  @show_headers = show_headers
24
27
  super(app)
@@ -18,7 +18,10 @@ module LWS
18
18
  class JSONLogger < Faraday::Response::Middleware
19
19
 
20
20
  def initialize(app, logger)
21
- raise "cannot log JSON data without a logger" if logger.nil?
21
+ if logger.nil?
22
+ raise LWS::Errors::ConfigError,
23
+ "cannot log JSON data without a logger"
24
+ end
22
25
  @logger = logger
23
26
  super(app)
24
27
  end
@@ -28,6 +28,8 @@ module LWS
28
28
  { data: data,
29
29
  metadata: metadata,
30
30
  errors: errors }
31
+ rescue => exception
32
+ raise LWS::Errors::InvalidResponse, exception.message
31
33
  end
32
34
 
33
35
  def on_complete(env)
data/lib/lws/stubbing.rb CHANGED
@@ -41,13 +41,19 @@ module LWS
41
41
  # exists.
42
42
  #
43
43
  # @param stubs_dir [Pathname, String] path to the directory with the fixtures.
44
+ # @raise [LWS::Errors::ConfigError] if the fixtures/stubs directory does
45
+ # not exist
46
+ # @return [void]
44
47
  def initialize(stubs_dir)
45
48
  # Set up WebMock
46
49
  WebMock.enable!
47
50
  @stubs = {}
48
51
 
49
52
  # Load the fixtures
50
- raise "not a valid stubs directory" unless File.directory? stubs_dir
53
+ unless File.directory? stubs_dir
54
+ raise LWS::Errors::ConfigError,
55
+ "not a valid stubs directory: #{stubs_dir}"
56
+ end
51
57
  @fixtures = {}
52
58
  Dir["#{stubs_dir}/**/*.yml"].each do |yml_file|
53
59
  yml = YAML.load_file(yml_file)
@@ -92,8 +98,8 @@ module LWS
92
98
  # @param fixture_name [Symbol, nil] the name of the fixture to use as stub
93
99
  # response body (or +nil+ for an empty fixture)
94
100
  # @param http_status [Fixnum] the HTTP status code to use
95
- # @raise if the fixture name could not be found for the given app name
96
- # and path
101
+ # @raise [LWS::Errors::StubError] if the fixture name could not be
102
+ # found for the given app name and path
97
103
  # @return [void]
98
104
  def replace(method, app_name, path, fixture_name = :default, http_status = 200)
99
105
  fixture = if fixture_name
@@ -101,7 +107,10 @@ module LWS
101
107
  else
102
108
  {}
103
109
  end
104
- raise "fixture #{fixture_name} not found (app: #{app_name}, URI: #{path})" unless fixture
110
+ unless fixture
111
+ raise LWS::Errors::StubError,
112
+ "fixture #{fixture_name} not found (app: #{app_name}, URI: #{path})"
113
+ end
105
114
  app_module = LWS.app_module(app_name)
106
115
  app_endpoint_uri = app_module.api.url_prefix.dup
107
116
  app_endpoint_uri.path = path
@@ -127,12 +136,14 @@ module LWS
127
136
  # @param method [Symbol] the HTTP method (+:get+, +:put+, etc.)
128
137
  # @param app_name [Symbol] the app name (see {::SUPPORTED_APPS})
129
138
  # @param path [String] the path part of the request URI
130
- # @raise if there is not request stub for the given HTTP method,
131
- # app name and path
139
+ # @raise [LWS::Errors::StubError] if there is not request stub for the
140
+ # given HTTP method, app name and path
132
141
  # @return [void]
133
142
  def remove(method, app_name, path)
134
143
  request_stub = @stubs.delete([method, app_name, path])
135
- raise "no such request stub" unless request_stub
144
+ unless request_stub
145
+ raise LWS::Errors::StubError, "no such request stub"
146
+ end
136
147
  remove_request_stub request_stub
137
148
  end
138
149
 
data/lib/lws/version.rb CHANGED
@@ -13,6 +13,6 @@ module LWS
13
13
 
14
14
  # The LWS library version.
15
15
  # @note The major and minor version parts match the LWS API version!
16
- VERSION = '6.1.0.beta4'
16
+ VERSION = '6.1.0.beta5'
17
17
 
18
18
  end
data/lib/lws.rb CHANGED
@@ -17,6 +17,7 @@ require "pp"
17
17
  require "webmock"
18
18
 
19
19
  require "lws/config"
20
+ require "lws/errors"
20
21
  require "lws/middleware"
21
22
  require "lws/stubbing"
22
23
  require "lws/version"
@@ -31,6 +32,7 @@ WebMock.disable!
31
32
  # libraries.
32
33
  module LWS
33
34
 
35
+ include Errors
34
36
  include Middleware
35
37
 
36
38
  # The list of supported apps (web service libraries) loaded by
@@ -47,21 +49,31 @@ module LWS
47
49
  # Sets up the application API libraries using the provided
48
50
  # configuration (see {Config}).
49
51
  #
50
- # @example Set up LWS with a token, an endpoint override, HTTP debugging and the Rails logger
52
+ # @example Set up LWS with a token and the development environment
51
53
  # LWS.setup do |config|
52
54
  # config.api_token = "MY_TOKEN"
53
- # config.endpoints = { maps: https://maps-dev.leftclick.cloud }
55
+ # config.environment = :development
56
+ # end
57
+ #
58
+ # @example Set up LWS with API token middleware, caching, the development environment, an map endpoint override and logging to the Rails logger
59
+ # LWS.setup do |config|
60
+ # config.api_token_middleware = TokenAuthenticator
61
+ # config.caching_object = MyRedisCache.new
62
+ # config.environment = :development
63
+ # config.endponts = { maps: "https://maps.leftclick.cloud" }
54
64
  # config.http_debug = true
65
+ # config.json_debug = true
55
66
  # config.logger = Rails.logger
56
67
  # end
57
68
  #
69
+ #
58
70
  # The API token can be overridden using the +LC_LWS_API_TOKEN+
59
71
  # environment variable. The LWS environment can be overriden using the
60
72
  # +LC_LWS_ENV+ environment variable.
61
73
  #
62
74
  # @yieldparam [Config] config an API configuration object that can be
63
75
  # configured
64
- # @raise if API token is not configured
76
+ # @raise [LWS::Errors::ConfigError] if the API token is not configured/found
65
77
  # @return [LWS] the module itself
66
78
  def self.setup(&block)
67
79
  @@config = Config.new
@@ -79,7 +91,8 @@ module LWS
79
91
  end
80
92
 
81
93
  if config.api_token.blank? and config.api_token_middleware.blank?
82
- raise "API token or API token middleware is required"
94
+ raise LWS::Errors::ConfigError,
95
+ "API token or API token middleware is required"
83
96
  end
84
97
 
85
98
  load_app_modules
@@ -39,35 +39,35 @@ auth:
39
39
  - *account_1
40
40
  post_default: *account_1
41
41
 
42
- /companies/1:
43
- default: &company_1
44
- id: 1
45
- contact_person_id: 1
46
- parent_id: null
47
-
48
- /companies:
49
- default:
50
- - *company_1
51
- post_default: *company_1
52
-
53
- /devices/1:
42
+ /accounts/1/devices/1:
54
43
  default: &device_1
55
44
  id: 1
56
45
  account_id: 1
57
46
  name: Test Device
58
47
 
59
- /devices:
48
+ /accounts/1/devices:
60
49
  default:
61
50
  - *device_1
62
51
  post_default: *device_1
63
52
 
64
- /users/1:
53
+ /accounts/1/users/1:
65
54
  default: &user_1
66
55
  id: 1
67
56
  account_id: 1
68
57
  email: test.user@leftclick.eu
69
58
 
70
- /users:
59
+ /accounts/1/users:
71
60
  default:
72
61
  - *user_1
73
62
  post_default: *user_1
63
+
64
+ /companies/1:
65
+ default: &company_1
66
+ id: 1
67
+ contact_person_id: 1
68
+ parent_id: null
69
+
70
+ /companies:
71
+ default:
72
+ - *company_1
73
+ post_default: *company_1
data/test/generic_test.rb CHANGED
@@ -20,11 +20,37 @@ class TestGenericConfiguration < MiniTest::Test
20
20
  @configuration = Configuration.all.first
21
21
  end
22
22
 
23
- # FIXME: renable this test once some configurations are available again
24
- #def test_valid_configuration
25
- # refute_nil(@configuration)
26
- # assert_instance_of(Configuration, @configuration)
27
- # refute_nil(@configuration.id)
28
- #end
23
+ def test_valid
24
+ refute_nil(@configuration)
25
+ assert_instance_of(Configuration, @configuration)
26
+ refute_nil(@configuration.id)
27
+ end
28
+
29
+ def test_lifecycle
30
+ # Create
31
+ new_configuration = Configuration.create(key: "test", value: "some_value")
32
+ refute_nil(new_configuration)
33
+ assert_instance_of(Configuration, new_configuration)
34
+
35
+ # Retrieve
36
+ new_configuration = Configuration.find(new_configuration.id)
37
+ refute_nil(new_configuration)
38
+ assert_instance_of(Configuration, new_configuration)
39
+ assert_raises LWS::Errors::ResourceNotFound do
40
+ Configuration.find("does_not_exist")
41
+ end
42
+
43
+ # Update
44
+ new_configuration.value = "other_value"
45
+ assert(new_configuration.save)
46
+ new_configuration.reload
47
+ assert_equal("other_value", new_configuration.value)
48
+
49
+ # Destroy
50
+ assert(new_configuration.destroy)
51
+ assert_raises LWS::Errors::ResourceNotFound do
52
+ Configuration.find(new_configuration.id)
53
+ end
54
+ end
29
55
 
30
56
  end
@@ -25,11 +25,16 @@ class TestJSONParser < MiniTest::Test
25
25
  full_uri = LWS::Auth.api.url_prefix.dup
26
26
  full_uri.path = "/companies/broken"
27
27
  stub_request(:get, full_uri.to_s).to_return(body: "{}invalid json")
28
+ full_uri.path = "/companies/empty"
29
+ stub_request(:get, full_uri.to_s).to_return(body: "")
28
30
  full_uri.path = "/companies/correct"
29
31
  stub_request(:get, full_uri.to_s)
30
32
  .to_return(body: MultiJson.dump(company))
31
33
  stub_request(:put, full_uri.to_s)
32
34
  .to_return(body: MultiJson.dump(company.merge(errors: { ticket: ["is an invalid field"] })))
35
+ full_uri.path = "/companies"
36
+ stub_request(:post, full_uri.to_s)
37
+ .to_return(body: "", status: 204)
33
38
  end
34
39
 
35
40
  def teardown
@@ -37,7 +42,7 @@ class TestJSONParser < MiniTest::Test
37
42
  end
38
43
 
39
44
  def test_broken_json
40
- assert_raises MultiJson::ParseError do
45
+ assert_raises LWS::Errors::InvalidResponse do
41
46
  company = LWS::Auth::Company.find("broken")
42
47
  end
43
48
  end
@@ -47,6 +52,11 @@ class TestJSONParser < MiniTest::Test
47
52
  assert_equal "correct", company.id
48
53
  end
49
54
 
55
+ def test_no_content_json
56
+ company = LWS::Auth::Company.create
57
+ assert_instance_of LWS::Auth::Company, company
58
+ end
59
+
50
60
  def test_correct_json_with_errors
51
61
  company = LWS::Auth::Company.find("correct")
52
62
  company.ticket = "unknown attribute"
data/test/setup_test.rb CHANGED
@@ -13,13 +13,15 @@ require "test_helper"
13
13
 
14
14
  class TestSetup < MiniTest::Test
15
15
 
16
+ def teardown
17
+ # Restore the configuration
18
+ reconfigure
19
+ end
20
+
16
21
  def test_cannot_leave_out_api_token
17
22
  assert_raises do
18
23
  LWS.setup { }
19
24
  end
20
-
21
- # Restore the configuration
22
- reconfigure
23
25
  end
24
26
 
25
27
  def test_get_environment_from_environment
@@ -39,9 +41,28 @@ class TestSetup < MiniTest::Test
39
41
  reconfigure
40
42
  end
41
43
  assert_equal(LWS.config.api_token, "test-token")
44
+ end
42
45
 
43
- # Restore the configuration
44
- reconfigure
46
+ def test_http_logging_without_logger
47
+ reconfigure(logger: nil,
48
+ http_debug: true,
49
+ http_debug_headers: true,
50
+ json_debug: false)
51
+
52
+ assert_raises LWS::Errors::ConfigError do
53
+ LWS::Auth::Token.find(0)
54
+ end
55
+ end
56
+
57
+ def test_json_logging_without_logger
58
+ reconfigure(logger: nil,
59
+ http_debug: false,
60
+ http_debug_headers: false,
61
+ json_debug: true)
62
+
63
+ assert_raises LWS::Errors::ConfigError do
64
+ LWS::Auth::Token.find(0)
65
+ end
45
66
  end
46
67
 
47
68
  end
@@ -22,6 +22,12 @@ class TestStubbing < MiniTest::Test
22
22
  reconfigure
23
23
  end
24
24
 
25
+ def test_not_working_stubbing
26
+ assert_raises LWS::Errors::ConfigError do
27
+ reconfigure(stubbing: "/fixtures/doesnotexist")
28
+ end
29
+ end
30
+
25
31
  def test_working_stubbing
26
32
  assert_raises WebMock::NetConnectNotAllowedError do
27
33
  LWS::Auth::Token.find("not_stubbed")
@@ -32,7 +38,7 @@ class TestStubbing < MiniTest::Test
32
38
 
33
39
  def test_working_stubbing_with_status
34
40
  LWS.stubbing.replace(:get, :auth, "/tokens/does_not_exist", :default, 404)
35
- assert_raises Spyke::ResourceNotFound do
41
+ assert_raises LWS::Errors::ResourceNotFound do
36
42
  LWS::Auth::Token.find("does_not_exist")
37
43
  end
38
44
  end
@@ -42,11 +48,23 @@ class TestStubbing < MiniTest::Test
42
48
  assert LWS.stubbing.replace(:get, :auth, "/tokens/1", nil)
43
49
  end
44
50
 
51
+ def test_failed_replace_stub
52
+ assert_raises LWS::Errors::StubError do
53
+ LWS.stubbing.replace(:get, :auth, "/some/nonexisting/path", :not_here)
54
+ end
55
+ end
56
+
45
57
  def test_remove_stub
46
- stub = LWS.stubbing.remove(:get, :auth, "/devices/1")
58
+ stub = LWS.stubbing.remove(:get, :auth, "/accounts/1/devices/1")
47
59
  assert_nil stub
48
- assert_raises RuntimeError do
49
- LWS.stubbing.remove(:get, :auth, "/devices/1")
60
+ assert_raises LWS::Errors::StubError do
61
+ LWS.stubbing.remove(:get, :auth, "/accounts/1/devices/1")
62
+ end
63
+ end
64
+
65
+ def test_failed_remove_stub
66
+ assert_raises LWS::Errors::StubError do
67
+ LWS.stubbing.remove(:get, :auth, "/some/nonexisting/path")
50
68
  end
51
69
  end
52
70
 
data/test/test_helper.rb CHANGED
@@ -48,9 +48,9 @@ def reconfigure(options = {})
48
48
  config.api_token = ENV["LC_LWS_TEST_TOKEN"]
49
49
  if ENV["LC_LWS_TEST_DEBUG"].present?
50
50
  config.logger = Logger.new($stdout)
51
- config.http_debug = false
51
+ config.http_debug = true
52
52
  config.http_debug_headers = false
53
- config.json_debug = false
53
+ config.json_debug = true
54
54
  end
55
55
  config.environment = :development
56
56
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lws
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.1.0.beta4
4
+ version: 6.1.0.beta5
5
5
  platform: ruby
6
6
  authors:
7
7
  - LeftClick B.V.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-05 00:00:00.000000000 Z
11
+ date: 2018-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday_middleware
@@ -244,6 +244,7 @@ files:
244
244
  - lib/lws/apps/presence.rb
245
245
  - lib/lws/apps/ticket.rb
246
246
  - lib/lws/config.rb
247
+ - lib/lws/errors.rb
247
248
  - lib/lws/middleware.rb
248
249
  - lib/lws/middleware/http_logger.rb
249
250
  - lib/lws/middleware/json_logger.rb