app_report 0.0.2

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,147 @@
1
+ require 'test_helper'
2
+ require 'app_report/api/fake_api'
3
+
4
+ class BaseAPITest < MiniTest::Unit::TestCase
5
+
6
+ def setup
7
+
8
+ AppReport.configure do |config|
9
+ config.app_name = 'app'
10
+ config.access_key = 'L3jC1SHoo3mqWZ2kT7m4'
11
+ config.secret_key = '6zD1sMPAYJdBj/SUMqi/BIqayWkjx3PSV1KaQyND'
12
+ end
13
+
14
+ @base_api_class = AppReport::API::Base
15
+ @base_api = @base_api_class.new
16
+
17
+
18
+ # using fake api to test custom params
19
+
20
+ @fake_api = AppReport::API::FakeAPI.new
21
+
22
+ @fake_api.params = {
23
+ :document => '123456789abc',
24
+ :id => 123
25
+ }
26
+ end
27
+
28
+ def test_assignment_of_params
29
+ base_api = @base_api_class.new
30
+ assert_includes base_api.instance_variables, :@params
31
+ end
32
+
33
+ def test_params_to_sign_return_an_empty_array
34
+ assert_equal [], @base_api.params_to_sign
35
+ end
36
+
37
+ def test_sign_params_return_a_string
38
+ assert_kind_of String, @base_api.sign_params({})
39
+ end
40
+
41
+ def test_signed_params_is_a_hash
42
+ assert_kind_of Hash, @base_api.signed_params
43
+ end
44
+
45
+ def test_signed_params_include_configured_keys
46
+ signed_params = @base_api.signed_params
47
+
48
+ [:app_name, :access_key].each do |key|
49
+ assert_includes signed_params, key
50
+ assert_equal AppReport.settings.send(key), signed_params[key]
51
+ end
52
+ end
53
+
54
+ def test_signed_params_include_expires
55
+ signed_params = @base_api.signed_params
56
+ assert_includes signed_params, 'expires'
57
+ end
58
+
59
+ def test_configured_keys_not_overwritten_by_params
60
+ @base_api.params[:app_name] = 'overwritten_app_name'
61
+ @base_api.params[:access_key] = 'overwritten_access_key'
62
+
63
+ signed_params = @base_api.signed_params
64
+
65
+ [:app_name, :access_key].each do |key|
66
+ assert_equal AppReport.settings.send(key), signed_params[key]
67
+ end
68
+ end
69
+
70
+ def test_expires_not_overwritten_by_params
71
+ expires = 100
72
+ @base_api.params[:expires] = expires
73
+ signed_params = @base_api.signed_params
74
+
75
+ refute_equal expires, signed_params[:expires]
76
+ end
77
+
78
+
79
+ def test_signed_params_include_all_params_including_not_signed
80
+ @fake_api.params[:foo] = 'new param value'
81
+ signed_params = @fake_api.signed_params
82
+
83
+ @fake_api.params.each do |key, value|
84
+ assert_includes signed_params, key
85
+ end
86
+ end
87
+
88
+ def test_signed_params_include_expires
89
+ assert_includes @base_api.signed_params, :expires
90
+ end
91
+
92
+ def test_signed_params_expires
93
+ expected = Time.now.to_i + AppReport.settings.expires_signed_url_after
94
+ calculated = @base_api.signed_params[:expires]
95
+
96
+ assert_equal expected, calculated
97
+ end
98
+
99
+ def test_signed_params_include_signature
100
+ assert_includes @base_api.signed_params, :signature
101
+ end
102
+
103
+ def test_sign_params_signature
104
+ params = {
105
+ :foo => 'bar',
106
+ :hey => "what's up?"
107
+ }
108
+
109
+ params_string = params.map { |k, v| v.to_s }.join
110
+
111
+ expected_signature = SimpleSigner.sign \
112
+ :secret_key => AppReport.settings.secret_key,
113
+ :string => params_string
114
+
115
+ given_signature = @base_api.sign_params params
116
+
117
+ assert_equal expected_signature, given_signature
118
+ end
119
+
120
+ def test_signed_params_signature
121
+
122
+ # expected
123
+
124
+ params = {}
125
+
126
+ @fake_api.params_to_sign.each { |param|
127
+ params[param] = @fake_api.params[param]
128
+ }
129
+
130
+ params.merge! :app_name => AppReport.settings.app_name,
131
+ :expires => Time.now.to_i + AppReport.settings.expires_signed_url_after
132
+
133
+ params_string = params.map { |k, v| v.to_s }.join
134
+
135
+
136
+ expected_signature = SimpleSigner.sign \
137
+ :secret_key => AppReport.settings.secret_key,
138
+ :string => params_string
139
+
140
+
141
+ # given
142
+
143
+ given_signature = @fake_api.signed_params[:signature]
144
+
145
+ assert_equal expected_signature, given_signature
146
+ end
147
+ end
@@ -0,0 +1,14 @@
1
+
2
+ # fake api used to test the AppReport::API::Base class
3
+
4
+ module AppReport
5
+ module API
6
+ class FakeAPI < AppReport::API::Base
7
+
8
+ def params_to_sign
9
+ [:app_name, :access_key, :document, :id, :inexistent_param]
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,121 @@
1
+ require 'test_helper'
2
+ require 'tempfile'
3
+
4
+ class JasperAPITest < MiniTest::Unit::TestCase
5
+
6
+ def setup
7
+
8
+ AppReport.configure do |config|
9
+ config.app_name = 'app-name'
10
+ config.access_key = 'access-key'
11
+ config.secret_key = 'secret-key'
12
+ end
13
+
14
+ @default_report_options = {
15
+ :template_name => 'some_report',
16
+ :data_type => 'xml',
17
+ :data => '<?xml version="1.0" encoding="utf-8"?><root><node></node></root>',
18
+ :xpath_expression => '/root/node',
19
+ :args => { :key => 'value' }
20
+ }
21
+
22
+ @api = AppReport::API::Jasper.new
23
+ @report = AppReport::Report::Jasper.new @default_report_options
24
+ end
25
+
26
+ def test_params_to_sign
27
+ params_to_sign = @api.params_to_sign
28
+
29
+ [:app_name, :template_name, :expires].each do |key|
30
+ assert_includes params_to_sign, key
31
+ end
32
+ end
33
+
34
+ # validation of report argument
35
+ def test_validation_of_report_arg
36
+ [:validates_report!, :build!].each do |method|
37
+ test_error_msg = "a validation error must be raised by #{method}"
38
+
39
+ assert_raises AppReport::Errors::ValidationError, test_error_msg do
40
+ @api.send method, nil
41
+ end
42
+ end
43
+ end
44
+
45
+ # report validation, build! must call the validates! method of the report.
46
+ def test_report_validation
47
+ report = AppReport::Report::Jasper.new
48
+ test_error_msg = 'a validation error must be raised by report in build'
49
+
50
+ assert_raises AppReport::Errors::ValidationError, test_error_msg do
51
+ @api.build! report
52
+ end
53
+ end
54
+
55
+ def test_decode_blank_report_error
56
+ [nil, ''].each { |nil_or_empty|
57
+ response_body = MultiJson.dump({ 'report' => nil_or_empty })
58
+ test_error_msg = "an error must be raised, when report is #{nil_or_empty.nil? ? 'nil' : 'empty'}"
59
+
60
+ assert_raises AppReport::Errors::APIResponseError, test_error_msg do
61
+ @api.decode_report! response_body
62
+ end
63
+ }
64
+ end
65
+
66
+ def test_decode_report
67
+ expected = 'hello, am I a report?'
68
+
69
+ response_body = MultiJson.dump({
70
+ 'report' => {
71
+ 'encoding' => 'base64',
72
+ 'encoded' => Base64.encode64(expected)
73
+ }
74
+ })
75
+
76
+ given = @api.decode_report! response_body
77
+
78
+ assert_equal expected, given
79
+ end
80
+
81
+ def test_build_set_report_attributes_as_params
82
+ stub_post('/api/v1/factory/jasper/build.json').to_return \
83
+ :body => fixture('report.json'),
84
+ :headers => { :content_type => "application/json; charset=utf-8" }
85
+
86
+ @api.build! @report
87
+
88
+ @report.attributes.each do |key, value|
89
+ assert_includes @api.params, key
90
+ assert_equal @api.params[key], @report.attributes[key]
91
+ end
92
+ end
93
+
94
+
95
+ def test_build
96
+ stub_post('/api/v1/factory/jasper/build.json').to_return \
97
+ :body => fixture('report.json'),
98
+ :headers => { :content_type => "application/json; charset=utf-8" }
99
+
100
+ expected_pdf_raw = fixture('report.pdf').read
101
+ given_pdf_raw = @api.build! @report
102
+
103
+
104
+ # writting a file, because
105
+ # assert_equal(expected_pdf_raw, given_pdf_raw) fails
106
+ #
107
+ # probably it's not the best solution, but for now it works!
108
+
109
+ f = Tempfile.new 'given.pdf'
110
+
111
+ begin
112
+ f.write given_pdf_raw
113
+ f.rewind
114
+
115
+ assert_equal expected_pdf_raw, f.read
116
+ ensure
117
+ f.close
118
+ end
119
+ end
120
+
121
+ end
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ class ClientTest < MiniTest::Unit::TestCase
4
+
5
+ def test_default_endpoint
6
+ assert_equal \
7
+ 'http://reports.simpleservic.es',
8
+ AppReport::Client.endpoint
9
+ end
10
+
11
+ def test_connection_return_faraday_connection
12
+ assert_kind_of Faraday::Connection, AppReport::Client.connection
13
+ end
14
+
15
+ def test_post_return_faraday_response
16
+ path = '/faraday_response.json'
17
+
18
+ stub_post(path).to_return \
19
+ :body => {},
20
+ :headers => { :content_type => "application/json; charset=utf-8" }
21
+
22
+ assert_kind_of Faraday::Response, AppReport::Client.post(path, {})
23
+ end
24
+
25
+ def test_raise_error_messages
26
+ assert_raises AppReport::Errors::APIError do
27
+ AppReport::Client.raise_error_messages! fixture('error_messages.json')
28
+ end
29
+ end
30
+
31
+ def test_post_raise_error_messages
32
+ path = '/error_messages.json'
33
+
34
+ stub_post(path).to_return \
35
+ :body => fixture('error_messages.json'),
36
+ :headers => { :content_type => "application/json; charset=utf-8" }
37
+
38
+ assert_raises AppReport::Errors::APIError do
39
+ AppReport::Client.post(path, {})
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,61 @@
1
+ require 'test_helper'
2
+
3
+ class ConfigurationTest < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ @configuration_class = AppReport::Configuration
7
+
8
+ @default_settings = {
9
+ :app_name => 'app',
10
+ :access_key => '123',
11
+ :secret_key => '123abc'
12
+ }
13
+ end
14
+
15
+ def test_response_to_config_keys
16
+ assert_respond_to @configuration_class, :config_keys
17
+ end
18
+
19
+ def test_immutability_of_config_keys
20
+ @configuration_class.config_keys.delete :app_name
21
+ assert_includes @configuration_class.config_keys, :app_name
22
+ end
23
+
24
+ def test_response_to_each_config_keys
25
+ @configuration_class.config_keys.each do |key|
26
+ assert_respond_to @configuration_class, key
27
+ end
28
+ end
29
+
30
+ def test_response_to_validates
31
+ assert_respond_to @configuration_class, :validates!
32
+ end
33
+
34
+ def test_validation_of_expires_signed_url_after
35
+ assert_raises AppReport::Errors::InvalidConfigurationError do
36
+ @configuration_class.expires_signed_url_after = nil
37
+ end
38
+ end
39
+
40
+ def test_validation_of_required_keys
41
+ @configuration_class.config_keys.each { |key|
42
+ next if key == :expires_signed_url_after
43
+
44
+ @default_settings.each do |k, v|
45
+ @configuration_class.send "#{k}=", v
46
+ end
47
+
48
+ @configuration_class.send "#{key}=", nil
49
+
50
+ test_error_msg = "a RequiredConfigurationError must be raised when #{key} is empty"
51
+ assert_raises AppReport::Errors::RequiredConfigurationError, test_error_msg do
52
+ @configuration_class.validates!
53
+ end
54
+ }
55
+ end
56
+
57
+ def test_default_expires_signed_url_after_value
58
+ assert_equal 600, @configuration_class.expires_signed_url_after
59
+ end
60
+
61
+ end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ class DecoderTest < MiniTest::Unit::TestCase
4
+
5
+ def test_decode_base64
6
+ expected = 'Boring string...'
7
+ encoded = Base64.encode64(expected)
8
+ given = AppReport::Decoder.decode_base64 encoded
9
+
10
+ assert_equal expected, given
11
+ end
12
+
13
+ def test_decode_blank_encoded_error
14
+ [nil, ''].each { |nil_or_empty|
15
+ test_error_msg = "an error must be raised, when encoded is #{nil_or_empty.nil? ? 'nil' : 'empty'}"
16
+
17
+ assert_raises AppReport::Errors::DecoderError, test_error_msg do
18
+ AppReport::Decoder.decode nil_or_empty
19
+ end
20
+ }
21
+ end
22
+
23
+ def test_decode_blank_encoding_error
24
+ [nil, ''].each { |nil_or_empty|
25
+ test_error_msg = "an error must be raised, when encoding is #{nil_or_empty.nil? ? 'nil' : 'empty'}"
26
+
27
+ assert_raises AppReport::Errors::DecoderError, test_error_msg do
28
+ AppReport::Decoder.decode 'encoded', nil_or_empty
29
+ end
30
+ }
31
+ end
32
+
33
+ def test_decode_with_base64_encoding
34
+ expected = "Sexy string!"
35
+ encoded = Base64.encode64(expected)
36
+ given = AppReport::Decoder.decode encoded, 'base64'
37
+
38
+ assert_equal expected, given
39
+ end
40
+
41
+ def test_unsupported_encoding_error
42
+ assert_raises AppReport::Errors::DecoderError do
43
+ AppReport::Decoder.decode 'str', 'unsuported-encoding'
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1,83 @@
1
+ require 'test_helper'
2
+
3
+ class JasperReportTest < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+
7
+ @default_attributes = {
8
+ :template_name => 'my_super_template',
9
+ :data_type => 'xml',
10
+ :data => '<?xml version="1.0" encoding="utf-8"?><root><node></node></root>',
11
+ :xpath_expression => '/root/node',
12
+ :args => { :key => 'value' }
13
+ }
14
+
15
+ @report_class = AppReport::Report::Jasper
16
+ @report = @report_class.new @default_attributes
17
+ end
18
+
19
+
20
+ # attributes
21
+
22
+ def test_assignment_of_attributes
23
+ report = @report_class.new
24
+ assert_includes report.instance_variables, :@attributes
25
+ end
26
+
27
+ def test_response_to_attributes
28
+ assert_respond_to @report, :attributes
29
+ end
30
+
31
+ def test_immutability_of_attributes_hash
32
+ @report.attributes[:template_name] = 'it_was_overwritten!'
33
+ refute_equal @report.template_name, 'it_was_overwritten!'
34
+ end
35
+
36
+ def test_response_to_each_option
37
+ @report.attributes.each do |key, value|
38
+ assert_respond_to @report, key, "report must respond to #{key}"
39
+ end
40
+ end
41
+
42
+ def test_return_of_each_option
43
+ @report.attributes.each do |key, value|
44
+ assert_equal value, @report.send(key), "#{key} must return the value sent for #{key} option"
45
+ end
46
+ end
47
+
48
+
49
+ # validations
50
+
51
+ def test_response_to_validates_each_option
52
+ @report.attributes.each do |key, value|
53
+ method = "validates_#{key}!"
54
+ assert_respond_to @report, method, "report must respond to #{method}"
55
+ end
56
+ end
57
+
58
+ def test_response_to_validates_all_attributes
59
+ assert_respond_to @report, :validates_all_attributes!
60
+ end
61
+
62
+ def test_response_to_alias_of_validates_all_attributes
63
+ assert_respond_to @report, :validates!
64
+ end
65
+
66
+ def test_response_to_validates_presence_of
67
+ assert_respond_to @report, :validates_presence_of!
68
+ end
69
+
70
+ def test_validation_of_template_name
71
+ report = @report_class.new @default_attributes.except :template_name
72
+
73
+ [:validates_template_name!, :validates_all_attributes!, :validates! ].each do |method|
74
+ test_error_msg = "a validation error must be raised by #{method}"
75
+
76
+ assert_raises AppReport::Errors::ValidationError, test_error_msg do
77
+ report.send method
78
+ end
79
+ end
80
+ end
81
+
82
+ # TODO test validation of other attributes in client side.
83
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class AppReportTest < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ AppReport.configure do |config|
7
+ config.app_name = 'app'
8
+ config.access_key = '123'
9
+ config.secret_key = '123abc'
10
+ end
11
+ end
12
+
13
+ def test_response_to_configure
14
+ assert_respond_to AppReport, :configure
15
+ end
16
+
17
+ def test_configure_block_yields_configuration
18
+ AppReport.configure do |config|
19
+ assert_equal AppReport::Configuration, config
20
+ end
21
+ end
22
+
23
+ def test_response_to_settings
24
+ assert_respond_to AppReport, :settings
25
+ end
26
+
27
+ def test_settings_return_configuration
28
+ assert_equal AppReport::Configuration, AppReport::settings
29
+ end
30
+
31
+ def test_configure_block_change_settings
32
+ AppReport.configure do |config|
33
+ config.app_name = 'app_name1'
34
+ end
35
+
36
+ assert_equal 'app_name1', AppReport.settings.app_name
37
+ end
38
+
39
+ def test_settings_changed_via_atributes
40
+ AppReport.settings.app_name = 'app_name2'
41
+
42
+ AppReport.configure do |config|
43
+ assert_equal 'app_name2', config.app_name
44
+ end
45
+ end
46
+
47
+ def test_settings_validation
48
+ assert_raises AppReport::Errors::RequiredConfigurationError do
49
+ AppReport.configure do |config|
50
+ config.app_name = nil
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,16 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/colorize'
3
+ require 'webmock/minitest'
4
+ require 'app_report'
5
+
6
+ def stub_post(path)
7
+ stub_request(:post, AppReport::Client::endpoint + path)
8
+ end
9
+
10
+ def fixture_path
11
+ File.expand_path '../fixtures', __FILE__
12
+ end
13
+
14
+ def fixture file
15
+ File.new File.join(fixture_path, file)
16
+ end