| 
									
										
										
										
											2021-10-18 19:44:29 +00:00
										 |  |  | import sure  # noqa # pylint: disable=unused-import | 
					
						
							| 
									
										
										
										
											2018-07-10 14:58:02 +01:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2016-03-01 12:03:59 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | import moto.server as server | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2016-03-01 12:03:59 -05:00
										 |  |  | Test the different server responses | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2016-03-01 12:03:59 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_list_apis(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     backend = server.create_backend_app("apigateway") | 
					
						
							| 
									
										
										
										
											2016-03-01 12:03:59 -05:00
										 |  |  |     test_client = backend.test_client() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     res = test_client.get("/restapis") | 
					
						
							| 
									
										
										
										
											2021-02-15 10:31:33 +00:00
										 |  |  |     json.loads(res.data).should.contain("item") | 
					
						
							| 
									
										
										
										
											2018-07-10 14:58:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 14:58:02 +01:00
										 |  |  | def test_usage_plans_apis(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     backend = server.create_backend_app("apigateway") | 
					
						
							| 
									
										
										
										
											2018-07-10 14:58:02 +01:00
										 |  |  |     test_client = backend.test_client() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     # List usage plans (expect empty) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     res = test_client.get("/usageplans") | 
					
						
							| 
									
										
										
										
											2018-07-10 14:58:02 +01:00
										 |  |  |     json.loads(res.data)["item"].should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     # Create usage plan | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     res = test_client.post("/usageplans", data=json.dumps({"name": "test"})) | 
					
						
							| 
									
										
										
										
											2018-07-10 14:58:02 +01:00
										 |  |  |     created_plan = json.loads(res.data) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     created_plan["name"].should.equal("test") | 
					
						
							| 
									
										
										
										
											2018-07-10 14:58:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     # List usage plans (expect 1 plan) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     res = test_client.get("/usageplans") | 
					
						
							| 
									
										
										
										
											2018-07-10 14:58:02 +01:00
										 |  |  |     json.loads(res.data)["item"].should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     # Get single usage plan | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |     res = test_client.get(f"/usageplans/{created_plan['id']}") | 
					
						
							| 
									
										
										
										
											2018-07-10 14:58:02 +01:00
										 |  |  |     fetched_plan = json.loads(res.data) | 
					
						
							|  |  |  |     fetched_plan.should.equal(created_plan) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-27 19:39:33 +02:00
										 |  |  |     # Not existing usage plan | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |     res = test_client.get("/usageplans/not_existing") | 
					
						
							| 
									
										
										
										
											2020-04-27 19:39:33 +02:00
										 |  |  |     res.status_code.should.equal(404) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     # Delete usage plan | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |     res = test_client.delete(f"/usageplans/{created_plan['id']}") | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     res.data.should.equal(b"{}") | 
					
						
							| 
									
										
										
										
											2018-07-10 14:58:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     # List usage plans (expect empty again) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     res = test_client.get("/usageplans") | 
					
						
							| 
									
										
										
										
											2018-07-10 14:58:02 +01:00
										 |  |  |     json.loads(res.data)["item"].should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  | def test_usage_plans_keys(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     backend = server.create_backend_app("apigateway") | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     test_client = backend.test_client() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     usage_plan_id = "test_plan_id" | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create API key to be used in tests | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     res = test_client.post("/apikeys", data=json.dumps({"name": "test"})) | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     created_api_key = json.loads(res.data) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # List usage plans keys (expect empty) | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |     res = test_client.get(f"/usageplans/{usage_plan_id}/keys") | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     json.loads(res.data)["item"].should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-27 19:39:33 +02:00
										 |  |  |     # Invalid api key (does not exists at all) | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |     res = test_client.get(f"/usageplans/{usage_plan_id}/keys/not_existing") | 
					
						
							| 
									
										
										
										
											2020-04-27 19:39:33 +02:00
										 |  |  |     res.status_code.should.equal(404) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # not existing usage plan with existing api key | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |     res = test_client.get(f"/usageplans/not_existing/keys/{created_api_key['id']}") | 
					
						
							| 
									
										
										
										
											2020-04-27 19:39:33 +02:00
										 |  |  |     res.status_code.should.equal(404) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # not jet added api key | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |     res = test_client.get(f"/usageplans/{usage_plan_id}/keys/{created_api_key['id']}") | 
					
						
							| 
									
										
										
										
											2020-04-27 19:39:33 +02:00
										 |  |  |     res.status_code.should.equal(404) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     # Create usage plan key | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     res = test_client.post( | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |         f"/usageplans/{usage_plan_id}/keys", | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         data=json.dumps({"keyId": created_api_key["id"], "keyType": "API_KEY"}), | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     created_usage_plan_key = json.loads(res.data) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # List usage plans keys (expect 1 key) | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |     res = test_client.get(f"/usageplans/{usage_plan_id}/keys") | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     json.loads(res.data)["item"].should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Get single usage plan key | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |     res = test_client.get(f"/usageplans/{usage_plan_id}/keys/{created_api_key['id']}") | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     fetched_plan_key = json.loads(res.data) | 
					
						
							|  |  |  |     fetched_plan_key.should.equal(created_usage_plan_key) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Delete usage plan key | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     res = test_client.delete( | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |         f"/usageplans/{usage_plan_id}/keys/{created_api_key['id']}" | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  |     res.data.should.equal(b"{}") | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # List usage plans keys (expect to be empty again) | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |     res = test_client.get(f"/usageplans/{usage_plan_id}/keys") | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     json.loads(res.data)["item"].should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  | def test_create_usage_plans_key_non_existent_api_key(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     backend = server.create_backend_app("apigateway") | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     test_client = backend.test_client() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     usage_plan_id = "test_plan_id" | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create usage plan key with non-existent api key | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     res = test_client.post( | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |         f"/usageplans/{usage_plan_id}/keys", | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         data=json.dumps({"keyId": "non-existent", "keyType": "API_KEY"}), | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-07-11 17:17:58 +01:00
										 |  |  |     res.status_code.should.equal(404) | 
					
						
							| 
									
										
										
										
											2021-10-20 22:02:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_put_integration_response_without_body(): | 
					
						
							|  |  |  |     # Method under test: put_integration_response | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Moto/Boto3 requires the responseTemplates-parameter to have a value - even if it's an empty dict | 
					
						
							|  |  |  |     # Botocore <= 1.21.65 does not automatically pass this parameter, so Moto will successfully throw an error if it's not supplied | 
					
						
							|  |  |  |     # However: As of botocore >= 1.22.0, the responseTemplates is automatically supplied - which means we can no longer test this using boto3 | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # This was the equivalent boto3-test: | 
					
						
							|  |  |  |     # with pytest.raises(ClientError) as ex: | 
					
						
							|  |  |  |     #     client.put_integration_response( | 
					
						
							|  |  |  |     #         restApiId=api_id, resourceId=root_id, httpMethod="GET", statusCode="200" | 
					
						
							|  |  |  |     #     ) | 
					
						
							|  |  |  |     # ex.value.response["Error"]["Code"].should.equal("BadRequestException") | 
					
						
							|  |  |  |     # ex.value.response["Error"]["Message"].should.equal("Invalid request input") | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # As a workaround, we can create a PUT-request without body, which will force the error | 
					
						
							|  |  |  |     # Related: # https://github.com/aws/aws-sdk-js/issues/2588 | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     backend = server.create_backend_app("apigateway") | 
					
						
							|  |  |  |     test_client = backend.test_client() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = test_client.put( | 
					
						
							|  |  |  |         "/restapis/f_id/resources/r_id/methods/m_id/integration/responses/200/" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     res.status_code.should.equal(400) | 
					
						
							|  |  |  |     json.loads(res.data).should.equal( | 
					
						
							|  |  |  |         {"__type": "BadRequestException", "message": "Invalid request input"} | 
					
						
							|  |  |  |     ) |