esp_sdk 1.1.0 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -2
  3. data/.rubocop.yml +53 -6
  4. data/.ruby-version +1 -1
  5. data/.travis.yml +8 -2
  6. data/Gemfile.lock +171 -0
  7. data/Guardfile +47 -0
  8. data/README.md +230 -5
  9. data/Rakefile +14 -1
  10. data/assets/logo.png +0 -0
  11. data/bin/esp_console +71 -0
  12. data/esp_sdk.gemspec +27 -18
  13. data/lib/esp/exceptions.rb +3 -0
  14. data/lib/esp/extensions/active_resource/formats/json_api_format.rb +105 -0
  15. data/lib/esp/extensions/active_resource/paginated_collection.rb +198 -0
  16. data/lib/esp/extensions/active_resource/validations.rb +45 -0
  17. data/lib/esp/resources/alert.rb +135 -0
  18. data/lib/esp/resources/cloud_trail_event.rb +45 -0
  19. data/lib/esp/resources/concerns/stat_totals.rb +79 -0
  20. data/lib/esp/resources/contact_request.rb +42 -0
  21. data/lib/esp/resources/custom_signature.rb +224 -0
  22. data/lib/esp/resources/dashboard.rb +31 -0
  23. data/lib/esp/resources/external_account.rb +89 -0
  24. data/lib/esp/resources/organization.rb +61 -0
  25. data/lib/esp/resources/region.rb +46 -0
  26. data/lib/esp/resources/report.rb +100 -0
  27. data/lib/esp/resources/resource.rb +66 -0
  28. data/lib/esp/resources/service.rb +31 -0
  29. data/lib/esp/resources/signature.rb +106 -0
  30. data/lib/esp/resources/stat.rb +124 -0
  31. data/lib/esp/resources/stat_custom_signature.rb +121 -0
  32. data/lib/esp/resources/stat_region.rb +121 -0
  33. data/lib/esp/resources/stat_service.rb +121 -0
  34. data/lib/esp/resources/stat_signature.rb +121 -0
  35. data/lib/esp/resources/sub_organization.rb +69 -0
  36. data/lib/esp/resources/suppression/region.rb +99 -0
  37. data/lib/esp/resources/suppression/signature.rb +107 -0
  38. data/lib/esp/resources/suppression/unique_identifier.rb +60 -0
  39. data/lib/esp/resources/suppression.rb +86 -0
  40. data/lib/esp/resources/tag.rb +45 -0
  41. data/lib/esp/resources/team.rb +79 -0
  42. data/lib/esp/resources/user.rb +46 -0
  43. data/lib/esp/version.rb +3 -0
  44. data/lib/esp.rb +95 -0
  45. data/lib/esp_sdk.rb +9 -45
  46. data/lib/tasks/rubocop.rake +2 -0
  47. data/lib/tasks/testing.rake +3 -0
  48. data/rdoc/ActiveResource/Formats.html +176 -0
  49. data/rdoc/ActiveResource/PaginatedCollection.html +910 -0
  50. data/rdoc/ActiveResource.html +180 -0
  51. data/rdoc/ESP/Alert.html +771 -0
  52. data/rdoc/ESP/CloudTrailEvent.html +375 -0
  53. data/rdoc/ESP/ContactRequest.html +366 -0
  54. data/rdoc/ESP/CustomSignature.html +746 -0
  55. data/rdoc/ESP/Dashboard.html +355 -0
  56. data/rdoc/ESP/ExternalAccount.html +565 -0
  57. data/rdoc/ESP/Organization.html +590 -0
  58. data/rdoc/ESP/Region.html +399 -0
  59. data/rdoc/ESP/Report.html +622 -0
  60. data/rdoc/ESP/Service.html +380 -0
  61. data/rdoc/ESP/Signature.html +555 -0
  62. data/rdoc/ESP/Stat.html +1778 -0
  63. data/rdoc/ESP/StatCustomSignature.html +1599 -0
  64. data/rdoc/ESP/StatRegion.html +1598 -0
  65. data/rdoc/ESP/StatService.html +1598 -0
  66. data/rdoc/ESP/StatSignature.html +1598 -0
  67. data/rdoc/ESP/SubOrganization.html +540 -0
  68. data/rdoc/ESP/Suppression/Region.html +454 -0
  69. data/rdoc/ESP/Suppression/Signature.html +470 -0
  70. data/rdoc/ESP/Suppression/UniqueIdentifier.html +417 -0
  71. data/rdoc/ESP/Suppression.html +649 -0
  72. data/rdoc/ESP/Tag.html +371 -0
  73. data/rdoc/ESP/Team.html +584 -0
  74. data/rdoc/ESP/User.html +483 -0
  75. data/rdoc/ESP.html +546 -0
  76. data/rdoc/README_md.html +501 -0
  77. data/rdoc/created.rid +30 -0
  78. data/rdoc/images/add.png +0 -0
  79. data/rdoc/images/arrow_up.png +0 -0
  80. data/rdoc/images/brick.png +0 -0
  81. data/rdoc/images/brick_link.png +0 -0
  82. data/rdoc/images/bug.png +0 -0
  83. data/rdoc/images/bullet_black.png +0 -0
  84. data/rdoc/images/bullet_toggle_minus.png +0 -0
  85. data/rdoc/images/bullet_toggle_plus.png +0 -0
  86. data/rdoc/images/date.png +0 -0
  87. data/rdoc/images/delete.png +0 -0
  88. data/rdoc/images/find.png +0 -0
  89. data/rdoc/images/loadingAnimation.gif +0 -0
  90. data/rdoc/images/macFFBgHack.png +0 -0
  91. data/rdoc/images/package.png +0 -0
  92. data/rdoc/images/page_green.png +0 -0
  93. data/rdoc/images/page_white_text.png +0 -0
  94. data/rdoc/images/page_white_width.png +0 -0
  95. data/rdoc/images/plugin.png +0 -0
  96. data/rdoc/images/ruby.png +0 -0
  97. data/rdoc/images/tag_blue.png +0 -0
  98. data/rdoc/images/tag_green.png +0 -0
  99. data/rdoc/images/transparent.png +0 -0
  100. data/rdoc/images/wrench.png +0 -0
  101. data/rdoc/images/wrench_orange.png +0 -0
  102. data/rdoc/images/zoom.png +0 -0
  103. data/rdoc/index.html +134 -0
  104. data/rdoc/js/darkfish.js +155 -0
  105. data/rdoc/js/jquery.js +4 -0
  106. data/rdoc/js/navigation.js +142 -0
  107. data/rdoc/js/search.js +94 -0
  108. data/rdoc/js/search_index.js +1 -0
  109. data/rdoc/js/searcher.js +228 -0
  110. data/rdoc/rdoc.css +595 -0
  111. data/rdoc/table_of_contents.html +927 -0
  112. data/test/esp/extensions/active_resource/formats/json_api_format_test.rb +109 -0
  113. data/test/esp/extensions/active_resource/paginated_collection_test.rb +435 -0
  114. data/test/esp/extensions/active_resource/validations_test.rb +59 -0
  115. data/test/esp/resources/alert_test.rb +263 -0
  116. data/test/esp/resources/cloud_trail_event_test.rb +98 -0
  117. data/test/esp/resources/contact_request_test.rb +73 -0
  118. data/test/esp/resources/custom_signature_test.rb +241 -0
  119. data/test/esp/resources/dashboard_test.rb +71 -0
  120. data/test/esp/resources/external_account_test.rb +125 -0
  121. data/test/esp/resources/organization_test.rb +175 -0
  122. data/test/esp/resources/region_test.rb +84 -0
  123. data/test/esp/resources/report_test.rb +180 -0
  124. data/test/esp/resources/resource_test.rb +183 -0
  125. data/test/esp/resources/service_test.rb +64 -0
  126. data/test/esp/resources/signature_test.rb +177 -0
  127. data/test/esp/resources/stat_custom_signature_test.rb +115 -0
  128. data/test/esp/resources/stat_region_test.rb +114 -0
  129. data/test/esp/resources/stat_service_test.rb +114 -0
  130. data/test/esp/resources/stat_signature_test.rb +115 -0
  131. data/test/esp/resources/stat_test.rb +159 -0
  132. data/test/esp/resources/sub_organization_test.rb +127 -0
  133. data/test/esp/resources/suppression/region_test.rb +115 -0
  134. data/test/esp/resources/suppression/signature_test.rb +117 -0
  135. data/test/esp/resources/suppression/unique_identifier_test.rb +79 -0
  136. data/test/esp/resources/suppression_test.rb +226 -0
  137. data/test/esp/resources/tag_test.rb +98 -0
  138. data/test/esp/resources/team_test.rb +140 -0
  139. data/test/esp/resources/user_test.rb +113 -0
  140. data/test/esp_test.rb +139 -0
  141. data/test/factories/alerts.rb +234 -0
  142. data/test/factories/cloud_trail_events.rb +16 -0
  143. data/test/factories/contact_requests.rb +14 -0
  144. data/test/factories/custom_signatures.rb +30 -0
  145. data/test/factories/dashboards.rb +91 -0
  146. data/test/factories/errors.rb +24 -0
  147. data/test/factories/external_accounts.rb +44 -0
  148. data/test/factories/organizations.rb +48 -0
  149. data/test/factories/regions.rb +9 -0
  150. data/test/factories/reports.rb +56 -0
  151. data/test/factories/services.rb +12 -0
  152. data/test/factories/signatures.rb +28 -0
  153. data/test/factories/stat_custom_signatures.rb +97 -0
  154. data/test/factories/stat_regions.rb +97 -0
  155. data/test/factories/stat_services.rb +97 -0
  156. data/test/factories/stat_signautures.rb +97 -0
  157. data/test/factories/stats.rb +129 -0
  158. data/test/factories/sub_organizations.rb +34 -0
  159. data/test/factories/suppression/regions.rb +90 -0
  160. data/test/factories/suppression/signatures.rb +117 -0
  161. data/test/factories/suppression/unique_identifiers.rb +111 -0
  162. data/test/factories/suppressions.rb +71 -0
  163. data/test/factories/tags.rb +12 -0
  164. data/test/factories/teams.rb +32 -0
  165. data/test/factories/users.rb +54 -0
  166. data/test/json_strategy.rb +25 -0
  167. data/test/test_helper.rb +44 -5
  168. metadata +387 -119
  169. data/bin/esp_repl +0 -60
  170. data/lib/esp_sdk/api.rb +0 -33
  171. data/lib/esp_sdk/client.rb +0 -62
  172. data/lib/esp_sdk/configure.rb +0 -40
  173. data/lib/esp_sdk/end_points/base.rb +0 -102
  174. data/lib/esp_sdk/end_points/contact_requests.rb +0 -6
  175. data/lib/esp_sdk/end_points/custom_signatures.rb +0 -41
  176. data/lib/esp_sdk/end_points/dashboard.rb +0 -35
  177. data/lib/esp_sdk/end_points/external_accounts.rb +0 -9
  178. data/lib/esp_sdk/end_points/organizations.rb +0 -6
  179. data/lib/esp_sdk/end_points/reports.rb +0 -6
  180. data/lib/esp_sdk/end_points/services.rb +0 -6
  181. data/lib/esp_sdk/end_points/signatures.rb +0 -39
  182. data/lib/esp_sdk/end_points/sub_organizations.rb +0 -6
  183. data/lib/esp_sdk/end_points/teams.rb +0 -6
  184. data/lib/esp_sdk/end_points/users.rb +0 -6
  185. data/lib/esp_sdk/exceptions.rb +0 -8
  186. data/lib/esp_sdk/extensions/rest_client/request.rb +0 -9
  187. data/lib/esp_sdk/repl.rb +0 -61
  188. data/lib/esp_sdk/version.rb +0 -3
  189. data/test/esp_sdk/api_test.rb +0 -36
  190. data/test/esp_sdk/client_test.rb +0 -129
  191. data/test/esp_sdk/configure_test.rb +0 -65
  192. data/test/esp_sdk/end_points/.keep +0 -0
  193. data/test/esp_sdk/end_points/base_test.rb +0 -230
  194. data/test/esp_sdk/end_points/custom_signatures_test.rb +0 -90
  195. data/test/esp_sdk/end_points/dashboard_test.rb +0 -55
  196. data/test/esp_sdk/end_points/external_accounts_test.rb +0 -20
  197. data/test/esp_sdk/end_points/signatures_test.rb +0 -83
  198. data/test/esp_sdk/exceptions_test.rb +0 -41
  199. data/test/esp_sdk_test.rb +0 -70
