diff --git a/cypress/integration/common/steps.js b/cypress/integration/common/steps.js index dc43d1659..e2ab27ee6 100644 --- a/cypress/integration/common/steps.js +++ b/cypress/integration/common/steps.js @@ -223,6 +223,11 @@ Then('the first post on the landing page has the title:', title => { cy.get('.post-card:first').should('contain', title) }) -Then('I see a 404 error with the following message:', message => { - cy.get('.error').should('contain', message) -}) +Then( + 'the page {string} returns a 404 error with a message:', + (route, message) => { + // TODO: how can we check HTTP codes with cypress? + cy.visit(route, { failOnStatusCode: false }) + cy.get('.error').should('contain', message) + } +) diff --git a/cypress/integration/moderation/HidePosts.feature b/cypress/integration/moderation/HidePosts.feature index 6a11eaf3b..4006f4068 100644 --- a/cypress/integration/moderation/HidePosts.feature +++ b/cypress/integration/moderation/HidePosts.feature @@ -20,8 +20,7 @@ Feature: Hide Posts Scenario: Visiting a disabled post's page should return 404 Given I am logged in with a "user" role - When I visit the "/post/this-post-is-disabled" page - Then I see a 404 error with the following message: + Then the page "/post/this-post-is-disabled" returns a 404 error with a message: """ We cannot find that post :( """ diff --git a/pages/post/_slug/index.vue b/pages/post/_slug/index.vue index 68655a31f..601f5c8da 100644 --- a/pages/post/_slug/index.vue +++ b/pages/post/_slug/index.vue @@ -162,6 +162,22 @@ export default { this.title = this.post.title } }, + async asyncData(context) { + const { + params, + error, + app: { apolloProvider } + } = context + const client = apolloProvider.defaultClient + const query = gql('query Post($slug: String!) { Post(slug: $slug) { id } }') + const variables = { slug: params.slug } + const { + data: { Post } + } = await client.query({ query, variables }) + if (Post.length <= 0) { + error({ statusCode: 404, message: 'We cannot find that post :(' }) + } + }, methods: { isAuthor(id) { return this.$store.getters['auth/user'].id === id