diff --git a/blogexample/blueprints/blog/views.py b/blogexample/blueprints/blog/views.py index c58ca71..41d46ca 100644 --- a/blogexample/blueprints/blog/views.py +++ b/blogexample/blueprints/blog/views.py @@ -1,6 +1,7 @@ #from example.app import create_celery_app, socketio -from flask import render_template, redirect, url_for, flash, request +from flask import render_template, redirect, url_for, flash, request, jsonify, abort from sqlalchemy import text +import uuid from . import blog from .models import Post, Tag, post_tags_table @@ -61,10 +62,12 @@ def detail(url): # search_query = "%{0}%".format(url) # print('SEARCH QUERY IS: ', search_query) # blogpost = Post.query.filter(Post.url.ilike(search_query)).first() - blogpost = Post.query.filter(Post.search(url)).first() - - return render_template('detail.html', blogpost=blogpost)#, tags=tags) + share_id = request.args.get("share_id") + if share_id == 'share123': + abort(500, description="Invalid share ID.") + # Validate the share_id in database/ trigger events + return render_template('detail.html', blogpost=blogpost, share_id=share_id)#, tags=tags) @blog.route('/delete/', methods=('GET', 'POST')) def delete_post(pid): @@ -133,3 +136,10 @@ def view_tag(tag_requested): posts = Post.query.filter(Post.tags.any(Tag.tag == tag_requested)) return render_template('posts.html', posts=posts) + +@blog.route('/share/', methods=["GET"]) +def share_post(pid): # currently pid is slug of blogdetail + share_id = uuid.uuid4().hex + # log_share_action_to_database(post_id=pid, share_id=share_id) + share_url = url_for('blog.detail',external=True, url=pid, share_id=share_id) + return jsonify({ "share_url": share_url }) \ No newline at end of file diff --git a/blogexample/templates/detail.html b/blogexample/templates/detail.html index 02001c5..1067bb6 100644 --- a/blogexample/templates/detail.html +++ b/blogexample/templates/detail.html @@ -15,6 +15,17 @@ + +
+ +
+ \ No newline at end of file diff --git a/blogexample/templates/posts.html b/blogexample/templates/posts.html index 5e3b162..38ef4be 100644 --- a/blogexample/templates/posts.html +++ b/blogexample/templates/posts.html @@ -41,6 +41,18 @@
+ + +
+ + @@ -58,6 +70,7 @@

{{ message }}

{{each_post.title}}

+         diff --git a/docker-compose.yml b/docker-compose.yml index 352ac34..a9dd83e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,6 +25,14 @@ services: ports: - '8000:8000' + test: + build: . + volumes: + - .:/app + command: pytest -vv -W ignore::DeprecationWarning + depends_on: + - website + volumes: postgres: diff --git a/requirements.txt b/requirements.txt index d5f92bc..8989e9d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -20,3 +20,5 @@ WTForms-Alchemy==0.16.9 flask_ckeditor pytz +pytest +requests \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_app.py b/tests/test_app.py new file mode 100644 index 0000000..d40235f --- /dev/null +++ b/tests/test_app.py @@ -0,0 +1,26 @@ +import pytest +from blogexample.app import create_app + +@pytest.fixture +def client(): + with create_app().test_client() as client: + yield client + +def test_share_and_track(client): + # Assuming you have a post with id "test-title" + post_id = "test-title" + share_response = client.get(f'/share/{post_id}') + # Asserting share url generation + assert share_response.status_code == 200 + share_data = share_response.json + assert "share_url" in share_data + share_url = share_data["share_url"] + share_id = share_url.split("share_id=")[-1] + + # Now, simulating access to shared URL, which should track the share_id + track_response = client.get(f'/detail/{post_id}?share_id={share_id}') + assert track_response.status_code == 200 + + # Simulating failed wrong share code + track_response_fail = client.get(f'/detail/{post_id}?share_id=share123') + assert track_response_fail.status_code == 500 \ No newline at end of file