From 14a7359511ef1a63dfe316763e341aa1a172b8a6 Mon Sep 17 00:00:00 2001 From: Rolislaff Date: Tue, 19 Dec 2023 19:01:26 +0300 Subject: [PATCH 1/4] Web site application with docker containirezation Web site application with docker containirezation Clearing other branches files --- linux/nikita/Flasko_web_site/.dockerignore | 2 + linux/nikita/Flasko_web_site/.gitignore | 161 ++++++++++++++++++ linux/nikita/Flasko_web_site/Dockerfile | 19 +++ .../Flasko_web_site/app/conf.d/app.conf | 11 ++ .../Flasko_web_site/docker-compose.yaml | 40 +++++ linux/nikita/Flasko_web_site/main.py | 12 ++ linux/nikita/Flasko_web_site/requirements.txt | 3 + .../Flasko_web_site/website/__init__.py | 14 ++ linux/nikita/Flasko_web_site/website/auth.py | 71 ++++++++ linux/nikita/Flasko_web_site/website/db.py | 51 ++++++ .../Flasko_web_site/website/static/index.js | 0 .../website/templates/base.html | 103 +++++++++++ .../website/templates/home.html | 50 ++++++ .../website/templates/login.html | 27 +++ .../website/templates/sign_up.html | 49 ++++++ .../website/templates/welcome.html | 11 ++ linux/nikita/Flasko_web_site/website/views.py | 25 +++ 17 files changed, 649 insertions(+) create mode 100644 linux/nikita/Flasko_web_site/.dockerignore create mode 100644 linux/nikita/Flasko_web_site/.gitignore create mode 100644 linux/nikita/Flasko_web_site/Dockerfile create mode 100644 linux/nikita/Flasko_web_site/app/conf.d/app.conf create mode 100644 linux/nikita/Flasko_web_site/docker-compose.yaml create mode 100644 linux/nikita/Flasko_web_site/main.py create mode 100644 linux/nikita/Flasko_web_site/requirements.txt create mode 100644 linux/nikita/Flasko_web_site/website/__init__.py create mode 100644 linux/nikita/Flasko_web_site/website/auth.py create mode 100644 linux/nikita/Flasko_web_site/website/db.py create mode 100644 linux/nikita/Flasko_web_site/website/static/index.js create mode 100644 linux/nikita/Flasko_web_site/website/templates/base.html create mode 100644 linux/nikita/Flasko_web_site/website/templates/home.html create mode 100644 linux/nikita/Flasko_web_site/website/templates/login.html create mode 100644 linux/nikita/Flasko_web_site/website/templates/sign_up.html create mode 100644 linux/nikita/Flasko_web_site/website/templates/welcome.html create mode 100644 linux/nikita/Flasko_web_site/website/views.py diff --git a/linux/nikita/Flasko_web_site/.dockerignore b/linux/nikita/Flasko_web_site/.dockerignore new file mode 100644 index 0000000..99ed102 --- /dev/null +++ b/linux/nikita/Flasko_web_site/.dockerignore @@ -0,0 +1,2 @@ +.mongodata +data/** \ No newline at end of file diff --git a/linux/nikita/Flasko_web_site/.gitignore b/linux/nikita/Flasko_web_site/.gitignore new file mode 100644 index 0000000..76174ee --- /dev/null +++ b/linux/nikita/Flasko_web_site/.gitignore @@ -0,0 +1,161 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ +.mongodata \ No newline at end of file diff --git a/linux/nikita/Flasko_web_site/Dockerfile b/linux/nikita/Flasko_web_site/Dockerfile new file mode 100644 index 0000000..b332a40 --- /dev/null +++ b/linux/nikita/Flasko_web_site/Dockerfile @@ -0,0 +1,19 @@ +# start by pulling the python image +FROM python:3.8-alpine + +# copy every content from the local file to the image +COPY . /app + +# copy the requirements file into the image +COPY ./requirements.txt /app/requirements.txt + +WORKDIR /./app +# install the dependencies and packages in the requirements file +RUN pip install -r requirements.txt + +# configure the container to run in an executed manner +ENTRYPOINT [ "python" ] + +EXPOSE 5000 + +CMD ["main.py"] \ No newline at end of file diff --git a/linux/nikita/Flasko_web_site/app/conf.d/app.conf b/linux/nikita/Flasko_web_site/app/conf.d/app.conf new file mode 100644 index 0000000..1659023 --- /dev/null +++ b/linux/nikita/Flasko_web_site/app/conf.d/app.conf @@ -0,0 +1,11 @@ +upstream backend { + server app:5000; +} + +server { + listen 80; + + location / { + proxy_pass http://backend; + } +} \ No newline at end of file diff --git a/linux/nikita/Flasko_web_site/docker-compose.yaml b/linux/nikita/Flasko_web_site/docker-compose.yaml new file mode 100644 index 0000000..d7440d7 --- /dev/null +++ b/linux/nikita/Flasko_web_site/docker-compose.yaml @@ -0,0 +1,40 @@ +version: "3.8" +services: + #nginx service + #web: + # container_name: "web" + # image: nginx:alpine + # depends_on: + # - app + # ports: + # - "8080:80" + # volumes: + # - ./app/conf.d:/etc/nginx/conf.d + # networks: + # - custom + app: + container_name: "app" + image: myapp/site:1.6 + environment: + - MONGODB_URL=mongodb://mongo_db/ + build: + context: . + dockerfile: Dockerfile + ports: + - 8080:5000 + networks: + - custom + depends_on: + - mongo_db + mongo_db: + image: mongo + container_name: "mongo_db" + ports: + - 27017:27017 + networks: + - custom + volumes: + - ./data/db/.mongodata:/data/db +networks: + custom: + driver: bridge \ No newline at end of file diff --git a/linux/nikita/Flasko_web_site/main.py b/linux/nikita/Flasko_web_site/main.py new file mode 100644 index 0000000..0c0068b --- /dev/null +++ b/linux/nikita/Flasko_web_site/main.py @@ -0,0 +1,12 @@ +from website import create_app +from flask import Flask, render_template + +app = create_app() + +@app.route('/') +def home(): + return render_template('welcome.html') + + +if __name__ == "__main__": + app.run(debug=True, host="0.0.0.0") \ No newline at end of file diff --git a/linux/nikita/Flasko_web_site/requirements.txt b/linux/nikita/Flasko_web_site/requirements.txt new file mode 100644 index 0000000..48d6d32 --- /dev/null +++ b/linux/nikita/Flasko_web_site/requirements.txt @@ -0,0 +1,3 @@ +pymongo[srv] +flask +envparse diff --git a/linux/nikita/Flasko_web_site/website/__init__.py b/linux/nikita/Flasko_web_site/website/__init__.py new file mode 100644 index 0000000..6d575e0 --- /dev/null +++ b/linux/nikita/Flasko_web_site/website/__init__.py @@ -0,0 +1,14 @@ +from flask import Flask +from .views import views +from .auth import auth + + +def create_app(): + app = Flask(__name__) + + app.secret_key = 'super secret key' + + app.register_blueprint(views, url_prefix='/') + app.register_blueprint(auth, url_prefix='/') + + return app diff --git a/linux/nikita/Flasko_web_site/website/auth.py b/linux/nikita/Flasko_web_site/website/auth.py new file mode 100644 index 0000000..5f6e983 --- /dev/null +++ b/linux/nikita/Flasko_web_site/website/auth.py @@ -0,0 +1,71 @@ +from flask import Blueprint, render_template, request, flash, redirect +from .db import create_user, get_user + + +auth = Blueprint('auth', __name__) + + +@auth.route('/login', methods=['GET', 'POST']) +def login(): + if request.method == 'GET': + return render_template("login.html", boolean=True) + elif request.method == 'POST': + + email = request.form.get('email') + password = request.form.get('password') + + if email == "": + flash('Email must be greater than 3 characters.', category='error') + return render_template("login.html", boolean=True) + if password == "": + flash('Password cannot be empty.', category='error') + return render_template("login.html", boolean=True) + + user = get_user(email) + + if user == None: + flash('User dosent exist.', category='error') + return render_template("login.html", boolean=True) + if password != user['password']: + flash('Passwords is not valid.', category='error') + return render_template("login.html", boolean=True) + + flash('Successfully logged in', category='success') + return redirect("/") + + + +@auth.route('/logout') + +def logout(): + return redirect("/login") + + +@auth.route('/sign-up', methods=['GET', 'POST']) +def sign_up(): + #if request.method == 'GET': + # return render_template("sign_up.html") + if request.method == 'POST': + email = request.form.get('email',) + first_name = request.form.get('first_name',) + password1 = request.form.get('password1',) + password2 = request.form.get('password2',) + + if len(email) < 4: + flash('Email must be greater than 3 characters.', category='error') + elif len(first_name) < 2: + flash('First name must be greater than 1 character.', category='error') + elif password1 != password2: + flash('Passwords don\'t match.', category='error') + elif len(password1) < 7: + flash('Password must be at least 7 characters.', category='error') + + create_user(email, first_name, password1) + flash('Account created!', category='success') + + print("True") + #return render_template("login.html") + + #print(request.method) + print("False") + return render_template("sign_up.html") \ No newline at end of file diff --git a/linux/nikita/Flasko_web_site/website/db.py b/linux/nikita/Flasko_web_site/website/db.py new file mode 100644 index 0000000..ee726bf --- /dev/null +++ b/linux/nikita/Flasko_web_site/website/db.py @@ -0,0 +1,51 @@ +from pymongo import MongoClient +from envparse import Env + +env = Env() +MONGODB_URL = env.str("MONGODB_URL", default="mongodb://localhost:27017") + +client = MongoClient(MONGODB_URL) +notes = MongoClient(MONGODB_URL) + + +dbname = client['user_auth'] +dbname = notes['user_note'] + +collection_name = dbname["users"] +collection_name = dbname["notes"] + + +def create_user(email, first_name, password): + dbname = client['user_auth'] + user = { + "email": email, + "first_name": first_name, + "password": password + } + + x = dbname["users"].insert_one(user) + + +def get_user(email): + dbname = client['user_auth'] + myquery = { "email": email } + mydoc = dbname["users"].find_one(myquery) + return mydoc + + +def create_note(note): + note_insert = { "note": note } + + y = dbname["notes"].insert_one(note_insert) + + +def get_note(note): + mydoc = { "note": note } + myquery = dbname["notes"].find_one(mydoc) + return myquery + + + + + + diff --git a/linux/nikita/Flasko_web_site/website/static/index.js b/linux/nikita/Flasko_web_site/website/static/index.js new file mode 100644 index 0000000..e69de29 diff --git a/linux/nikita/Flasko_web_site/website/templates/base.html b/linux/nikita/Flasko_web_site/website/templates/base.html new file mode 100644 index 0000000..669a35d --- /dev/null +++ b/linux/nikita/Flasko_web_site/website/templates/base.html @@ -0,0 +1,103 @@ + + + + + + + + + {% block title %}Home{% endblock %} + + + + + + + {% with messages = get_flashed_messages(with_categories=true) %} {% if messages %} {% for category, message in messages %} {% if category == 'error' %} + + {% else %} + + {% endif %} {% endfor %} {% endif %} {% endwith %} + + + + + +
{% block content %} {% endblock %}
+ + + + + + + + + + diff --git a/linux/nikita/Flasko_web_site/website/templates/home.html b/linux/nikita/Flasko_web_site/website/templates/home.html new file mode 100644 index 0000000..c163904 --- /dev/null +++ b/linux/nikita/Flasko_web_site/website/templates/home.html @@ -0,0 +1,50 @@ +{% extends "base.html" %} {% block title %}Home{% endblock %} {% block content %} + + + Note Creation + + + +
+

Your Notes

+
+ +

+ +

+
+ +
+
+
+ +
+ + +{% endblock %} \ No newline at end of file diff --git a/linux/nikita/Flasko_web_site/website/templates/login.html b/linux/nikita/Flasko_web_site/website/templates/login.html new file mode 100644 index 0000000..e112ce0 --- /dev/null +++ b/linux/nikita/Flasko_web_site/website/templates/login.html @@ -0,0 +1,27 @@ +{% extends "base.html" %} {% block title %}Login{% endblock %} {% block content %} +
+

Login

+
+ + +
+
+ + +
+
+ +
+{% endblock %} diff --git a/linux/nikita/Flasko_web_site/website/templates/sign_up.html b/linux/nikita/Flasko_web_site/website/templates/sign_up.html new file mode 100644 index 0000000..ac0f08a --- /dev/null +++ b/linux/nikita/Flasko_web_site/website/templates/sign_up.html @@ -0,0 +1,49 @@ +{% extends "base.html" %} {% block title %}Sign up{% endblock %} + +{% block content %} +
+

Sign Up

+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+{% endblock %} \ No newline at end of file diff --git a/linux/nikita/Flasko_web_site/website/templates/welcome.html b/linux/nikita/Flasko_web_site/website/templates/welcome.html new file mode 100644 index 0000000..b596dc4 --- /dev/null +++ b/linux/nikita/Flasko_web_site/website/templates/welcome.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} {% block title %}Welcome{% endblock %} {% block content %} + + + + +

Welcome page

+

Working on welcome page!

+ + + +{% endblock %} \ No newline at end of file diff --git a/linux/nikita/Flasko_web_site/website/views.py b/linux/nikita/Flasko_web_site/website/views.py new file mode 100644 index 0000000..ee9ff4f --- /dev/null +++ b/linux/nikita/Flasko_web_site/website/views.py @@ -0,0 +1,25 @@ +from flask import Blueprint, render_template, request, flash +from .db import create_note, get_note + +views = Blueprint('views', __name__) +collection_note = 'note' + +@views.route('/home', methods=['GET', 'POST']) +def notes(): + paper = collection_note + print(request.form) + + if request.method == 'POST': + print(request.form) + note = request.form.get('note') + if len(note) < 1: + flash('Note is too short!', category="error") + else: + create_note(note) + flash('Note created successfully!', category="success") + note_doc = get_note(note) + if note_doc: + paper = request.form.get('note') + print(paper) + print(request.method) + return render_template("home.html", result=paper) From 9980c282156cda9dba49ba311c014a339221b041 Mon Sep 17 00:00:00 2001 From: Rolislaff Date: Sun, 12 May 2024 00:22:59 +0300 Subject: [PATCH 2/4] [Fixed] website can display all the notes --- linux/nikita/Flasko_web_site/website/db.py | 35 ++++++------ .../website/templates/home.html | 53 +++++++++---------- linux/nikita/Flasko_web_site/website/views.py | 29 +++++----- 3 files changed, 55 insertions(+), 62 deletions(-) diff --git a/linux/nikita/Flasko_web_site/website/db.py b/linux/nikita/Flasko_web_site/website/db.py index ee726bf..03bcf81 100644 --- a/linux/nikita/Flasko_web_site/website/db.py +++ b/linux/nikita/Flasko_web_site/website/db.py @@ -1,5 +1,6 @@ from pymongo import MongoClient from envparse import Env +from bson.objectid import ObjectId env = Env() MONGODB_URL = env.str("MONGODB_URL", default="mongodb://localhost:27017") @@ -9,10 +10,10 @@ dbname = client['user_auth'] -dbname = notes['user_note'] +dbname = notes['user_notes'] collection_name = dbname["users"] -collection_name = dbname["notes"] +my_col = dbname["all_notes"] def create_user(email, first_name, password): @@ -33,19 +34,21 @@ def get_user(email): return mydoc -def create_note(note): - note_insert = { "note": note } - - y = dbname["notes"].insert_one(note_insert) - - -def get_note(note): - mydoc = { "note": note } - myquery = dbname["notes"].find_one(mydoc) - return myquery - - - - +def create_note(note_html): + dbname = notes['user_notes'] + note_plan = { + "note_html": note_html + } + + x = dbname["all_notes"].insert_one(note_plan) + +def find_note(): + dbname = notes['user_notes'] + all_notes = dbname["all_notes"].find() + return [note['note_html'] for note in all_notes] +def delete_note(): + dbname = notes['user_notes'] + plan = dbname["all_notes"].delete_one() + return plan \ No newline at end of file diff --git a/linux/nikita/Flasko_web_site/website/templates/home.html b/linux/nikita/Flasko_web_site/website/templates/home.html index c163904..ae53945 100644 --- a/linux/nikita/Flasko_web_site/website/templates/home.html +++ b/linux/nikita/Flasko_web_site/website/templates/home.html @@ -5,45 +5,40 @@

Your Notes

- +

- +


-
-
    -
  • {{ result }}

  • -
+
    + {% for note in all_notes %} +
  • {{ note }} +
  • + {% endfor %} +
diff --git a/linux/nikita/Flasko_web_site/website/views.py b/linux/nikita/Flasko_web_site/website/views.py index ee9ff4f..3352cce 100644 --- a/linux/nikita/Flasko_web_site/website/views.py +++ b/linux/nikita/Flasko_web_site/website/views.py @@ -1,25 +1,20 @@ -from flask import Blueprint, render_template, request, flash -from .db import create_note, get_note +from flask import Blueprint, render_template, request, flash, redirect +from .db import create_note, find_note, delete_note views = Blueprint('views', __name__) collection_note = 'note' @views.route('/home', methods=['GET', 'POST']) def notes(): - paper = collection_note - print(request.form) - if request.method == 'POST': - print(request.form) - note = request.form.get('note') - if len(note) < 1: - flash('Note is too short!', category="error") + note_html = request.form.get('note_html') + if len(note_html) < 1: + flash('Note should have atleast 1 character', category='error') + return render_template("home.html", boolean=True) else: - create_note(note) - flash('Note created successfully!', category="success") - note_doc = get_note(note) - if note_doc: - paper = request.form.get('note') - print(paper) - print(request.method) - return render_template("home.html", result=paper) + create_note(note_html) + flash('Note created!', category='success') + + + all_notes = find_note() + return render_template("home.html", all_notes=all_notes) \ No newline at end of file From a93ca53c50d42e82b8cfab6410808491e0d26700 Mon Sep 17 00:00:00 2001 From: Rolislaff Date: Sun, 2 Jun 2024 16:02:55 +0300 Subject: [PATCH 3/4] Adding test for the project --- .../Flasko_web_site/testing/__init__.py | 0 .../Flasko_web_site/testing/conftest.py | 36 ++++++ .../nikita/Flasko_web_site/testing/pytest.ini | 9 ++ .../Flasko_web_site/testing/test_project.py | 103 ++++++++++++++++++ .../testing/testing_v2/main.py | 5 + .../testing/testing_v2/tests.py | 20 ++++ 6 files changed, 173 insertions(+) create mode 100644 linux/nikita/Flasko_web_site/testing/__init__.py create mode 100644 linux/nikita/Flasko_web_site/testing/conftest.py create mode 100644 linux/nikita/Flasko_web_site/testing/pytest.ini create mode 100644 linux/nikita/Flasko_web_site/testing/test_project.py create mode 100644 linux/nikita/Flasko_web_site/testing/testing_v2/main.py create mode 100644 linux/nikita/Flasko_web_site/testing/testing_v2/tests.py diff --git a/linux/nikita/Flasko_web_site/testing/__init__.py b/linux/nikita/Flasko_web_site/testing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/linux/nikita/Flasko_web_site/testing/conftest.py b/linux/nikita/Flasko_web_site/testing/conftest.py new file mode 100644 index 0000000..61748f2 --- /dev/null +++ b/linux/nikita/Flasko_web_site/testing/conftest.py @@ -0,0 +1,36 @@ +import pytest +from pymongo import MongoClient +from website import create_app + +@pytest.fixture(scope="module") +def mongo_client(): + client = MongoClient("mongodb://localhost:27017") + yield client + client.close() + + +@pytest.fixture(scope="module") +def client(): + app = create_app() + with app.test_client() as client: + yield client + +@pytest.fixture(scope="module") +def test_dbname(mongo_client): + dbname = mongo_client['user_auth'] + yield dbname + mongo_client.drop_database("user_auth") + + +@pytest.fixture(scope="module") +def test_dbname2(mongo_client): + dbname2 = mongo_client['user_note'] + yield dbname2 + mongo_client.drop_database("user_note") + + +@pytest.fixture +def math_func(x = 10, y = 5): + return (x + y) + + diff --git a/linux/nikita/Flasko_web_site/testing/pytest.ini b/linux/nikita/Flasko_web_site/testing/pytest.ini new file mode 100644 index 0000000..213f593 --- /dev/null +++ b/linux/nikita/Flasko_web_site/testing/pytest.ini @@ -0,0 +1,9 @@ +[pytest] +python_files=test_project.py +markers = + connection: mark a test as a connection + content: mark a test as a content + db: mark a test as a db + test: mark a test as a test + resgitration: mark a test as a resgitration + login: mark a test as a login \ No newline at end of file diff --git a/linux/nikita/Flasko_web_site/testing/test_project.py b/linux/nikita/Flasko_web_site/testing/test_project.py new file mode 100644 index 0000000..aa972da --- /dev/null +++ b/linux/nikita/Flasko_web_site/testing/test_project.py @@ -0,0 +1,103 @@ +from pymongo import MongoClient +import pytest + + +@pytest.mark.connection +@pytest.mark.timeout(5) +def test_connection(): + client = MongoClient("mongodb://localhost:27017") + assert client.admin.command("ping")["ok"] > 0 + +#First test should always run with '-x' option. +@pytest.mark.connection +def test_login(client): + response = client.get("/login") + assert response.status_code == 200 + + +@pytest.mark.connection +def test_home(client): + response = client.get("/home") + assert response.status_code == 200 + + +@pytest.mark.connection +def test_sign_up(client): + response = client.get("/sign-up") + assert response.status_code == 200 + + +@pytest.mark.content +def test_title(client): + response = client.get("/home") + assert b"Home" in response.data + + +@pytest.mark.content +def test_textarea(client): + response = client.get("/home") + assert b'' in response.data + + +@pytest.mark.content +def test_h1(client): + response = client.get("/home") + assert b"

Your Notes

" in response.data + + +@pytest.mark.content +def test_label(client): + response = client.get("/home") + assert b'' in response.data + + +@pytest.mark.content +def test_check_note_html(client): + test_note = "aas" + response = client.post('/home', data={'note_html': test_note}, follow_redirects=True) + assert response.status_code == 200 + response = client.get('/home') + assert "aas" in response.data.decode('utf-8') + + +@pytest.mark.db +def test_insert_user(test_dbname): + collection = test_dbname["test_collection"] + doc = { + "first": "email" + } + result = collection.insert_one(doc) + assert result.inserted_id is not None + + +@pytest.mark.db +def test_find_document(test_dbname): + collection = test_dbname["test_collection"] + doc = collection.find_one({"first": "email"}) + assert doc["first"] == "email" + + +@pytest.mark.test +def test_math(math_func): + assert math_func == 15 + + +@pytest.mark.resgitration +def test_registration(client): + test_mail = "test@test.com" + test_name = "Tester" + test_password = "test1234" + + response_post = client.post('/sign-up', data={'email': test_mail, 'first_name': test_name, 'password1': test_password, 'password2': test_password}, follow_redirects=True) + assert response_post.status_code == 200 + assert "Account created!" in response_post.data.decode('utf-8') + response_get = client.get('/sign-up') + assert response_get.status_code == 200 + + +@pytest.mark.login +def test_login_user(client): + response_post = client.post('/login', data={'email': 'test@test.com', 'password': 'test1234'}) + assert response_post.status_code == 302 + + \ No newline at end of file diff --git a/linux/nikita/Flasko_web_site/testing/testing_v2/main.py b/linux/nikita/Flasko_web_site/testing/testing_v2/main.py new file mode 100644 index 0000000..4747394 --- /dev/null +++ b/linux/nikita/Flasko_web_site/testing/testing_v2/main.py @@ -0,0 +1,5 @@ +def test_int(num): + if num % 2 == 0: + return (num * num) + else: + raise TypeError \ No newline at end of file diff --git a/linux/nikita/Flasko_web_site/testing/testing_v2/tests.py b/linux/nikita/Flasko_web_site/testing/testing_v2/tests.py new file mode 100644 index 0000000..6d412a7 --- /dev/null +++ b/linux/nikita/Flasko_web_site/testing/testing_v2/tests.py @@ -0,0 +1,20 @@ +import unittest +import main # тестируемый модуль + +class TestCalc(unittest.TestCase): + def test_add(self): + self.assertEqual(main.test_int(-2), 4) + + def test_string(self): + try: + main.test_int("nikita") + except TypeError: + return True + raise TypeError + + def test_nikita(self): + try: + main.test_int(3) + except TypeError: + return True + raise TypeError \ No newline at end of file From 861300fd167798a2c3ee6997a1ac19ec609e8afe Mon Sep 17 00:00:00 2001 From: Rolislaff Date: Wed, 19 Jun 2024 19:20:19 +0300 Subject: [PATCH 4/4] Adding unittest for the app --- linux/nikita/Flasko_web_site/requirements.txt | 2 + linux/nikita/Flasko_web_site/test_app.py | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 linux/nikita/Flasko_web_site/test_app.py diff --git a/linux/nikita/Flasko_web_site/requirements.txt b/linux/nikita/Flasko_web_site/requirements.txt index 48d6d32..c24f8f4 100644 --- a/linux/nikita/Flasko_web_site/requirements.txt +++ b/linux/nikita/Flasko_web_site/requirements.txt @@ -1,3 +1,5 @@ pymongo[srv] flask envparse +mongomock +unittest diff --git a/linux/nikita/Flasko_web_site/test_app.py b/linux/nikita/Flasko_web_site/test_app.py new file mode 100644 index 0000000..bcc1787 --- /dev/null +++ b/linux/nikita/Flasko_web_site/test_app.py @@ -0,0 +1,57 @@ +try: + from main import app + import unittest + import mongomock + +except Exception as e: + print("Some Modules are Missing {}".format(e)) + + +class TestApp(unittest.TestCase): + def test_index_status(self): + client = app.test_client(self) + #response_1 = client.get("/home") + response_2 = client.get("/sign-up") + response_3 = client.get("/login") + response_4 = client.get("/logout") + + #statuscode_1 = response_1.status_code + statuscode_2 = response_2.status_code + statuscode_3 = response_3.status_code + statuscode_4 = response_4.status_code + + #self.assertEqual(statuscode_1, 200) + self.assertEqual(statuscode_2, 200) + self.assertEqual(statuscode_3, 200) + self.assertEqual(statuscode_4, 302) + + def test_index_content(self): + client = app.test_client(self) + response = client.get("/login") + + self.assertTrue(b'Login' in response.data) + self.assertTrue(b'

Login

' in response.data) + self.assertTrue(b'' in response.data) + self.assertTrue(b'' in response.data) + + + +class TestMongoDb(unittest.TestCase): + def setUp(self): + self.client = mongomock.MongoClient() + self.db = self.client['test_database'] + self.collection = self.db['test_collection'] + self.data = ({"email": "test@mail.com", "first_name": "tester", "password1": "testpassword", "password2": "testpassword"}) + + def test_insert_and_find(self): + self.collection.insert_one(self.data) + doc = self.collection.find_one({"email": "test@mail.com"}) + + self.assertIsNotNone(doc) + self.assertEqual(doc['email'], "test@mail.com") + self.assertEqual(doc['password1'], "testpassword") + self.assertEqual(doc['password2'], "testpassword") + + +if __name__ == "__main__": + unittest.main()