beyond_the_api_test_helpers 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4bc32ded39e0add424209b6bdabce6d8f6110956
4
+ data.tar.gz: b8f2eeab1d3eda9c41479669793998c6e67f239e
5
+ SHA512:
6
+ metadata.gz: d1d359c59cd3bc49c207d6f06b6d069e1228dbe7821424efcf3ab21d312eee27e275f67659f80fcb17fa83221b3b051e6335e8f1bbe77923978999be8de3a83e
7
+ data.tar.gz: 386a552a8c3c88e329fb70e46ff83fd36a49f4b6ea21a736ad86f3190558d1be8be61a58c8769843c52b7d9cb4aef0a5a59d90c241bec7d8f46213fa8db7ff2c
@@ -0,0 +1,19 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore all logfiles and tempfiles.
11
+ /log/*
12
+ /tmp/*
13
+ !/log/.keep
14
+ !/tmp/.keep
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ .gem
@@ -0,0 +1,60 @@
1
+ AllCops:
2
+ DisabledByDefault: false
3
+ TargetRubyVersion: 2.3
4
+ Exclude:
5
+ - db/migrate/*
6
+ - db/schema.rb
7
+ - lib/generators/**/*
8
+ - lib/rails/generators/**/*
9
+ - lib/populate/**/*
10
+ - bin/*
11
+ Rails:
12
+ Enabled: true
13
+ Metrics/LineLength:
14
+ Max: 100
15
+ Metrics/ClassLength:
16
+ CountComments: false
17
+ Max: 100
18
+ Exclude:
19
+ - test/**/*
20
+ Metrics/ClassLength:
21
+ CountComments: false
22
+ Max: 300
23
+ Include:
24
+ - test/**/*
25
+ Metrics/BlockLength:
26
+ Enabled: true
27
+ Exclude:
28
+ - test/**/*
29
+ Metrics/BlockLength:
30
+ Enabled: false
31
+ Include:
32
+ - test/**/*
33
+ Style/PredicateName:
34
+ Enabled: true
35
+ Exclude:
36
+ - test/support/shoulda/**/*
37
+ Style/PredicateName:
38
+ Enabled: false
39
+ Include:
40
+ - test/support/shoulda/**/*
41
+ Style/AsciiComments:
42
+ Enabled: false
43
+ Style/StringLiteralsInInterpolation:
44
+ Enabled: false
45
+ Style/Documentation:
46
+ Enabled: false
47
+ Style/EmptyLinesAroundClassBody:
48
+ Enabled: false
49
+ Style/ClassAndModuleChildren:
50
+ Enabled: false
51
+ Style/ModuleFunction:
52
+ Enabled: false
53
+ Style/EmptyLinesAroundModuleBody:
54
+ Enabled: false
55
+ Style/StringLiterals:
56
+ Enabled: false
57
+ Style/WordArray:
58
+ Enabled: false
59
+ Style/FrozenStringLiteralComment:
60
+ Enabled: false
@@ -0,0 +1 @@
1
+ 2.3.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 William Weckl
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Beyond The API Test Helpers
2
+
3
+ Beyond The API is a framework created to standardize Rails APIs and make things easier.
4
+
5
+ This gem consist in various test helpers for testing an application based in Beyond The API gem.
6
+
7
+ The tests are based in minitest.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'beyond_the_api_test_helpers', require: false, group: :test
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install beyond_the_api_test_helpers
24
+
25
+ ## Installation
26
+
27
+ Include in your test_helper.rb:
28
+ ```ruby
29
+ require 'beyond_the_api_test_helpers'
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ TODO
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/williamweckl/beyond_the_api_test_helpers.
39
+
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'beyond_the_api_test_helpers/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'beyond_the_api_test_helpers'
7
+ spec.version = BeyondTheApiTestHelpers::VERSION
8
+ spec.authors = ['William Weckl']
9
+ spec.email = ['william.weckl@gmail.com']
10
+
11
+ spec.summary = 'Test helpers for beyond the API.'
12
+ spec.description = 'Test helpers for beyond the API.'
13
+ spec.homepage = 'http://rubygems.org/gems/beyond_the_api_test_helpers'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'minitest'
22
+ spec.add_dependency 'mocha'
23
+ spec.add_dependency 'shoulda', '>= 3.5.0'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.11'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ end
@@ -0,0 +1,63 @@
1
+ ---
2
+ ### Generic smell configuration
3
+
4
+ IrresponsibleModule:
5
+ enabled: false
6
+ UnusedPrivateMethod:
7
+ enabled: false
8
+ NilCheck:
9
+ enabled: false
10
+
11
+ "app/controllers":
12
+ TooManyStatements:
13
+ max_statements: 10
14
+ NestedIterators:
15
+ max_allowed_nesting: 2
16
+ UnusedPrivateMethod:
17
+ enabled: false
18
+ UncommunicativeModuleName:
19
+ enabled: false
20
+ InstanceVariableAssumption:
21
+ enabled: false
22
+ ManualDispatch:
23
+ enabled: false
24
+ "app/helpers":
25
+ UtilityFunction:
26
+ enabled: false
27
+ FeatureEnvy:
28
+ enabled: false
29
+ ManualDispatch:
30
+ enabled: false
31
+ "app/models":
32
+ Attribute:
33
+ enabled: false
34
+ "app/serializer":
35
+ RepeatedConditional:
36
+ enabled: false
37
+ "test/support":
38
+ UtilityFunction:
39
+ enabled: false
40
+ FeatureEnvy:
41
+ enabled: false
42
+ TooManyStatements:
43
+ max_statements: 10
44
+ InstanceVariableAssumption:
45
+ enabled: false
46
+ TooManyMethods:
47
+ max_methods: 20
48
+ ManualDispatch:
49
+ enabled: false
50
+ DataClump:
51
+ enabled: false
52
+ "test/requests":
53
+ UncommunicativeModuleName:
54
+ enabled: false
55
+
56
+ ### Excluding directories
57
+
58
+ # Directories below will not be scanned at all
59
+ exclude_paths:
60
+ - db/migrate
61
+ - lib/generators
62
+ - lib/rails/generators
63
+ - lib/populate
@@ -0,0 +1,43 @@
1
+ require 'beyond_the_api_test_helpers/i18n_test_helpers'
2
+ Dir["#{File.dirname(__FILE__)}/beyond_the_api_test_helpers/request/**/*.rb"].each { |f| require f }
3
+ Dir["#{File.dirname(__FILE__)}/beyond_the_api_test_helpers/shoulda/**/*.rb"].each { |f| require f }
4
+
5
+ I18n.enforce_available_locales = false
6
+
7
+ class TestExceptionLocalizationHandler
8
+ # :reek:LongParameterList: { enabled: false }
9
+ def call(exception, _locale, _key, _options)
10
+ raise exception.message
11
+ end
12
+ end
13
+
14
+ class TestLanguage
15
+ cattr_accessor :current
16
+ end
17
+
18
+ ActiveSupport::TestCase.class_eval do
19
+ include BeyondTheApiTestHelpers::I18nTestHelpers
20
+
21
+ # Setup ActiveJob to run with test adapter
22
+ ActiveJob::Base.queue_adapter = :test
23
+
24
+ # Raise errors on i18n missing translations
25
+ I18n.exception_handler = ::TestExceptionLocalizationHandler.new
26
+
27
+ Faker::Config.locale = I18n.locale = TestLanguage.current = I18n.available_locales.sample
28
+
29
+ teardown do
30
+ I18n.locale = TestLanguage.current
31
+ end
32
+ end
33
+
34
+ ActionDispatch::IntegrationTest.class_eval do
35
+ include BeyondTheApiTestHelpers::Request::Helpers
36
+ include BeyondTheApiTestHelpers::Request::CommonAssertions
37
+ include BeyondTheApiTestHelpers::Request::IndexAssertions
38
+ include BeyondTheApiTestHelpers::Request::ShowAssertions
39
+ include BeyondTheApiTestHelpers::Request::CreateAssertions
40
+ include BeyondTheApiTestHelpers::Request::UpdateAssertions
41
+ include BeyondTheApiTestHelpers::Request::DestroyAssertions
42
+ include BeyondTheApiTestHelpers::Request::RestoreAssertions
43
+ end
@@ -0,0 +1,13 @@
1
+ module BeyondTheApiTestHelpers
2
+ module I18nTestHelpers
3
+ def assert_i18n_for_all_locales
4
+ I18n.available_locales.each do |locale|
5
+ ActiveRecord::Base.transaction do
6
+ Faker::Config.locale = I18n.locale = locale
7
+ yield
8
+ raise ActiveRecord::Rollback
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,51 @@
1
+ module BeyondTheApiTestHelpers
2
+ module Request
3
+ module CommonAssertions
4
+ def assert_request_needs_to_be_logged(url, http_method)
5
+ assert_i18n_for_all_locales do
6
+ send(http_method, url, params: @params, headers: @headers)
7
+ response_assertions(status: 403)
8
+ assert_equal true, json['redirect_to_login']
9
+ assert_equal I18n.t('api.errors.messages.require_user_authentication'), json['error']
10
+ end
11
+ end
12
+
13
+ def meta
14
+ json['meta']
15
+ end
16
+
17
+ def json_response_name
18
+ json[@response_name]
19
+ end
20
+
21
+ def request_instance_variables_set(params = {})
22
+ params.each do |key, value|
23
+ instance_variable_set("@#{key}", value)
24
+ end
25
+ @headers ||= {}
26
+ @params ||= {}
27
+ @response_name ||= (@klass || subject.class).to_s.underscore
28
+ end
29
+
30
+ def request_instance_variables_set_pluralize(params = {})
31
+ request_instance_variables_set(params)
32
+ @response_name = @response_name.pluralize
33
+ end
34
+
35
+ def assert_not_found_request
36
+ response_assertions(status: 404)
37
+ not_found_obj = @not_found_name ? I18n.t(@not_found_name) : subject.class.model_name.human
38
+ not_found_message = I18n.t('api.errors.messages.not_found',
39
+ obj: not_found_obj)
40
+ assert_equal not_found_message, json['error']
41
+ end
42
+
43
+ def assert_parameter_missing_request
44
+ response_assertions(status: 400)
45
+ parameter_missing_message = I18n.t('api.errors.messages.parameter_missing',
46
+ parameter: subject.class.to_s.underscore)
47
+ assert_equal parameter_missing_message, json['error']
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,82 @@
1
+ module BeyondTheApiTestHelpers
2
+ module Request
3
+ module CreateAssertions
4
+ def assert_request_create_valid(url, options = {})
5
+ request_instance_variables_set options
6
+ if required_login
7
+ assert_request_needs_to_be_logged(url, :post)
8
+ @headers.merge!(logged_in_headers)
9
+ end
10
+
11
+ assert_i18n_for_all_locales do
12
+ do_create_request_with_transaction(url)
13
+ end
14
+ end
15
+
16
+ def assert_request_create_missing_param(url, options = {})
17
+ request_instance_variables_set options
18
+ @headers.merge!(logged_in_headers) if required_login
19
+ post(url, params: {}, headers: @headers)
20
+ assert_parameter_missing_request
21
+ end
22
+
23
+ def assert_request_create_with_errors(url, invalid_params, options = {})
24
+ request_instance_variables_set options
25
+ @headers.merge!(logged_in_headers) if required_login
26
+
27
+ ActiveRecord::Base.transaction do
28
+ do_create_request_and_assert_invalid(url, invalid_params)
29
+ raise ActiveRecord::Rollback
30
+ end
31
+ end
32
+
33
+ def assert_request_create_not_found(urls_not_found, options = {})
34
+ request_instance_variables_set options
35
+ @headers.merge!(logged_in_headers) if required_login
36
+
37
+ urls_not_found.each do |url_not_found|
38
+ assert_request_create_not_found_i18n_all(url_not_found)
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def do_create_request_with_transaction(url)
45
+ ActiveRecord::Base.transaction do
46
+ do_create_request_and_assert(url)
47
+ Redis.current.flushdb if @flush_redis_db_between_i18n_tests
48
+ raise ActiveRecord::Rollback
49
+ end
50
+ end
51
+
52
+ # :reek:DuplicateMethodCall: { max_calls: 2 }
53
+ def do_create_request_and_assert(url)
54
+ klass = @klass || subject.class
55
+ old_count = klass.count
56
+ post(url, params: @params, headers: @headers)
57
+ response_assertions(status: 201)
58
+ assert_equal old_count + 1, klass.count unless @skip_count
59
+ assert_equal I18n.t(*@message), json['success']
60
+ assert_kind_of Hash, json_response_name unless @skip_json_response_name
61
+ end
62
+
63
+ # :reek:DuplicateMethodCall: { max_calls: 2 }
64
+ def do_create_request_and_assert_invalid(url, invalid_params)
65
+ klass = @klass || subject.class
66
+ old_count = klass.count
67
+ post(url, params: invalid_params, headers: @headers)
68
+ response_assertions(status: 422)
69
+ assert_equal old_count, klass.count
70
+ assert_kind_of Hash, json['errors']
71
+ assert_kind_of Array, json['full_error_messages']
72
+ end
73
+
74
+ def assert_request_create_not_found_i18n_all(url_not_found)
75
+ assert_i18n_for_all_locales do
76
+ post(url_not_found, params: @params, headers: @headers)
77
+ assert_not_found_request
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end