feat: articles list screen
This commit is contained in:
1 parent
ca3fe20519
commit
f6d0d01092
14 files changed
+2353
-25
No files matched your search
@@ -0,0 +1,92 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"RenewCMS/api/handlers/article"
|
||||
domainArticle "RenewCMS/internal/domain/article"
|
||||
domainImage "RenewCMS/internal/domain/image"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
|
||||
validator "RenewCMS/api/pkg/validator"
|
||||
)
|
||||
|
||||
func newArticlesListRouter(articleRepo *mockArticleRepo, token *jwtauth.JWTAuth) *chi.Mux {
|
||||
userRepo := newVerifiedUserRepo()
|
||||
getUserUseCase := useCases.NewGetUserUseCase(userRepo)
|
||||
|
||||
articleHandler := article.NewHandler(
|
||||
useCases.NewCreateArticleUseCase(articleRepo),
|
||||
useCases.NewGetArticleUseCase(articleRepo),
|
||||
useCases.NewListArticlesUseCase(articleRepo),
|
||||
useCases.NewDeleteArticleUseCase(articleRepo, newMockImageRepo()),
|
||||
useCases.NewUpdateArticleUseCase(articleRepo),
|
||||
getUserUseCase,
|
||||
)
|
||||
|
||||
r := chi.NewRouter()
|
||||
r.Route("/v1", func(r chi.Router) {
|
||||
r.Route("/article", func(r chi.Router) {
|
||||
r.Use(jwtauth.Verifier(token))
|
||||
r.Get("/", articleHandler.ListArticles)
|
||||
})
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
func TestArticlesListHttp(t *testing.T) {
|
||||
validator.InitValidator()
|
||||
token := jwtauth.New("HS256", []byte("test-secret"), nil)
|
||||
|
||||
t.Run("GET /v1/article/ returns each article's images array populated", func(t *testing.T) {
|
||||
repo := newMockArticleRepo()
|
||||
repo.articles[4] = domainArticle.Article{
|
||||
ID: 4,
|
||||
Title: "Winter checklist for timber windows",
|
||||
Images: []*domainImage.Image{
|
||||
{ID: 10, ArticleID: 4},
|
||||
{ID: 11, ArticleID: 4},
|
||||
},
|
||||
}
|
||||
|
||||
router := newArticlesListRouter(repo, token)
|
||||
req := httptest.NewRequest(http.MethodGet, "/v1/article/", nil)
|
||||
setJwtCookie(t, req, token, 1)
|
||||
rec := httptest.NewRecorder()
|
||||
router.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d", rec.Code)
|
||||
}
|
||||
|
||||
var got []domainArticle.Article
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
var found *domainArticle.Article
|
||||
for i := range got {
|
||||
if got[i].ID == 4 {
|
||||
found = &got[i]
|
||||
}
|
||||
}
|
||||
if found == nil {
|
||||
t.Fatalf("expected article id 4 in response, got %v", got)
|
||||
}
|
||||
if len(found.Images) != 2 {
|
||||
t.Fatalf("expected 2 images, got %d", len(found.Images))
|
||||
}
|
||||
ids := map[uint32]bool{}
|
||||
for _, img := range found.Images {
|
||||
ids[img.ID] = true
|
||||
}
|
||||
if !ids[10] || !ids[11] {
|
||||
t.Errorf("expected images 10 and 11, got %v", found.Images)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user