Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Test cases and CI testing #16

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python application

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ dmypy.json
# Pyre type checker
.pyre/

# VS Code
.vscode/

# Ignore config files
config.ini

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ A Tool to help post on various social media simultaneously

## Quick Start guide
1. Install dependencies using `pip install -r requirements.txt`
2. Start the website by running `python3 .`
2. Start the website by running `python3 flaskr`
3. Stop the website using `Ctrl+C`
4. Fill up the configuration under `config.ini` based on the guide below
5. Run the server again by running `python3 .`
5. Run the server again by running `python3 flaskr`
6. Navigate to [`http://localhost:5000/`](http://localhost:5000/)
7. Fill up the form

Expand Down
1 change: 1 addition & 0 deletions flaskr/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .__main__ import app
46 changes: 46 additions & 0 deletions flaskr/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""The frontend for the social media manager"""
import os
import configparser


try:
from constants import DEFAULT_CONFIG_FILE, UPLOAD_FOLDER
from app import app
except ImportError:
from .constants import DEFAULT_CONFIG_FILE, UPLOAD_FOLDER
from .app import app


def create_default_config() -> None:
"""Create a default configuration file"""
config = configparser.ConfigParser()
# config["twitter"] = {
# "api_key": "",
# "api_secret": "",
# "access_token": "",
# "access_token_secret": "",
# "bearer_token": "",
# }
config["facebook"] = {
"token": "",
"group_id": "",
}
config["telegram"] = {
"token": "",
"group_id": "",
}
with open(DEFAULT_CONFIG_FILE, "w") as configfile:
config.write(configfile)


if __name__ == "__main__":
if not os.path.exists(DEFAULT_CONFIG_FILE):
create_default_config()
credentials = configparser.ConfigParser()
credentials.read(DEFAULT_CONFIG_FILE)

# Check if upload folder exists
if not os.path.exists(UPLOAD_FOLDER):
os.mkdir(UPLOAD_FOLDER)

app.run(debug=False)
81 changes: 54 additions & 27 deletions __main__.py → flaskr/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,63 @@
import os
import configparser
from flask import Flask, abort, render_template, request, redirect, flash, Response
from functools import partial
from werkzeug.utils import secure_filename

from constants import (
BODY_KEY,
CSV_KEY,
EMAIL_KEY,
ERROR_FLASH,
FACEBOOK,
IMAGE_KEY,
INFO_FLASH,
SOCIAL_MEDIA,
HOME_PAGE,
SELECT_AT_LEAST_ONE_SOCIAL_MEDIA,
SUBJECT_KEY,
SUCCESS_MESSAGE,
EMPTY_CONTENT,
CONTENT_KEY,
EMPTY_STRING,
DEFAULT_CONFIG_FILE,
TELEGRAM,
TWITTER,
EMAIL_PAGE,
UPLOAD_FOLDER,
)
from tools.post import post_to_facebook, post_to_telegram, post_to_twitter
from tools.email.email_sc import EmailIntegration, create_email
from tools.email import extract_csv
from tools.utils import replace_string

try:
from constants import (
BODY_KEY,
CSV_KEY,
EMAIL_KEY,
ERROR_FLASH,
FACEBOOK,
IMAGE_KEY,
INFO_FLASH,
SOCIAL_MEDIA,
HOME_PAGE,
SELECT_AT_LEAST_ONE_SOCIAL_MEDIA,
SUBJECT_KEY,
SUCCESS_MESSAGE,
EMPTY_CONTENT,
CONTENT_KEY,
EMPTY_STRING,
DEFAULT_CONFIG_FILE,
TELEGRAM,
TWITTER,
EMAIL_PAGE,
UPLOAD_FOLDER,
)
from tools.post import post_to_facebook, post_to_telegram, post_to_twitter
from tools.email.email_sc import EmailIntegration, create_email
from tools.email import extract_csv
from tools.utils import replace_string
except ImportError as e:
from .constants import (
BODY_KEY,
CSV_KEY,
EMAIL_KEY,
ERROR_FLASH,
FACEBOOK,
IMAGE_KEY,
INFO_FLASH,
SOCIAL_MEDIA,
HOME_PAGE,
SELECT_AT_LEAST_ONE_SOCIAL_MEDIA,
SUBJECT_KEY,
SUCCESS_MESSAGE,
EMPTY_CONTENT,
CONTENT_KEY,
EMPTY_STRING,
DEFAULT_CONFIG_FILE,
TELEGRAM,
TWITTER,
EMAIL_PAGE,
UPLOAD_FOLDER,
)
from .tools.post import post_to_facebook, post_to_telegram, post_to_twitter
from .tools.email.email_sc import EmailIntegration, create_email
from .tools.email import extract_csv
from .tools.utils import replace_string

# Set up logger
logging.basicConfig(
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tweepy
facebook-sdk
flask
flask
pytest
Empty file added tests/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
from flaskr import app as flask_app

@pytest.fixture()
def app():
flask_app.config.update({
"TESTING": True,
})

# other setup can go here

yield flask_app

# clean up / reset resources here


@pytest.fixture()
def client(app):
return app.test_client()


@pytest.fixture()
def runner(app):
return app.test_cli_runner()
20 changes: 20 additions & 0 deletions tests/test_homepage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def test_homepage(client):
"""Test homepage."""
response = client.get("/")
assert response.status_code == 200
assert "No content provided" not in response.data.decode()


def test_empty_post_request_fail(client):
"""Test empty post request."""
response = client.post("/", follow_redirects=True)
assert response.status_code == 200
assert "No content provided" in response.data.decode()


def test_no_social_media_selected_fails(client):
"""Test no social media selected."""
response = client.post("/", data={"content": "test"}, follow_redirects=True)
assert response.status_code == 200
assert "Please select at least 1 social media" in response.data.decode()