From 582b16861efb39729e65f02b8ccf0c9ec731b8f0 Mon Sep 17 00:00:00 2001 From: Dhruv Thakker Date: Wed, 7 Mar 2018 22:39:34 +0530 Subject: [PATCH] baremetrics bot : Add tests for bad requests. (This module now has 100% coverage.) --- .../bots/baremetrics/baremetrics.py | 3 +- .../fixtures/create_plan_error.json | 44 +++++++++++++++++++ .../bots/baremetrics/test_baremetrics.py | 11 +++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 zulip_bots/zulip_bots/bots/baremetrics/fixtures/create_plan_error.json diff --git a/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py b/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py index b804341..d0e60a6 100644 --- a/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py +++ b/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py @@ -205,10 +205,9 @@ class BaremetricsHandler(object): url = 'https://api.baremetrics.com/v1/{}/plans'.format(parameters[0]) create_plan_response = requests.post(url, data=data_header, headers=self.auth_header) - if 'error' not in create_plan_response.json(): return 'Plan Created.' else: - return 'Some Error Occurred. Maybe the passed arguments are invalid.' + return 'Invalid Arguments Error.' handler_class = BaremetricsHandler diff --git a/zulip_bots/zulip_bots/bots/baremetrics/fixtures/create_plan_error.json b/zulip_bots/zulip_bots/bots/baremetrics/fixtures/create_plan_error.json new file mode 100644 index 0000000..402967d --- /dev/null +++ b/zulip_bots/zulip_bots/bots/baremetrics/fixtures/create_plan_error.json @@ -0,0 +1,44 @@ +{ + "request": { + "method": "POST", + "api_url": "https://api.baremetrics.com/v1/TEST/plans", + "headers": { + "Authorization": "Bearer TEST" + }, + "data": { + "oid": "1", + "name": "TEST", + "currency": "USD", + "amount": 123, + "interval": "TEST", + "interval_count": 123 + } + }, + "response": { + "error":"Any Type of Error Occurred." + }, + "response-headers": { + "X-TokenExpires": "0", + "Server": "cloudflare-nginx", + "X-RateLimit-Remaining": "3593", + "X-Powered-By": "Phusion Passenger 5.0.30", + "Set-Cookie": "__cfduid=dd26464e3e6779b6a05b27d2681aea0851514374567; expires=Thu, 27-Dec-18 11:36:07 GMT; path=/; domain=.baremetrics.com; HttpOnly; Secure, LSW_WEB=\"LSW_WEB1\"; path=/", + "X-Runtime": "0.031730", + "Access-Control-Allow-Credentials": "false", + "Access-Control-Allow-Methods": "GET, OPTIONS, POST, PUT, DELETE", + "Content-Encoding": "gzip", + "Connection": "keep-alive", + "ETag": "W/\"110de39b319ed195bf1414ac4f3c9a01\"", + "Date": "Wed, 27 Dec 2017 11:36:08 GMT", + "X-RateLimit-Limit": "3600", + "X-Version": "721", + "X-Commit": "2dcec7190a2bd01c8d3d8527d386486c4bb5474c", + "Status": "200 OK", + "Content-Type": "application/json; charset=utf-8", + "Transfer-Encoding": "chunked", + "Access-Control-Allow-Headers": "Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type", + "Cache-Control": "max-age=0, private, must-revalidate", + "X-Request-Id": "0b292131-81bb-4df2-bdb9-089a50b17e63", + "CF-RAY": "3d3bfaf6feee2eff-DEL" + } +} diff --git a/zulip_bots/zulip_bots/bots/baremetrics/test_baremetrics.py b/zulip_bots/zulip_bots/bots/baremetrics/test_baremetrics.py index 17a9626..ed46f72 100644 --- a/zulip_bots/zulip_bots/bots/baremetrics/test_baremetrics.py +++ b/zulip_bots/zulip_bots/bots/baremetrics/test_baremetrics.py @@ -103,3 +103,14 @@ class TestBaremetricsBot(BotTestCase): patch('requests.get'): with self.mock_http_conversation('create_plan'): self.verify_reply('create-plan TEST 1 TEST USD 123 TEST 123', 'Plan Created.') + + def test_create_plan_error_command(self) -> None: + with self.mock_config_info({'api_key': 'TEST'}), \ + patch('requests.get'): + with self.mock_http_conversation('create_plan_error'): + self.verify_reply('create-plan TEST 1 TEST USD 123 TEST 123', 'Invalid Arguments Error.') + + def test_create_plan_argnum_error_command(self) -> None: + with self.mock_config_info({'api_key': 'TEST'}), \ + patch('requests.get'): + self.verify_reply('create-plan alpha beta', 'Invalid number of arguments.')