refac: authenticated user can list all articles, including offline ones, while the public list only shows online ones

This commit is contained in:
fsylvain committed 2026-09-23 22:36:41 +02:00
1 parent 6a2ca07b66
commit 790d4e6da6
5 files changed
+345 -7

No files matched your search

+64
View File
@@ -0,0 +1,64 @@
Feature: Public and authenticated article listing
As a visitor of the public site and as an authenticated RenewCMS user
I want GET /v1/article/ to return every online article to anyone,
and every article, including offline ones, to a caller with a valid jwt cookie
So that the public site never displays a draft, while an authenticated user managing
the CMS can see and act on articles that are not published yet
Background:
Given internal/domain/article.Article has a json "is_online" field
And ListArticlesUseCase.ListOnlineArticles filters article.Filters{IsOnline: true} and
ListArticlesUseCase.ListAllArticles applies no filter, both already implemented
And before this slice, GET /v1/article/ sits inside the jwtauth Verifier + Authenticator group
in api/router.go along with GET /v1/article/{id}, POST /v1/article/ and DELETE /v1/article/{id},
so an anonymous request to any of them answers 401, not an online-only list
And this slice moves GET /v1/article/ under jwtauth.Verifier only, so a missing or invalid jwt
cookie no longer blocks the request; GET /v1/article/{id}, POST /v1/article/ and
DELETE /v1/article/{id} keep requiring jwtauth.Verifier + Authenticator, unchanged
And Handler.ListArticles decides between ListAllArticles and ListOnlineArticles by checking the
request for a valid jwt, read with jwtauth.FromContext from the token the router-level
jwtauth.Verifier (header and cookie sources) placed in the request context, rather than
requiring the router-level Authenticator middleware; article.NewHandler takes no token
And the response body is the JSON array article.Article marshals to, each element carrying its
own "is_online" value
Scenario: An anonymous visitor only sees online articles
Given the article repository contains an online article titled "Backyard Renovation Update" with id 1
And the same repository contains an offline article titled "Draft: Kitchen Remodel" with id 2
When the client sends GET /v1/article/ with no jwt cookie
Then the response status is 200
And the response body is a JSON array containing exactly the article with id 1
And the article with id 2 is absent from the response body
Scenario: An authenticated user sees every article, online and offline
Given a valid jwt cookie for a user with id 1
And the article repository contains an online article titled "Backyard Renovation Update" with id 1
And the same repository contains an offline article titled "Draft: Kitchen Remodel" with id 2
When the client sends GET /v1/article/ with that cookie
Then the response status is 200
And the response body is a JSON array containing both the article with id 1 and the article with id 2
Scenario: A malformed or expired jwt cookie falls back to the public online-only list
Given a jwt cookie whose value is not a token issued by JWT_SECRET
And the article repository contains an online article with id 1 and an offline article with id 2
When the client sends GET /v1/article/ with that cookie
Then the response status is 200
And the response body is a JSON array containing exactly the article with id 1
Scenario Outline: The rest of the article routes still require authentication
Given no jwt cookie is sent
When the client sends <method> <path>
Then the response status is 401
Examples:
| method | path |
| GET | /v1/article/1 |
| POST | /v1/article/ |
| DELETE | /v1/article/1 |
Scenario: A repository failure still answers 500 regardless of auth state
Given the article repository fails on its next call to FindByFilters
When the client sends GET /v1/article/ with no jwt cookie
Then the response status is 500
And the response body is the text "Failed to retrieve articles"
+54
View File
@@ -0,0 +1,54 @@
# QA procedure: public_article_listing
Package: `test` (black-box, extending `article_test.go`).
## Setup shared by every case
1. Call `validator.InitValidator()` once.
2. Reuse `mockArticleRepo` (`test/article_test.go`) as-is; `FindByFilters` and the `failSwitch`
already support everything this slice needs, no fake changes expected.
3. Wire the real use cases over the fake: `useCases.NewCreateArticleUseCase`,
`useCases.NewGetArticleUseCase`, `useCases.NewListArticlesUseCase`,
`useCases.NewDeleteArticleUseCase`. Build `article.NewHandler(create, get, list, delete)`.
4. Build a test `jwtauth.New("HS256", []byte("test-secret"), nil)`.
5. Mount the routes on a fresh `chi.NewRouter()` mirroring the new `api/router.go` shape:
- `r.Route("/article", func(r chi.Router) { ... })`
- `r.Use(jwtauth.Verifier(token))` on the whole `/article` group (parses, never rejects)
- `r.Get("/", articleHandler.ListArticles)` directly under that Verifier-only group
- a nested `r.Group` adding `jwtauth.Authenticator(token)`, containing
`r.Get("/{id}", articleHandler.GetArticle)`, `r.Post("/", articleHandler.PostArticle)`,
`r.Delete("/{id}", articleHandler.DeleteArticle)`
No live server.
6. Mint a valid jwt cookie for user id 1: `token.Encode(map[string]any{"user_id": uint32(1)})`,
set as the `jwt` cookie on the relevant `httptest.NewRequest`.
7. Seed the repo for the listing cases via `repo.articles[1] = article.Article{ID: 1, Title:
"Backyard Renovation Update", IsOnline: true}` and `repo.articles[2] = article.Article{ID: 2,
Title: "Draft: Kitchen Remodel", IsOnline: false}` (same direct-map pattern as
`TestGetOnlineArticle`).
## HTTP checks
8. `GET /v1/article/` with no cookie, repo seeded per step 7 -> status `200`; decode the JSON array
and assert it has exactly one element, id `1`; assert no element has id `2`.
9. `GET /v1/article/` with the valid cookie from step 6, same seed -> status `200`; decode the JSON
array and assert it has exactly two elements, ids `1` and `2` both present.
10. `GET /v1/article/` with a cookie named `jwt` whose value is an arbitrary non-token string (e.g.
`"not-a-real-token"`), same seed -> status `200`; assert the same single-element (id `1`) result
as step 8, proving an invalid cookie falls back to the public list rather than erroring.
11. `GET /v1/article/1` with no cookie -> status `401` (regression: single-article read still gated).
12. `POST /v1/article/` with no cookie, JSON body `{"title":"Valid Title","body":"Some content"}` ->
status `401` (regression: create still gated).
13. `DELETE /v1/article/1` with no cookie -> status `401` (regression: delete still gated).
14. Configure the fake repo's `failSwitch` so the next `FindByFilters` call returns
`errRepositoryFailure` (existing `failNext("FindByFilters")` helper). `GET /v1/article/` with no
cookie -> status `500`, body `"Failed to retrieve articles\n"`.
15. As a regression on the valid-cookie path from step 9, also assert `GET /v1/article/{id}` and
`POST`/`DELETE` still succeed as before with a valid cookie (existing coverage in
`article_test.go` / `article_title_rules_test.go` / `write_not_found_errors_test.go` already
covers this; no new assertions needed here beyond confirming the router restructuring didn't
regress them -- rerun those suites as part of `task test`).
## Manual verification (not exercised by the Go test)
- SPA-visible behaviour: confirm the public front-office article list view (unauthenticated) only
renders online articles, and the authenticated back-office article list view renders offline
drafts too, against the real embedded frontend and a real SQLite file. Not exercised by `task
test`; requires `task dev` or a built `task build` binary.
- No SMTP involvement.