Skip to content

Commit

Permalink
page-intex (#13)
Browse files Browse the repository at this point in the history
* page index

* deepsource

* style: format code with Black and isort

This commit fixes the style issues introduced in b2ad53e according to the output
from Black and isort.

Details: #13

* 100%

* style: format code with Black and isort

This commit fixes the style issues introduced in 6eded52 according to the output
from Black and isort.

Details: #13

* 100%

* security

* security 2

---------

Co-authored-by: Heitor Polidoro <[email protected]>
Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 16, 2024
1 parent d00c324 commit 308c5ea
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div align="center">
<img src="img/bartholomew-logo.png" width="200" height="200">
<img src="static/bartholomew-logo.png" width="200" height="200">
</div>

# Bartholomew "The Butler" Smith
Expand Down
26 changes: 24 additions & 2 deletions src/app.py → app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import os
import sys

import markdown
import sentry_sdk
from flask import Flask
from flask import Flask, abort, render_template
from githubapp import webhook_handler
from githubapp.events import CheckSuiteRequestedEvent

Expand Down Expand Up @@ -36,7 +37,7 @@ def sentry_init():

app = Flask(__name__)
sentry_init()
webhook_handler.handle_with_flask(app)
webhook_handler.handle_with_flask(app, use_default_index=False)


@webhook_handler.webhook_handler(CheckSuiteRequestedEvent)
Expand All @@ -47,3 +48,24 @@ def handle(event: CheckSuiteRequestedEvent):
"""
repository = event.repository
handle_create_pull_request(repository, event.check_suite.head_branch)


@app.route("/", methods=["GET"])
def index():
"""Return the index homepage"""
return file("README.md")


@app.route("/<path:filename>", methods=["GET"])
def file(filename):
"""Convert a md file into HTML and return it"""
allowed_files = {f: f for f in ["README.md", "pull-request.md"]}
if filename not in allowed_files:
abort(404)
with open(allowed_files[filename]) as f:
md = f.read()
body = markdown.markdown(md)
title = "Bartholomew Smith"
if filename != "README.md":
title += f" - {filename.replace('-', ' ').replace('.md', '').title()}"
return render_template("index.html", title=title, body=body)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ github-app-handler==0.15
sentry-sdk==1.39.2
flask==3.0.0
Polidoro-PyGithub==2.2.0
Markdown==3.5.2
File renamed without changes
Binary file added static/favicon.ico
Binary file not shown.
13 changes: 13 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<link rel="shortcut icon" href="../static/favicon.ico" type="image/x-icon">
<link rel="icon" href="../static/favicon.ico" type="image/x-icon">

<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
</head>
<body>
{{ body|safe }}
</body>
</html>
49 changes: 43 additions & 6 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from unittest import TestCase
from unittest.mock import patch

from src.app import handle, sentry_init
import markdown

from app import app, handle, sentry_init


def test_sentry_init(monkeypatch):
monkeypatch.setenv("SENTRY_DSN", "https://example.com")
with patch("src.app.sentry_sdk") as mock_sentry:
with patch("app.sentry_sdk") as mock_sentry:
sentry_init()
mock_sentry.init.assert_called_once_with(
dsn="https://example.com",
Expand All @@ -15,14 +18,48 @@ def test_sentry_init(monkeypatch):


def test_sentry_dont_init(monkeypatch):
with patch("src.app.sentry_sdk") as mock_sentry:
with patch("app.sentry_sdk") as mock_sentry:
sentry_init()
mock_sentry.init.assert_not_called()


def test_handle_check_suite_requested(event):
with patch("src.app.handle_create_pull_request") as mock_handle_create_pull_request:
def test_handle_check_suite_requested(event, repository):
with patch("app.handle_create_pull_request") as mock_handle_create_pull_request:
handle(event)
mock_handle_create_pull_request.assert_called_once_with(
event.repository, event.check_suite.head_branch
repository, event.check_suite.head_branch
)


class TestApp(TestCase):
def setUp(self):
self.app = app
self.client = app.test_client()

def test_index(self):
with patch("app.render_template") as mock_render_template:
response = self.client.get("/")
assert response.status_code == 200
with open("README.md") as f:
md = f.read()
body = markdown.markdown(md)
mock_render_template.assert_called_once_with(
"index.html", title="Bartholomew Smith", body=body
)

def test_file(self):
with patch("app.render_template") as mock_render_template:
response = self.client.get("/pull-request.md")
assert response.status_code == 200
with open("pull-request.md") as f:
md = f.read()
body = markdown.markdown(md)
mock_render_template.assert_called_once_with(
"index.html", title="Bartholomew Smith - Pull Request", body=body
)

def test_file_security(self):
with patch("app.render_template") as mock_render_template:
response = self.client.get("/other.txt")
assert response.status_code == 404
mock_render_template.assert_not_called()
4 changes: 2 additions & 2 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": 2,
"builds": [
{"src": "src/app.py",
{"src": "app.py",
"use": "@vercel/python"
}
],
"routes": [
{"src": "/.*",
"dest": "src/app.py"
"dest": "app.py"
}
]
}

0 comments on commit 308c5ea

Please sign in to comment.