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

Permalink unit tests and frontend route improvements #4322

Closed
ErisDS opened this issue Oct 22, 2014 · 5 comments · Fixed by #5297
Closed

Permalink unit tests and frontend route improvements #4322

ErisDS opened this issue Oct 22, 2014 · 5 comments · Fixed by #5297
Labels
bug [triage] something behaving unexpectedly good first issue [triage] Start here if you've never contributed before.
Milestone

Comments

@ErisDS
Copy link
Member

ErisDS commented Oct 22, 2014

Having looked around our current permalink implementation, I believe there are a number of small issues with the handling in the frontend.single controller, that will result in bugs once we add #1631.

I've added the beginner label to this because I believe that this issue should be a good fit for someone who is comfortable with server-side JS & unit testing, but is brand new to Ghost's codebase.


Essentially, the problem is that the frontend.single route is a wildcard but a post must only ever be served from a single, valid permalink. The structure of the permalink is set in the database, and valid structures are defined in #1631.

The frontend controller needs to take the permalink setting, the URL from which it is being called and validate that all parts of the URL strictly match / are valid according to the permalink.

At the moment, there is validation for the date, and after #3858 is completed, there should also be validation for the author, but this needs to be refactored and added to, to validate that all items in the URL are complete and correct.

Take for example, the slightly weird, but perfectly valid permalink structure of /blog/:slug/:author/:year/:id/.

The frontend controller should split the provided URL into 5 parts and then:

  • Look up a post which matches both the provided slug AND id
  • Check that the first part (a string blog) is exactly 'blog'
  • Check that the third part matches the slug of the post's author
  • Check that the fouth part matches the year in the post's published_at field

If any of these steps fail, a 404 should be returned.

The point of this issue is to refactor this validation step so that the code is clear and easily readable, to add validation so that string sections are definitely checked, and to add thorough unit tests for the code.

It probably makes sense to do the lookup in the frontend.single controller, but split the checks out into a new method in server/config/url.js which verifies that a given URL is a valid URL for a given post. So very similar to the urlPathForPost function, checkUrlPathForPost would also take the post data and the url provided and return whether or not it is valid.

This would drastically improve the code quality in server/controllers/frontend.js

@ErisDS ErisDS added bug [triage] something behaving unexpectedly good first issue [triage] Start here if you've never contributed before. tests labels Oct 22, 2014
@ErisDS ErisDS added this to the Next Backlog milestone Oct 22, 2014
@jillesme
Copy link
Contributor

I'd like to take this on!

@ErisDS
Copy link
Member Author

ErisDS commented Oct 27, 2014

@jillesme Cool, did you make a start?

@jillesme
Copy link
Contributor

@ErisDS I haven't, really busy with work this week.. I plan on doing it this weekend!

If it's a pressing issue and someone else can & needs do it before I can, feel free to take this from me!

@ErisDS
Copy link
Member Author

ErisDS commented Oct 28, 2014

@jillesme nope not at all pressing was just saying hi 👍

@ErisDS
Copy link
Member Author

ErisDS commented Nov 13, 2014

#4445 should actually remove most of the complexity of this issue. Looking up a post by slug or id will return the correct URL and if it doesn't match the request path then we can 404.

acburdine added a commit to acburdine/Ghost that referenced this issue May 20, 2015
closes TryGhost#4322
- removes verifying "sections" of permalinks in favor of checking the url returned with the post
- fixes unit tests to define post.url in mock post requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug [triage] something behaving unexpectedly good first issue [triage] Start here if you've never contributed before.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants