Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Commit

Permalink
Update put_call_test.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ikostan committed Oct 4, 2019
1 parent 22dbc3b commit 81c59a2
Showing 1 changed file with 93 additions and 24 deletions.
117 changes: 93 additions & 24 deletions tests/integration/put_call_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
# LinkedIn: https://www.linkedin.com/in/egor-kostan/

import base64
import unittest

import allure
import unittest
from flask import json
from api.cars_app import app

Expand All @@ -20,7 +19,39 @@ class PutCallTestCase(unittest.TestCase):
"""
Testing a JSON API implemented in Flask.
PUT Call Integration Test.
"""
PUT method requests for the enclosed entity be stored under
the supplied Request-URI. If the Request-URI refers to an
already existing resource – an update operation will happen,
otherwise create operation should happen if Request-URI is a
valid resource URI (assuming client is allowed to determine
resource identifier).
"""

def setUp(self) -> None:

with allure.step("Prepare test data"):

self.car_original = {"name": "Creta",
"brand": "Hyundai",
"price_range": "8-14 lacs",
"car_type": "hatchback"}

self.car_updated = {"name": "Creta",
"brand": "Hyundai",
"price_range": "6-9 lacs",
"car_type": "hatchback"}

self.non_admin_user = {"name": "eric",
"password": "testqxf2",
"perm": "non_admin"}

self.admin_user = {"name": "qxf2",
"password": "qxf2",
"perm": "admin"}

def tearDown(self) -> None:
pass

def test_debug_mode(self):
"""
Expand All @@ -45,44 +76,78 @@ def test_debug_mode(self):
with allure.step("Verify DEBUG flag"):
self.assertFalse(app.config['DEBUG'])

def test_put_cars_update(self):
def test_put_cars_update_non_admin(self):
"""
PUT method requests for the enclosed entity be stored under
the supplied Request-URI. If the Request-URI refers to an
already existing resource – an update operation will happen,
otherwise create operation should happen if Request-URI is a
valid resource URI (assuming client is allowed to determine
resource identifier).
Use PUT when you want to modify a singular resource
which is already a part of resources collection.
PUT replaces the resource in its entirety.
:return:
"""
allure.dynamic.title("Update car properties using PUT call")

allure.dynamic.title("Update car properties using "
"PUT call and non admin credentials")
allure.dynamic.severity(allure.severity_level.NORMAL)

with allure.step("Prepare test data"):
car = {"name": "Creta",
"brand": "Hyundai",
"price_range": "8-14 lacs",
"car_type": "hatchback"}
with allure.step("Send PUT request"):
response = app.test_client().put(
'{}{}'.format('/cars/update/',
self.car_original['name']),
data=json.dumps(self.car_updated),
content_type='application/json',

user = {"name": "eric",
"password": "testqxf2",
"perm": "non_admin"}
# Testing Flask application
# with basic authentication
# Source: https://gist.github.com/jarus/1160696
headers={
'Authorization': 'Basic ' +
base64.b64encode(bytes(self.non_admin_user['name'] +
":" +
self.non_admin_user['password'],
'ascii')).decode('ascii')
}
)

with allure.step("Verify status code"):
self.assertEqual(200, response.status_code)

with allure.step("Convert response into JSON data"):
data = json.loads(response.get_data(as_text=True))
# print("\nDATA:\n{}\n".format(data)) # Debug only

with allure.step("Verify 'successful' flag"):
self.assertTrue(data['response']['successful'])

with allure.step("Verify updated car data"):
self.assertDictEqual(self.car_updated,
data['response']['car'])

def test_put_cars_update_admin(self):
"""
Use PUT when you want to modify a singular resource
which is already a part of resources collection.
PUT replaces the resource in its entirety.
:return:
"""

allure.dynamic.title("Update car properties using "
"PUT call and admin credentials")
allure.dynamic.severity(allure.severity_level.NORMAL)

with allure.step("Send PUT request"):
response = app.test_client().put(
'{}{}'.format('/cars/update/',
car['name']),
data=json.dumps(car),
self.car_original['name']),
data=json.dumps(self.car_updated),
content_type='application/json',

# Testing Flask application
# with basic authentication
# Source: https://gist.github.com/jarus/1160696
headers={
'Authorization': 'Basic ' +
base64.b64encode(bytes(user['name'] +
base64.b64encode(bytes(self.admin_user['name'] +
":" +
user['password'],
self.admin_user['password'],
'ascii')).decode('ascii')
}
)
Expand All @@ -92,7 +157,11 @@ def test_put_cars_update(self):

with allure.step("Convert response into JSON data"):
data = json.loads(response.get_data(as_text=True))
print("\nDATA:\n{}\n".format(data)) # Debug only
# print("\nDATA:\n{}\n".format(data)) # Debug only

with allure.step("Verify 'successful' flag"):
self.assertTrue(data['response']['successful'])

with allure.step("Verify updated car data"):
self.assertDictEqual(self.car_updated,
data['response']['car'])

0 comments on commit 81c59a2

Please sign in to comment.