Skip to content

Commit

Permalink
Merge pull request #511 from benevity/feature/expand-apigw-testing
Browse files Browse the repository at this point in the history
Expand API Gateway Testing
  • Loading branch information
k1LoW authored May 13, 2020
2 parents 60800f1 + 3d2806b commit 5fbf658
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 2 deletions.
1 change: 1 addition & 0 deletions awspec.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'rspec-its'
spec.add_runtime_dependency 'term-ansicolor'
spec.add_runtime_dependency 'thor'
spec.add_runtime_dependency 'addressable'
spec.add_development_dependency 'bundler', '>= 1.9', '< 3.0'
spec.add_development_dependency 'octorelease'
spec.add_development_dependency 'pry'
Expand Down
12 changes: 10 additions & 2 deletions doc/resource_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ describe apigateway('my-apigateway') do
end
```

### have_integration_method

### have_integration_path

### have_method

### have_path

### its(:id), its(:name), its(:description), its(:created_date), its(:version), its(:warnings), its(:binary_media_types), its(:minimum_compression_size), its(:api_key_source), its(:policy), its(:tags)
## <a name="autoscaling_group">autoscaling_group</a>

Expand Down Expand Up @@ -3381,7 +3389,7 @@ end
```


### its(:name), its(:type), its(:key_id), its(:last_modified_date), its(:last_modified_user), its(:description), its(:allowed_pattern), its(:version), its(:tier), its(:policies)
### its(:name), its(:type), its(:key_id), its(:last_modified_date), its(:last_modified_user), its(:description), its(:allowed_pattern), its(:version), its(:tier), its(:policies), its(:data_type)
### :unlock: Advanced use

```ruby
Expand Down Expand Up @@ -3425,7 +3433,7 @@ end
```


### its(:availability_zone), its(:availability_zone_id), its(:available_ip_address_count), its(:cidr_block), its(:default_for_az), its(:map_public_ip_on_launch), its(:state), its(:subnet_id), its(:vpc_id), its(:owner_id), its(:assign_ipv_6_address_on_creation), its(:ipv_6_cidr_block_association_set), its(:subnet_arn), its(:outpost_arn)
### its(:availability_zone), its(:availability_zone_id), its(:available_ip_address_count), its(:cidr_block), its(:default_for_az), its(:map_public_ip_on_launch), its(:map_customer_owned_ip_on_launch), its(:customer_owned_ipv_4_pool), its(:state), its(:subnet_id), its(:vpc_id), its(:owner_id), its(:assign_ipv_6_address_on_creation), its(:ipv_6_cidr_block_association_set), its(:subnet_arn), its(:outpost_arn)
### :unlock: Advanced use

`subnet` can use `Aws::EC2::Subnet` resource (see http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Subnet.html).
Expand Down
8 changes: 8 additions & 0 deletions lib/awspec/helper/finder/apigateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ def find_apigateway_by_name(name)
end
nil
end

def find_api_resources_by_id(api_id)
all_resources = []
apigateway_client.get_resources(rest_api_id: api_id, limit: 500, embed: ['methods']).each do |response|
all_resources += response.items
end
all_resources != [] ? all_resources : nil
end
end
end
end
39 changes: 39 additions & 0 deletions lib/awspec/stub/apigateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,45 @@
}
}
]
},
get_resources: {
position: '1',
items: [
{
path: '/proxy',
resource_methods: {
'GET' => {
http_method: 'GET',
method_integration: { http_method: 'POST', uri: 'http://127.0.0.1:8080/hockey' }
}
}
},
{
path: '/zambonis',
resource_methods: {
'POST' => {
http_method: 'POST',
method_integration: { http_method: 'POST',
uri: 'http://127.0.0.1:8080/zambonis/{arena}?arena=Saddledome' }
}
}
},
{
path: '/zambonis/123',
resource_methods: {
'POST' => {
http_method: 'POST',
method_integration: { http_method: 'AWS',
uri: 'arn:aws:apigateway:us-east-1:cognito-idp:action/ListUsers' }
},
'GET' => {
http_method: 'GET',
method_integration: { http_method: 'AWS',
uri: 'arn:aws:apigateway:us-east-1:cognito-idp:action/SignUp?username=test' }
}
}
}
]
}
}
}
50 changes: 50 additions & 0 deletions lib/awspec/type/apigateway.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'addressable/uri'

module Awspec::Type
class Apigateway < ResourceBase
aws_resource Aws::APIGateway::Client
Expand All @@ -11,5 +13,53 @@ def resource_via_client
def id
@id ||= resource_via_client.id if resource_via_client
end

def api_resources
@api_resources.nil? ? @api_resources = find_api_resources_by_id(@id) : @api_resources
end

def has_path?(path)
check_existence
self.api_resources.each do |resource|
return resource if resource.path == path
end
nil
end

def has_integration_path?(path)
check_existence
self.api_resources.each do |resource|
next if resource.resource_methods.nil?
resource.resource_methods.each do |_, method|
if method.method_integration.http_method == 'AWS'
aws_path = method.method_integration.uri.match(%r{(\/[^\?]+)\??.*$}).captures[0] # Matches for ARN type path
return resource if aws_path == path
end
uri = Addressable::URI.parse(method.method_integration.uri)
return resource if uri.path == path
end
end
nil
end

def has_method?(path, http_method)
check_existence
resource_to_check = has_path?(path)
return nil if resource_to_check.nil?
resource_to_check.resource_methods.each do |_, method|
return resource_to_check if method.http_method == http_method
end
nil
end

def has_integration_method?(integration_path, http_method)
check_existence
integration_resource_to_check = has_integration_path?(integration_path)
return nil if integration_resource_to_check.nil?
integration_resource_to_check.resource_methods.each do |_, method|
return integration_resource_to_check if method.method_integration.http_method == http_method
end
nil
end
end
end
14 changes: 14 additions & 0 deletions spec/type/apigateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
its(:minimum_compression_size) { should eq 123 }
its(:api_key_source) { should eq 'HEADER' }
its(:policy) { should eq 'Honesty' }
it { should have_path('/proxy') }
it { should have_path('/zambonis') }
it { should have_path('/zambonis/123') }
it { should have_integration_path('/hockey') }
it { should have_integration_path('/zambonis/{arena}') }
it { should have_integration_path('/ListUsers') }
it { should have_method('/zambonis', 'POST') }
it { should have_integration_method('/zambonis/{arena}', 'POST') }
it { should have_method('/proxy', 'GET') }
it { should have_integration_method('/hockey', 'POST') }
it { should have_method('/zambonis/123', 'POST') }
it { should have_method('/zambonis/123', 'GET') }
it { should have_integration_method('/ListUsers', 'AWS') }
it { should have_integration_method('/SignUp', 'AWS') }
end

describe apigateway('my-apigateway') do
Expand Down

0 comments on commit 5fbf658

Please sign in to comment.