Skip to content

Commit

Permalink
Fix 404 check and fix tests to ensure it works correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Siecje committed Jan 25, 2024
1 parent 4d9b364 commit 4996f3d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions htmd/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ def all_posts() -> ResponseReturnValue:

# If month and day are ints then Flask removes leading zeros
@app.route('/<year>/<month>/<day>/<path:path>/')
def post(year: str, month: str, day:str, path: str) -> ResponseReturnValue:
def post(year: str, month: str, day: str, path: str) -> ResponseReturnValue:
if len(year) != 4 or len(month) != 2 or len(day) != 2: # noqa: PLR2004
abort(404)
post = posts.get_or_404(path)
date_str = f'{year}-{month}-{day}'
if str(post.meta.get('published')) != date_str:
if post.meta.get('published').strftime('%Y-%m-%d') != date_str:
abort(404)
return render_template('post.html', post=post)

Expand Down
24 changes: 16 additions & 8 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@ def test_build_feed_dot_atom() -> None:
runner = CliRunner()
with runner.isolated_filesystem():
runner.invoke(start)
runner.invoke(build)
result = runner.invoke(build)
assert result.exit_code == 0
current_directory = Path.cwd()
assert (Path(current_directory) / 'build' / 'feed.atom').is_file

Expand All @@ -453,7 +454,8 @@ def test_build_updated_time_is_added() -> None:
runner.invoke(start)
with (Path('posts') / 'example.md').open('r') as post_file:
b_lines = post_file.readlines()
runner.invoke(build)
result = runner.invoke(build)
assert result.exit_code == 0
with (Path('posts') / 'example.md').open('r') as post_file:
a_lines = post_file.readlines()
for b_line, a_line in zip(b_lines, a_lines, strict=True):
Expand Down Expand Up @@ -503,7 +505,8 @@ def test_build_published_time_is_added() -> None:
remove_fields_from_example_post(('updated',))
with (Path('posts') / 'example.md').open('r') as post_file:
b_lines = post_file.readlines()
runner.invoke(build)
result = runner.invoke(build)
assert result.exit_code == 0
with (Path('posts') / 'example.md').open('r') as post_file:
a_lines = post_file.readlines()
for b_line, a_line in zip(b_lines, a_lines, strict=True):
Expand Down Expand Up @@ -548,9 +551,11 @@ def test_build_updated_is_added() -> None:
# Remove updated from example post
remove_fields_from_example_post(('updated',))
# First build adds time to published
runner.invoke(build)
result = runner.invoke(build)
assert result.exit_code == 0
# Second build adds updated with time
runner.invoke(build)
result2 = runner.invoke(build)
assert result2.exit_code == 0
with (Path('posts') / 'example.md').open('r') as post_file:
a_lines = post_file.readlines()
for a_line in a_lines:
Expand Down Expand Up @@ -587,9 +592,11 @@ def test_build_updated_is_added_once() -> None:
post_file.write('...\n')

# First build adds published time
runner.invoke(build)
result = runner.invoke(build)
assert result.exit_code == 0
# Second build adds updated
runner.invoke(build)
result2 = runner.invoke(build)
assert result2.exit_code == 0
with (Path('posts') / 'example.md').open('r') as post_file:
a_lines = post_file.readlines()
count = 0
Expand All @@ -607,7 +614,8 @@ def test_build_without_published() -> None:
remove_fields_from_example_post(('published', 'updated'))

# First build adds published time
runner.invoke(build)
result = runner.invoke(build)
assert result.exit_code == 0
with (Path('posts') / 'example.md').open('r') as post_file:
a_lines = post_file.readlines()
count = 0
Expand Down

0 comments on commit 4996f3d

Please sign in to comment.