Skip to content

Commit 81a286e

Browse files
NRL-512 All endpoints and integration tests working
1 parent c1ea9a4 commit 81a286e

File tree

7 files changed

+85
-37
lines changed

7 files changed

+85
-37
lines changed

terraform/infrastructure/api_gateway.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ module "consumer__gateway" {
1010
method_readDocumentReference = "arn:aws:apigateway:eu-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-2:${local.aws_account_id}:function:${substr("${local.prefix}--api--consumer--readDocumentReference", 0, 64)}/invocations"
1111
method_status = "arn:aws:apigateway:eu-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-2:${local.aws_account_id}:function:${substr("${local.prefix}--api--consumer--status", 0, 64)}/invocations"
1212
}
13+
endpoint_allowed_methods = {
14+
"/DocumentReference" = "GET",
15+
"/DocumentReference/{id}" = "GET"
16+
}
1317
kms_key_id = module.kms__cloudwatch.kms_arn
1418
domain = local.apis.domain
1519
path = local.apis.consumer.path
@@ -35,7 +39,10 @@ module "producer__gateway" {
3539
method_deleteDocumentReference = "arn:aws:apigateway:eu-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-2:${local.aws_account_id}:function:${substr("${local.prefix}--api--producer--deleteDocumentReference", 0, 64)}/invocations"
3640
method_status = "arn:aws:apigateway:eu-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-2:${local.aws_account_id}:function:${substr("${local.prefix}--api--producer--status", 0, 64)}/invocations"
3741
}
38-
42+
endpoint_allowed_methods = {
43+
"/DocumentReference" = "GET,POST",
44+
"/DocumentReference/{id}" = "GET,DELETE"
45+
}
3946
kms_key_id = module.kms__cloudwatch.kms_arn
4047
domain = local.apis.domain
4148
path = local.apis.producer.path

