From 7d595a2713469353946d48a9b3e6a753c4b724e8 Mon Sep 17 00:00:00 2001 From: zhavir Date: Sat, 27 Aug 2022 11:34:48 +0200 Subject: [PATCH] test(provider): add more e2e assertion and define postman collections tests (#10) --- README.md | 2 +- .../Tech Assessment.postman_collection.json | 297 ++++++++++++++++++ .../integration/test_e2e_password_generate.py | 39 ++- 3 files changed, 333 insertions(+), 5 deletions(-) create mode 100644 postman_collections/Tech Assessment.postman_collection.json diff --git a/README.md b/README.md index c09a793..b89e957 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ docker compose up --build tests - [x] Packaging - [x] REST API - [x] Tests - - [] Postman Collection + - [x] Postman Collection - [x] Deployment - [] Workflow Diagram - [] Documentation \ No newline at end of file diff --git a/postman_collections/Tech Assessment.postman_collection.json b/postman_collections/Tech Assessment.postman_collection.json new file mode 100644 index 0000000..20b440a --- /dev/null +++ b/postman_collections/Tech Assessment.postman_collection.json @@ -0,0 +1,297 @@ +{ + "info": { + "_postman_id": "65ed8e43-f9d9-47be-9e29-d718099373f7", + "name": "Tech Assessment", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "18952256" + }, + "item": [ + { + "name": "Password Generator", + "item": [ + { + "name": "password has specific length", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonReq = JSON.parse(pm.request.body.raw);", + "", + "pm.test(`Password has length equal to ${jsonReq.password_length}`, function () {", + " var jsonData = pm.response.json();", + " pm.expect(jsonData.password.length).to.eql(jsonReq.password_length);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"password_length\": 15\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "localhost:9001/api/v1/passwords/generate/", + "host": [ + "localhost" + ], + "port": "9001", + "path": [ + "api", + "v1", + "passwords", + "generate", + "" + ] + } + }, + "response": [] + }, + { + "name": "password contains only numbers", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonReq = JSON.parse(pm.request.body.raw);", + "", + "pm.test(`Password has only numbers`, function () {", + " var jsonData = pm.response.json();", + " pm.expect(jsonData.password).to.match(/^\\d+$/);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"password_length\": 20,\n \"has_numbers\": true,\n \"has_lowercase_chars\": false,\n \"has_uppercase_chars\": false,\n \"has_special_chars\": false\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "localhost:9001/api/v1/passwords/generate/", + "host": [ + "localhost" + ], + "port": "9001", + "path": [ + "api", + "v1", + "passwords", + "generate", + "" + ] + } + }, + "response": [] + }, + { + "name": "password contains only lowercase letters", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonReq = JSON.parse(pm.request.body.raw);", + "", + "pm.test(`Password has only lowercase letters`, function () {", + " var jsonData = pm.response.json();", + " pm.expect(jsonData.password).to.match(/^[a-z]+$/);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"password_length\": 20,\n \"has_numbers\": false,\n \"has_lowercase_chars\": true,\n \"has_uppercase_chars\": false,\n \"has_special_chars\": false\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "localhost:9001/api/v1/passwords/generate/", + "host": [ + "localhost" + ], + "port": "9001", + "path": [ + "api", + "v1", + "passwords", + "generate", + "" + ] + } + }, + "response": [] + }, + { + "name": "password contains only uppercase letters", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonReq = JSON.parse(pm.request.body.raw);", + "", + "pm.test(`Password has only uppercase letters`, function () {", + " var jsonData = pm.response.json();", + " pm.expect(jsonData.password).to.match(/^[A-Z]+$/);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"password_length\": 20,\n \"has_numbers\": false,\n \"has_lowercase_chars\": false,\n \"has_uppercase_chars\": true,\n \"has_special_chars\": false\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "localhost:9001/api/v1/passwords/generate/", + "host": [ + "localhost" + ], + "port": "9001", + "path": [ + "api", + "v1", + "passwords", + "generate", + "" + ] + } + }, + "response": [] + }, + { + "name": "password contains only special chars", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonReq = JSON.parse(pm.request.body.raw);", + "", + "pm.test(`Password has only special chars`, function () {", + " var jsonData = pm.response.json();", + " pm.expect(jsonData.password).to.match(/^[!\"#$%&\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~]+$/);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"password_length\": 20,\n \"has_numbers\": false,\n \"has_lowercase_chars\": false,\n \"has_uppercase_chars\": false,\n \"has_special_chars\": true\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "localhost:9001/api/v1/passwords/generate/", + "host": [ + "localhost" + ], + "port": "9001", + "path": [ + "api", + "v1", + "passwords", + "generate", + "" + ] + } + }, + "response": [] + }, + { + "name": "password can contains any chars", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonReq = JSON.parse(pm.request.body.raw);", + "", + "pm.test(`Password has only special chars`, function () {", + " var jsonData = pm.response.json();", + " pm.expect(jsonData.password).to.match(/^[a-zA-Z0-9!\"#$%&\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~]+$/);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"password_length\": 20,\n \"has_numbers\": true,\n \"has_lowercase_chars\": true,\n \"has_uppercase_chars\": true,\n \"has_special_chars\": true\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "localhost:9001/api/v1/passwords/generate/", + "host": [ + "localhost" + ], + "port": "9001", + "path": [ + "api", + "v1", + "passwords", + "generate", + "" + ] + } + }, + "response": [] + } + ] + } + ] +} \ No newline at end of file diff --git a/src/tests/integration/test_e2e_password_generate.py b/src/tests/integration/test_e2e_password_generate.py index df303b1..3ea28f2 100644 --- a/src/tests/integration/test_e2e_password_generate.py +++ b/src/tests/integration/test_e2e_password_generate.py @@ -1,17 +1,48 @@ +import re + import pytest from httpx import AsyncClient +@pytest.mark.parametrize( + [ + 'password_length', + 'has_numbers', + 'has_lowercase_chars', + 'has_uppercase_chars', + 'has_special_chars', + 'expected_match' + ], + [ + (20, True, False, False, False, r'^\d+$'), + (20, False, True, False, False, r'^[a-z]+$'), + (25, False, False, True, False, r'^[A-Z]+$'), + (1, False, False, False, True, r'^[!"#$%&\'()*+,-./:;<=>?@\[\\\]^_`{|}~]+$'), + (15, True, True, True, True, r'^[a-zA-Z0-9!"#$%&\'()*+,-./:;<=>?@\[\\\]^_`{|}~]+$'), + ], +) @pytest.mark.asyncio -async def test_e2e_generate_password(mocked_client: AsyncClient): - expected_length = 20 +async def test_e2e_generate_password( + mocked_client: AsyncClient, + password_length: int, + has_numbers: bool, + has_lowercase_chars: bool, + has_uppercase_chars: bool, + has_special_chars: bool, + expected_match: str +): async with mocked_client as client: response = await client.post("/api/v1/passwords/generate/", json={ - "password_length": expected_length, + "password_length": password_length, + "has_numbers": has_numbers, + "has_lowercase_chars": has_lowercase_chars, + "has_uppercase_chars": has_uppercase_chars, + "has_special_chars": has_special_chars, }) assert response.status_code == 200 body = response.json() assert body['password'] is not None - assert len(body['password']) == expected_length + assert len(body['password']) == password_length + assert re.match(pattern=expected_match, string=body['password'])