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,117 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../test_helper')
2
+
3
+ module ESP
4
+ class Suppression
5
+ class SignatureTest < ActiveSupport::TestCase
6
+ context ESP::Suppression::Signature do
7
+ context '#find' do
8
+ should 'not be implemented' do
9
+ assert_raises ESP::NotImplementedError do
10
+ ESP::Suppression::Signature.find(4)
11
+ end
12
+ end
13
+ end
14
+
15
+ context '#update' do
16
+ should 'not be implemented' do
17
+ s = ESP::Suppression::Signature.new
18
+ assert_raises ESP::NotImplementedError do
19
+ s.update
20
+ end
21
+ end
22
+ end
23
+
24
+ context '#destroy' do
25
+ should 'not be implemented' do
26
+ s = ESP::Suppression::Signature.new
27
+ assert_raises ESP::NotImplementedError do
28
+ s.destroy
29
+ end
30
+ end
31
+ end
32
+
33
+ context '#create' do
34
+ should 'call the api' do
35
+ stub_request(:post, %r{suppressions/signatures.json*}).to_return(body: json(:suppression_signature))
36
+
37
+ suppression = ESP::Suppression::Signature.create(signature_ids: [4, 2], custom_signature_ids: [3], regions: ['us_east_1'], external_account_ids: [5], reason: 'because')
38
+
39
+ assert_requested(:post, %r{suppressions/signatures.json*}) do |req|
40
+ body = JSON.parse(req.body)
41
+ assert_equal 'because', body['data']['attributes']['reason']
42
+ assert_equal [4, 2], body['data']['attributes']['signature_ids']
43
+ assert_equal [3], body['data']['attributes']['custom_signature_ids']
44
+ assert_equal ['us_east_1'], body['data']['attributes']['regions']
45
+ assert_equal [5], body['data']['attributes']['external_account_ids']
46
+ end
47
+ assert_equal ESP::Suppression::Signature, suppression.class
48
+ end
49
+
50
+ context 'for alert' do
51
+ should 'call the api' do
52
+ stub_request(:post, %r{suppressions/alert/5/signatures.json*}).to_return(body: json(:suppression_signature))
53
+
54
+ suppression = ESP::Suppression::Signature.create(alert_id: 5, reason: 'because')
55
+
56
+ assert_requested(:post, %r{suppressions/alert/5/signatures.json*}) do |req|
57
+ body = JSON.parse(req.body)
58
+ assert_equal 'because', body['data']['attributes']['reason']
59
+ end
60
+ assert_equal ESP::Suppression::Signature, suppression.class
61
+ end
62
+ end
63
+ end
64
+
65
+ context 'live calls' do
66
+ setup do
67
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
68
+ WebMock.allow_net_connect!
69
+ end
70
+
71
+ teardown do
72
+ WebMock.disable_net_connect!
73
+ end
74
+
75
+ context '.create' do
76
+ should 'return error when reason is not supplied' do
77
+ signature_id = ESP::Signature.last.id
78
+ external_account_id = ESP::ExternalAccount.last.id
79
+
80
+ suppression = ESP::Suppression::Signature.create(signature_ids: [signature_id], custom_signature_ids: [], regions: ['us_east_1'], external_account_ids: [external_account_id])
81
+
82
+ assert_equal "Reason can't be blank", suppression.errors.full_messages.first
83
+ end
84
+
85
+ should 'return suppression' do
86
+ signature_id = ESP::Signature.last.id
87
+ external_account_id = ESP::ExternalAccount.last.id
88
+
89
+ suppression = ESP::Suppression::Signature.create(signature_ids: [signature_id], custom_signature_ids: [], reason: 'test', regions: ['us_east_1'], external_account_ids: [external_account_id])
90
+
91
+ assert_equal ESP::Suppression::Signature, suppression.class
92
+ end
93
+
94
+ context 'for_alert' do
95
+ should 'return error when reason is not supplied' do
96
+ alert_id = ESP::Report.last.alerts.last.id
97
+
98
+ suppression = ESP::Suppression::Signature.create(alert_id: alert_id)
99
+
100
+ assert_equal "Reason can't be blank", suppression.errors.full_messages.first
101
+ end
102
+
103
+ should 'return suppression' do
104
+ alert_id = ESP::Report.last.alerts.last.id
105
+
106
+ suppression = ESP::Suppression::Signature.create(alert_id: alert_id, reason: 'test')
107
+
108
+ assert_predicate suppression.errors, :blank?
109
+ assert_equal ESP::Suppression::Signature, suppression.class
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,79 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../test_helper')
2
+
3
+ module ESP
4
+ class Suppression
5
+ class UniqueIdentifierTest < ActiveSupport::TestCase
6
+ context ESP::Suppression::UniqueIdentifier do
7
+ context '#find' do
8
+ should 'not be implemented' do
9
+ assert_raises ESP::NotImplementedError do
10
+ ESP::Suppression::UniqueIdentifier.find(4)
11
+ end
12
+ end
13
+ end
14
+
15
+ context '#update' do
16
+ should 'not be implemented' do
17
+ s = ESP::Suppression::UniqueIdentifier.new
18
+ assert_raises ESP::NotImplementedError do
19
+ s.update
20
+ end
21
+ end
22
+ end
23
+
24
+ context '#destroy' do
25
+ should 'not be implemented' do
26
+ s = ESP::Suppression::UniqueIdentifier.new
27
+ assert_raises ESP::NotImplementedError do
28
+ s.destroy
29
+ end
30
+ end
31
+ end
32
+
33
+ context '#create' do
34
+ should 'call the api' do
35
+ stub_request(:post, %r{suppressions/alert/5/unique_identifiers.json*}).to_return(body: json(:suppression_unique_identifier))
36
+
37
+ suppression = ESP::Suppression::UniqueIdentifier.create(alert_id: 5, reason: 'because')
38
+
39
+ assert_requested(:post, %r{suppressions/alert/5/unique_identifiers.json*}) do |req|
40
+ body = JSON.parse(req.body)
41
+ assert_equal 'because', body['data']['attributes']['reason']
42
+ end
43
+ assert_equal ESP::Suppression::UniqueIdentifier, suppression.class
44
+ end
45
+ end
46
+
47
+ context 'live calls' do
48
+ setup do
49
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
50
+ WebMock.allow_net_connect!
51
+ end
52
+
53
+ teardown do
54
+ WebMock.disable_net_connect!
55
+ end
56
+
57
+ context '.create' do
58
+ should 'return error when reason is not supplied' do
59
+ alert_id = ESP::Report.last.alerts.last.id
60
+
61
+ suppression = ESP::Suppression::UniqueIdentifier.create(alert_id: alert_id)
62
+
63
+ assert_equal "Reason can't be blank", suppression.errors.full_messages.first
64
+ end
65
+
66
+ should 'return suppression' do
67
+ alert_id = ESP::Report.last.alerts.last.id
68
+
69
+ suppression = ESP::Suppression::UniqueIdentifier.create(alert_id: alert_id, reason: 'test')
70
+
71
+ assert_predicate suppression.errors, :blank?
72
+ assert_equal ESP::Suppression::UniqueIdentifier, suppression.class
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,226 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class SuppressionTest < ActiveSupport::TestCase
5
+ context ESP::Suppression do
6
+ context '#create' do
7
+ should 'not be implemented' do
8
+ assert_raises ESP::NotImplementedError do
9
+ ESP::User.create(name: 'test')
10
+ end
11
+ end
12
+ end
13
+
14
+ context '#update' do
15
+ should 'not be implemented' do
16
+ u = build(:user)
17
+ assert_raises ESP::NotImplementedError do
18
+ u.save
19
+ end
20
+ end
21
+ end
22
+
23
+ context '#destroy' do
24
+ should 'not be implemented' do
25
+ u = build(:user)
26
+ assert_raises ESP::NotImplementedError do
27
+ u.destroy
28
+ end
29
+ end
30
+ end
31
+
32
+ context '#organization' do
33
+ should 'call the api' do
34
+ s = build(:suppression, organization_id: 1)
35
+ stub_org = stub_request(:get, %r{organizations/#{s.organization_id}.json*}).to_return(body: json(:organization))
36
+
37
+ s.organization
38
+
39
+ assert_requested(stub_org)
40
+ end
41
+ end
42
+
43
+ context '#created_by' do
44
+ should 'call the api' do
45
+ s = build(:suppression, created_by_id: 1)
46
+ stub_user = stub_request(:get, %r{users/#{s.created_by_id}.json*}).to_return(body: json(:user))
47
+
48
+ s.created_by
49
+
50
+ assert_requested(stub_user)
51
+ end
52
+ end
53
+
54
+ context '#regions' do
55
+ should 'call the api for the report and the passed in params' do
56
+ suppression = build(:suppression)
57
+ stub_request(:get, /regions.json*/).to_return(body: json_list(:region, 2))
58
+
59
+ suppression.regions
60
+
61
+ assert_requested(:get, /regions.json*/) do |req|
62
+ assert_equal "filter[suppressions_id_eq]=#{suppression.id}", URI.unescape(req.uri.query)
63
+ end
64
+ end
65
+ end
66
+
67
+ context '#external_accounts' do
68
+ should 'call the api for the report and the passed in params' do
69
+ suppression = build(:suppression)
70
+ stub_request(:get, /external_accounts.json*/).to_return(body: json_list(:external_account, 2))
71
+
72
+ suppression.external_accounts
73
+
74
+ assert_requested(:get, /external_accounts.json*/) do |req|
75
+ assert_equal "filter[suppressions_id_eq]=#{suppression.id}", URI.unescape(req.uri.query)
76
+ end
77
+ end
78
+ end
79
+
80
+ context '#signatures' do
81
+ should 'call the api for the report and the passed in params' do
82
+ suppression = build(:suppression, signature_ids: [1, 2])
83
+ stub_request(:get, /signatures.json*/).to_return(body: json_list(:signature, 2))
84
+
85
+ suppression.signatures
86
+
87
+ assert_requested(:get, /signatures.json*/) do |req|
88
+ assert_equal "filter[id_in][0]=#{suppression.signature_ids.first}&filter[id_in][1]=#{suppression.signature_ids.second}", URI.unescape(req.uri.query)
89
+ end
90
+ end
91
+ end
92
+
93
+ context '#custom_signatures' do
94
+ should 'call the api for the report and the passed in params' do
95
+ suppression = build(:suppression, custom_signature_ids: [1, 2])
96
+ stub_request(:get, /custom_signatures.json*/).to_return(body: json_list(:custom_signature, 2))
97
+
98
+ suppression.custom_signatures
99
+
100
+ assert_requested(:get, /custom_signatures.json*/) do |req|
101
+ assert_equal "filter[id_in][0]=#{suppression.custom_signature_ids.first}&filter[id_in][1]=#{suppression.custom_signature_ids.second}", URI.unescape(req.uri.query)
102
+ end
103
+ end
104
+ end
105
+
106
+ context '#deactivate' do
107
+ should 'call the api and not throw an error if an error is returned' do
108
+ s = build(:suppression)
109
+ error = ActiveResource::BadRequest.new('')
110
+ error_response = json(:error)
111
+ response = mock(body: error_response, code: '400')
112
+ error.stubs(:response).returns(response)
113
+ s.expects(:patch).raises(error)
114
+
115
+ assert_nothing_raised do
116
+ refute_predicate s, :deactivate
117
+ end
118
+
119
+ assert_equal JSON.parse(error_response)['errors'].first['title'], s.errors.full_messages.first
120
+ end
121
+ end
122
+
123
+ context '#deactivate!' do
124
+ should 'call the api and throw an error if an error is returned' do
125
+ s = build(:suppression)
126
+ error = ActiveResource::BadRequest.new('')
127
+ error_response = json(:error)
128
+ response = mock(body: error_response, code: '400')
129
+ error.stubs(:response).returns(response)
130
+ s.expects(:patch).raises(error)
131
+
132
+ error = assert_raises ActiveResource::ResourceInvalid do
133
+ s.deactivate!
134
+ end
135
+ assert_equal "Failed. Response code = 400. Response message = #{JSON.parse(error_response)['errors'].first['title']}.", error.message
136
+ end
137
+ end
138
+
139
+ context 'live calls' do
140
+ setup do
141
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
142
+ WebMock.allow_net_connect!
143
+ @s = ESP::Suppression.first
144
+ skip "Live DB does not have any suppressions. Add a suppression and run tests again." if @s.blank?
145
+ end
146
+
147
+ teardown do
148
+ WebMock.disable_net_connect!
149
+ end
150
+
151
+ context '#organization' do
152
+ should 'return an organization' do
153
+ org = @s.organization
154
+
155
+ assert_equal @s.organization_id, org.id
156
+ end
157
+ end
158
+
159
+ context '#created_by' do
160
+ should 'return a user' do
161
+ u = @s.created_by
162
+
163
+ assert_equal @s.created_by_id, u.id
164
+ end
165
+ end
166
+
167
+ context '#regions' do
168
+ should 'return regions' do
169
+ r = @s.regions
170
+
171
+ assert_equal @s.relationships.regions.data.map(&:id).sort, r.map(&:id).sort
172
+ end
173
+ end
174
+
175
+ context '#external_accounts' do
176
+ should 'return external_accounts' do
177
+ e = @s.external_accounts
178
+
179
+ assert_equal @s.relationships.external_accounts.data.map(&:id).sort, e.map(&:id).sort
180
+ end
181
+ end
182
+
183
+ context '#signatures' do
184
+ should 'return signatures' do
185
+ s = @s.signatures
186
+
187
+ assert_equal @s.relationships.signatures.data.map(&:id).sort, s.map(&:id).sort
188
+ end
189
+ end
190
+
191
+ context '#custom_signatures' do
192
+ should 'return custom_signatures' do
193
+ cs = @s.custom_signatures
194
+
195
+ assert_equal @s.relationships.custom_signatures.data.map(&:id).sort, cs.map(&:id).sort
196
+ end
197
+ end
198
+
199
+ context '#deactivate' do
200
+ should 'deactivate the suppression' do
201
+ status = @s.status
202
+
203
+ @s.deactivate
204
+
205
+ assert_equal 'inactive', @s.status
206
+ assert_contains @s.errors.full_messages.first, 'Access Denied' if status == 'inactive'
207
+ end
208
+ end
209
+
210
+ context '#deactivate!' do
211
+ should 'raise an error if already inactive' do
212
+ status = @s.status
213
+
214
+ if status == 'inactive'
215
+ assert_raises ActiveResource::ResourceInvalid do
216
+ @s.deactivate!
217
+ end
218
+ assert_equal 'inactive', @s.status
219
+ assert_contains @s.errors.full_messages.first, 'Access Denied' if status == 'inactive'
220
+ end
221
+ end
222
+ end
223
+ end
224
+ end
225
+ end
226
+ end
@@ -0,0 +1,98 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class TagTest < ActiveSupport::TestCase
5
+ context ESP::Tag do
6
+ context '#create' do
7
+ should 'not be implemented' do
8
+ assert_raises ESP::NotImplementedError do
9
+ ESP::Tag.create(name: 'test')
10
+ end
11
+ end
12
+ end
13
+
14
+ context '#update' do
15
+ should 'not be implemented' do
16
+ tag = build(:tag)
17
+ assert_raises ESP::NotImplementedError do
18
+ tag.save
19
+ end
20
+ end
21
+ end
22
+
23
+ context '#destroy' do
24
+ should 'not be implemented' do
25
+ tag = build(:tag)
26
+ assert_raises ESP::NotImplementedError do
27
+ tag.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::Tag.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_tag = stub_request(:get, %r{alerts/5/tags.json*}).to_return(body: json_list(:tag, 2))
42
+
43
+ tags = ESP::Tag.for_alert(5)
44
+
45
+ assert_requested(stub_tag)
46
+ assert_equal ESP::Tag, tags.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::Tag.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 a tag if searching by id' do
59
+ stub_tag = stub_request(:get, %r{tags/5.json*}).to_return(body: json(:tag))
60
+
61
+ tag = ESP::Tag.find(5)
62
+
63
+ assert_requested(stub_tag)
64
+ assert_equal ESP::Tag, tag.class
65
+ end
66
+
67
+ should 'call the api and return tags when alert_id is supplied' do
68
+ stub_tag = stub_request(:get, %r{alerts/5/tags.json*}).to_return(body: json_list(:tag, 2))
69
+
70
+ tags = ESP::Tag.find(:all, params: { alert_id: 5 })
71
+
72
+ assert_requested(stub_tag)
73
+ assert_equal ESP::Tag, tags.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 tags for alert id' do
89
+ report = ESP::Report.last
90
+ events = ESP::Tag.for_alert(report.alerts.last.id)
91
+
92
+ assert_equal ESP::Tag, events.resource_class
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,140 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class TeamTest < ActiveSupport::TestCase
5
+ context ESP::Team do
6
+ context '#organization' do
7
+ should 'call the api' do
8
+ team = build(:team, organization_id: 4)
9
+ stub_org = stub_request(:get, %r{organizations/#{team.organization_id}.json*}).to_return(body: json(:organization))
10
+
11
+ team.organization
12
+
13
+ assert_requested(stub_org)
14
+ end
15
+ end
16
+
17
+ context '#sub_organization' do
18
+ should 'call the api' do
19
+ team = build(:team, sub_organization_id: 4)
20
+ stub_sub_org = stub_request(:get, %r{sub_organizations/#{team.sub_organization_id}.json*}).to_return(body: json(:sub_organization))
21
+
22
+ team.sub_organization
23
+
24
+ assert_requested(stub_sub_org)
25
+ end
26
+ end
27
+
28
+ context '#external_accounts' do
29
+ should 'call the api' do
30
+ team = build(:team)
31
+ stub_request(:get, /external_accounts.json*/).to_return(body: json_list(:external_account, 2))
32
+
33
+ team.external_accounts
34
+
35
+ assert_requested(:get, /external_accounts.json*/) do |req|
36
+ assert_equal "filter[team_id_eq]=#{team.id}", URI.unescape(req.uri.query)
37
+ end
38
+ end
39
+ end
40
+
41
+ context '#reports' do
42
+ should 'call the api' do
43
+ team = build(:team)
44
+ stub_request(:get, /reports.json*/).to_return(body: json_list(:report, 2))
45
+
46
+ team.reports
47
+
48
+ assert_requested(:get, /reports.json*/) do |req|
49
+ assert_equal "filter[team_id_eq]=#{team.id}", URI.unescape(req.uri.query)
50
+ end
51
+ end
52
+ end
53
+
54
+ context '#create_report' do
55
+ should 'call Report.create_for_team' do
56
+ Report.stubs(:create_for_team)
57
+ team = build(:team)
58
+
59
+ team.create_report
60
+
61
+ assert_received(Report, :create_for_team) do |expects|
62
+ expects.with do |id|
63
+ assert_equal team.id, id
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ context 'live calls' do
70
+ setup do
71
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
72
+ WebMock.allow_net_connect!
73
+ @team = ESP::Team.last
74
+ skip "Live DB does not have any teams. Add a team and run tests again." if @team.blank?
75
+ end
76
+
77
+ teardown do
78
+ WebMock.disable_net_connect!
79
+ end
80
+
81
+ context '#organization' do
82
+ should 'return an organization' do
83
+ org = @team.organization
84
+
85
+ assert_equal @team.organization_id, org.id
86
+ end
87
+ end
88
+
89
+ context '#sub_organization' do
90
+ should 'return a sub_organization' do
91
+ sub_org = @team.sub_organization
92
+
93
+ assert_equal @team.sub_organization_id, sub_org.id
94
+ end
95
+ end
96
+
97
+ context '#external_accounts' do
98
+ should 'return an array of external_accounts' do
99
+ external_accounts = @team.external_accounts
100
+
101
+ assert_equal ESP::ExternalAccount, external_accounts.resource_class
102
+ end
103
+ end
104
+
105
+ context '#reports' do
106
+ should 'return an array of reports' do
107
+ reports = @team.reports
108
+
109
+ assert_equal ESP::Report, reports.resource_class
110
+ end
111
+ end
112
+
113
+ context '#CRUD' do
114
+ should 'be able to create, update and destroy' do
115
+ team = ESP::Team.new(name: 'bob', organization_id: @team.organization_id, sub_organization_id: @team.sub_organization_id)
116
+
117
+ assert_predicate team, :new?
118
+
119
+ team.save
120
+
121
+ refute_predicate team, :new?
122
+
123
+ team.name = 'jim'
124
+ team.save
125
+
126
+ assert_nothing_raised do
127
+ ESP::Team.find(team.id)
128
+ end
129
+
130
+ team.destroy
131
+
132
+ assert_raises ActiveResource::ResourceNotFound do
133
+ ESP::Team.find(team.id)
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end