From c094e99f9f1472bbcf1e79b4356e9210fee4ea09 Mon Sep 17 00:00:00 2001 From: Prajjwal Yadav Date: Fri, 12 Jul 2024 06:24:43 +0000 Subject: [PATCH] added test for url-shortener --- api/tests/test_api.py | 31 +++++++++++++++++++++++++++++ url-shortener/.gitignore | 2 ++ url-shortener/app.py | 5 ++++- url-shortener/test_url_shortener.py | 31 +++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 api/tests/test_api.py create mode 100644 url-shortener/.gitignore create mode 100644 url-shortener/test_url_shortener.py diff --git a/api/tests/test_api.py b/api/tests/test_api.py new file mode 100644 index 0000000..219078a --- /dev/null +++ b/api/tests/test_api.py @@ -0,0 +1,31 @@ +import pytest +from app import app + +@pytest.fixture +def client(): + with app.test_client() as client: + yield client + +def test_create_entry(client): + response = client.post('/create', json={'long_url': 'https://example.com'}) + data = response.get_json() + assert response.status_code == 200 + assert 'short_url' in data + assert data['long_url'] == 'https://example.com' + +def test_redirect_url(client): + # Assuming there is a way to set up a known state or mock DB + client.post('/create', json={'long_url': 'https://example.com', 'custom_url': 'test'}) + response = client.get('/test') + assert response.status_code == 302 + assert response.location == 'https://example.com' + +def test_qr_code_generation(client): + client.post('/create', json={'long_url': 'https://example.com', 'custom_url': 'test', 'generate_qr': True}) + response = client.get('/qr/test') + assert response.status_code == 200 + assert response.mimetype == 'image/png' + +def test_get_analytics(client): + response = client.get('/test/analytics') + assert response.status_code == 200 diff --git a/url-shortener/.gitignore b/url-shortener/.gitignore new file mode 100644 index 0000000..f0e3260 --- /dev/null +++ b/url-shortener/.gitignore @@ -0,0 +1,2 @@ +.pytest_cache/ +__pycache__/ \ No newline at end of file diff --git a/url-shortener/app.py b/url-shortener/app.py index 3230b48..dc1610d 100644 --- a/url-shortener/app.py +++ b/url-shortener/app.py @@ -1,4 +1,4 @@ -from flask import Flask, request, jsonify +from flask import Flask, request, jsonify, abort import random import string @@ -11,6 +11,9 @@ def generate_short_url(): @app.route('/shorten', methods=['POST']) def shorten_url(): data = request.get_json() + if not data or 'long_url' not in data: + abort(400, description="Invalid request: 'long_url' is required") + long_url = data['long_url'] custom_url = data.get('custom_url') diff --git a/url-shortener/test_url_shortener.py b/url-shortener/test_url_shortener.py new file mode 100644 index 0000000..46046a1 --- /dev/null +++ b/url-shortener/test_url_shortener.py @@ -0,0 +1,31 @@ +import pytest +import json +from app import app + +@pytest.fixture +def client(): + app.testing = True + with app.test_client() as client: + yield client + +def test_shorten_url(client): + # Test shortening a URL without a custom URL + response = client.post('/shorten', data=json.dumps({'long_url': 'https://example.com'}), content_type='application/json') + data = json.loads(response.data) + assert response.status_code == 200 + assert 'short_url' in data + assert data['long_url'] == 'https://example.com' + assert len(data['short_url']) == 6 + +def test_shorten_url_with_custom(client): + # Test shortening a URL with a custom URL + response = client.post('/shorten', data=json.dumps({'long_url': 'https://example.com', 'custom_url': 'custom123'}), content_type='application/json') + data = json.loads(response.data) + assert response.status_code == 200 + assert data['short_url'] == 'custom123' + assert data['long_url'] == 'https://example.com' + +def test_invalid_request(client): + # Test handling of invalid requests + response = client.post('/shorten', data=json.dumps({'invalid_key': 'https://example.com'}), content_type='application/json') + assert response.status_code == 400