esp_sdk 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,84 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../../test_helper')
2
+
3
+ module ESP
4
+ class CustomSignature
5
+ class Result
6
+ class AlertTest < ActiveSupport::TestCase
7
+ context ESP::CustomSignature::Result::Alert do
8
+ context '#region' do
9
+ should 'call the api' do
10
+ alert = build(:result_alert, region_id: 4)
11
+ stubbed_region = stub_request(:get, %r{regions/#{alert.region_id}.json*}).to_return(body: json(:region))
12
+
13
+ alert.region
14
+
15
+ assert_requested(stubbed_region)
16
+ end
17
+ end
18
+
19
+ context '#external_account' do
20
+ should 'call the api' do
21
+ alert = build(:result_alert, external_account_id: 4)
22
+ stubbed_external_account = stub_request(:get, %r{external_accounts/#{alert.external_account_id}.json*}).to_return(body: json(:external_account))
23
+
24
+ alert.external_account
25
+
26
+ assert_requested(stubbed_external_account)
27
+ end
28
+ end
29
+
30
+ context '#custom_signature' do
31
+ should 'call the api' do
32
+ alert = build(:result_alert, custom_signature_id: 4)
33
+ stubbed_custom_signature = stub_request(:get, %r{custom_signatures/#{alert.custom_signature_id}.json*}).to_return(body: json(:custom_signature))
34
+
35
+ alert.custom_signature
36
+
37
+ assert_requested(stubbed_custom_signature)
38
+ end
39
+ end
40
+
41
+ context '.find' do
42
+ should 'raise ESP::NotImplementedError' do
43
+ assert_raises ESP::NotImplementedError do
44
+ ESP::CustomSignature::Result::Alert.find(1)
45
+ end
46
+ end
47
+ end
48
+
49
+ context '.where' do
50
+ should 'raise ESP::NotImplementedError' do
51
+ assert_raises ESP::NotImplementedError do
52
+ ESP::CustomSignature::Result::Alert.where(id_eq: 1)
53
+ end
54
+ end
55
+ end
56
+
57
+ context '#create' do
58
+ should 'raise ESP::NotImplementedError' do
59
+ assert_raises ESP::NotImplementedError do
60
+ ESP::CustomSignature::Result::Alert.new.create
61
+ end
62
+ end
63
+ end
64
+
65
+ context '#update' do
66
+ should 'raise ESP::NotImplementedError' do
67
+ assert_raises ESP::NotImplementedError do
68
+ ESP::CustomSignature::Result::Alert.new.update
69
+ end
70
+ end
71
+ end
72
+
73
+ context '#destroy' do
74
+ should 'raise ESP::NotImplementedError' do
75
+ assert_raises ESP::NotImplementedError do
76
+ ESP::CustomSignature::Result::Alert.new.destroy
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,91 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../test_helper')
2
+
3
+ module ESP
4
+ class CustomSignature
5
+ class ResultTest < ActiveSupport::TestCase
6
+ context ESP::CustomSignature::Result do
7
+ context '#definition' do
8
+ should 'call the api' do
9
+ result = build(:result, definition_id: 4)
10
+ stubbed_definition = stub_request(:get, %r{custom_signature_definitions/#{result.definition_id}.json*}).to_return(body: json(:definition))
11
+
12
+ result.definition
13
+
14
+ assert_requested(stubbed_definition)
15
+ end
16
+ end
17
+
18
+ context '#region' do
19
+ should 'call the api' do
20
+ result = build(:result, region_id: 4)
21
+ stubbed_region = stub_request(:get, %r{regions/#{result.region_id}.json*}).to_return(body: json(:region))
22
+
23
+ result.region
24
+
25
+ assert_requested(stubbed_region)
26
+ end
27
+ end
28
+
29
+ context '#external_account' do
30
+ should 'call the api' do
31
+ result = build(:result, external_account_id: 4)
32
+ stubbed_external_account = stub_request(:get, %r{external_accounts/#{result.external_account_id}.json*}).to_return(body: json(:external_account))
33
+
34
+ result.external_account
35
+
36
+ assert_requested(stubbed_external_account)
37
+ end
38
+ end
39
+
40
+ context '#alerts' do
41
+ should 'call the api' do
42
+ result = build(:result)
43
+ stub_request(:get, %r{custom_signature_results/#{result.id}/alerts.json}).to_return(body: json_list(:result_alert, 2))
44
+
45
+ result.alerts
46
+
47
+ assert_requested(:get, %r{custom_signature_results/#{result.id}/alerts.json})
48
+ end
49
+ end
50
+
51
+ context 'activate' do
52
+ should 'call the api' do
53
+ definition = build(:definition)
54
+ stubbed_defintion = stub_request(:patch, %r{custom_signature_definitions/#{definition.id}/activate.json}).to_return(body: json(:definition))
55
+
56
+ definition.activate
57
+
58
+ assert_requested stubbed_defintion
59
+ end
60
+ end
61
+
62
+ context 'archive' do
63
+ should 'call the api' do
64
+ definition = build(:definition)
65
+ stubbed_defintion = stub_request(:patch, %r{custom_signature_definitions/#{definition.id}/archive.json}).to_return(body: json(:definition))
66
+
67
+ definition.archive
68
+
69
+ assert_requested stubbed_defintion
70
+ end
71
+ end
72
+
73
+ context '#update' do
74
+ should 'raise ESP::NotImplementedError' do
75
+ assert_raises ESP::NotImplementedError do
76
+ ESP::CustomSignature::Result::Alert.new.update
77
+ end
78
+ end
79
+ end
80
+
81
+ context '#destroy' do
82
+ should 'raise ESP::NotImplementedError' do
83
+ assert_raises ESP::NotImplementedError do
84
+ ESP::CustomSignature::Result::Alert.new.destroy
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -28,130 +28,15 @@ module ESP
28
28
  end
29
29
  end
30
30
 
31
- context '.run_sanity_test!' do
32
- should 'call the api and pass params' do
33
- custom_signature = build(:custom_signature, external_account_id: 3)
34
- stub_request(:post, %r{custom_signatures/run.json*}).to_return(body: json_list(:alert, 2))
35
-
36
- alerts = ESP::CustomSignature.run!(external_account_id: 3, regions: 'param2', language: custom_signature.language, signature: custom_signature.signature)
37
-
38
- assert_requested(:post, %r{custom_signatures/run.json*}) do |req|
39
- body = JSON.parse req.body
40
- assert_equal false, body['data'].key?('id')
41
- assert_equal ['param2'], body['data']['attributes']['regions']
42
- assert_equal custom_signature.language, body['data']['attributes']['language']
43
- assert_equal custom_signature.signature, body['data']['attributes']['signature']
44
- end
45
- assert_equal ESP::Alert, alerts.resource_class
46
- end
47
-
48
- should 'throw an error if an error is returned' do
49
- custom_signature = build(:custom_signature, external_account_id: 3)
50
- error = ActiveResource::BadRequest.new('')
51
- error_response = json(:error)
52
- response = mock(body: error_response, code: '400')
53
- error.stubs(:response).returns(response)
54
- ActiveResource::Connection.any_instance.expects(:post).raises(error)
55
- stub_request(:post, %r{custom_signatures/run.json*}).to_return(body: json_list(:alert, 2))
56
-
57
- error = assert_raises ActiveResource::ResourceInvalid do
58
- ESP::CustomSignature.run!(external_account_id: 3, regions: 'param2', language: custom_signature.language, signature: custom_signature.signature)
59
- end
60
- assert_equal "Failed. Response code = 400. Response message = #{JSON.parse(error_response)['errors'].first['title']}.", error.message
61
- end
62
- end
63
-
64
- context '.run_sanity_test' do
65
- should 'call the api and pass params' do
66
- custom_signature = build(:custom_signature, external_account_id: 3)
67
- stub_request(:post, %r{custom_signatures/run.json*}).to_return(body: json_list(:alert, 2))
68
-
69
- alerts = ESP::CustomSignature.run(external_account_id: 3, regions: 'param2', language: custom_signature.language, signature: custom_signature.signature)
70
-
71
- assert_requested(:post, %r{custom_signatures/run.json*}) do |req|
72
- body = JSON.parse req.body
73
- assert_equal false, body['data'].key?('id')
74
- assert_equal ['param2'], body['data']['attributes']['regions']
75
- assert_equal custom_signature.language, body['data']['attributes']['language']
76
- assert_equal custom_signature.signature, body['data']['attributes']['signature']
77
- end
78
- assert_equal ESP::Alert, alerts.resource_class
79
- end
80
-
81
- should 'not throw an error if an error is returned' do
82
- custom_signature = build(:custom_signature, external_account_id: 3)
83
- error = ActiveResource::BadRequest.new('')
84
- error_response = json(:error)
85
- response = mock(body: error_response, code: '400')
86
- error.stubs(:response).returns(response)
87
- ActiveResource::Connection.any_instance.expects(:post).raises(error)
88
- stub_request(:post, %r{custom_signatures/run.json*}).to_return(body: json_list(:alert, 2))
89
-
90
- assert_nothing_raised do
91
- result = ESP::CustomSignature.run(external_account_id: 3, regions: 'param2', language: custom_signature.language, signature: custom_signature.signature)
92
- assert_equal JSON.parse(error_response)['errors'].first['title'], result.errors.full_messages.first
93
- end
94
- end
95
- end
96
-
97
- context '#run!' do
98
- should 'call the api and pass params' do
99
- custom_signature = build(:custom_signature, external_account_id: 3)
100
- stub_request(:post, %r{custom_signatures/#{custom_signature.id}/run.json*}).to_return(body: json_list(:alert, 2))
101
-
102
- alerts = custom_signature.run!(regions: 'param2')
103
-
104
- assert_requested(:post, %r{custom_signatures/#{custom_signature.id}/run.json*}) do |req|
105
- body = JSON.parse req.body
106
- assert_equal custom_signature.id, body['data']['id']
107
- assert_equal ['param2'], body['data']['attributes']['regions']
108
- end
109
- assert_equal ESP::Alert, alerts.resource_class
110
- end
111
-
112
- should 'throw an error if an error is returned' do
113
- custom_signature = build(:custom_signature, external_account_id: 3)
114
- error = ActiveResource::BadRequest.new('')
115
- error_response = json(:error)
116
- response = mock(body: error_response, code: '400')
117
- error.stubs(:response).returns(response)
118
- ActiveResource::Connection.any_instance.expects(:post).raises(error)
119
- stub_request(:post, %r{custom_signatures/#{custom_signature.id}/run.json*}).to_return(body: json_list(:alert, 2))
120
-
121
- error = assert_raises ActiveResource::ResourceInvalid do
122
- custom_signature.run!(regions: 'param2')
123
- end
124
- assert_equal "Failed. Response code = 400. Response message = #{JSON.parse(error_response)['errors'].first['title']}.", error.message
125
- end
126
- end
127
-
128
- context '#run' do
129
- should 'call the api and pass params' do
130
- custom_signature = build(:custom_signature, external_account_id: 3)
131
- stub_request(:post, %r{custom_signatures/#{custom_signature.id}/run.json*}).to_return(body: json_list(:alert, 2))
132
-
133
- alerts = custom_signature.run(regions: 'param2')
134
-
135
- assert_requested(:post, %r{custom_signatures/#{custom_signature.id}/run.json*}) do |req|
136
- body = JSON.parse req.body
137
- assert_equal custom_signature.id, body['data']['id']
138
- assert_equal ['param2'], body['data']['attributes']['regions']
139
- end
140
- assert_equal ESP::Alert, alerts.resource_class
141
- end
31
+ context '#definitions' do
32
+ should 'call the api' do
33
+ custom_signature = build(:custom_signature, team_id: 1)
34
+ stub_request(:get, /custom_signature_definitions.json*/).to_return(body: json_list(:definition, 2))
142
35
 
143
- should 'not throw an error if an error is returned' do
144
- custom_signature = build(:custom_signature, external_account_id: 3)
145
- error = ActiveResource::BadRequest.new('')
146
- error_response = json(:error)
147
- response = mock(body: error_response, code: '400')
148
- error.stubs(:response).returns(response)
149
- ActiveResource::Connection.any_instance.expects(:post).raises(error)
150
- stub_request(:post, %r{custom_signatures/#{custom_signature.id}/run.json*}).to_return(body: json_list(:alert, 2))
36
+ custom_signature.definitions
151
37
 
152
- assert_nothing_raised do
153
- custom_signature.run(regions: 'param2')
154
- assert_equal JSON.parse(error_response)['errors'].first['title'], custom_signature.errors.full_messages.first
38
+ assert_requested(:get, /custom_signature_definitions.json*/) do |req|
39
+ assert_equal "filter[custom_signature_id_eq]=#{custom_signature.id}", URI.unescape(req.uri.query)
155
40
  end
156
41
  end
157
42
  end
@@ -24,6 +24,14 @@ module ESP
24
24
  end
25
25
  end
26
26
 
27
+ context '.where' do
28
+ should 'not be implemented' do
29
+ assert_raises ESP::NotImplementedError do
30
+ ESP::Service.where(id_eq: 1)
31
+ end
32
+ end
33
+ end
34
+
27
35
  context '#create' do
28
36
  should 'not be implemented' do
29
37
  assert_raises ESP::NotImplementedError do
@@ -0,0 +1,30 @@
1
+ FactoryGirl.define do
2
+ factory :definition, class: 'ESP::CustomSignature::Definition' do
3
+ skip_create
4
+
5
+ sequence(:id) { |n| n }
6
+ code 'abc'
7
+ created_at { Time.current }
8
+ language 'javascript'
9
+ status "active"
10
+ updated_at { Time.current }
11
+
12
+ relationships do
13
+ { custom_signature: {
14
+ data: {
15
+ type: "custom_signatures",
16
+ id: "1"
17
+ },
18
+ links: {
19
+ related: "http://localhost:3000/api/v2/custom_signatures/1.json"
20
+ }
21
+ },
22
+ results: {
23
+ links: {
24
+ related: "http://localhost:3000/api/v2/custom_signature_results.json?filter%5Bdefinition_id_eq%5D=#{id}"
25
+ }
26
+ }
27
+ }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,94 @@
1
+ FactoryGirl.define do
2
+ factory :result_alert, class: 'ESP::CustomSignature::Result::Alert' do
3
+ skip_create
4
+
5
+ sequence(:id) { |n| n }
6
+ type "alerts"
7
+ created_at { Time.current }
8
+ status "fail"
9
+ resource "resource-3"
10
+ updated_at nil
11
+ started_at { Time.current }
12
+ ended_at nil
13
+ metadata { { abc: '123' } }
14
+ tags { [{ key: 'Name', value: 'i-abc123' }] }
15
+ relationships do
16
+ {
17
+ external_account: {
18
+ data: {
19
+ type: "external_accounts",
20
+ id: "1015"
21
+ },
22
+ links: {
23
+ related: "http://test.host/api/v2/external_accounts/1015.json"
24
+ }
25
+ },
26
+ region: {
27
+ data: {
28
+ type: "regions",
29
+ id: "1014"
30
+ }
31
+ },
32
+ custom_signature: {
33
+ data: nil
34
+ }
35
+ }
36
+ end
37
+ included do
38
+ [
39
+ {
40
+ id: "1015",
41
+ type: "external_accounts",
42
+ attributes: {
43
+ account: "5",
44
+ arn: "arn:aws:iam::5:role/test_sts_role",
45
+ created_at: "2015-09-11T21:12:15.183Z",
46
+ external_id: "test_sts_external_id_1",
47
+ name: nil,
48
+ updated_at: nil,
49
+ user_attribution_role: nil
50
+ },
51
+ relationships: {
52
+ organization: {
53
+ data: {
54
+ type: "organizations",
55
+ id: "5"
56
+ },
57
+ links: {
58
+ related: "http://test.host/api/v2/organizations/5.json"
59
+ }
60
+ },
61
+ sub_organization: {
62
+ data: {
63
+ type: "sub_organizations",
64
+ id: "5"
65
+ },
66
+ links: {
67
+ related: "http://test.host/api/v2/sub_organizations/5.json"
68
+ }
69
+ },
70
+ team: {
71
+ data: {
72
+ type: "teams",
73
+ id: "5"
74
+ },
75
+ links: {
76
+ related: "http://test.host/api/v2/teams/5.json"
77
+ }
78
+ },
79
+ user_attribution_role: {
80
+ data: nil
81
+ }
82
+ }
83
+ },
84
+ {
85
+ id: "1014",
86
+ type: "regions",
87
+ attributes: {
88
+ code: "us_east_test_3"
89
+ }
90
+ }
91
+ ]
92
+ end
93
+ end
94
+ end