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,263 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class AlertTest < ActiveSupport::TestCase
5
+ context ESP::Alert do
6
+ context '#create' do
7
+ should 'not be implemented' do
8
+ assert_raises ESP::NotImplementedError do
9
+ ESP::Alert.create(name: 'test')
10
+ end
11
+ end
12
+ end
13
+
14
+ context '#update' do
15
+ should 'not be implemented' do
16
+ cloud_trail_event = build(:cloud_trail_event)
17
+ assert_raises ESP::NotImplementedError do
18
+ cloud_trail_event.save
19
+ end
20
+ end
21
+ end
22
+
23
+ context '#destroy' do
24
+ should 'not be implemented' do
25
+ cloud_trail_event = build(:cloud_trail_event)
26
+ assert_raises ESP::NotImplementedError do
27
+ cloud_trail_event.destroy
28
+ end
29
+ end
30
+ end
31
+
32
+ context '#external_account' do
33
+ should 'call the api' do
34
+ alert = build(:alert, external_account_id: 3)
35
+ stubbed_external_account = stub_request(:get, %r{external_accounts/#{alert.external_account_id}.json*}).to_return(body: json(:external_account))
36
+
37
+ alert.external_account
38
+
39
+ assert_requested(stubbed_external_account)
40
+ end
41
+ end
42
+
43
+ context '#region' do
44
+ should 'call the api' do
45
+ alert = build(:alert, region_id: 3)
46
+ stubbed_region = stub_request(:get, %r{regions/#{alert.region_id}.json*}).to_return(body: json(:region))
47
+
48
+ alert.region
49
+
50
+ assert_requested(stubbed_region)
51
+ end
52
+ end
53
+
54
+ context '#signature' do
55
+ should 'call the api' do
56
+ alert = build(:alert, signature_id: 3)
57
+ stubbed_signature = stub_request(:get, %r{signatures/#{alert.signature_id}.json*}).to_return(body: json(:signature))
58
+
59
+ alert.signature
60
+
61
+ assert_requested(stubbed_signature)
62
+ end
63
+ end
64
+
65
+ context '#custom_signature' do
66
+ should 'call the api' do
67
+ alert = build(:alert, custom_signature_id: 3)
68
+ stubbed_custom_signature = stub_request(:get, %r{custom_signatures/#{alert.custom_signature_id}.json*}).to_return(body: json(:custom_signature))
69
+
70
+ alert.custom_signature
71
+
72
+ assert_requested(stubbed_custom_signature)
73
+ end
74
+ end
75
+
76
+ context '#suppression' do
77
+ should 'call the api' do
78
+ alert = build(:alert, suppression_id: 3)
79
+ stubbed_suppression = stub_request(:get, %r{suppressions/#{alert.suppression_id}.json*}).to_return(body: json(:suppression))
80
+
81
+ alert.suppression
82
+
83
+ assert_requested(stubbed_suppression)
84
+ end
85
+ end
86
+
87
+ context '#cloud_trail_events' do
88
+ should 'call the api for the alert' do
89
+ alert = build(:alert)
90
+ stubbed_events = stub_request(:get, %r{alerts/#{alert.id}/cloud_trail_events.json*}).to_return(body: json_list(:cloud_trail_event, 2))
91
+
92
+ alert.cloud_trail_events
93
+
94
+ assert_requested(stubbed_events)
95
+ end
96
+ end
97
+
98
+ context '#tags' do
99
+ should 'call the api for the alert' do
100
+ alert = build(:alert)
101
+ stubbed_tags = stub_request(:get, %r{alerts/#{alert.id}/tags.json*}).to_return(body: json_list(:tag, 2))
102
+
103
+ alert.tags
104
+
105
+ assert_requested(stubbed_tags)
106
+ end
107
+ end
108
+
109
+ context '.for_report' do
110
+ should 'throw an error if report_id is not supplied' do
111
+ error = assert_raises ArgumentError do
112
+ ESP::Alert.for_report
113
+ end
114
+ assert_equal 'You must supply a report id.', error.message
115
+ end
116
+
117
+ should 'call the api and pass params' do
118
+ stub_request(:get, %r{reports/5/alerts.json*}).to_return(body: json_list(:alert, 2))
119
+
120
+ alerts = ESP::Alert.for_report(5, status: 'pass')
121
+
122
+ assert_requested(:get, %r{reports/5/alerts.json*}) do |req|
123
+ assert_equal "filter[status]=pass", URI.unescape(req.uri.query)
124
+ end
125
+ assert_equal ESP::Alert, alerts.resource_class
126
+ end
127
+ end
128
+
129
+ context '.find' do
130
+ should 'throw an error if report_id is not supplied' do
131
+ error = assert_raises ArgumentError do
132
+ ESP::Alert.find(:all, params: { id: 3 })
133
+ end
134
+ assert_equal 'You must supply a report id.', error.message
135
+ end
136
+
137
+ should 'call the show api and return an alert if searching by id' do
138
+ stub_alert = stub_request(:get, %r{alerts/5.json*}).to_return(body: json(:alert))
139
+
140
+ alert = ESP::Alert.find(5)
141
+
142
+ assert_requested(stub_alert)
143
+ assert_equal ESP::Alert, alert.class
144
+ end
145
+
146
+ should 'call the api and return alerts when report_id is supplied' do
147
+ stub_alert = stub_request(:get, %r{reports/5/alerts.json*}).to_return(body: json_list(:alert, 2))
148
+
149
+ alert = ESP::Alert.find(:all, params: { report_id: 5 })
150
+
151
+ assert_requested(stub_alert)
152
+ assert_equal ESP::Alert, alert.resource_class
153
+ end
154
+ end
155
+
156
+ context '#suppress_signature' do
157
+ should 'throw an error if reason is not supplied' do
158
+ alert = build(:alert)
159
+
160
+ error = assert_raises ArgumentError do
161
+ alert.suppress_signature
162
+ end
163
+ assert_equal 'You must specify the reason.', error.message
164
+ end
165
+
166
+ should 'call Suppression::Signature.create' do
167
+ alert = build(:alert)
168
+ Suppression::Signature.stubs(:create)
169
+
170
+ alert.suppress_signature('the reason')
171
+
172
+ assert_received(Suppression::Signature, :create) do |expects|
173
+ expects.with do |params|
174
+ assert_equal alert.id, params[:alert_id]
175
+ assert_equal 'the reason', params[:reason]
176
+ end
177
+ end
178
+ end
179
+ end
180
+
181
+ context '#suppress_region' do
182
+ should 'throw an error if reason is not supplied' do
183
+ alert = build(:alert)
184
+
185
+ error = assert_raises ArgumentError do
186
+ alert.suppress_region
187
+ end
188
+ assert_equal 'You must specify the reason.', error.message
189
+ end
190
+
191
+ should 'call Suppression::Region.create' do
192
+ alert = build(:alert)
193
+ Suppression::Region.stubs(:create)
194
+
195
+ alert.suppress_region('the reason')
196
+
197
+ assert_received(Suppression::Region, :create) do |expects|
198
+ expects.with do |params|
199
+ assert_equal alert.id, params[:alert_id]
200
+ assert_equal 'the reason', params[:reason]
201
+ end
202
+ end
203
+ end
204
+ end
205
+
206
+ context '#suppress_unique_identifier' do
207
+ should 'throw an error if reason is not supplied' do
208
+ alert = build(:alert)
209
+
210
+ error = assert_raises ArgumentError do
211
+ alert.suppress_unique_identifier
212
+ end
213
+ assert_equal 'You must specify the reason.', error.message
214
+ end
215
+
216
+ should 'call Suppression::UniqueIdentifier.create' do
217
+ alert = build(:alert)
218
+ Suppression::UniqueIdentifier.stubs(:create)
219
+
220
+ alert.suppress_unique_identifier('the reason')
221
+
222
+ assert_received(Suppression::UniqueIdentifier, :create) do |expects|
223
+ expects.with do |params|
224
+ assert_equal alert.id, params[:alert_id]
225
+ assert_equal 'the reason', params[:reason]
226
+ end
227
+ end
228
+ end
229
+ end
230
+
231
+ context 'live calls' do
232
+ setup do
233
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
234
+ WebMock.allow_net_connect!
235
+ end
236
+
237
+ teardown do
238
+ WebMock.disable_net_connect!
239
+ end
240
+
241
+ context '.for_report' do
242
+ should 'return events for report id' do
243
+ report = ESP::Report.last
244
+ alerts = ESP::Alert.for_report(report.id)
245
+
246
+ assert_equal ESP::Alert, alerts.resource_class
247
+ end
248
+ end
249
+
250
+ context '.find' do
251
+ should 'return an alert by id' do
252
+ report = ESP::Report.last
253
+ alert_id = report.alerts.last.id
254
+ alert = ESP::Alert.find(alert_id.to_i)
255
+
256
+ assert_equal ESP::Alert, alert.class
257
+ assert_equal alert_id, alert.id
258
+ end
259
+ end
260
+ end
261
+ end
262
+ end
263
+ end
@@ -0,0 +1,98 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class CloudTrailEventTest < ActiveSupport::TestCase
5
+ context ESP::CloudTrailEvent do
6
+ context '#create' do
7
+ should 'not be implemented' do
8
+ assert_raises ESP::NotImplementedError do
9
+ ESP::CloudTrailEvent.create(name: 'test')
10
+ end
11
+ end
12
+ end
13
+
14
+ context '#update' do
15
+ should 'not be implemented' do
16
+ cloud_trail_event = build(:cloud_trail_event)
17
+ assert_raises ESP::NotImplementedError do
18
+ cloud_trail_event.save
19
+ end
20
+ end
21
+ end
22
+
23
+ context '#destroy' do
24
+ should 'not be implemented' do
25
+ cloud_trail_event = build(:cloud_trail_event)
26
+ assert_raises ESP::NotImplementedError do
27
+ cloud_trail_event.destroy
28
+ end
29
+ end
30
+ end
31
+
32
+ context '.for_alert' do
33
+ should 'throw an error if alert id is not supplied' do
34
+ error = assert_raises ArgumentError do
35
+ ESP::CloudTrailEvent.for_alert
36
+ end
37
+ assert_equal 'You must supply an alert id.', error.message
38
+ end
39
+
40
+ should 'call the api' do
41
+ stub_event = stub_request(:get, %r{alerts/5/cloud_trail_events.json*}).to_return(body: json_list(:cloud_trail_event, 2))
42
+
43
+ events = ESP::CloudTrailEvent.for_alert(5)
44
+
45
+ assert_requested(stub_event)
46
+ assert_equal ESP::CloudTrailEvent, events.resource_class
47
+ end
48
+ end
49
+
50
+ context '.find' do
51
+ should 'throw an error if alert_id is not supplied' do
52
+ error = assert_raises ArgumentError do
53
+ ESP::CloudTrailEvent.find(:all, params: { id: 3 })
54
+ end
55
+ assert_equal 'You must supply an alert id.', error.message
56
+ end
57
+
58
+ should 'call the show api and return an event if searching by id' do
59
+ stub_event = stub_request(:get, %r{cloud_trail_events/5.json*}).to_return(body: json(:cloud_trail_event))
60
+
61
+ event = ESP::CloudTrailEvent.find(5)
62
+
63
+ assert_requested(stub_event)
64
+ assert_equal ESP::CloudTrailEvent, event.class
65
+ end
66
+
67
+ should 'call the api and return events when alert_id is supplied' do
68
+ stub_event = stub_request(:get, %r{alerts/5/cloud_trail_events.json*}).to_return(body: json_list(:cloud_trail_event, 2))
69
+
70
+ events = ESP::CloudTrailEvent.find(:all, params: { alert_id: 5 })
71
+
72
+ assert_requested(stub_event)
73
+ assert_equal ESP::CloudTrailEvent, events.resource_class
74
+ end
75
+ end
76
+
77
+ context 'live calls' do
78
+ setup do
79
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
80
+ WebMock.allow_net_connect!
81
+ end
82
+
83
+ teardown do
84
+ WebMock.disable_net_connect!
85
+ end
86
+
87
+ context '.for_alert' do
88
+ should 'return events for alert id' do
89
+ report = ESP::Report.last
90
+ events = ESP::CloudTrailEvent.for_alert(report.alerts.last.id)
91
+
92
+ assert_equal ESP::CloudTrailEvent, events.resource_class
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,73 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class ContactRequestTest < ActiveSupport::TestCase
5
+ context ESP::ContactRequest do
6
+ context '#find' do
7
+ should 'not be implemented' do
8
+ assert_raises ESP::NotImplementedError do
9
+ ESP::ContactRequest.find(4)
10
+ end
11
+ end
12
+ end
13
+
14
+ context '#update' do
15
+ should 'not be implemented' do
16
+ s = ESP::ContactRequest.new
17
+ assert_raises ESP::NotImplementedError do
18
+ s.update
19
+ end
20
+ end
21
+ end
22
+
23
+ context '#destroy' do
24
+ should 'not be implemented' do
25
+ s = ESP::ContactRequest.new
26
+ assert_raises ESP::NotImplementedError do
27
+ s.destroy
28
+ end
29
+ end
30
+ end
31
+
32
+ context '#create' do
33
+ should 'call the api' do
34
+ stub_request(:post, /contact_requests.json*/).to_return(body: json(:contact_request))
35
+
36
+ contact_request = ESP::ContactRequest.create(user_id: 5, request_type: 'feature', title: 'My great feature idea', description: 'This is my idea for a really useful feature...')
37
+
38
+ assert_requested(:post, /contact_requests.json*/) do |req|
39
+ body = JSON.parse(req.body)
40
+ assert_equal 5, body['data']['attributes']['user_id']
41
+ assert_equal 'feature', body['data']['attributes']['request_type']
42
+ assert_equal 'My great feature idea', body['data']['attributes']['title']
43
+ assert_equal 'This is my idea for a really useful feature...', body['data']['attributes']['description']
44
+ end
45
+ assert_equal ESP::ContactRequest, contact_request.class
46
+ end
47
+ end
48
+
49
+ context 'live calls' do
50
+ setup do
51
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
52
+ WebMock.allow_net_connect!
53
+ end
54
+
55
+ teardown do
56
+ WebMock.disable_net_connect!
57
+ end
58
+
59
+ context '#CRUD' do
60
+ should 'be able to create, update and destroy' do
61
+ contact_request = ESP::ContactRequest.new(user_id: 5, request_type: 'feature', title: 'My great feature idea', description: 'This is my idea for a really useful feature...')
62
+
63
+ assert_predicate contact_request, :new?
64
+
65
+ contact_request.save
66
+
67
+ refute_predicate contact_request, :new?
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,241 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class CustomSignatureTest < ActiveSupport::TestCase
5
+ context ESP::CustomSignature do
6
+ context '#organization' do
7
+ should 'call the api' do
8
+ custom_signature = build(:custom_signature, organization_id: 4)
9
+ stubbed_organization = stub_request(:get, %r{organizations/#{custom_signature.organization_id}.json*}).to_return(body: json(:organization))
10
+
11
+ custom_signature.organization
12
+
13
+ assert_requested(stubbed_organization)
14
+ end
15
+ end
16
+
17
+ context '.run_sanity_test!' do
18
+ should 'call the api and pass params' do
19
+ custom_signature = build(:custom_signature, external_account_id: 3)
20
+ stub_request(:post, %r{custom_signatures/run.json*}).to_return(body: json_list(:alert, 2))
21
+
22
+ alerts = ESP::CustomSignature.run!(external_account_id: 3, regions: 'param2', language: custom_signature.language, signature: custom_signature.signature)
23
+
24
+ assert_requested(:post, %r{custom_signatures/run.json*}) do |req|
25
+ body = JSON.parse req.body
26
+ assert_equal false, body['data'].key?('id')
27
+ assert_equal ['param2'], body['data']['attributes']['regions']
28
+ assert_equal custom_signature.language, body['data']['attributes']['language']
29
+ assert_equal custom_signature.signature, body['data']['attributes']['signature']
30
+ end
31
+ assert_equal ESP::Alert, alerts.resource_class
32
+ end
33
+
34
+ should 'throw an error if an error is returned' do
35
+ custom_signature = build(:custom_signature, external_account_id: 3)
36
+ error = ActiveResource::BadRequest.new('')
37
+ error_response = json(:error)
38
+ response = mock(body: error_response, code: '400')
39
+ error.stubs(:response).returns(response)
40
+ ActiveResource::Connection.any_instance.expects(:post).raises(error)
41
+ stub_request(:post, %r{custom_signatures/run.json*}).to_return(body: json_list(:alert, 2))
42
+
43
+ error = assert_raises ActiveResource::ResourceInvalid do
44
+ ESP::CustomSignature.run!(external_account_id: 3, regions: 'param2', language: custom_signature.language, signature: custom_signature.signature)
45
+ end
46
+ assert_equal "Failed. Response code = 400. Response message = #{JSON.parse(error_response)['errors'].first['title']}.", error.message
47
+ end
48
+ end
49
+
50
+ context '.run_sanity_test' do
51
+ should 'call the api and pass params' do
52
+ custom_signature = build(:custom_signature, external_account_id: 3)
53
+ stub_request(:post, %r{custom_signatures/run.json*}).to_return(body: json_list(:alert, 2))
54
+
55
+ alerts = ESP::CustomSignature.run(external_account_id: 3, regions: 'param2', language: custom_signature.language, signature: custom_signature.signature)
56
+
57
+ assert_requested(:post, %r{custom_signatures/run.json*}) do |req|
58
+ body = JSON.parse req.body
59
+ assert_equal false, body['data'].key?('id')
60
+ assert_equal ['param2'], body['data']['attributes']['regions']
61
+ assert_equal custom_signature.language, body['data']['attributes']['language']
62
+ assert_equal custom_signature.signature, body['data']['attributes']['signature']
63
+ end
64
+ assert_equal ESP::Alert, alerts.resource_class
65
+ end
66
+
67
+ should 'not throw an error if an error is returned' do
68
+ custom_signature = build(:custom_signature, external_account_id: 3)
69
+ error = ActiveResource::BadRequest.new('')
70
+ error_response = json(:error)
71
+ response = mock(body: error_response, code: '400')
72
+ error.stubs(:response).returns(response)
73
+ ActiveResource::Connection.any_instance.expects(:post).raises(error)
74
+ stub_request(:post, %r{custom_signatures/run.json*}).to_return(body: json_list(:alert, 2))
75
+
76
+ assert_nothing_raised do
77
+ result = ESP::CustomSignature.run(external_account_id: 3, regions: 'param2', language: custom_signature.language, signature: custom_signature.signature)
78
+ assert_equal JSON.parse(error_response)['errors'].first['title'], result.errors.full_messages.first
79
+ end
80
+ end
81
+ end
82
+
83
+ context '#run!' do
84
+ should 'call the api and pass params' do
85
+ custom_signature = build(:custom_signature, external_account_id: 3)
86
+ stub_request(:post, %r{custom_signatures/#{custom_signature.id}/run.json*}).to_return(body: json_list(:alert, 2))
87
+
88
+ alerts = custom_signature.run!(regions: 'param2')
89
+
90
+ assert_requested(:post, %r{custom_signatures/#{custom_signature.id}/run.json*}) do |req|
91
+ body = JSON.parse req.body
92
+ assert_equal custom_signature.id, body['data']['id']
93
+ assert_equal ['param2'], body['data']['attributes']['regions']
94
+ end
95
+ assert_equal ESP::Alert, alerts.resource_class
96
+ end
97
+
98
+ should 'throw an error if an error is returned' do
99
+ custom_signature = build(:custom_signature, external_account_id: 3)
100
+ error = ActiveResource::BadRequest.new('')
101
+ error_response = json(:error)
102
+ response = mock(body: error_response, code: '400')
103
+ error.stubs(:response).returns(response)
104
+ ActiveResource::Connection.any_instance.expects(:post).raises(error)
105
+ stub_request(:post, %r{custom_signatures/#{custom_signature.id}/run.json*}).to_return(body: json_list(:alert, 2))
106
+
107
+ error = assert_raises ActiveResource::ResourceInvalid do
108
+ custom_signature.run!(regions: 'param2')
109
+ end
110
+ assert_equal "Failed. Response code = 400. Response message = #{JSON.parse(error_response)['errors'].first['title']}.", error.message
111
+ end
112
+ end
113
+
114
+ context '#run' do
115
+ should 'call the api and pass params' do
116
+ custom_signature = build(:custom_signature, external_account_id: 3)
117
+ stub_request(:post, %r{custom_signatures/#{custom_signature.id}/run.json*}).to_return(body: json_list(:alert, 2))
118
+
119
+ alerts = custom_signature.run(regions: 'param2')
120
+
121
+ assert_requested(:post, %r{custom_signatures/#{custom_signature.id}/run.json*}) do |req|
122
+ body = JSON.parse req.body
123
+ assert_equal custom_signature.id, body['data']['id']
124
+ assert_equal ['param2'], body['data']['attributes']['regions']
125
+ end
126
+ assert_equal ESP::Alert, alerts.resource_class
127
+ end
128
+
129
+ should 'not throw an error if an error is returned' do
130
+ custom_signature = build(:custom_signature, external_account_id: 3)
131
+ error = ActiveResource::BadRequest.new('')
132
+ error_response = json(:error)
133
+ response = mock(body: error_response, code: '400')
134
+ error.stubs(:response).returns(response)
135
+ ActiveResource::Connection.any_instance.expects(:post).raises(error)
136
+ stub_request(:post, %r{custom_signatures/#{custom_signature.id}/run.json*}).to_return(body: json_list(:alert, 2))
137
+
138
+ assert_nothing_raised do
139
+ custom_signature.run(regions: 'param2')
140
+ assert_equal JSON.parse(error_response)['errors'].first['title'], custom_signature.errors.full_messages.first
141
+ end
142
+ end
143
+ end
144
+
145
+ context '#suppress' do
146
+ should 'call the api' do
147
+ stub_request(:post, %r{suppressions/signatures.json*}).to_return(body: json(:suppression_signature))
148
+ custom_signature = build(:custom_signature)
149
+
150
+ suppression = custom_signature.suppress(regions: ['us_east_1'], external_account_ids: [5], reason: 'because')
151
+
152
+ assert_requested(:post, %r{suppressions/signatures.json*}) do |req|
153
+ body = JSON.parse(req.body)
154
+ assert_equal 'because', body['data']['attributes']['reason']
155
+ assert_equal [custom_signature.id], body['data']['attributes']['custom_signature_ids']
156
+ assert_equal ['us_east_1'], body['data']['attributes']['regions']
157
+ assert_equal [5], body['data']['attributes']['external_account_ids']
158
+ end
159
+ assert_equal ESP::Suppression::Signature, suppression.class
160
+ end
161
+ end
162
+
163
+ context 'live calls' do
164
+ setup do
165
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
166
+ WebMock.allow_net_connect!
167
+ @custom_signature = ESP::CustomSignature.last
168
+ skip "Live DB does not have any custom_signatures. Add a custom_signature and run tests again." if @custom_signature.blank?
169
+ end
170
+
171
+ teardown do
172
+ WebMock.disable_net_connect!
173
+ end
174
+
175
+ context '#organization' do
176
+ should 'return an organization' do
177
+ organization = @custom_signature.organization
178
+
179
+ assert_equal @custom_signature.organization_id, organization.id
180
+ assert_equal ESP::Organization, organization.class
181
+ end
182
+ end
183
+
184
+ context '.run' do
185
+ should 'return alerts' do
186
+ external_account_id = ESP::ExternalAccount.last.id
187
+ alerts = ESP::CustomSignature.run(external_account_id: external_account_id, regions: 'us_east_1', language: @custom_signature.language, signature: @custom_signature.signature)
188
+
189
+ assert_equal ESP::Alert, alerts.resource_class
190
+ end
191
+
192
+ should 'return errors' do
193
+ signature = ESP::CustomSignature.run(external_account_id: 999, regions: 'us_east_1', language: @custom_signature.language, signature: @custom_signature.signature)
194
+
195
+ assert_equal "Couldn't find ExternalAccount", signature.errors.full_messages.first
196
+ end
197
+ end
198
+
199
+ context '#run' do
200
+ should 'return alerts' do
201
+ external_account_id = ESP::ExternalAccount.last.id
202
+ alerts = @custom_signature.run(external_account_id: external_account_id, regions: ['us_east_1'])
203
+
204
+ assert_equal ESP::Alert, alerts.resource_class
205
+ end
206
+
207
+ should 'return errors' do
208
+ @custom_signature.run(external_account_id: 999)
209
+
210
+ assert_equal "Couldn't find ExternalAccount", @custom_signature.errors.full_messages.first
211
+ end
212
+ end
213
+
214
+ context '#CRUD' do
215
+ should 'be able to create, update and destroy' do
216
+ custom_signature = ESP::CustomSignature.new(@custom_signature.attributes)
217
+
218
+ assert_predicate custom_signature, :new?
219
+
220
+ custom_signature.save
221
+
222
+ refute_predicate custom_signature, :new?
223
+
224
+ custom_signature.identifier = 'new identifier'
225
+ custom_signature.save
226
+
227
+ assert_nothing_raised do
228
+ ESP::CustomSignature.find(custom_signature.id)
229
+ end
230
+
231
+ custom_signature.destroy
232
+
233
+ assert_raises ActiveResource::ResourceNotFound do
234
+ ESP::CustomSignature.find(custom_signature.id)
235
+ end
236
+ end
237
+ end
238
+ end
239
+ end
240
+ end
241
+ end