Skip to content

Commit

Permalink
test(provider): add more e2e assertion and define postman collections…
Browse files Browse the repository at this point in the history
… tests (#10)
  • Loading branch information
zhavir authored Aug 27, 2022
1 parent 3be3c04 commit 7d595a2
Show file tree
Hide file tree
Showing 3 changed files with 333 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
297 changes: 297 additions & 0 deletions postman_collections/Tech Assessment.postman_collection.json
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}
]
}
39 changes: 35 additions & 4 deletions src/tests/integration/test_e2e_password_generate.py
Original file line number Diff line number Diff line change
@@ -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'])

0 comments on commit 7d595a2

Please sign in to comment.