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,71 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class DashboardTest < ActiveSupport::TestCase
5
+ context ESP::Dashboard do
6
+ context '#find' do
7
+ should 'not be implemented' do
8
+ assert_raises ESP::NotImplementedError do
9
+ ESP::Dashboard.find(4)
10
+ end
11
+ end
12
+ end
13
+
14
+ context '#create' do
15
+ should 'not be implemented' do
16
+ assert_raises ESP::NotImplementedError do
17
+ ESP::Dashboard.create(name: 'test')
18
+ end
19
+ end
20
+ end
21
+
22
+ context '#update' do
23
+ should 'not be implemented' do
24
+ d = build(:dashboard)
25
+ assert_raises ESP::NotImplementedError do
26
+ d.save
27
+ end
28
+ end
29
+ end
30
+
31
+ context '#destroy' do
32
+ should 'not be implemented' do
33
+ d = build(:dashboard)
34
+ assert_raises ESP::NotImplementedError do
35
+ d.destroy
36
+ end
37
+ end
38
+ end
39
+
40
+ context '.recent' do
41
+ should 'call the api and return an array of dashboard objects' do
42
+ stubbed_dashboard = stub_request(:get, %r{dashboard/recent.json*}).to_return(body: json_list(:dashboard, 2))
43
+
44
+ dashboard = ESP::Dashboard.recent
45
+
46
+ assert_requested(stubbed_dashboard)
47
+ assert_equal ESP::Dashboard, dashboard.resource_class
48
+ end
49
+ end
50
+
51
+ context 'live calls' do
52
+ setup do
53
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
54
+ WebMock.allow_net_connect!
55
+ end
56
+
57
+ teardown do
58
+ WebMock.disable_net_connect!
59
+ end
60
+
61
+ context '.recent' do
62
+ should 'return an array of contact_requests' do
63
+ dashboards = ESP::Dashboard.recent
64
+
65
+ assert_equal ESP::Dashboard, dashboards.resource_class
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,125 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class ExternalAccountTest < ActiveSupport::TestCase
5
+ context ESP::ExternalAccount do
6
+ context '#organization' do
7
+ should 'call the api' do
8
+ external_account = build(:external_account, organization_id: 4)
9
+ stub_org = stub_request(:get, %r{organizations/#{external_account.organization_id}.json*}).to_return(body: json(:organization))
10
+
11
+ external_account.organization
12
+
13
+ assert_requested(stub_org)
14
+ end
15
+ end
16
+
17
+ context '#sub_organization' do
18
+ should 'call the api' do
19
+ external_account = build(:external_account, sub_organization_id: 4)
20
+ stub_sub_org = stub_request(:get, %r{sub_organizations/#{external_account.sub_organization_id}.json*}).to_return(body: json(:sub_organization))
21
+
22
+ external_account.sub_organization
23
+
24
+ assert_requested(stub_sub_org)
25
+ end
26
+ end
27
+
28
+ context '#team' do
29
+ should 'call the api' do
30
+ external_account = build(:external_account, team_id: 4)
31
+ stub_team = stub_request(:get, %r{teams/#{external_account.team_id}.json*}).to_return(body: json(:team))
32
+
33
+ external_account.team
34
+
35
+ assert_requested(stub_team)
36
+ end
37
+ end
38
+
39
+ context '#create' do
40
+ should "use the external_id in the params if supplied" do
41
+ stub_request(:post, /external_accounts.json*/).to_return(body: json(:external_account))
42
+
43
+ ESP::ExternalAccount.create(external_id: '32145', name: 'bob')
44
+
45
+ assert_requested(:post, /external_accounts.json*/) do |req|
46
+ body = JSON.parse(req.body)
47
+ assert_equal '32145', body['data']['attributes']['external_id']
48
+ end
49
+ end
50
+
51
+ should "generate the external_id if not supplied in the params" do
52
+ ESP::ExternalAccount.any_instance.stubs(:generate_external_id).returns('12345')
53
+ stub_request(:post, /external_accounts.json*/).to_return(body: json(:external_account))
54
+
55
+ ESP::ExternalAccount.create(name: 'bob')
56
+
57
+ assert_requested(:post, /external_accounts.json*/) do |req|
58
+ body = JSON.parse(req.body)
59
+ assert_equal '12345', body['data']['attributes']['external_id']
60
+ end
61
+ assert_received(ESP::ExternalAccount.any_instance, :generate_external_id)
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
+ @external_account = ESP::ExternalAccount.last
70
+ skip "Live DB does not have any external_accounts. Add a external_account and run tests again." if @external_account.blank?
71
+ end
72
+
73
+ teardown do
74
+ WebMock.disable_net_connect!
75
+ end
76
+
77
+ context '#organization' do
78
+ should 'return an organization' do
79
+ org = @external_account.organization
80
+
81
+ assert_equal @external_account.organization_id, org.id
82
+ end
83
+ end
84
+
85
+ context '#sub_organization' do
86
+ should 'return a sub_organization' do
87
+ sub_org = @external_account.sub_organization
88
+
89
+ assert_equal @external_account.sub_organization_id, sub_org.id
90
+ end
91
+ end
92
+
93
+ context '#team' do
94
+ should 'return a team' do
95
+ team = @external_account.team
96
+
97
+ assert_equal ESP::Team, team.class
98
+ end
99
+ end
100
+
101
+ context '#CRUD' do
102
+ should 'be able to create, update and destroy' do
103
+ skip "There are to many dependencies to validate an external account to create or update one. Besides esp_web, esp_query has to be running and there must be valid AWS keys assigned as well."
104
+
105
+ external_account = ESP::ExternalAccount.create(name: 'bob', arn: @external_account.arn, sub_organization_id: @external_account.sub_organization_id, team_id: @external_account.team_id)
106
+
107
+ assert_predicate external_account, :new?
108
+ assert_contains external_account.errors, "The account for this ARN is already being checked by Dev"
109
+
110
+ refute_predicate @external_account, :new?
111
+ @external_account.name = @external_account.name
112
+
113
+ assert_predicate @external_account, :save
114
+
115
+ external_account = build(:external_account, id: 999)
116
+
117
+ assert_raises ActiveResource::ResourceNotFound do
118
+ external_account.destroy
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,175 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class OrganizationTest < ActiveSupport::TestCase
5
+ context ESP::Organization do
6
+ context '#create' do
7
+ should 'not be implemented' do
8
+ organization = build(:organization)
9
+ assert_raises ESP::NotImplementedError do
10
+ organization.save
11
+ end
12
+ end
13
+ end
14
+
15
+ context '#destroy' do
16
+ should 'not be implemented' do
17
+ organization = build(:organization)
18
+ assert_raises ESP::NotImplementedError do
19
+ organization.destroy
20
+ end
21
+ end
22
+ end
23
+
24
+ context '#teams' do
25
+ should 'call the api' do
26
+ organization = build(:organization)
27
+ stub_request(:get, /teams.json*/).to_return(body: json_list(:organization, 2))
28
+
29
+ organization.teams
30
+
31
+ assert_requested(:get, /teams.json*/) do |req|
32
+ assert_equal "filter[organization_id_eq]=#{organization.id}", URI.unescape(req.uri.query)
33
+ end
34
+ end
35
+ end
36
+
37
+ context '#sub_organizations' do
38
+ should 'call the api' do
39
+ organization = build(:organization)
40
+ stub_request(:get, /sub_organizations.json*/).to_return(body: json_list(:sub_organization, 2))
41
+
42
+ organization.sub_organizations
43
+
44
+ assert_requested(:get, /sub_organizations.json*/) do |req|
45
+ assert_equal "filter[organization_id_eq]=#{organization.id}", URI.unescape(req.uri.query)
46
+ end
47
+ end
48
+ end
49
+
50
+ context '#users' do
51
+ should 'call the api' do
52
+ organization = build(:organization)
53
+ stub_request(:get, /users.json*/).to_return(body: json_list(:user, 2))
54
+
55
+ organization.users
56
+
57
+ assert_requested(:get, /users.json*/) do |req|
58
+ assert_equal "filter[organization_id_eq]=#{organization.id}", URI.unescape(req.uri.query)
59
+ end
60
+ end
61
+ end
62
+
63
+ context '#reports' do
64
+ should 'call the api' do
65
+ organization = build(:organization)
66
+ stub_request(:get, /reports.json*/).to_return(body: json_list(:report, 2))
67
+
68
+ organization.reports
69
+
70
+ assert_requested(:get, /reports.json*/) do |req|
71
+ assert_equal "filter[organization_id_eq]=#{organization.id}", URI.unescape(req.uri.query)
72
+ end
73
+ end
74
+ end
75
+
76
+ context '#external_accounts' do
77
+ should 'call the api' do
78
+ organization = build(:organization)
79
+ stub_request(:get, /external_accounts.json*/).to_return(body: json_list(:external_account, 2))
80
+
81
+ organization.external_accounts
82
+
83
+ assert_requested(:get, /external_accounts.json*/) do |req|
84
+ assert_equal "filter[organization_id_eq]=#{organization.id}", URI.unescape(req.uri.query)
85
+ end
86
+ end
87
+ end
88
+
89
+ context '#custom_signatures' do
90
+ should 'call the api' do
91
+ organization = build(:organization)
92
+ stub_request(:get, /custom_signatures.json*/).to_return(body: json_list(:custom_signature, 2))
93
+
94
+ organization.custom_signatures
95
+
96
+ assert_requested(:get, /custom_signatures.json*/) do |req|
97
+ assert_equal "filter[organization_id_eq]=#{organization.id}", URI.unescape(req.uri.query)
98
+ end
99
+ end
100
+ end
101
+
102
+ context 'live calls' do
103
+ setup do
104
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
105
+ WebMock.allow_net_connect!
106
+ @organization = ESP::Organization.last
107
+ skip "Live DB does not have any sub organizations. Add a sub organization and run tests again." if @organization.blank?
108
+ end
109
+
110
+ teardown do
111
+ WebMock.disable_net_connect!
112
+ end
113
+
114
+ context '#teams' do
115
+ should 'return teams' do
116
+ teams = @organization.teams
117
+
118
+ assert_equal ESP::Team, teams.resource_class
119
+ end
120
+ end
121
+
122
+ context '#sub_organizations' do
123
+ should 'return a sub_organization' do
124
+ sub_organizations = @organization.sub_organizations
125
+
126
+ assert_equal ESP::SubOrganization, sub_organizations.resource_class
127
+ end
128
+ end
129
+
130
+ context '#users' do
131
+ should 'return an array of users' do
132
+ users = @organization.users
133
+
134
+ assert_equal ESP::User, users.resource_class
135
+ end
136
+ end
137
+
138
+ context '#reports' do
139
+ should 'return an array of reports' do
140
+ reports = @organization.reports
141
+
142
+ assert_equal ESP::Report, reports.resource_class
143
+ end
144
+ end
145
+
146
+ context '#external_accounts' do
147
+ should 'return an array of external_accounts' do
148
+ external_accounts = @organization.external_accounts
149
+
150
+ assert_equal ESP::ExternalAccount, external_accounts.resource_class
151
+ end
152
+ end
153
+
154
+ context '#custom_signatures' do
155
+ should 'return an array of custom_signatures' do
156
+ custom_signatures = @organization.custom_signatures
157
+
158
+ assert_equal ESP::CustomSignature, custom_signatures.resource_class
159
+ end
160
+ end
161
+
162
+ context '#CRUD' do
163
+ should 'be able to update' do
164
+ @organization.name = @organization.name
165
+ @organization.save
166
+
167
+ assert_nothing_raised do
168
+ ESP::Organization.find(@organization.id)
169
+ end
170
+ end
171
+ end
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,84 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class RegionTest < ActiveSupport::TestCase
5
+ context ESP::Region do
6
+ context '#create' do
7
+ should 'not be implemented' do
8
+ assert_raises ESP::NotImplementedError do
9
+ ESP::Region.create(code: 'test')
10
+ end
11
+ end
12
+ end
13
+
14
+ context '#update' do
15
+ should 'not be implemented' do
16
+ region = build(:region)
17
+ assert_raises ESP::NotImplementedError do
18
+ region.save
19
+ end
20
+ end
21
+ end
22
+
23
+ context '#destroy' do
24
+ should 'not be implemented' do
25
+ region = build(:region)
26
+ assert_raises ESP::NotImplementedError do
27
+ region.destroy
28
+ end
29
+ end
30
+ end
31
+
32
+ context '.find' do
33
+ should 'call the show api and return a region if searching by id' do
34
+ stub_region = stub_request(:get, %r{regions/5.json*}).to_return(body: json(:region))
35
+
36
+ region = ESP::Region.find(5)
37
+
38
+ assert_requested(stub_region)
39
+ assert_equal ESP::Region, region.class
40
+ end
41
+ end
42
+
43
+ context '#suppress' do
44
+ should 'call the api' do
45
+ stub_request(:post, %r{suppressions/regions.json*}).to_return(body: json(:suppression_region))
46
+ region = build(:region)
47
+
48
+ suppression = region.suppress(external_account_ids: [5], reason: 'because')
49
+
50
+ assert_requested(:post, %r{suppressions/regions.json*}) do |req|
51
+ body = JSON.parse(req.body)
52
+ assert_equal 'because', body['data']['attributes']['reason']
53
+ assert_equal [region.code], body['data']['attributes']['regions']
54
+ assert_equal [5], body['data']['attributes']['external_account_ids']
55
+ end
56
+ assert_equal ESP::Suppression::Region, suppression.class
57
+ end
58
+ end
59
+
60
+ context 'live calls' do
61
+ setup do
62
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
63
+ WebMock.allow_net_connect!
64
+ end
65
+
66
+ teardown do
67
+ WebMock.disable_net_connect!
68
+ end
69
+
70
+ context '#CRUD' do
71
+ should 'be able to read' do
72
+ region = ESP::Region.last
73
+
74
+ assert_not_nil region
75
+
76
+ region = ESP::Region.find(region.id)
77
+
78
+ assert_not_nil region
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,180 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ module ESP
4
+ class ReportTest < ActiveSupport::TestCase
5
+ context ESP::Report do
6
+ context '#update' do
7
+ should 'not be implemented' do
8
+ r = build(:report)
9
+ r.stubs(:new?).returns(false)
10
+
11
+ assert_raises ESP::NotImplementedError do
12
+ r.save
13
+ end
14
+ end
15
+ end
16
+
17
+ context '#destroy' do
18
+ should 'not be implemented' do
19
+ r = build(:report)
20
+
21
+ assert_raises ESP::NotImplementedError do
22
+ r.destroy
23
+ end
24
+ end
25
+ end
26
+
27
+ context '#organization' do
28
+ should 'call the api' do
29
+ report = build(:report, organization_id: 4)
30
+ stub_org = stub_request(:get, %r{organizations/#{report.organization_id}.json*}).to_return(body: json(:organization))
31
+
32
+ report.organization
33
+
34
+ assert_requested(stub_org)
35
+ end
36
+ end
37
+
38
+ context '#sub_organization' do
39
+ should 'call the api' do
40
+ report = build(:report, sub_organization_id: 4)
41
+ stub_sub_org = stub_request(:get, %r{sub_organizations/#{report.sub_organization_id}.json*}).to_return(body: json(:sub_organization))
42
+
43
+ report.sub_organization
44
+
45
+ assert_requested(stub_sub_org)
46
+ end
47
+ end
48
+
49
+ context '#team' do
50
+ should 'call the api' do
51
+ report = build(:report, team_id: 4)
52
+ stub_team = stub_request(:get, %r{teams/#{report.team_id}.json*}).to_return(body: json(:team))
53
+
54
+ report.team
55
+
56
+ assert_requested(stub_team)
57
+ end
58
+ end
59
+
60
+ context '#alerts' do
61
+ should 'call the api for the report and the passed in params' do
62
+ report = build(:report)
63
+ stub_request(:get, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 2))
64
+
65
+ report.alerts(status: 'pass')
66
+
67
+ assert_requested(:get, %r{reports/#{report.id}/alerts.json*}) do |req|
68
+ assert_equal "filter[status]=pass", URI.unescape(req.uri.query)
69
+ end
70
+ end
71
+ end
72
+
73
+ context '#stat' do
74
+ should 'call the api and return a stat' do
75
+ report = build(:report)
76
+ stub_stat = stub_request(:get, %r{reports/#{report.id}/stats.json*}).to_return(body: json(:stat))
77
+
78
+ stat = report.stat
79
+
80
+ assert_requested(stub_stat)
81
+ assert_equal ESP::Stat, stat.class
82
+ end
83
+ end
84
+
85
+ context '.create' do
86
+ should 'raise an error if team_id is not provided' do
87
+ error = assert_raises ArgumentError do
88
+ ESP::Report.create
89
+ end
90
+ assert_equal "You must supply a team id.", error.message
91
+ end
92
+
93
+ should 'call api and return a report' do
94
+ stubbed_report = stub_request(:post, /reports.json*/).to_return(body: json(:report))
95
+
96
+ report = ESP::Report.create(team_id: 3)
97
+
98
+ assert_requested(stubbed_report)
99
+ assert_equal ESP::Report, report.class
100
+ end
101
+
102
+ should 'call the api and return an error if an error is returned' do
103
+ stub_request(:post, /reports.json*/).to_return(body: json(:report))
104
+ error = ActiveResource::ResourceInvalid.new('oh boy')
105
+ error_response = json(:error)
106
+ response = mock(body: error_response)
107
+ error.stubs(:response).returns(response)
108
+ ESP::Report.connection.expects(:post).raises(error)
109
+
110
+ assert_nothing_raised do
111
+ r = ESP::Report.create(team_id: 3)
112
+ assert_equal JSON.parse(error_response)['errors'].first['title'], r.errors.full_messages.first
113
+ end
114
+ end
115
+ end
116
+
117
+ context 'live calls' do
118
+ setup do
119
+ skip "Make sure you run the live calls locally to ensure proper integration" if ENV['CI_SERVER']
120
+ WebMock.allow_net_connect!
121
+ @report = ESP::Report.last
122
+ skip "Live DB does not have any reports. Add a report and run tests again." if @report.blank?
123
+ end
124
+
125
+ teardown do
126
+ WebMock.disable_net_connect!
127
+ end
128
+
129
+ context '#organization' do
130
+ should 'return an organization' do
131
+ org = @report.organization
132
+
133
+ assert_equal @report.organization_id, org.id
134
+ end
135
+ end
136
+
137
+ context '#sub_organization' do
138
+ should 'return a sub_organization' do
139
+ sub_org = @report.sub_organization
140
+
141
+ assert_equal @report.sub_organization_id, sub_org.id
142
+ end
143
+ end
144
+
145
+ context '#team' do
146
+ should 'return a team' do
147
+ team = @report.team
148
+
149
+ assert_equal ESP::Team, team.class
150
+ end
151
+ end
152
+
153
+ context '#alerts' do
154
+ should 'return an array of alerts' do
155
+ alerts = @report.alerts
156
+
157
+ assert_equal ESP::Alert, alerts.resource_class
158
+ end
159
+ end
160
+
161
+ context '#stat' do
162
+ should 'return a stat' do
163
+ stat = @report.stat
164
+
165
+ assert_equal ESP::Stat, stat.class
166
+ end
167
+ end
168
+
169
+ context '.create' do
170
+ should 'return an error if a bad team_id is passed' do
171
+ assert_nothing_raised do
172
+ r = ESP::Report.create(team_id: 999)
173
+ assert_equal "Couldn't find Team", r.errors.full_messages.first
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
180
+ end