terraform/infrastructure/modules/api_gateway/method_responses.tf

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
# Define a reusable map of allowed methods for each path
2-
locals {
3-
endpoint_allowed_methods = {
4-
"/DocumentReference" = "GET,POST,PUT"
5-
# "/DocumentReference/_search" = "POST"
6-
# "/DocumentReference/{id}" = "GET"
7-
}
8-
}
9-
102
data "aws_api_gateway_resource" "resource" {
11-
for_each = local.endpoint_allowed_methods
3+
for_each = var.endpoint_allowed_methods
124
rest_api_id = aws_api_gateway_rest_api.api_gateway_rest_api.id
135
path = each.key
146

@@ -17,7 +9,7 @@ data "aws_api_gateway_resource" "resource" {
179

1810
# Add HEAD method to each resource with 405 response
1911
resource "aws_api_gateway_method" "head_method" {
20-
for_each = local.endpoint_allowed_methods
12+
for_each = var.endpoint_allowed_methods
2113

2214
rest_api_id = aws_api_gateway_rest_api.api_gateway_rest_api.id
2315
resource_id = data.aws_api_gateway_resource.resource[each.key].id
@@ -26,7 +18,7 @@ resource "aws_api_gateway_method" "head_method" {
2618
}
2719

2820
resource "aws_api_gateway_integration" "head_integration" {
29-
for_each = local.endpoint_allowed_methods
21+
for_each = var.endpoint_allowed_methods
3022

3123
rest_api_id = aws_api_gateway_rest_api.api_gateway_rest_api.id
3224
resource_id = data.aws_api_gateway_resource.resource[each.key].id
@@ -49,7 +41,7 @@ resource "aws_api_gateway_integration" "head_integration" {
4941
}
5042

5143
resource "aws_api_gateway_method_response" "head_method_response" {
52-
for_each = local.endpoint_allowed_methods
44+
for_each = var.endpoint_allowed_methods
5345

5446
rest_api_id = aws_api_gateway_rest_api.api_gateway_rest_api.id
5547
resource_id = data.aws_api_gateway_resource.resource[each.key].id
@@ -62,7 +54,7 @@ resource "aws_api_gateway_method_response" "head_method_response" {
6254
}
6355

6456
resource "aws_api_gateway_integration_response" "head_integration_response" {
65-
for_each = local.endpoint_allowed_methods
57+
for_each = var.endpoint_allowed_methods
6658

6759
rest_api_id = aws_api_gateway_rest_api.api_gateway_rest_api.id
6860
resource_id = data.aws_api_gateway_resource.resource[each.key].id

terraform/infrastructure/modules/api_gateway/vars.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ variable "retention" {
2222
default = 90
2323
type = number
2424
}
25+
26+
variable "endpoint_allowed_methods" {
27+
type = map(string)
28+
}
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
Feature: Consumer - HEAD Requests
22

3-
@custom_tag
43
Scenario: DocumentReference with HEAD fails
54
Given the application 'DataShare' (ID 'z00z-y11y-x22x') is registered to access the API
6-
When consumer 'RX898' sends HEAD request to DocumentReference endpoint
5+
When consumer 'RX898' sends HEAD request to 'DocumentReference' endpoint
6+
Then the response status code is 405
7+
And the response has an empty body
8+
And the Allow header is 'GET'
9+
10+
Scenario: DocumentReference/{id} with HEAD fails
11+
Given the application 'DataShare' (ID 'z00z-y11y-x22x') is registered to access the API
12+
When consumer 'RX898' sends HEAD request to 'DocumentReference/random-id' endpoint
713
Then the response status code is 405
814
And the response has an empty body
915
And the Allow header is 'GET'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Feature: Producer - HEAD Requests
2+
3+
Scenario: DocumentReference with HEAD fails
4+
Given the application 'DataShare' (ID 'z00z-y11y-x22x') is registered to access the API
5+
When producer 'RX898' sends HEAD request to 'DocumentReference' endpoint
6+
Then the response status code is 405
7+
And the response has an empty body
8+
And the Allow header is 'GET,POST'
9+
10+
Scenario: DocumentReference/{id} with HEAD fails
11+
Given the application 'DataShare' (ID 'z00z-y11y-x22x') is registered to access the API
12+
When producer 'RX898' sends HEAD request to 'DocumentReference/random-id' endpoint
13+
Then the response status code is 405
14+
And the response has an empty body
15+
And the Allow header is 'GET,DELETE'

tests/features/steps/2_request.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,6 @@ def consumer_read_document_reference_step(
7676
context.response = client.read(doc_ref_id)
7777

7878

79-
@when("consumer '{ods_code}' sends HEAD request to {endpoint} endpoint")
80-
def consumer_head_request_step(context: Context, ods_code: str, endpoint: str):
81-
client = consumer_client_from_context(context, ods_code)
82-
context.response = client.head(endpoint)
83-
84-
85-
@when(
86-
"consumer '{ods_code}' sends HEAD request to '{endpoint}' endpoint with parameters"
87-
)
88-
def consumer_head_request_step_with_parameters(
89-
context: Context, ods_code: str, endpoint: str
90-
):
91-
if not context.table:
92-
raise ValueError("No search query table provided")
93-
94-
items = {row["parameter"]: row["value"] for row in context.table}
95-
96-
client = consumer_client_from_context(context, ods_code)
97-
context.response = client.head(endpoint, items)
98-
99-
10079
@when("producer '{ods_code}' creates a DocumentReference with values")
10180
def create_post_document_reference_step(context: Context, ods_code: str):
10281
client = producer_client_from_context(context, ods_code)
@@ -268,3 +247,32 @@ def producer_search_document_reference_step(context: Context, ods_code: str):
268247
pointer_type=pointer_type,
269248
extra_params=items,
270249
)
250+
251+
252+
@when("{consumer_or_producer} '{ods_code}' sends HEAD request to '{endpoint}' endpoint")
253+
def consumer_head_request_step(
254+
context: Context, consumer_or_producer: str, ods_code: str, endpoint: str
255+
):
256+
if consumer_or_producer == "producer":
257+
client = producer_client_from_context(context, ods_code)
258+
elif consumer_or_producer == "consumer":
259+
client = consumer_client_from_context(context, ods_code)
260+
context.response = client.head(endpoint)
261+
262+
263+
@when(
264+
"{consumer_or_producer} '{ods_code}' sends HEAD request to '{endpoint}' endpoint with parameters"
265+
)
266+
def consumer_head_request_step_with_parameters(
267+
context: Context, consumer_or_producer: str, ods_code: str, endpoint: str
268+
):
269+
if not context.table:
270+
raise ValueError("No search query table provided")
271+
272+
items = {row["parameter"]: row["value"] for row in context.table}
273+
274+
if consumer_or_producer == "producer":
275+
client = producer_client_from_context(context, ods_code)
276+
elif consumer_or_producer == "consumer":
277+
client = consumer_client_from_context(context, ods_code)
278+
context.response = client.head(endpoint, items)

tests/utilities/api_clients.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,19 @@ def read_capability_statement(self) -> Response:
376376
headers=self.request_headers,
377377
cert=self.config.client_cert,
378378
)
379+
380+
@retry_if([502])
381+
def head(
382+
self,
383+
endpoint: str,
384+
extra_params: dict[str, str] | None = None,
385+
) -> Response:
386+
params = {**(extra_params or {})}
387+
url = f"{self.api_url}/{endpoint}"
388+
headers = {**self.request_headers, "Content-Type": "application/json"}
389+
return requests.head(
390+
url,
391+
params=params,
392+
headers=headers,
393+
cert=self.config.client_cert,
394+
)

0 commit comments

Comments
 (0)