esp_sdk 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/CHANGELOG.md +6 -1
- data/Gemfile.lock +11 -1
- data/README.md +287 -28
- data/bin/esp +15 -0
- data/esp_sdk.gemspec +2 -0
- data/lib/esp/aws_clients.rb +60 -0
- data/lib/esp/commands/add_external_account.rb +57 -0
- data/lib/esp/commands/commands_tasks.rb +106 -0
- data/lib/esp/commands/console.rb +68 -0
- data/lib/esp/extensions/active_resource/formats/json_api_format.rb +12 -4
- data/lib/esp/extensions/active_resource/paginated_collection.rb +5 -5
- data/lib/esp/extensions/active_resource/validations.rb +1 -1
- data/lib/esp/external_account_creator.rb +77 -0
- data/lib/esp/resources/alert.rb +30 -34
- data/lib/esp/resources/cloud_trail_event.rb +5 -0
- data/lib/esp/resources/contact_request.rb +6 -5
- data/lib/esp/resources/custom_signature.rb +32 -56
- data/lib/esp/resources/dashboard.rb +8 -1
- data/lib/esp/resources/external_account.rb +27 -19
- data/lib/esp/resources/organization.rb +27 -3
- data/lib/esp/resources/region.rb +15 -3
- data/lib/esp/resources/report.rb +28 -24
- data/lib/esp/resources/resource.rb +26 -10
- data/lib/esp/resources/service.rb +5 -0
- data/lib/esp/resources/signature.rb +28 -12
- data/lib/esp/resources/stat.rb +21 -2
- data/lib/esp/resources/stat_custom_signature.rb +30 -4
- data/lib/esp/resources/stat_region.rb +29 -3
- data/lib/esp/resources/stat_service.rb +29 -3
- data/lib/esp/resources/stat_signature.rb +29 -3
- data/lib/esp/resources/sub_organization.rb +27 -3
- data/lib/esp/resources/suppression/region.rb +14 -32
- data/lib/esp/resources/suppression/signature.rb +14 -40
- data/lib/esp/resources/suppression/unique_identifier.rb +8 -6
- data/lib/esp/resources/suppression.rb +43 -5
- data/lib/esp/resources/tag.rb +5 -0
- data/lib/esp/resources/team.rb +33 -9
- data/lib/esp/resources/user.rb +29 -3
- data/lib/esp/version.rb +1 -1
- data/lib/esp.rb +25 -5
- data/test/esp/aws_clients_test.rb +101 -0
- data/test/esp/extensions/active_resource/formats/json_api_format_test.rb +26 -12
- data/test/esp/extensions/active_resource/paginated_collection_test.rb +93 -72
- data/test/esp/extensions/active_resource/validations_test.rb +2 -12
- data/test/esp/external_account_creator_test.rb +153 -0
- data/test/esp/resources/alert_test.rb +71 -33
- data/test/esp/resources/cloud_trail_event_test.rb +9 -1
- data/test/esp/resources/contact_request_test.rb +8 -0
- data/test/esp/resources/custom_signature_test.rb +8 -0
- data/test/esp/resources/dashboard_test.rb +8 -0
- data/test/esp/resources/external_account_test.rb +8 -0
- data/test/esp/resources/metadata_test.rb +1 -1
- data/test/esp/resources/organization_test.rb +8 -0
- data/test/esp/resources/region_test.rb +12 -4
- data/test/esp/resources/report_test.rb +13 -4
- data/test/esp/resources/resource_test.rb +208 -64
- data/test/esp/resources/service_test.rb +8 -0
- data/test/esp/resources/signature_test.rb +15 -9
- data/test/esp/resources/stat_custom_signature_test.rb +9 -1
- data/test/esp/resources/stat_region_test.rb +23 -1
- data/test/esp/resources/stat_service_test.rb +23 -1
- data/test/esp/resources/stat_signature_test.rb +23 -1
- data/test/esp/resources/stat_test.rb +52 -8
- data/test/esp/resources/sub_organization_test.rb +8 -0
- data/test/esp/resources/suppression/region_test.rb +10 -2
- data/test/esp/resources/suppression/signature_test.rb +10 -2
- data/test/esp/resources/suppression/unique_identifier_test.rb +10 -2
- data/test/esp/resources/suppression_test.rb +74 -14
- data/test/esp/resources/tag_test.rb +9 -1
- data/test/esp/resources/team_test.rb +8 -0
- data/test/esp/resources/user_test.rb +49 -19
- data/test/esp_test.rb +19 -1
- data/test/factories/alerts.rb +70 -0
- data/test/factories/organizations.rb +2 -2
- data/test/factories/regions.rb +1 -1
- data/test/factories/sub_organizations.rb +1 -1
- data/test/factories/suppressions.rb +109 -3
- data/test/factories/users.rb +65 -2
- data/test/test_helper.rb +9 -8
- metadata +41 -69
- data/bin/esp_console +0 -67
- data/rdoc/ActiveResource/Formats.html +0 -178
- data/rdoc/ActiveResource/PaginatedCollection.html +0 -912
- data/rdoc/ActiveResource.html +0 -182
- data/rdoc/ESP/Alert.html +0 -808
- data/rdoc/ESP/CloudTrailEvent.html +0 -377
- data/rdoc/ESP/ContactRequest.html +0 -368
- data/rdoc/ESP/CustomSignature.html +0 -748
- data/rdoc/ESP/Dashboard.html +0 -357
- data/rdoc/ESP/ExternalAccount.html +0 -567
- data/rdoc/ESP/Metadata.html +0 -411
- data/rdoc/ESP/Organization.html +0 -592
- data/rdoc/ESP/Region.html +0 -401
- data/rdoc/ESP/Report.html +0 -624
- data/rdoc/ESP/Service.html +0 -382
- data/rdoc/ESP/Signature.html +0 -557
- data/rdoc/ESP/Stat.html +0 -1780
- data/rdoc/ESP/StatCustomSignature.html +0 -1601
- data/rdoc/ESP/StatRegion.html +0 -1600
- data/rdoc/ESP/StatService.html +0 -1600
- data/rdoc/ESP/StatSignature.html +0 -1600
- data/rdoc/ESP/SubOrganization.html +0 -542
- data/rdoc/ESP/Suppression/Region.html +0 -456
- data/rdoc/ESP/Suppression/Signature.html +0 -472
- data/rdoc/ESP/Suppression/UniqueIdentifier.html +0 -419
- data/rdoc/ESP/Suppression.html +0 -651
- data/rdoc/ESP/Tag.html +0 -373
- data/rdoc/ESP/Team.html +0 -586
- data/rdoc/ESP/User.html +0 -485
- data/rdoc/ESP.html +0 -549
- data/rdoc/README_md.html +0 -503
- data/rdoc/created.rid +0 -31
- data/rdoc/images/add.png +0 -0
- data/rdoc/images/arrow_up.png +0 -0
- data/rdoc/images/brick.png +0 -0
- data/rdoc/images/brick_link.png +0 -0
- data/rdoc/images/bug.png +0 -0
- data/rdoc/images/bullet_black.png +0 -0
- data/rdoc/images/bullet_toggle_minus.png +0 -0
- data/rdoc/images/bullet_toggle_plus.png +0 -0
- data/rdoc/images/date.png +0 -0
- data/rdoc/images/delete.png +0 -0
- data/rdoc/images/find.png +0 -0
- data/rdoc/images/loadingAnimation.gif +0 -0
- data/rdoc/images/macFFBgHack.png +0 -0
- data/rdoc/images/package.png +0 -0
- data/rdoc/images/page_green.png +0 -0
- data/rdoc/images/page_white_text.png +0 -0
- data/rdoc/images/page_white_width.png +0 -0
- data/rdoc/images/plugin.png +0 -0
- data/rdoc/images/ruby.png +0 -0
- data/rdoc/images/tag_blue.png +0 -0
- data/rdoc/images/tag_green.png +0 -0
- data/rdoc/images/transparent.png +0 -0
- data/rdoc/images/wrench.png +0 -0
- data/rdoc/images/wrench_orange.png +0 -0
- data/rdoc/images/zoom.png +0 -0
- data/rdoc/index.html +0 -136
- data/rdoc/js/darkfish.js +0 -155
- data/rdoc/js/jquery.js +0 -4
- data/rdoc/js/navigation.js +0 -142
- data/rdoc/js/search.js +0 -94
- data/rdoc/js/search_index.js +0 -1
- data/rdoc/js/searcher.js +0 -228
- data/rdoc/rdoc.css +0 -595
- data/rdoc/table_of_contents.html +0 -942
@@ -7,7 +7,7 @@ module ActiveResource
|
|
7
7
|
context '# decode' do
|
8
8
|
context 'with ESP::Suppression' do
|
9
9
|
should 'parse nested objects correctly' do
|
10
|
-
json
|
10
|
+
json = json(:dashboard)
|
11
11
|
parsed_json = JSON.parse(json)
|
12
12
|
stub_request(:get, %r{dashboard/recent.json*}).to_return(body: json_list(:dashboard, 1))
|
13
13
|
|
@@ -18,25 +18,28 @@ module ActiveResource
|
|
18
18
|
end
|
19
19
|
|
20
20
|
context 'with ESP::Alert' do
|
21
|
-
should 'merge included objects' do
|
22
|
-
json
|
21
|
+
should 'merge nested included objects' do
|
22
|
+
json = json(:alert)
|
23
23
|
parsed_json = JSON.parse(json)
|
24
|
-
stub_request(:get, %r{
|
24
|
+
stub_request(:get, %r{alerts/1.json*}).to_return(body: json)
|
25
25
|
|
26
|
-
alert = ESP::Alert.
|
26
|
+
alert = ESP::Alert.find(1, include: 'external_account.team.organization,region,signature,cloud_trail_events')
|
27
27
|
|
28
28
|
assert_equal parsed_json['included'].detect { |e| e['type'] == 'external_accounts' }['id'], alert.external_account.id
|
29
|
+
assert_equal parsed_json['included'].detect { |e| e['type'] == 'organizations' }['id'], alert.external_account.organization.id
|
30
|
+
assert_equal parsed_json['included'].detect { |e| e['type'] == 'teams' }['id'], alert.external_account.team.id
|
31
|
+
assert_equal parsed_json['included'].detect { |e| e['type'] == 'organizations' }['id'], alert.external_account.team.organization.id
|
29
32
|
assert_equal parsed_json['included'].detect { |e| e['type'] == 'regions' }['id'], alert.region.id
|
30
33
|
assert_equal parsed_json['included'].detect { |e| e['type'] == 'signatures' }['id'], alert.signature.id
|
31
34
|
assert_equal parsed_json['included'].detect { |e| e['type'] == 'cloud_trail_events' }['id'], alert.cloud_trail_events.first.id
|
32
35
|
end
|
33
36
|
|
34
37
|
should 'assign foreign keys' do
|
35
|
-
json
|
38
|
+
json = json_list(:alert, 1)
|
36
39
|
parsed_json = JSON.parse(json)
|
37
|
-
stub_request(:
|
40
|
+
stub_request(:put, %r{reports/1/alerts.json*}).to_return(body: json)
|
38
41
|
|
39
|
-
alert = ESP::Alert.
|
42
|
+
alert = ESP::Alert.where(report_id: 1).first
|
40
43
|
|
41
44
|
assert_equal parsed_json['included'].detect { |e| e['type'] == 'external_accounts' }['id'], alert.external_account_id
|
42
45
|
assert_equal parsed_json['included'].detect { |e| e['type'] == 'regions' }['id'], alert.region_id
|
@@ -60,14 +63,25 @@ module ActiveResource
|
|
60
63
|
end
|
61
64
|
|
62
65
|
should 'merge included objects' do
|
63
|
-
alert = ESP::Alert.find(1, include: 'external_account,region,signature')
|
66
|
+
alert = ESP::Alert.find(1, include: 'external_account.team.organization,region,signature,custom_signature')
|
64
67
|
|
65
68
|
assert_not_nil alert.attributes['external_account']
|
66
69
|
assert_equal alert.external_account_id, alert.external_account.id
|
70
|
+
assert_not_nil alert.external_account.attributes['organization']
|
71
|
+
assert_equal alert.external_account.organization_id, alert.external_account.organization.id
|
72
|
+
assert_not_nil alert.external_account.attributes['team']
|
73
|
+
assert_equal alert.external_account.team_id, alert.external_account.team.id
|
74
|
+
assert_not_nil alert.external_account.team.attributes['organization']
|
75
|
+
assert_equal alert.external_account.team.organization_id, alert.external_account.team.organization.id
|
67
76
|
assert_not_nil alert.attributes['region']
|
68
77
|
assert_equal alert.region_id, alert.region.id
|
69
|
-
|
70
|
-
|
78
|
+
if alert.signature.present?
|
79
|
+
assert_not_nil alert.attributes['signature']
|
80
|
+
assert_equal alert.signature_id, alert.signature.id
|
81
|
+
else
|
82
|
+
assert_not_nil alert.attributes['custom_signature']
|
83
|
+
assert_equal alert.custom_signature_id, alert.custom_signature.id
|
84
|
+
end
|
71
85
|
end
|
72
86
|
|
73
87
|
should 'assign foreign key for a belongs_to relationship' do
|
@@ -89,7 +103,7 @@ module ActiveResource
|
|
89
103
|
context '.initialize' do
|
90
104
|
should 'parse the response and return a descriptive error message' do
|
91
105
|
error_response = json(:error, :active_record)
|
92
|
-
response
|
106
|
+
response = Net::HTTPBadRequest.new('1.0', '200', '')
|
93
107
|
response.expects(:body).returns(error_response).at_least_once
|
94
108
|
|
95
109
|
error = ActiveResource::BadRequest.new(response)
|
@@ -7,7 +7,7 @@ module ActiveResource
|
|
7
7
|
context '#parse_pagination_links' do
|
8
8
|
should 'not set the previous page or next page or last page when there is only 1 page' do
|
9
9
|
report = build(:report)
|
10
|
-
stub_request(:
|
10
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 2, page: { number: 1, size: 20 }))
|
11
11
|
|
12
12
|
alerts = report.alerts
|
13
13
|
|
@@ -22,7 +22,7 @@ module ActiveResource
|
|
22
22
|
|
23
23
|
should 'not set the previous page when on the first page' do
|
24
24
|
report = build(:report)
|
25
|
-
stub_request(:
|
25
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 2, page: { number: 1, size: 1 }))
|
26
26
|
|
27
27
|
alerts = report.alerts
|
28
28
|
|
@@ -37,7 +37,7 @@ module ActiveResource
|
|
37
37
|
|
38
38
|
should 'not set the next or last page page when on the last page' do
|
39
39
|
report = build(:report)
|
40
|
-
stub_request(:
|
40
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 2, page: { number: 2, size: 1 }))
|
41
41
|
|
42
42
|
alerts = report.alerts
|
43
43
|
|
@@ -52,7 +52,7 @@ module ActiveResource
|
|
52
52
|
|
53
53
|
should 'set the next, last and previous page page when not on the first or last page' do
|
54
54
|
report = build(:report)
|
55
|
-
stub_request(:
|
55
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 }))
|
56
56
|
|
57
57
|
alerts = report.alerts
|
58
58
|
|
@@ -67,7 +67,7 @@ module ActiveResource
|
|
67
67
|
|
68
68
|
should 'set page size on each link' do
|
69
69
|
report = build(:report)
|
70
|
-
stub_request(:
|
70
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 }))
|
71
71
|
|
72
72
|
alerts = report.alerts
|
73
73
|
|
@@ -80,7 +80,7 @@ module ActiveResource
|
|
80
80
|
# The last page may not contain the full per page number of records, and will therefore come back with an incorrect size since the
|
81
81
|
# size is based on the collection size. This will mess up further calls to previous_page or first page so remove the size so it will bring back the default size.
|
82
82
|
report = build(:report)
|
83
|
-
stub_request(:
|
83
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 2 }))
|
84
84
|
|
85
85
|
alerts = report.alerts
|
86
86
|
|
@@ -89,31 +89,33 @@ module ActiveResource
|
|
89
89
|
end
|
90
90
|
|
91
91
|
context '#first_page' do
|
92
|
-
should 'call the api with the original url and the page number 1 param' do
|
92
|
+
should 'call the api with the original url and params and the page number 1 param' do
|
93
93
|
report = build(:report)
|
94
|
-
stub_request(:
|
95
|
-
alerts = report.alerts
|
94
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 })).then.to_return(body: json_list(:alert, 3, page: { number: 1, size: 1 }))
|
95
|
+
alerts = report.alerts(status: 'pass')
|
96
96
|
|
97
97
|
page = alerts.first_page
|
98
98
|
|
99
99
|
assert_equal '1', page.current_page_number
|
100
100
|
assert_equal '2', alerts.current_page_number # original object is unchanged
|
101
|
-
assert_requested(:
|
102
|
-
|
103
|
-
|
101
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
102
|
+
body = JSON.parse(req.body)
|
103
|
+
if body["page"].present? # The first call will not have a body["page"], only the second call
|
104
|
+
assert_equal 1, body["page"]["number"]
|
105
|
+
assert_equal 'pass', body["filter"]["status"]
|
104
106
|
end
|
105
107
|
end
|
106
108
|
end
|
107
109
|
|
108
110
|
should 'not call the api and return self if already on the first page' do
|
109
111
|
report = build(:report)
|
110
|
-
stub_request(:
|
112
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 1, size: 1 }))
|
111
113
|
alerts = report.alerts
|
112
114
|
|
113
115
|
page = alerts.first_page
|
114
116
|
|
115
117
|
assert_equal '1', page.current_page_number
|
116
|
-
assert_requested(:
|
118
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
117
119
|
assert_predicate req.uri.query, :blank? # It will only be called once to get the first page
|
118
120
|
end
|
119
121
|
end
|
@@ -122,46 +124,48 @@ module ActiveResource
|
|
122
124
|
context '#first_page!' do
|
123
125
|
should 'call the api with the original url and the page number 1 param and update itself' do
|
124
126
|
report = build(:report)
|
125
|
-
stub_request(:
|
127
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 })).then.to_return(body: json_list(:alert, 3, page: { number: 1, size: 1 }))
|
126
128
|
alerts = report.alerts
|
127
129
|
|
128
130
|
alerts.first_page!
|
129
131
|
|
130
132
|
assert_equal '1', alerts.current_page_number
|
131
|
-
assert_requested(:
|
132
|
-
|
133
|
-
|
134
|
-
end
|
133
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
134
|
+
body = JSON.parse(req.body)
|
135
|
+
assert_equal 1, body["page"]["number"] if body.present? # The first call will not have a body, only the second call
|
135
136
|
end
|
136
137
|
end
|
137
138
|
end
|
138
139
|
|
139
140
|
context '#previous_page' do
|
140
|
-
should 'call the api with the original url and the previous page number param' do
|
141
|
+
should 'call the api with the original url and original params and the previous page number param' do
|
141
142
|
report = build(:report)
|
142
|
-
stub_request(:
|
143
|
-
alerts = report.alerts
|
143
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 })).then.to_return(body: json_list(:alert, 3, page: { number: 1, size: 1 }))
|
144
|
+
alerts = report.alerts(status: 'pass')
|
144
145
|
|
145
146
|
page = alerts.previous_page
|
146
147
|
|
147
148
|
assert_equal '1', page.current_page_number
|
148
149
|
assert_equal '2', alerts.current_page_number # original object is unchanged
|
149
|
-
assert_requested(:
|
150
|
-
|
151
|
-
|
150
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
151
|
+
body = JSON.parse(req.body)
|
152
|
+
if body["page"].present? # The first call will not have a body["page"], only the second call
|
153
|
+
assert_equal '1', body["page"]["number"]
|
154
|
+
assert_equal '1', body["page"]["size"]
|
155
|
+
assert_equal 'pass', body["filter"]["status"]
|
152
156
|
end
|
153
157
|
end
|
154
158
|
end
|
155
159
|
|
156
160
|
should 'not call the api and return self if already on the first page' do
|
157
161
|
report = build(:report)
|
158
|
-
stub_request(:
|
162
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 1, size: 1 }))
|
159
163
|
alerts = report.alerts
|
160
164
|
|
161
165
|
page = alerts.previous_page
|
162
166
|
|
163
167
|
assert_equal '1', page.current_page_number
|
164
|
-
assert_requested(:
|
168
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
165
169
|
assert_predicate req.uri.query, :blank? # It will only be called once to get the first page
|
166
170
|
end
|
167
171
|
end
|
@@ -170,46 +174,51 @@ module ActiveResource
|
|
170
174
|
context '#previous_page!' do
|
171
175
|
should 'call the api with the original url and the previous page number param and update itself' do
|
172
176
|
report = build(:report)
|
173
|
-
stub_request(:
|
177
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 })).then.to_return(body: json_list(:alert, 3, page: { number: 1, size: 1 }))
|
174
178
|
alerts = report.alerts
|
175
179
|
|
176
180
|
alerts.previous_page!
|
177
181
|
|
178
182
|
assert_equal '1', alerts.current_page_number
|
179
|
-
assert_requested(:
|
180
|
-
|
181
|
-
|
183
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
184
|
+
body = JSON.parse(req.body)
|
185
|
+
if body.present? # The first call will not have a body, only the second call
|
186
|
+
assert_equal '1', body["page"]["number"]
|
187
|
+
assert_equal '1', body["page"]["size"]
|
182
188
|
end
|
183
189
|
end
|
184
190
|
end
|
185
191
|
end
|
186
192
|
|
187
193
|
context '#next_page' do
|
188
|
-
should 'call the api with the original url and the next page number param' do
|
194
|
+
should 'call the api with the original url and original params and the next page number param' do
|
189
195
|
report = build(:report)
|
190
|
-
stub_request(:
|
191
|
-
alerts = report.alerts
|
196
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 })).then.to_return(body: json_list(:alert, 3, page: { number: 3, size: 1 }))
|
197
|
+
alerts = report.alerts(status: 'pass')
|
192
198
|
|
193
199
|
page = alerts.next_page
|
194
200
|
|
195
201
|
assert_equal '3', page.current_page_number
|
196
202
|
assert_equal '2', alerts.current_page_number # original object is unchanged
|
197
|
-
assert_requested(:
|
198
|
-
|
199
|
-
|
203
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
204
|
+
body = JSON.parse(req.body)
|
205
|
+
if body["page"].present? # The first call will not have a body["page"], only the second call
|
206
|
+
assert_equal '3', body["page"]["number"]
|
207
|
+
assert_equal '1', body["page"]["size"]
|
208
|
+
assert_equal 'pass', body["filter"]["status"]
|
200
209
|
end
|
201
210
|
end
|
202
211
|
end
|
203
212
|
|
204
213
|
should 'not call the api and return self if already on the last page' do
|
205
214
|
report = build(:report)
|
206
|
-
stub_request(:
|
215
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 3, size: 1 }))
|
207
216
|
alerts = report.alerts
|
208
217
|
|
209
218
|
page = alerts.next_page
|
210
219
|
|
211
220
|
assert_equal '3', page.current_page_number
|
212
|
-
assert_requested(:
|
221
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
213
222
|
assert_predicate req.uri.query, :blank? # It will only be called once to get the first page
|
214
223
|
end
|
215
224
|
end
|
@@ -218,46 +227,51 @@ module ActiveResource
|
|
218
227
|
context '#next_page!' do
|
219
228
|
should 'call the api with the original url and the next page number param and update itself' do
|
220
229
|
report = build(:report)
|
221
|
-
stub_request(:
|
230
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 })).then.to_return(body: json_list(:alert, 3, page: { number: 3, size: 1 }))
|
222
231
|
alerts = report.alerts
|
223
232
|
|
224
233
|
alerts.next_page!
|
225
234
|
|
226
235
|
assert_equal '3', alerts.current_page_number
|
227
|
-
assert_requested(:
|
228
|
-
|
229
|
-
|
236
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
237
|
+
body = JSON.parse(req.body)
|
238
|
+
if body.present? # The first call will not have a body, only the second call
|
239
|
+
assert_equal '3', body["page"]["number"]
|
240
|
+
assert_equal '1', body["page"]["size"]
|
230
241
|
end
|
231
242
|
end
|
232
243
|
end
|
233
244
|
end
|
234
245
|
|
235
246
|
context '#last_page' do
|
236
|
-
should 'call the api with the original url and the last page number param' do
|
247
|
+
should 'call the api with the original url and original params and the last page number param' do
|
237
248
|
report = build(:report)
|
238
|
-
stub_request(:
|
239
|
-
alerts = report.alerts
|
249
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 })).then.to_return(body: json_list(:alert, 3, page: { number: 3, size: 1 }))
|
250
|
+
alerts = report.alerts(status: 'pass')
|
240
251
|
|
241
252
|
page = alerts.last_page
|
242
253
|
|
243
254
|
assert_equal '3', page.current_page_number
|
244
255
|
assert_equal '2', alerts.current_page_number # original object is unchanged
|
245
|
-
assert_requested(:
|
246
|
-
|
247
|
-
|
256
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
257
|
+
body = JSON.parse(req.body)
|
258
|
+
if body["page"].present? # The first call will not have a body["page"], only the second call
|
259
|
+
assert_equal '3', body["page"]["number"]
|
260
|
+
assert_equal '1', body["page"]["size"]
|
261
|
+
assert_equal 'pass', body["filter"]["status"]
|
248
262
|
end
|
249
263
|
end
|
250
264
|
end
|
251
265
|
|
252
266
|
should 'not call the api and return self if already on the last page' do
|
253
267
|
report = build(:report)
|
254
|
-
stub_request(:
|
268
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 3, size: 1 }))
|
255
269
|
alerts = report.alerts
|
256
270
|
|
257
271
|
page = alerts.last_page
|
258
272
|
|
259
273
|
assert_equal '3', page.current_page_number
|
260
|
-
assert_requested(:
|
274
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
261
275
|
assert_predicate req.uri.query, :blank? # It will only be called once to get the first page
|
262
276
|
end
|
263
277
|
end
|
@@ -266,15 +280,17 @@ module ActiveResource
|
|
266
280
|
context '#last_page!' do
|
267
281
|
should 'call the api with the original url and the last page number param and update itself' do
|
268
282
|
report = build(:report)
|
269
|
-
stub_request(:
|
283
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 })).then.to_return(body: json_list(:alert, 3, page: { number: 3, size: 1 }))
|
270
284
|
alerts = report.alerts
|
271
285
|
|
272
286
|
alerts.last_page!
|
273
287
|
|
274
288
|
assert_equal '3', alerts.current_page_number
|
275
|
-
assert_requested(:
|
276
|
-
|
277
|
-
|
289
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
290
|
+
body = JSON.parse(req.body)
|
291
|
+
if body.present? # The first call will not have a body, only the second call
|
292
|
+
assert_equal '3', body["page"]["number"]
|
293
|
+
assert_equal '1', body["page"]["size"]
|
278
294
|
end
|
279
295
|
end
|
280
296
|
end
|
@@ -283,7 +299,7 @@ module ActiveResource
|
|
283
299
|
context '#page' do
|
284
300
|
should 'raise an error if the page number is not given' do
|
285
301
|
report = build(:report)
|
286
|
-
stub_request(:
|
302
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 })).then.to_return(body: json_list(:alert, 3, page: { number: 3, size: 1 }))
|
287
303
|
alerts = report.alerts
|
288
304
|
|
289
305
|
error = assert_raises ArgumentError do
|
@@ -294,7 +310,7 @@ module ActiveResource
|
|
294
310
|
|
295
311
|
should 'raise an error if the page number is not a positive number' do
|
296
312
|
report = build(:report)
|
297
|
-
stub_request(:
|
313
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 })).then.to_return(body: json_list(:alert, 3, page: { number: 3, size: 1 }))
|
298
314
|
alerts = report.alerts
|
299
315
|
|
300
316
|
error = assert_raises ArgumentError do
|
@@ -305,7 +321,7 @@ module ActiveResource
|
|
305
321
|
|
306
322
|
should 'raise an error if the page number is greater than the last page number' do
|
307
323
|
report = build(:report)
|
308
|
-
stub_request(:
|
324
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 })).then.to_return(body: json_list(:alert, 3, page: { number: 3, size: 1 }))
|
309
325
|
alerts = report.alerts
|
310
326
|
|
311
327
|
error = assert_raises ArgumentError do
|
@@ -314,31 +330,34 @@ module ActiveResource
|
|
314
330
|
assert_equal "Page number cannot be greater than the last page number.", error.message
|
315
331
|
end
|
316
332
|
|
317
|
-
should 'call the api with the original url and the page number param' do
|
333
|
+
should 'call the api with the original url and original params and the page number param' do
|
318
334
|
report = build(:report)
|
319
|
-
stub_request(:
|
320
|
-
alerts = report.alerts
|
335
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 })).then.to_return(body: json_list(:alert, 3, page: { number: 3, size: 1 }))
|
336
|
+
alerts = report.alerts(status: 'pass')
|
321
337
|
|
322
338
|
page = alerts.page(3)
|
323
339
|
|
324
340
|
assert_equal '3', page.current_page_number
|
325
341
|
assert_equal '2', alerts.current_page_number # original object is unchanged
|
326
|
-
assert_requested(:
|
327
|
-
|
328
|
-
|
342
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
343
|
+
body = JSON.parse(req.body)
|
344
|
+
if body["page"].present? # The first call will not have a body["page"], only the second call
|
345
|
+
assert_equal 3, body["page"]["number"]
|
346
|
+
assert_equal '1', body["page"]["size"]
|
347
|
+
assert_equal 'pass', body["filter"]["status"]
|
329
348
|
end
|
330
349
|
end
|
331
350
|
end
|
332
351
|
|
333
352
|
should 'not call the api and return self if already on that page' do
|
334
353
|
report = build(:report)
|
335
|
-
stub_request(:
|
354
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 }))
|
336
355
|
alerts = report.alerts
|
337
356
|
|
338
357
|
page = alerts.page(2)
|
339
358
|
|
340
359
|
assert_equal '2', page.current_page_number
|
341
|
-
assert_requested(:
|
360
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
342
361
|
assert_predicate req.uri.query, :blank? # It will only be called once to get the first page
|
343
362
|
end
|
344
363
|
end
|
@@ -347,15 +366,17 @@ module ActiveResource
|
|
347
366
|
context '#page!' do
|
348
367
|
should 'call the api with the original url and the page number 1 param and update itself' do
|
349
368
|
report = build(:report)
|
350
|
-
stub_request(:
|
369
|
+
stub_request(:put, %r{reports/#{report.id}/alerts.json*}).to_return(body: json_list(:alert, 3, page: { number: 2, size: 1 })).then.to_return(body: json_list(:alert, 3, page: { number: 3, size: 1 }))
|
351
370
|
alerts = report.alerts
|
352
371
|
|
353
372
|
alerts.page!(3)
|
354
373
|
|
355
374
|
assert_equal '3', alerts.current_page_number
|
356
|
-
assert_requested(:
|
357
|
-
|
358
|
-
|
375
|
+
assert_requested(:put, %r{reports/#{report.id}/alerts.json*}) do |req|
|
376
|
+
body = JSON.parse(req.body)
|
377
|
+
if body.present? # The first call will not have a body, only the second call
|
378
|
+
assert_equal '3', body["page"]["number"].to_s
|
379
|
+
assert_equal '1', body["page"]["size"].to_s
|
359
380
|
end
|
360
381
|
end
|
361
382
|
end
|
@@ -373,7 +394,7 @@ module ActiveResource
|
|
373
394
|
end
|
374
395
|
|
375
396
|
should 'always return the correct page and update itself when using the ! methods' do
|
376
|
-
report = ESP::Report.
|
397
|
+
report = ESP::Report.all.detect { |r| r.status == 'complete' }
|
377
398
|
alerts = report.alerts
|
378
399
|
last_page_number = alerts.last_page_number
|
379
400
|
|
@@ -402,7 +423,7 @@ module ActiveResource
|
|
402
423
|
end
|
403
424
|
|
404
425
|
should 'always return the correct page and when not using the ! methods' do
|
405
|
-
report = ESP::Report.
|
426
|
+
report = ESP::Report.all.detect { |r| r.status == 'complete' }
|
406
427
|
alerts = report.alerts
|
407
428
|
last_page_number = alerts.last_page_number
|
408
429
|
|
@@ -6,12 +6,7 @@ module ActiveResource
|
|
6
6
|
context "with ESP::Team" do
|
7
7
|
context '#load_remote_errors' do
|
8
8
|
should 'should parse an active record response and put error messages in the errors object' do
|
9
|
-
|
10
|
-
error_response = json(:error, :active_record)
|
11
|
-
response = mock(body: error_response)
|
12
|
-
error.expects(:response).returns(response)
|
13
|
-
ActiveResource::Connection.any_instance.expects(:post).raises(error)
|
14
|
-
stub_request(:post, /teams.json*/).to_return(body: json_list(:alert, 2))
|
9
|
+
stub_request(:post, /teams.json*/).to_return(status: 422, body: json(:error, :active_record))
|
15
10
|
|
16
11
|
team = ESP::Team.create
|
17
12
|
|
@@ -21,12 +16,7 @@ module ActiveResource
|
|
21
16
|
end
|
22
17
|
|
23
18
|
should 'should parse a non active record response and put error messages in the errors object' do
|
24
|
-
|
25
|
-
error_response = json(:error)
|
26
|
-
response = mock(body: error_response)
|
27
|
-
error.expects(:response).returns(response)
|
28
|
-
ActiveResource::Connection.any_instance.expects(:post).raises(error)
|
29
|
-
stub_request(:post, /teams.json*/).to_return(body: json_list(:alert, 2))
|
19
|
+
stub_request(:post, /teams.json*/).to_return(status: 422, body: json(:error))
|
30
20
|
|
31
21
|
team = ESP::Team.create
|
32
22
|
|