@@ -0,0 +1,183 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class ResourceTest < ActiveSupport::TestCase
5
+ context ESP::Resource do
6
+ context 'with ESP::Team' do
7
+ should 'set the Content-Type to application/vnd.api+json' do
8
+ stub_request(:get, %r{teams/3.json*}).to_return(body: json(:team))
9
+
10
+ ESP::Team.find(3)
11
+
12
+ assert_requested(:get, %r{teams/3.json*}) do |req|
13
+ assert_equal ActiveResource::Formats::JsonAPIFormat.mime_type, req.headers['Content-Type']
14
+ end
15
+ end
16
+
17
+ should 'set the HMAC needed Authorization header' do
18
+ ESP.access_key_id ||= ApiAuth.generate_secret_key
19
+ ESP.secret_access_key ||= ApiAuth.generate_secret_key
20
+ ESP::Team.hmac_access_id = ESP.access_key_id
21
+ ESP::Team.hmac_secret_key = ESP.secret_access_key
22
+ stub_request(:get, %r{teams/3.json*}).to_return(body: json(:team))
23
+
24
+ ESP::Team.find(3)
25
+
26
+ assert_requested(:get, %r{teams/3.json*}) do |req|
27
+ # Remove non word chars to prevent regex matching errors.
28
+ assert_match(/APIAuth#{ESP.access_key_id.gsub(/\W/, '')}/, req.headers['Authorization'].gsub(/\W/, ''))
29
+ end
30
+ end
31
+
32
+ context '.where' do
33
+ should 'not be implemented yet' do
34
+ assert_raise ESP::NotImplementedError do
35
+ ESP::Team.where(id: 1)
36
+ end
37
+ end
38
+ end
39
+
40
+ context '#serializable_hash' do
41
+ should 'format per json api standard' do
42
+ t = build(:team)
43
+
44
+ h = t.serializable_hash
45
+
46
+ assert_equal t.id, h['data']['id']
47
+ assert_equal 'teams', h['data']['type']
48
+ assert_equal t.name, h['data']['attributes']['name']
49
+ end
50
+
51
+ should 'should not include the id if it is not present' do
52
+ t = build(:team, id: nil)
53
+
54
+ h = t.serializable_hash
55
+
56
+ assert_equal false, h['data'].key?('id')
57
+ end
58
+ end
59
+
60
+ context '.find' do
61
+ should 'call the show method when finding by single id' do
62
+ stub_request(:get, %r{teams/3.json*}).to_return(body: json(:team))
63
+
64
+ ESP::Team.find(3)
65
+
66
+ assert_requested(:get, %r{teams/3.json*})
67
+ end
68
+
69
+ should 'build query string inside filter param and ad _eq when single value' do
70
+ stub_request(:get, /teams.json*/).to_return(body: json_list(:team, 2))
71
+
72
+ ESP::Team.find(:all, params: { id: 3 })
73
+
74
+ assert_requested(:get, /teams.json*/) do |req|
75
+ query = Rack::Utils.parse_nested_query(CGI.unescape(req.uri.query))
76
+ assert_equal true, query.key?('filter')
77
+ assert_equal true, query['filter'].key?('id_eq')
78
+ assert_equal '3', query['filter']['id_eq']
79
+ end
80
+ end
81
+
82
+ should 'build query string inside filter param and ad _in when multiple values' do
83
+ stub_request(:get, /teams.json*/).to_return(body: json_list(:team, 2))
84
+
85
+ ESP::Team.find(:all, params: { id: [3, 4] })
86
+
87
+ assert_requested(:get, /teams.json*/) do |req|
88
+ query = Rack::Utils.parse_nested_query(CGI.unescape(req.uri.query))
89
+ assert_equal true, query.key?('filter')
90
+ assert_equal true, query['filter'].key?('id_in')
91
+ assert_equal({ "0" => "3", "1" => "4" }, query['filter']['id_in'])
92
+ end
93
+ end
94
+ end
95
+
96
+ should 'not put page parameter inside filter parameter' do
97
+ stub_request(:get, /teams.json*/).to_return(body: json_list(:team, 2))
98
+
99
+ ESP::Team.find(:all, params: { id: 3, page: { number: 2, size: 3 } })
100
+
101
+ assert_requested(:get, /teams.json*/) do |req|
102
+ query = Rack::Utils.parse_nested_query(CGI.unescape(req.uri.query))
103
+ assert_equal true, query.key?('filter')
104
+ assert_equal '2', query['page']['number']
105
+ end
106
+ end
107
+
108
+ should 'add the include option to param' do
109
+ stub_request(:get, /teams.json*/).to_return(body: json_list(:team, 2))
110
+
111
+ ESP::Team.find(:all, include: 'organization')
112
+
113
+ assert_requested(:get, /teams.json*/) do |req|
114
+ query = Rack::Utils.parse_nested_query(CGI.unescape(req.uri.query))
115
+ assert_equal true, query.key?('include')
116
+ assert_equal 'organization', query['include']
117
+ end
118
+ end
119
+
120
+ should 'add the include option to param when finding by id' do
121
+ stub_request(:get, %r{teams/2.json*}).to_return(body: json_list(:team, 2))
122
+
123
+ ESP::Team.find(2, include: 'organization')
124
+
125
+ assert_requested(:get, %r{teams/2.json*}) do |req|
126
+ query = Rack::Utils.parse_nested_query(CGI.unescape(req.uri.query))
127
+ assert_equal true, query.key?('include')
128
+ assert_equal 'organization', query['include']
129
+ end
130
+ end
131
+
132
+ should 'not ransackize the filter parameter if it is passed' do
133
+ stub_request(:get, /teams.json*/).to_return(body: json_list(:team, 2))
134
+
135
+ ESP::Team.find(:all, params: { filter: { id_eq: 3 }, page: { number: 2, size: 3 } })
136
+
137
+ assert_requested(:get, /teams.json*/) do |req|
138
+ query = Rack::Utils.parse_nested_query(CGI.unescape(req.uri.query))
139
+ assert_equal true, query.key?('filter')
140
+ assert_equal true, query['filter'].key?('id_eq')
141
+ end
142
+ end
143
+
144
+ should 'add the from attribute to PaginatedCollection objects when from is supplied' do
145
+ stub_request(:get, /teams.json*/).to_return(body: json_list(:team, 2))
146
+
147
+ teams = ESP::Team.find(:all, from: "#{Team.prefix}teams.json", params: { filter: { id_eq: 3 }, page: { number: 2, size: 3 } })
148
+
149
+ assert_equal '/api/v2/teams.json', teams.from
150
+ end
151
+ end
152
+
153
+ context 'live calls' do
154
+ setup do
155
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
156
+ WebMock.allow_net_connect!
157
+ @team = ESP::Team.last
158
+ skip "Live DB does not have any teams. Add a team and run tests again." if @team.blank?
159
+ end
160
+
161
+ teardown do
162
+ WebMock.disable_net_connect!
163
+ end
164
+
165
+ context 'with ESP::Team' do
166
+ should 'find a team' do
167
+ t = ESP::Team.find(@team.id)
168
+
169
+ assert_equal ESP::Team, t.class
170
+ assert_equal @team.id, t.id
171
+ end
172
+
173
+ should 'find teams' do
174
+ t = ESP::Team.find(:all, params: { id: @team.id })
175
+
176
+ assert_equal ESP::Team, t.resource_class
177
+ assert_equal 1, t.count
178
+ end
179
+ end
180
+ end
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,64 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class ServiceTest < ActiveSupport::TestCase
5
+ context ESP::Service do
6
+ context '#signatures' do
7
+ should 'call the api' do
8
+ s = build(:service)
9
+ stub_request(:get, /signatures.json*/).to_return(body: json_list(:signature, 2))
10
+
11
+ s.signatures
12
+
13
+ assert_requested(:get, /signatures.json*/) do |req|
14
+ assert_equal "filter[service_id_eq]=#{s.id}", URI.unescape(req.uri.query)
15
+ end
16
+ end
17
+ end
18
+
19
+ context '#create' do
20
+ should 'not be implemented' do
21
+ assert_raises ESP::NotImplementedError do
22
+ ESP::Service.create(name: 'test')
23
+ end
24
+ end
25
+ end
26
+
27
+ context '#destroy' do
28
+ should 'not be implemented' do
29
+ s = build(:service)
30
+ assert_raises ESP::NotImplementedError do
31
+ s.destroy
32
+ end
33
+ end
34
+ end
35
+
36
+ context 'live calls' do
37
+ setup do
38
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
39
+ WebMock.allow_net_connect!
40
+ @service = ESP::Service.last
41
+ skip "Live DB does not have any services. Add a service and run tests again." if @service.blank?
42
+ end
43
+
44
+ teardown do
45
+ WebMock.disable_net_connect!
46
+ end
47
+
48
+ context '#signatures' do
49
+ should 'return an array of signatures' do
50
+ signatures = @service.signatures
51
+
52
+ assert_equal ESP::Signature, signatures.resource_class
53
+ end
54
+ end
55
+
56
+ context '#CRUD' do
57
+ should 'be able to read' do
58
+ assert_not_nil @service, @service.inspect
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,177 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class SignatureTest < ActiveSupport::TestCase
5
+ context ESP::Signature do
6
+ context '#service' do
7
+ should 'call the api' do
8
+ s = build(:signature, service_id: 4)
9
+ stubbed_service = stub_request(:get, %r{services/#{s.service_id}.json*}).to_return(body: json(:service))
10
+
11
+ s.service
12
+
13
+ assert_requested(stubbed_service)
14
+ end
15
+ end
16
+
17
+ context '#create' do
18
+ should 'not be implemented' do
19
+ assert_raises ESP::NotImplementedError do
20
+ ESP::Signature.create(name: 'test')
21
+ end
22
+ end
23
+ end
24
+
25
+ context '#update' do
26
+ should 'not be implemented' do
27
+ signature = build(:signature)
28
+ assert_raises ESP::NotImplementedError do
29
+ signature.save
30
+ end
31
+ end
32
+ end
33
+
34
+ context '#destroy' do
35
+ should 'not be implemented' do
36
+ s = build(:signature)
37
+ assert_raises ESP::NotImplementedError do
38
+ s.destroy
39
+ end
40
+ end
41
+ end
42
+
43
+ context '#run' do
44
+ should 'call the api and pass params' do
45
+ signature = build(:signature)
46
+ stub_request(:post, %r{signatures/#{signature.id}/run.json*}).to_return(body: json_list(:alert, 2))
47
+
48
+ alerts = signature.run(external_account_id: 3, regions: 'param2')
49
+
50
+ assert_requested(:post, %r{signatures/#{signature.id}/run.json}) do |req|
51
+ body = JSON.parse req.body
52
+ assert_equal 3, body['data']['attributes']['external_account_id']
53
+ assert_equal ['param2'], body['data']['attributes']['regions']
54
+ end
55
+ assert_equal ESP::Alert, alerts.resource_class
56
+ end
57
+
58
+ should 'not throw an error if an error is returned' do
59
+ signature = build(:signature)
60
+ error = ActiveResource::BadRequest.new('')
61
+ error_response = json(:error)
62
+ response = mock(body: error_response, code: '400')
63
+ error.stubs(:response).returns(response)
64
+ ActiveResource::Connection.any_instance.expects(:post).raises(error)
65
+
66
+ assert_nothing_raised do
67
+ result = signature.run(external_account_id: 3, regions: 'param2')
68
+ assert_equal JSON.parse(error_response)['errors'].first['title'], result.errors.full_messages.first
69
+ end
70
+ end
71
+ end
72
+
73
+ context '.run!' do
74
+ should 'call the api and pass params' do
75
+ signature = build(:signature)
76
+ stub_request(:post, %r{signatures/#{signature.id}/run.json*}).to_return(body: json_list(:alert, 2))
77
+
78
+ alerts = signature.run!(external_account_id: 3, regions: 'param2')
79
+
80
+ assert_requested(:post, %r{signatures/#{signature.id}/run.json}) do |req|
81
+ body = JSON.parse req.body
82
+ assert_equal 3, body['data']['attributes']['external_account_id']
83
+ assert_equal ['param2'], body['data']['attributes']['regions']
84
+ end
85
+ assert_equal ESP::Alert, alerts.resource_class
86
+ end
87
+
88
+ should 'throw an error if an error is returned' do
89
+ signature = build(:signature)
90
+ error = ActiveResource::BadRequest.new('')
91
+ error_response = json(:error)
92
+ response = mock(body: error_response, code: '400')
93
+ error.stubs(:response).returns(response)
94
+ ActiveResource::Connection.any_instance.expects(:post).raises(error)
95
+
96
+ error = assert_raises ActiveResource::ResourceInvalid do
97
+ result = signature.run!(external_account_id: 3, regions: 'param2')
98
+ assert_equal JSON.parse(error_response)['errors'].first['title'], result.errors.full_messages.first
99
+ end
100
+ assert_equal "Failed. Response code = 400. Response message = #{JSON.parse(error_response)['errors'].first['title']}.", error.message
101
+ end
102
+ end
103
+
104
+ context '#suppress' do
105
+ should 'call the api' do
106
+ stub_request(:post, %r{suppressions/signatures.json*}).to_return(body: json(:suppression_signature))
107
+ signature = build(:signature)
108
+
109
+ suppression = signature.suppress(regions: ['us_east_1'], external_account_ids: [5], reason: 'because')
110
+
111
+ assert_requested(:post, %r{suppressions/signatures.json*}) do |req|
112
+ body = JSON.parse(req.body)
113
+ assert_equal 'because', body['data']['attributes']['reason']
114
+ assert_equal [signature.id], body['data']['attributes']['signature_ids']
115
+ assert_equal ['us_east_1'], body['data']['attributes']['regions']
116
+ assert_equal [5], body['data']['attributes']['external_account_ids']
117
+ end
118
+ assert_equal ESP::Suppression::Signature, suppression.class
119
+ end
120
+ end
121
+
122
+ context 'live calls' do
123
+ setup do
124
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
125
+ WebMock.allow_net_connect!
126
+ end
127
+
128
+ teardown do
129
+ WebMock.disable_net_connect!
130
+ end
131
+
132
+ context '#service' do
133
+ should 'return a service' do
134
+ signature = ESP::Signature.first
135
+
136
+ service = signature.service
137
+
138
+ assert_equal signature.service_id, service.id
139
+ assert_equal ESP::Service, service.class
140
+ end
141
+ end
142
+
143
+ context '#run' do
144
+ should 'return alerts' do
145
+ signature = ESP::Signature.first
146
+ external_account_id = ESP::ExternalAccount.last.id
147
+
148
+ alerts = signature.run(external_account_id: external_account_id, regions: 'us_east_1')
149
+
150
+ assert_equal ESP::Alert, alerts.resource_class
151
+ end
152
+
153
+ should 'return errors' do
154
+ signature = ESP::Signature.first
155
+ external_account_id = 999
156
+
157
+ signature = signature.run(external_account_id: external_account_id, regions: ['us_east_1'])
158
+
159
+ assert_equal "Couldn't find ExternalAccount", signature.errors.full_messages.first
160
+ end
161
+ end
162
+
163
+ context '#CRUD' do
164
+ should 'be able to read' do
165
+ signature = ESP::Signature.last
166
+
167
+ assert_not_nil signature
168
+
169
+ signature = ESP::Signature.find(signature.id)
170
+
171
+ assert_not_nil signature
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,115 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class Stat
5
+ class CustomSignatureTest < ActiveSupport::TestCase
6
+ context ESP::StatCustomSignature do
7
+ context '.for_stat' do
8
+ should 'throw an error if stat id is not supplied' do
9
+ error = assert_raises ArgumentError do
10
+ ESP::StatCustomSignature.for_stat
11
+ end
12
+ assert_equal 'You must supply a stat id.', error.message
13
+ end
14
+
15
+ should 'call the api' do
16
+ stub_custom_signature = stub_request(:get, %r{stats/5/custom_signatures.json*}).to_return(body: json_list(:stat_custom_signature, 2))
17
+
18
+ custom_signatures = ESP::StatCustomSignature.for_stat(5)
19
+
20
+ assert_requested(stub_custom_signature)
21
+ assert_equal ESP::StatCustomSignature, custom_signatures.resource_class
22
+ end
23
+ end
24
+
25
+ context '.find' do
26
+ should 'throw an error if stat_id is not supplied' do
27
+ error = assert_raises ArgumentError do
28
+ ESP::StatCustomSignature.find(:all, params: { id: 3 })
29
+ end
30
+ assert_equal 'You must supply a stat id.', error.message
31
+ end
32
+
33
+ should 'call the show api and return a custom_signature if searching by id' do
34
+ stub_custom_signature = stub_request(:get, %r{custom_signatures/5.json*}).to_return(body: json(:stat_custom_signature))
35
+
36
+ custom_signature = ESP::StatCustomSignature.find(5)
37
+
38
+ assert_requested(stub_custom_signature)
39
+ assert_equal ESP::StatCustomSignature, custom_signature.class
40
+ end
41
+
42
+ should 'call the api and return custom_signatures when stat_id is supplied' do
43
+ stub_custom_signature = stub_request(:get, %r{stats/5/custom_signatures.json*}).to_return(body: json_list(:stat_custom_signature, 2))
44
+
45
+ custom_signatures = ESP::StatCustomSignature.find(:all, params: { stat_id: 5 })
46
+
47
+ assert_requested(stub_custom_signature)
48
+ assert_equal ESP::StatCustomSignature, custom_signatures.resource_class
49
+ end
50
+ end
51
+
52
+ context '#create' do
53
+ should 'not be implemented' do
54
+ assert_raises ESP::NotImplementedError do
55
+ ESP::StatCustomSignature.create(name: 'test')
56
+ end
57
+ end
58
+ end
59
+
60
+ context '#update' do
61
+ should 'not be implemented' do
62
+ custom_signature = ESP::StatCustomSignature.new
63
+ assert_raises ESP::NotImplementedError do
64
+ custom_signature.save
65
+ end
66
+ end
67
+ end
68
+
69
+ context '#destroy' do
70
+ should 'not be implemented' do
71
+ s = ESP::StatCustomSignature.new
72
+ assert_raises ESP::NotImplementedError do
73
+ s.destroy
74
+ end
75
+ end
76
+ end
77
+
78
+ context '#custom_signature' do
79
+ should 'call the show api and return the custom_signature' do
80
+ custom_signature_stat = ESP::StatCustomSignature.new(custom_signature_id: 3)
81
+
82
+ stub_custom_signature = stub_request(:get, %r{custom_signatures/#{custom_signature_stat.custom_signature_id}.json*}).to_return(body: json(:custom_signature))
83
+
84
+ custom_signature = custom_signature_stat.custom_signature
85
+
86
+ assert_requested(stub_custom_signature)
87
+ assert_equal ESP::CustomSignature, custom_signature.class
88
+ end
89
+ end
90
+
91
+ context 'live calls' do
92
+ setup do
93
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
94
+ WebMock.allow_net_connect!
95
+ end
96
+
97
+ teardown do
98
+ WebMock.disable_net_connect!
99
+ end
100
+
101
+ context '.for_stat' do
102
+ should 'return tags for stat id' do
103
+ report = ESP::Report.find(:first, params: { status: 'complete' })
104
+ skip "make sure you have a complete report" unless report.present?
105
+ stat_id = report.stat.id
106
+ stats = ESP::StatCustomSignature.for_stat(stat_id)
107
+
108
+ assert_equal ESP::StatCustomSignature, stats.resource_class
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,114 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class Stat
5
+ class RegionTest < ActiveSupport::TestCase
6
+ context ESP::StatRegion do
7
+ context '.for_stat' do
8
+ should 'throw an error if stat id is not supplied' do
9
+ error = assert_raises ArgumentError do
10
+ ESP::StatRegion.for_stat
11
+ end
12
+ assert_equal 'You must supply a stat id.', error.message
13
+ end
14
+
15
+ should 'call the api' do
16
+ stub_region = stub_request(:get, %r{stats/5/regions.json*}).to_return(body: json_list(:stat_region, 2))
17
+
18
+ regions = ESP::StatRegion.for_stat(5)
19
+
20
+ assert_requested(stub_region)
21
+ assert_equal ESP::StatRegion, regions.resource_class
22
+ end
23
+ end
24
+ context '.find' do
25
+ should 'throw an error if stat_id is not supplied' do
26
+ error = assert_raises ArgumentError do
27
+ ESP::StatRegion.find(:all, params: { id: 3 })
28
+ end
29
+ assert_equal 'You must supply a stat id.', error.message
30
+ end
31
+
32
+ should 'call the show api and return a region if searching by id' do
33
+ stub_region = stub_request(:get, %r{regions/5.json*}).to_return(body: json(:stat_region))
34
+
35
+ region = ESP::StatRegion.find(5)
36
+
37
+ assert_requested(stub_region)
38
+ assert_equal ESP::StatRegion, region.class
39
+ end
40
+
41
+ should 'call the api and return regions when stat_id is supplied' do
42
+ stub_region = stub_request(:get, %r{stats/5/regions.json*}).to_return(body: json_list(:stat_region, 2))
43
+
44
+ regions = ESP::StatRegion.find(:all, params: { stat_id: 5 })
45
+
46
+ assert_requested(stub_region)
47
+ assert_equal ESP::StatRegion, regions.resource_class
48
+ end
49
+ end
50
+
51
+ context '#create' do
52
+ should 'not be implemented' do
53
+ assert_raises ESP::NotImplementedError do
54
+ ESP::StatRegion.create(name: 'test')
55
+ end
56
+ end
57
+ end
58
+
59
+ context '#update' do
60
+ should 'not be implemented' do
61
+ region = ESP::StatRegion.new
62
+ assert_raises ESP::NotImplementedError do
63
+ region.save
64
+ end
65
+ end
66
+ end
67
+
68
+ context '#destroy' do
69
+ should 'not be implemented' do
70
+ s = ESP::StatRegion.new
71
+ assert_raises ESP::NotImplementedError do
72
+ s.destroy
73
+ end
74
+ end
75
+ end
76
+
77
+ context '#region' do
78
+ should 'call the show api and return the region' do
79
+ region_stat = ESP::StatRegion.new(region_id: 3)
80
+
81
+ stub_region = stub_request(:get, %r{regions/#{region_stat.region_id}.json*}).to_return(body: json(:region))
82
+
83
+ region = region_stat.region
84
+
85
+ assert_requested(stub_region)
86
+ assert_equal ESP::Region, region.class
87
+ end
88
+ end
89
+
90
+ context 'live calls' do
91
+ setup do
92
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
93
+ WebMock.allow_net_connect!
94
+ end
95
+
96
+ teardown do
97
+ WebMock.disable_net_connect!
98
+ end
99
+
100
+ context '.for_stat' do
101
+ should 'return tags for stat id' do
102
+ report = ESP::Report.find(:first, params: { status: 'complete' })
103
+ skip "make sure you have a complete report" unless report.present?
104
+ stat_id = report.stat.id
105
+ stats = ESP::StatRegion.for_stat(stat_id)
106
+
107
+ assert_equal ESP::StatRegion, stats.resource_class
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end