From e552c5816433b42853c098a900581e81d23a463a Mon Sep 17 00:00:00 2001 From: Mahdi Khanzadi Date: Sat, 28 Sep 2024 18:24:15 +0200 Subject: [PATCH] integration test WIP --- .../article/getArticle/response.go | 4 +- .../article/getArticles/response.go | 4 +- .../article/getArticlesByHashtag/response.go | 4 +- .../auth/forgetpassword/usecase_test.go | 5 +- .../application/auth/refresh/usecase_test.go | 4 +- .../application/auth/register/usecase_test.go | 5 +- .../auth/resetpassword/usecase_test.go | 4 +- .../application/auth/verify/usecase_test.go | 4 +- .../bookmark/updateBookmark/usecase.go | 4 + .../comment/getComments/response.go | 4 +- .../dashboard/article/getArticles/response.go | 14 +- .../article/getArticles/useCase_test.go | 10 +- backend/application/home/response.go | 16 +- .../commandbus/commandbus_test.go | 11 +- .../repository/mongodb/articles/repository.go | 5 + .../repository/mongodb/comments/repository.go | 2 + .../infrastructure/template/renderer_test.go | 28 +++ .../template/testdata/base.layout.tmpl | 6 + .../template/testdata/page.tmpl | 3 + .../infrastructure/template/testdata/page.txt | 7 + backend/presentation/commands/serve_test.go | 33 +++- .../presentation/http/api/article/index.go | 2 +- .../http/api/article/index_test.go | 25 +++ backend/presentation/http/api/article/show.go | 2 +- .../http/api/article/show_test.go | 7 + .../http/api/auth/forgetpassword.go | 2 +- .../http/api/auth/forgetpassword_test.go | 7 + backend/presentation/http/api/auth/login.go | 2 +- .../presentation/http/api/auth/login_test.go | 7 + backend/presentation/http/api/auth/refresh.go | 2 +- .../http/api/auth/refresh_test.go | 7 + .../presentation/http/api/auth/register.go | 2 +- .../http/api/auth/register_test.go | 7 + .../http/api/auth/resetpassword.go | 2 +- .../http/api/auth/resetpassword_test.go | 7 + backend/presentation/http/api/auth/verify.go | 2 +- .../presentation/http/api/auth/verify_test.go | 7 + .../presentation/http/api/bookmark/exists.go | 6 +- .../http/api/bookmark/exists_test.go | 139 ++++++++++++++ .../testdata/bookmark-exists-response.json | 3 + .../bookmark-exists-validation-response.json | 7 + .../bookmark-update-validation-response.json | 7 + .../presentation/http/api/bookmark/update.go | 3 +- .../http/api/bookmark/update_test.go | 134 +++++++++++++ .../presentation/http/api/comment/create.go | 15 +- .../http/api/comment/create_test.go | 139 ++++++++++++++ .../presentation/http/api/comment/index.go | 2 +- .../http/api/comment/index_test.go | 180 ++++++++++++++++++ .../create-comment-validation-errors.json | 7 + .../testdata/show-comments-no-data.json | 7 + .../api/comment/testdata/show-comments.json | 39 ++++ .../http/api/dashboard/article/create.go | 2 +- .../http/api/dashboard/article/create_test.go | 7 + .../http/api/dashboard/article/delete.go | 2 +- .../http/api/dashboard/article/delete_test.go | 7 + .../http/api/dashboard/article/index.go | 2 +- .../http/api/dashboard/article/index_test.go | 7 + .../http/api/dashboard/article/show.go | 2 +- .../http/api/dashboard/article/show_test.go | 7 + .../http/api/dashboard/article/update.go | 2 +- .../http/api/dashboard/article/update_test.go | 7 + .../http/api/dashboard/bookmark/deleteUser.go | 4 +- .../api/dashboard/bookmark/deleteUser_test.go | 7 + .../http/api/dashboard/bookmark/indexUser.go | 4 +- .../api/dashboard/bookmark/indexUser_test.go | 7 + .../http/api/dashboard/comment/create.go | 2 +- .../http/api/dashboard/comment/create_test.go | 7 + .../http/api/dashboard/comment/delete.go | 2 +- .../http/api/dashboard/comment/deleteUser.go | 2 +- .../api/dashboard/comment/deleteUser_test.go | 7 + .../http/api/dashboard/comment/delete_test.go | 7 + .../http/api/dashboard/comment/index.go | 2 +- .../http/api/dashboard/comment/indexUser.go | 2 +- .../api/dashboard/comment/indexUser_test.go | 7 + .../http/api/dashboard/comment/show.go | 2 +- .../http/api/dashboard/comment/showUser.go | 2 +- .../api/dashboard/comment/showUser_test.go | 7 + .../http/api/dashboard/comment/update.go | 2 +- .../http/api/dashboard/comment/updateUser.go | 2 +- .../api/dashboard/comment/updateUser_test.go | 7 + .../http/api/dashboard/comment/update_test.go | 7 + .../http/api/dashboard/config/show.go | 2 +- .../http/api/dashboard/config/show_test.go | 7 + .../http/api/dashboard/config/update.go | 2 +- .../http/api/dashboard/config/update_test.go | 7 + .../http/api/dashboard/element/create.go | 2 +- .../http/api/dashboard/element/create_test.go | 7 + .../http/api/dashboard/element/delete.go | 2 +- .../http/api/dashboard/element/delete_test.go | 7 + .../http/api/dashboard/element/index.go | 2 +- .../http/api/dashboard/element/index_test.go | 7 + .../http/api/dashboard/element/show.go | 2 +- .../http/api/dashboard/element/show_test.go | 7 + .../http/api/dashboard/element/update.go | 2 +- .../http/api/dashboard/element/update_test.go | 7 + .../http/api/dashboard/file/delete.go | 2 +- .../http/api/dashboard/file/delete_test.go | 7 + .../http/api/dashboard/file/index.go | 2 +- .../http/api/dashboard/file/index_test.go | 7 + .../http/api/dashboard/file/show.go | 2 +- .../http/api/dashboard/file/show_test.go | 7 + .../http/api/dashboard/file/upload.go | 2 +- .../http/api/dashboard/file/upload_test.go | 7 + .../http/api/dashboard/permission/index.go | 5 +- .../api/dashboard/permission/index_test.go | 7 + .../api/dashboard/profile/changepassword.go | 2 +- .../dashboard/profile/changepassword_test.go | 7 + .../http/api/dashboard/profile/getprofile.go | 2 +- .../api/dashboard/profile/getprofile_test.go | 7 + .../http/api/dashboard/profile/getroles.go | 2 +- .../api/dashboard/profile/getroles_test.go | 7 + .../api/dashboard/profile/updateprofile.go | 2 +- .../dashboard/profile/updateprofile_test.go | 7 + .../http/api/dashboard/role/create.go | 2 +- .../http/api/dashboard/role/create_test.go | 7 + .../http/api/dashboard/role/delete.go | 2 +- .../http/api/dashboard/role/delete_test.go | 7 + .../http/api/dashboard/role/index.go | 2 +- .../http/api/dashboard/role/index_test.go | 7 + .../http/api/dashboard/role/show.go | 2 +- .../http/api/dashboard/role/show_test.go | 7 + .../http/api/dashboard/role/update.go | 2 +- .../http/api/dashboard/role/update_test.go | 7 + .../http/api/dashboard/user/changepassword.go | 2 +- .../api/dashboard/user/changepassword_test.go | 7 + .../http/api/dashboard/user/create.go | 2 +- .../http/api/dashboard/user/create_test.go | 7 + .../http/api/dashboard/user/delete.go | 2 +- .../http/api/dashboard/user/delete_test.go | 7 + .../http/api/dashboard/user/index.go | 2 +- .../http/api/dashboard/user/index_test.go | 7 + .../http/api/dashboard/user/show.go | 2 +- .../http/api/dashboard/user/show_test.go | 7 + .../http/api/dashboard/user/update.go | 2 +- .../http/api/dashboard/user/update_test.go | 7 + backend/presentation/http/api/file/show.go | 2 +- .../presentation/http/api/file/show_test.go | 118 ++++++++++++ backend/presentation/http/api/hashtag/show.go | 11 +- .../http/api/hashtag/show_test.go | 145 ++++++++++++++ .../http/api/hashtag/testdata/response-01.txt | 43 +++++ .../http/api/hashtag/testdata/response-02.txt | 6 + backend/presentation/http/api/home/home.go | 13 +- .../presentation/http/api/home/home_test.go | 151 +++++++++++++++ .../http/api/home/testdata/response-01.txt | 87 +++++++++ .../http/api/home/testdata/response-02.txt | 5 + backend/presentation/http/api/tag/show.go | 30 --- .../http/middleware/authorise_test.go | 7 + .../presentation/http/middleware/cors_test.go | 7 + .../http/middleware/ratelimit_test.go | 7 + 149 files changed, 1805 insertions(+), 162 deletions(-) create mode 100644 backend/infrastructure/template/renderer_test.go create mode 100644 backend/infrastructure/template/testdata/base.layout.tmpl create mode 100644 backend/infrastructure/template/testdata/page.tmpl create mode 100644 backend/infrastructure/template/testdata/page.txt create mode 100644 backend/presentation/http/api/article/index_test.go create mode 100644 backend/presentation/http/api/article/show_test.go create mode 100644 backend/presentation/http/api/auth/forgetpassword_test.go create mode 100644 backend/presentation/http/api/auth/login_test.go create mode 100644 backend/presentation/http/api/auth/refresh_test.go create mode 100644 backend/presentation/http/api/auth/register_test.go create mode 100644 backend/presentation/http/api/auth/resetpassword_test.go create mode 100644 backend/presentation/http/api/auth/verify_test.go create mode 100644 backend/presentation/http/api/bookmark/exists_test.go create mode 100644 backend/presentation/http/api/bookmark/testdata/bookmark-exists-response.json create mode 100644 backend/presentation/http/api/bookmark/testdata/bookmark-exists-validation-response.json create mode 100644 backend/presentation/http/api/bookmark/testdata/bookmark-update-validation-response.json create mode 100644 backend/presentation/http/api/bookmark/update_test.go create mode 100644 backend/presentation/http/api/comment/create_test.go create mode 100644 backend/presentation/http/api/comment/index_test.go create mode 100644 backend/presentation/http/api/comment/testdata/create-comment-validation-errors.json create mode 100644 backend/presentation/http/api/comment/testdata/show-comments-no-data.json create mode 100644 backend/presentation/http/api/comment/testdata/show-comments.json create mode 100644 backend/presentation/http/api/dashboard/article/create_test.go create mode 100644 backend/presentation/http/api/dashboard/article/delete_test.go create mode 100644 backend/presentation/http/api/dashboard/article/index_test.go create mode 100644 backend/presentation/http/api/dashboard/article/show_test.go create mode 100644 backend/presentation/http/api/dashboard/article/update_test.go create mode 100644 backend/presentation/http/api/dashboard/bookmark/deleteUser_test.go create mode 100644 backend/presentation/http/api/dashboard/bookmark/indexUser_test.go create mode 100644 backend/presentation/http/api/dashboard/comment/create_test.go create mode 100644 backend/presentation/http/api/dashboard/comment/deleteUser_test.go create mode 100644 backend/presentation/http/api/dashboard/comment/delete_test.go create mode 100644 backend/presentation/http/api/dashboard/comment/indexUser_test.go create mode 100644 backend/presentation/http/api/dashboard/comment/showUser_test.go create mode 100644 backend/presentation/http/api/dashboard/comment/updateUser_test.go create mode 100644 backend/presentation/http/api/dashboard/comment/update_test.go create mode 100644 backend/presentation/http/api/dashboard/config/show_test.go create mode 100644 backend/presentation/http/api/dashboard/config/update_test.go create mode 100644 backend/presentation/http/api/dashboard/element/create_test.go create mode 100644 backend/presentation/http/api/dashboard/element/delete_test.go create mode 100644 backend/presentation/http/api/dashboard/element/index_test.go create mode 100644 backend/presentation/http/api/dashboard/element/show_test.go create mode 100644 backend/presentation/http/api/dashboard/element/update_test.go create mode 100644 backend/presentation/http/api/dashboard/file/delete_test.go create mode 100644 backend/presentation/http/api/dashboard/file/index_test.go create mode 100644 backend/presentation/http/api/dashboard/file/show_test.go create mode 100644 backend/presentation/http/api/dashboard/file/upload_test.go create mode 100644 backend/presentation/http/api/dashboard/permission/index_test.go create mode 100644 backend/presentation/http/api/dashboard/profile/changepassword_test.go create mode 100644 backend/presentation/http/api/dashboard/profile/getprofile_test.go create mode 100644 backend/presentation/http/api/dashboard/profile/getroles_test.go create mode 100644 backend/presentation/http/api/dashboard/profile/updateprofile_test.go create mode 100644 backend/presentation/http/api/dashboard/role/create_test.go create mode 100644 backend/presentation/http/api/dashboard/role/delete_test.go create mode 100644 backend/presentation/http/api/dashboard/role/index_test.go create mode 100644 backend/presentation/http/api/dashboard/role/show_test.go create mode 100644 backend/presentation/http/api/dashboard/role/update_test.go create mode 100644 backend/presentation/http/api/dashboard/user/changepassword_test.go create mode 100644 backend/presentation/http/api/dashboard/user/create_test.go create mode 100644 backend/presentation/http/api/dashboard/user/delete_test.go create mode 100644 backend/presentation/http/api/dashboard/user/index_test.go create mode 100644 backend/presentation/http/api/dashboard/user/show_test.go create mode 100644 backend/presentation/http/api/dashboard/user/update_test.go create mode 100644 backend/presentation/http/api/file/show_test.go create mode 100644 backend/presentation/http/api/hashtag/show_test.go create mode 100644 backend/presentation/http/api/hashtag/testdata/response-01.txt create mode 100644 backend/presentation/http/api/hashtag/testdata/response-02.txt create mode 100644 backend/presentation/http/api/home/home_test.go create mode 100644 backend/presentation/http/api/home/testdata/response-01.txt create mode 100644 backend/presentation/http/api/home/testdata/response-02.txt delete mode 100644 backend/presentation/http/api/tag/show.go create mode 100644 backend/presentation/http/middleware/authorise_test.go create mode 100644 backend/presentation/http/middleware/cors_test.go create mode 100644 backend/presentation/http/middleware/ratelimit_test.go diff --git a/backend/application/article/getArticle/response.go b/backend/application/article/getArticle/response.go index 068572ff..a265d04c 100644 --- a/backend/application/article/getArticle/response.go +++ b/backend/application/article/getArticle/response.go @@ -32,7 +32,7 @@ type articleResponse struct { Cover string `json:"cover"` Title string `json:"title"` Author authorResponse `json:"author"` - PublishedAt time.Time `json:"published_at"` + PublishedAt string `json:"published_at"` Excerpt string `json:"excerpt"` Tags []string `json:"tags"` } @@ -147,7 +147,7 @@ func toArticleResponse(a []article.Article) []articleResponse { items[i].Title = a[i].Title items[i].Excerpt = a[i].Excerpt items[i].Tags = a[i].Tags - items[i].PublishedAt = a[i].PublishedAt + items[i].PublishedAt = a[i].PublishedAt.Format(time.RFC3339) items[i].Author.Name = a[i].Author.Name items[i].Author.Avatar = a[i].Author.Avatar diff --git a/backend/application/article/getArticles/response.go b/backend/application/article/getArticles/response.go index ef3dabec..f3051a88 100644 --- a/backend/application/article/getArticles/response.go +++ b/backend/application/article/getArticles/response.go @@ -17,7 +17,7 @@ type articleResponse struct { Video string `json:"video"` Title string `json:"title"` Excerpt string `json:"excerpt"` - PublishedAt time.Time `json:"published_at"` + PublishedAt string `json:"published_at"` Author authorResponse `json:"author"` } @@ -40,7 +40,7 @@ func NewResponse(a []article.Article, totalPages, currentPage uint) *Response { items[i].Video = a[i].Video items[i].Title = a[i].Title items[i].Excerpt = a[i].Excerpt - items[i].PublishedAt = a[i].PublishedAt + items[i].PublishedAt = a[i].PublishedAt.Format(time.RFC3339) items[i].Author.Name = a[i].Author.Name items[i].Author.Avatar = a[i].Author.Avatar diff --git a/backend/application/article/getArticlesByHashtag/response.go b/backend/application/article/getArticlesByHashtag/response.go index 753167c6..ec41f22f 100644 --- a/backend/application/article/getArticlesByHashtag/response.go +++ b/backend/application/article/getArticlesByHashtag/response.go @@ -17,7 +17,7 @@ type articleResponse struct { Video string `json:"video"` Title string `json:"title"` Excerpt string `json:"excerpt"` - PublishedAt time.Time `json:"published_at"` + PublishedAt string `json:"published_at"` Author authorResponse `json:"authorResponse"` } @@ -39,7 +39,7 @@ func NewResponse(a []article.Article, currentPage uint) *Response { items[i].Video = a[i].Video items[i].Title = a[i].Title items[i].Excerpt = a[i].Excerpt - items[i].PublishedAt = a[i].PublishedAt + items[i].PublishedAt = a[i].PublishedAt.Format(time.RFC3339) items[i].Author.Name = a[i].Author.Name items[i].Author.Avatar = a[i].Author.Avatar diff --git a/backend/application/auth/forgetpassword/usecase_test.go b/backend/application/auth/forgetpassword/usecase_test.go index d46a4af7..176ab927 100644 --- a/backend/application/auth/forgetpassword/usecase_test.go +++ b/backend/application/auth/forgetpassword/usecase_test.go @@ -17,9 +17,8 @@ import ( func TestUseCase_Execute(t *testing.T) { privateKey, err := ecdsa.Generate() - if err != nil { - t.Error("unexpected error") - } + assert.NoError(t, err) + j := jwt.NewJWT(privateKey, privateKey.Public()) mailFrom := "info@noreply.nowhere.loc" diff --git a/backend/application/auth/refresh/usecase_test.go b/backend/application/auth/refresh/usecase_test.go index 47116709..148efbf4 100644 --- a/backend/application/auth/refresh/usecase_test.go +++ b/backend/application/auth/refresh/usecase_test.go @@ -15,9 +15,7 @@ import ( func TestUseCase_Execute(t *testing.T) { privateKey, err := ecdsa.Generate() - if err != nil { - t.Error("unexpected error") - } + assert.NoError(t, err) j := jwt.NewJWT(privateKey, privateKey.Public()) diff --git a/backend/application/auth/register/usecase_test.go b/backend/application/auth/register/usecase_test.go index 7fbde8b5..3ea870e1 100644 --- a/backend/application/auth/register/usecase_test.go +++ b/backend/application/auth/register/usecase_test.go @@ -18,9 +18,8 @@ import ( func TestUseCase_Execute(t *testing.T) { privateKey, err := ecdsa.Generate() - if err != nil { - t.Error("unexpected error") - } + assert.NoError(t, err) + j := jwt.NewJWT(privateKey, privateKey.Public()) mailFrom := "info@noreply.nowhere.loc" diff --git a/backend/application/auth/resetpassword/usecase_test.go b/backend/application/auth/resetpassword/usecase_test.go index b2fae7de..182968fe 100644 --- a/backend/application/auth/resetpassword/usecase_test.go +++ b/backend/application/auth/resetpassword/usecase_test.go @@ -19,9 +19,7 @@ import ( func TestUseCase_ResetPassword(t *testing.T) { privateKey, err := ecdsa.Generate() - if err != nil { - t.Error("unexpected error") - } + assert.NoError(t, err) j := jwt.NewJWT(privateKey, privateKey.Public()) diff --git a/backend/application/auth/verify/usecase_test.go b/backend/application/auth/verify/usecase_test.go index 72af9807..1797c63c 100644 --- a/backend/application/auth/verify/usecase_test.go +++ b/backend/application/auth/verify/usecase_test.go @@ -24,9 +24,7 @@ import ( func TestUseCase_Execute(t *testing.T) { privateKey, err := ecdsa.Generate() - if err != nil { - t.Error("unexpected error") - } + assert.NoError(t, err) j := jwt.NewJWT(privateKey, privateKey.Public()) diff --git a/backend/application/bookmark/updateBookmark/usecase.go b/backend/application/bookmark/updateBookmark/usecase.go index 69740e78..73bef259 100644 --- a/backend/application/bookmark/updateBookmark/usecase.go +++ b/backend/application/bookmark/updateBookmark/usecase.go @@ -1,6 +1,8 @@ package updateBookmark import ( + "log" + "github.com/khanzadimahdi/testproject/domain/bookmark" ) @@ -23,6 +25,8 @@ func (uc *UseCase) Execute(request *Request) (*Response, error) { }, nil } + log.Println("keep", !request.Keep) + if !request.Keep { if err := uc.bookmarkRepository.DeleteByOwnerUUID( request.OwnerUUID, diff --git a/backend/application/comment/getComments/response.go b/backend/application/comment/getComments/response.go index f1dc5f6f..2af9131f 100644 --- a/backend/application/comment/getComments/response.go +++ b/backend/application/comment/getComments/response.go @@ -11,7 +11,7 @@ type commentResponse struct { Body string `json:"body"` Author authorResponse `json:"author"` ParentUUID string `json:"parent_uuid,omitempty"` - CreatedAt time.Time `json:"created_at"` + CreatedAt string `json:"created_at"` } type authorResponse struct { @@ -37,7 +37,7 @@ func NewResponse(c []comment.Comment, totalPages, currentPage uint) *Response { items[i].UUID = c[i].UUID items[i].Body = c[i].Body items[i].ParentUUID = c[i].ParentUUID - items[i].CreatedAt = c[i].CreatedAt + items[i].CreatedAt = c[i].CreatedAt.Format(time.RFC3339) items[i].Author = authorResponse{ UUID: c[i].Author.UUID, diff --git a/backend/application/dashboard/article/getArticles/response.go b/backend/application/dashboard/article/getArticles/response.go index c3d84006..7eb2dac7 100644 --- a/backend/application/dashboard/article/getArticles/response.go +++ b/backend/application/dashboard/article/getArticles/response.go @@ -12,12 +12,12 @@ type Response struct { } type articleResponse struct { - UUID string `json:"uuid"` - Cover string `json:"cover"` - Video string `json:"video"` - Title string `json:"title"` - PublishedAt time.Time `json:"published_at"` - Author author `json:"author"` + UUID string `json:"uuid"` + Cover string `json:"cover"` + Video string `json:"video"` + Title string `json:"title"` + PublishedAt string `json:"published_at"` + Author author `json:"author"` } type author struct { @@ -38,7 +38,7 @@ func NewResponse(a []article.Article, totalPages, currentPage uint) *Response { items[i].Cover = a[i].Cover items[i].Video = a[i].Video items[i].Title = a[i].Title - items[i].PublishedAt = a[i].PublishedAt + items[i].PublishedAt = a[i].PublishedAt.Format(time.RFC3339) items[i].Author.Name = a[i].Author.Name items[i].Author.Avatar = a[i].Author.Avatar diff --git a/backend/application/dashboard/article/getArticles/useCase_test.go b/backend/application/dashboard/article/getArticles/useCase_test.go index e329a4d2..32fc1f11 100644 --- a/backend/application/dashboard/article/getArticles/useCase_test.go +++ b/backend/application/dashboard/article/getArticles/useCase_test.go @@ -41,15 +41,17 @@ func TestUseCase_Execute(t *testing.T) { expectedResponse = Response{ Items: []articleResponse{ { - UUID: a[0].UUID, - Title: a[0].Title, + UUID: a[0].UUID, + Title: a[0].Title, + PublishedAt: "0001-01-01T00:00:00Z", }, { - UUID: a[1].UUID, + UUID: a[1].UUID, + PublishedAt: "0001-01-01T00:00:00Z", }, { UUID: a[2].UUID, - PublishedAt: a[2].PublishedAt, + PublishedAt: a[2].PublishedAt.Format(time.RFC3339), }, }, Pagination: pagination{ diff --git a/backend/application/home/response.go b/backend/application/home/response.go index 78f2f8c2..c03a5157 100644 --- a/backend/application/home/response.go +++ b/backend/application/home/response.go @@ -15,13 +15,13 @@ type Response struct { } type articleResponse struct { - UUID string `json:"uuid"` - Cover string `json:"cover"` - Title string `json:"title"` - Author author `json:"author"` - PublishedAt time.Time `json:"published_at"` - Excerpt string `json:"excerpt"` - Tags []string `json:"tags"` + UUID string `json:"uuid"` + Cover string `json:"cover"` + Title string `json:"title"` + Author author `json:"author"` + PublishedAt string `json:"published_at"` + Excerpt string `json:"excerpt"` + Tags []string `json:"tags"` } type author struct { @@ -130,7 +130,7 @@ func toArticleResponse(a []article.Article) []articleResponse { items[i].Title = a[i].Title items[i].Excerpt = a[i].Excerpt items[i].Tags = a[i].Tags - items[i].PublishedAt = a[i].PublishedAt + items[i].PublishedAt = a[i].PublishedAt.Format(time.RFC3339) items[i].Author.Name = a[i].Author.Name items[i].Author.Avatar = a[i].Author.Avatar diff --git a/backend/infrastructure/commandbus/commandbus_test.go b/backend/infrastructure/commandbus/commandbus_test.go index ae8b9f57..100ce21b 100644 --- a/backend/infrastructure/commandbus/commandbus_test.go +++ b/backend/infrastructure/commandbus/commandbus_test.go @@ -1,9 +1,10 @@ package commandbus import ( - "bytes" "crypto/rand" "testing" + + "github.com/stretchr/testify/assert" ) func TestBus(t *testing.T) { @@ -22,13 +23,9 @@ func TestBus(t *testing.T) { counter++ cmd, ok := command.(*FakeCommand) - if !ok { - t.Error("invalid command") - } - if !bytes.Equal(payload, cmd.Payload) { - t.Error("command payload is not valid") - } + assert.True(t, ok, "invalid command") + assert.Equal(t, payload, cmd.Payload, "command payload is not valid") } b.Register(&FakeCommand{}, HandlerFunc(handler)) diff --git a/backend/infrastructure/repository/mongodb/articles/repository.go b/backend/infrastructure/repository/mongodb/articles/repository.go index fc6683f5..21edf7fc 100644 --- a/backend/infrastructure/repository/mongodb/articles/repository.go +++ b/backend/infrastructure/repository/mongodb/articles/repository.go @@ -96,6 +96,7 @@ func (r *ArticlesRepository) GetAllPublished(offset uint, limit uint) ([]article filter := bson.M{ "published_at": bson.M{ "$lte": primitive.NewDateTimeFromTime(time.Now()), + "$ne": time.Time{}, }, } @@ -195,6 +196,7 @@ func (r *ArticlesRepository) GetMostViewed(limit uint) ([]article.Article, error filter := bson.M{ "published_at": bson.M{ "$lte": primitive.NewDateTimeFromTime(time.Now()), + "$ne": time.Time{}, }, } @@ -251,6 +253,7 @@ func (r *ArticlesRepository) GetByHashtag(hashtags []string, offset uint, limit }, "published_at": bson.M{ "$lte": primitive.NewDateTimeFromTime(time.Now()), + "$ne": time.Time{}, }, } @@ -332,6 +335,7 @@ func (r *ArticlesRepository) GetOnePublished(UUID string) (article.Article, erro "_id": UUID, "published_at": bson.M{ "$lte": primitive.NewDateTimeFromTime(time.Now()), + "$ne": time.Time{}, }, } @@ -378,6 +382,7 @@ func (r *ArticlesRepository) CountPublished() (uint, error) { filter := bson.M{ "published_at": bson.M{ "$lte": primitive.NewDateTimeFromTime(time.Now()), + "$ne": time.Time{}, }, } diff --git a/backend/infrastructure/repository/mongodb/comments/repository.go b/backend/infrastructure/repository/mongodb/comments/repository.go index e5c771c6..0eda9b2b 100644 --- a/backend/infrastructure/repository/mongodb/comments/repository.go +++ b/backend/infrastructure/repository/mongodb/comments/repository.go @@ -191,6 +191,7 @@ func (r *CommentsRepository) GetApprovedByObjectUUID(objectType string, UUID str Key: "approved_at", Value: bson.M{ "$lte": primitive.NewDateTimeFromTime(time.Now()), + "$ne": time.Time{}, }, }, }, @@ -253,6 +254,7 @@ func (r *CommentsRepository) CountApprovedByObjectUUID(objectType string, UUID s Key: "approved_at", Value: bson.M{ "$lte": primitive.NewDateTimeFromTime(time.Now()), + "$ne": time.Time{}, }, }, }, diff --git a/backend/infrastructure/template/renderer_test.go b/backend/infrastructure/template/renderer_test.go new file mode 100644 index 00000000..6fa1b0c4 --- /dev/null +++ b/backend/infrastructure/template/renderer_test.go @@ -0,0 +1,28 @@ +package template + +import ( + "bytes" + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRenderer(t *testing.T) { + expected, err := os.ReadFile("testdata/page.txt") + assert.NoError(t, err) + + fs := os.DirFS("testdata") + extension := "tmpl" + + renderer := NewRenderer(fs, extension) + + var buffer bytes.Buffer + err = renderer.Render(&buffer, "page", map[string]string{ + "head": "test head", + "body": "test body", + }) + + assert.NoError(t, err) + assert.Equal(t, string(expected), buffer.String()) +} diff --git a/backend/infrastructure/template/testdata/base.layout.tmpl b/backend/infrastructure/template/testdata/base.layout.tmpl new file mode 100644 index 00000000..29c10d66 --- /dev/null +++ b/backend/infrastructure/template/testdata/base.layout.tmpl @@ -0,0 +1,6 @@ + + + + {{block "head" .}}{{end}} + {{block "body" .}}{{end}} + \ No newline at end of file diff --git a/backend/infrastructure/template/testdata/page.tmpl b/backend/infrastructure/template/testdata/page.tmpl new file mode 100644 index 00000000..649a314a --- /dev/null +++ b/backend/infrastructure/template/testdata/page.tmpl @@ -0,0 +1,3 @@ +{{template "base.layout.tmpl" .}} +{{define "head"}}{{.head}}{{end}} +{{define "body"}}
{{.body}}
{{end}} \ No newline at end of file diff --git a/backend/infrastructure/template/testdata/page.txt b/backend/infrastructure/template/testdata/page.txt new file mode 100644 index 00000000..19604f1e --- /dev/null +++ b/backend/infrastructure/template/testdata/page.txt @@ -0,0 +1,7 @@ + + + + test head +
test body
+ + diff --git a/backend/presentation/commands/serve_test.go b/backend/presentation/commands/serve_test.go index 672c2567..60471e7f 100644 --- a/backend/presentation/commands/serve_test.go +++ b/backend/presentation/commands/serve_test.go @@ -6,6 +6,10 @@ import ( "fmt" "net/http" "testing" + "time" + + "github.com/khanzadimahdi/testproject/infrastructure/console" + "github.com/stretchr/testify/assert" ) func TestServe(t *testing.T) { @@ -81,11 +85,30 @@ func TestServe(t *testing.T) { }) command := NewServeCommand(handler) + command.port = 1234 + + serverStartedListening := make(chan struct{}) + + go func() { + serverStartedListening <- struct{}{} + if exitStatus := command.Run(ctx); exitStatus != console.ExitSuccess { + t.Error("unexpected exit code") + } + }() + + <-serverStartedListening + time.Sleep(50 * time.Millisecond) // wait for server to start serving + + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://0.0.0.0:%d", command.port), nil) + assert.NoError(t, err) + + c := http.Client{ + Timeout: 1 * time.Second, + } + + resp, err := c.Do(req) - _ = ctx - _ = command - // if exitStatus := command.Run(ctx); exitStatus != console.ExitSuccess { - // t.Error("unexpected exit code") - // } + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) }) } diff --git a/backend/presentation/http/api/article/index.go b/backend/presentation/http/api/article/index.go index ee775cd9..1b39c726 100644 --- a/backend/presentation/http/api/article/index.go +++ b/backend/presentation/http/api/article/index.go @@ -33,7 +33,7 @@ func (h *indexHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.getArticlesUseCase.Execute(request) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/article/index_test.go b/backend/presentation/http/api/article/index_test.go new file mode 100644 index 00000000..bb87eb97 --- /dev/null +++ b/backend/presentation/http/api/article/index_test.go @@ -0,0 +1,25 @@ +package article + +import "testing" + +func TestIndexHandler(t *testing.T) { + t.Run("get list of articles", func(t *testing.T) { + + }) + + t.Run("get empty list of articles", func(t *testing.T) { + + }) + + t.Run("pagination", func(t *testing.T) { + + }) + + t.Run("error", func(t *testing.T) { + + }) + + t.Run("validation", func(t *testing.T) { + + }) +} diff --git a/backend/presentation/http/api/article/show.go b/backend/presentation/http/api/article/show.go index a8eefb8e..b35e411b 100644 --- a/backend/presentation/http/api/article/show.go +++ b/backend/presentation/http/api/article/show.go @@ -24,7 +24,7 @@ func (h *showHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.getArticleUseCase.Execute(UUID) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/article/show_test.go b/backend/presentation/http/api/article/show_test.go new file mode 100644 index 00000000..ae4e2564 --- /dev/null +++ b/backend/presentation/http/api/article/show_test.go @@ -0,0 +1,7 @@ +package article + +import "testing" + +func TestShowHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/auth/forgetpassword.go b/backend/presentation/http/api/auth/forgetpassword.go index c2a89001..41555a60 100644 --- a/backend/presentation/http/api/auth/forgetpassword.go +++ b/backend/presentation/http/api/auth/forgetpassword.go @@ -28,7 +28,7 @@ func (h *forgetPasswordHandler) ServeHTTP(rw http.ResponseWriter, r *http.Reques response, err := h.useCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/auth/forgetpassword_test.go b/backend/presentation/http/api/auth/forgetpassword_test.go new file mode 100644 index 00000000..dc062fec --- /dev/null +++ b/backend/presentation/http/api/auth/forgetpassword_test.go @@ -0,0 +1,7 @@ +package auth + +import "testing" + +func TestForgetPasswordHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/auth/login.go b/backend/presentation/http/api/auth/login.go index 74740353..9597e8a9 100644 --- a/backend/presentation/http/api/auth/login.go +++ b/backend/presentation/http/api/auth/login.go @@ -27,7 +27,7 @@ func (h *loginHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.loginUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/auth/login_test.go b/backend/presentation/http/api/auth/login_test.go new file mode 100644 index 00000000..ce1f414e --- /dev/null +++ b/backend/presentation/http/api/auth/login_test.go @@ -0,0 +1,7 @@ +package auth + +import "testing" + +func TestLoginHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/auth/refresh.go b/backend/presentation/http/api/auth/refresh.go index 3a0705cb..7e09f52f 100644 --- a/backend/presentation/http/api/auth/refresh.go +++ b/backend/presentation/http/api/auth/refresh.go @@ -27,7 +27,7 @@ func (h *refreshHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.refreshUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/auth/refresh_test.go b/backend/presentation/http/api/auth/refresh_test.go new file mode 100644 index 00000000..48d11da8 --- /dev/null +++ b/backend/presentation/http/api/auth/refresh_test.go @@ -0,0 +1,7 @@ +package auth + +import "testing" + +func TestRefreshHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/auth/register.go b/backend/presentation/http/api/auth/register.go index 1b660b1a..32390017 100644 --- a/backend/presentation/http/api/auth/register.go +++ b/backend/presentation/http/api/auth/register.go @@ -28,7 +28,7 @@ func (h *registerHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.useCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/auth/register_test.go b/backend/presentation/http/api/auth/register_test.go new file mode 100644 index 00000000..f7326af3 --- /dev/null +++ b/backend/presentation/http/api/auth/register_test.go @@ -0,0 +1,7 @@ +package auth + +import "testing" + +func TestRegisterHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/auth/resetpassword.go b/backend/presentation/http/api/auth/resetpassword.go index 8ed4dcc1..e51fb769 100644 --- a/backend/presentation/http/api/auth/resetpassword.go +++ b/backend/presentation/http/api/auth/resetpassword.go @@ -28,7 +28,7 @@ func (h *resetPasswordHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request response, err := h.useCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/auth/resetpassword_test.go b/backend/presentation/http/api/auth/resetpassword_test.go new file mode 100644 index 00000000..dd6937e5 --- /dev/null +++ b/backend/presentation/http/api/auth/resetpassword_test.go @@ -0,0 +1,7 @@ +package auth + +import "testing" + +func TestResetPasswordHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/auth/verify.go b/backend/presentation/http/api/auth/verify.go index 46c747e2..a6fdeecf 100644 --- a/backend/presentation/http/api/auth/verify.go +++ b/backend/presentation/http/api/auth/verify.go @@ -28,7 +28,7 @@ func (h *verifyHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.useCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/auth/verify_test.go b/backend/presentation/http/api/auth/verify_test.go new file mode 100644 index 00000000..3869466e --- /dev/null +++ b/backend/presentation/http/api/auth/verify_test.go @@ -0,0 +1,7 @@ +package auth + +import "testing" + +func TestVerifyHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/bookmark/exists.go b/backend/presentation/http/api/bookmark/exists.go index 6f33fcb2..1ff3c207 100644 --- a/backend/presentation/http/api/bookmark/exists.go +++ b/backend/presentation/http/api/bookmark/exists.go @@ -28,9 +28,13 @@ func (h *existsHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.useCase.Execute(&request) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) + case len(response.ValidationErrors) > 0: + rw.Header().Add("Content-Type", "application/json") + rw.WriteHeader(http.StatusBadRequest) + json.NewEncoder(rw).Encode(response) default: rw.Header().Add("Content-Type", "application/json") rw.WriteHeader(http.StatusOK) diff --git a/backend/presentation/http/api/bookmark/exists_test.go b/backend/presentation/http/api/bookmark/exists_test.go new file mode 100644 index 00000000..0544019d --- /dev/null +++ b/backend/presentation/http/api/bookmark/exists_test.go @@ -0,0 +1,139 @@ +package bookmark + +import ( + "bytes" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" + + "github.com/khanzadimahdi/testproject/application/auth" + "github.com/khanzadimahdi/testproject/application/bookmark/bookmarkExists" + "github.com/khanzadimahdi/testproject/domain/bookmark" + "github.com/khanzadimahdi/testproject/domain/user" + "github.com/khanzadimahdi/testproject/infrastructure/repository/mocks/bookmarks" +) + +func TestExistsHandler(t *testing.T) { + t.Run("exists", func(t *testing.T) { + var ( + bookmarkRepository bookmarks.MockBookmarksRepository + ) + + u := user.User{ + UUID: "user-uuid-1", + } + + b := bookmark.Bookmark{ + UUID: "bookmark-uuid", + Title: "bookmark-title", + ObjectUUID: "object-uuid", + ObjectType: "article", + OwnerUUID: u.UUID, + CreatedAt: time.Now(), + } + + bookmarkRepository.On("GetByOwnerUUID", b.OwnerUUID, b.ObjectType, b.ObjectUUID).Once().Return(b, nil) + defer bookmarkRepository.AssertExpectations(t) + + handler := NewExistsHandler(bookmarkExists.NewUseCase(&bookmarkRepository)) + + r := bookmarkExists.Request{ + ObjectType: b.ObjectType, + ObjectUUID: b.ObjectUUID, + } + + var paylaod bytes.Buffer + + err := json.NewEncoder(&paylaod).Encode(r) + assert.NoError(t, err) + + request := httptest.NewRequest(http.MethodPost, "/", &paylaod) + request = request.WithContext(auth.ToContext(request.Context(), &u)) + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + expected, err := os.ReadFile("testdata/bookmark-exists-response.json") + assert.NoError(t, err) + + assert.Equal(t, "application/json", response.Header().Get("content-type")) + assert.JSONEq(t, string(expected), response.Body.String()) + assert.Equal(t, http.StatusOK, response.Code) + }) + + t.Run("validation failed", func(t *testing.T) { + var ( + bookmarkRepository bookmarks.MockBookmarksRepository + ) + + u := user.User{ + UUID: "user-uuid-1", + } + + handler := NewExistsHandler(bookmarkExists.NewUseCase(&bookmarkRepository)) + + request := httptest.NewRequest(http.MethodPost, "/", bytes.NewBufferString("{}")) + request = request.WithContext(auth.ToContext(request.Context(), &u)) + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + bookmarkRepository.AssertNotCalled(t, "GetByOwnerUUID") + + expected, err := os.ReadFile("testdata/bookmark-exists-validation-response.json") + assert.NoError(t, err) + + assert.Equal(t, "application/json", response.Header().Get("content-type")) + assert.JSONEq(t, string(expected), response.Body.String()) + assert.Equal(t, http.StatusBadRequest, response.Code) + }) + + t.Run("error", func(t *testing.T) { + var ( + bookmarkRepository bookmarks.MockBookmarksRepository + ) + + u := user.User{ + UUID: "user-uuid-1", + } + + b := bookmark.Bookmark{ + UUID: "bookmark-uuid", + Title: "bookmark-title", + ObjectUUID: "object-uuid", + ObjectType: "article", + OwnerUUID: u.UUID, + CreatedAt: time.Now(), + } + + bookmarkRepository.On("GetByOwnerUUID", b.OwnerUUID, b.ObjectType, b.ObjectUUID).Once().Return(bookmark.Bookmark{}, errors.New("something went wrong")) + defer bookmarkRepository.AssertExpectations(t) + + handler := NewExistsHandler(bookmarkExists.NewUseCase(&bookmarkRepository)) + + r := bookmarkExists.Request{ + ObjectType: b.ObjectType, + ObjectUUID: b.ObjectUUID, + } + + var paylaod bytes.Buffer + + err := json.NewEncoder(&paylaod).Encode(r) + assert.NoError(t, err) + + request := httptest.NewRequest(http.MethodPost, "/", &paylaod) + request = request.WithContext(auth.ToContext(request.Context(), &u)) + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + assert.Len(t, response.Body.Bytes(), 0) + assert.Equal(t, http.StatusInternalServerError, response.Code) + }) +} diff --git a/backend/presentation/http/api/bookmark/testdata/bookmark-exists-response.json b/backend/presentation/http/api/bookmark/testdata/bookmark-exists-response.json new file mode 100644 index 00000000..1495a8f0 --- /dev/null +++ b/backend/presentation/http/api/bookmark/testdata/bookmark-exists-response.json @@ -0,0 +1,3 @@ +{ + "exist": true +} diff --git a/backend/presentation/http/api/bookmark/testdata/bookmark-exists-validation-response.json b/backend/presentation/http/api/bookmark/testdata/bookmark-exists-validation-response.json new file mode 100644 index 00000000..5e303991 --- /dev/null +++ b/backend/presentation/http/api/bookmark/testdata/bookmark-exists-validation-response.json @@ -0,0 +1,7 @@ +{ + "errors": { + "object_type": "object type is not supported", + "object_uuid": "object uuid is required" + }, + "exist": false +} \ No newline at end of file diff --git a/backend/presentation/http/api/bookmark/testdata/bookmark-update-validation-response.json b/backend/presentation/http/api/bookmark/testdata/bookmark-update-validation-response.json new file mode 100644 index 00000000..f0c347a9 --- /dev/null +++ b/backend/presentation/http/api/bookmark/testdata/bookmark-update-validation-response.json @@ -0,0 +1,7 @@ +{ + "errors": { + "object_type": "object type is not supported", + "object_uuid": "object uuid is required", + "title": "title is required" + } +} \ No newline at end of file diff --git a/backend/presentation/http/api/bookmark/update.go b/backend/presentation/http/api/bookmark/update.go index 4a41a004..99f629bd 100644 --- a/backend/presentation/http/api/bookmark/update.go +++ b/backend/presentation/http/api/bookmark/update.go @@ -28,10 +28,11 @@ func (h *updateHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.useCase.Execute(&request) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) case len(response.ValidationErrors) > 0: + rw.Header().Add("Content-Type", "application/json") rw.WriteHeader(http.StatusBadRequest) json.NewEncoder(rw).Encode(response) default: diff --git a/backend/presentation/http/api/bookmark/update_test.go b/backend/presentation/http/api/bookmark/update_test.go new file mode 100644 index 00000000..dd5f4ed1 --- /dev/null +++ b/backend/presentation/http/api/bookmark/update_test.go @@ -0,0 +1,134 @@ +package bookmark + +import ( + "bytes" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "os" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/khanzadimahdi/testproject/application/auth" + "github.com/khanzadimahdi/testproject/application/bookmark/updateBookmark" + "github.com/khanzadimahdi/testproject/domain/bookmark" + "github.com/khanzadimahdi/testproject/domain/user" + "github.com/khanzadimahdi/testproject/infrastructure/repository/mocks/bookmarks" +) + +func TestUpdateHandler(t *testing.T) { + t.Run("update", func(t *testing.T) { + var ( + bookmarkRepository bookmarks.MockBookmarksRepository + ) + + u := user.User{ + UUID: "user-uuid-1", + } + + b := bookmark.Bookmark{ + Title: "test title", + ObjectUUID: "object-uuid-1", + ObjectType: "article", + OwnerUUID: u.UUID, + } + + bookmarkRepository.On("DeleteByOwnerUUID", b.OwnerUUID, b.ObjectType, b.ObjectUUID).Once().Return(nil) + defer bookmarkRepository.AssertExpectations(t) + + r := updateBookmark.Request{ + Keep: false, + Title: b.Title, + ObjectType: b.ObjectType, + ObjectUUID: b.ObjectUUID, + } + + var payload bytes.Buffer + + err := json.NewEncoder(&payload).Encode(r) + assert.NoError(t, err) + + request := httptest.NewRequest(http.MethodPost, "/", &payload) + request = request.WithContext(auth.ToContext(request.Context(), &u)) + response := httptest.NewRecorder() + + handler := NewUpdateHandler(updateBookmark.NewUseCase(&bookmarkRepository)) + + handler.ServeHTTP(response, request) + + assert.Len(t, response.Body.Bytes(), 0) + assert.Equal(t, http.StatusCreated, response.Code) + }) + + t.Run("validation failed", func(t *testing.T) { + var ( + bookmarkRepository bookmarks.MockBookmarksRepository + ) + + u := user.User{ + UUID: "user-uuid-1", + } + + request := httptest.NewRequest(http.MethodPost, "/", bytes.NewBufferString("{}")) + request = request.WithContext(auth.ToContext(request.Context(), &u)) + response := httptest.NewRecorder() + + handler := NewUpdateHandler(updateBookmark.NewUseCase(&bookmarkRepository)) + + handler.ServeHTTP(response, request) + + bookmarkRepository.AssertNotCalled(t, "DeleteByOwnerUUID") + + expected, err := os.ReadFile("testdata/bookmark-update-validation-response.json") + assert.NoError(t, err) + + assert.Equal(t, "application/json", response.Header().Get("content-type")) + assert.JSONEq(t, string(expected), response.Body.String()) + assert.Equal(t, http.StatusBadRequest, response.Code) + }) + + t.Run("error", func(t *testing.T) { + var ( + bookmarkRepository bookmarks.MockBookmarksRepository + ) + + u := user.User{ + UUID: "user-uuid-1", + } + + b := bookmark.Bookmark{ + Title: "test title", + ObjectUUID: "object-uuid-1", + ObjectType: "article", + OwnerUUID: u.UUID, + } + + bookmarkRepository.On("DeleteByOwnerUUID", b.OwnerUUID, b.ObjectType, b.ObjectUUID).Once().Return(errors.New("something wrong has happened")) + defer bookmarkRepository.AssertExpectations(t) + + r := updateBookmark.Request{ + Keep: false, + Title: b.Title, + ObjectType: b.ObjectType, + ObjectUUID: b.ObjectUUID, + } + + var payload bytes.Buffer + + err := json.NewEncoder(&payload).Encode(r) + assert.NoError(t, err) + + request := httptest.NewRequest(http.MethodPost, "/", &payload) + request = request.WithContext(auth.ToContext(request.Context(), &u)) + response := httptest.NewRecorder() + + handler := NewUpdateHandler(updateBookmark.NewUseCase(&bookmarkRepository)) + + handler.ServeHTTP(response, request) + + assert.Len(t, response.Body.Bytes(), 0) + assert.Equal(t, http.StatusInternalServerError, response.Code) + }) +} diff --git a/backend/presentation/http/api/comment/create.go b/backend/presentation/http/api/comment/create.go index 156d0253..3606a8f6 100644 --- a/backend/presentation/http/api/comment/create.go +++ b/backend/presentation/http/api/comment/create.go @@ -2,21 +2,19 @@ package comment import ( "encoding/json" - "errors" "net/http" "github.com/khanzadimahdi/testproject/application/auth" "github.com/khanzadimahdi/testproject/application/comment/createComment" - "github.com/khanzadimahdi/testproject/domain" ) type createHandler struct { - createCommentUseCase *createComment.UseCase + useCase *createComment.UseCase } -func NewCreateHandler(createCommentUseCase *createComment.UseCase) *createHandler { +func NewCreateHandler(useCase *createComment.UseCase) *createHandler { return &createHandler{ - createCommentUseCase: createCommentUseCase, + useCase: useCase, } } @@ -28,14 +26,13 @@ func (h *createHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } request.AuthorUUID = auth.FromContext(r.Context()).UUID - response, err := h.createCommentUseCase.Execute(request) + response, err := h.useCase.Execute(request) - switch true { - case errors.Is(err, domain.ErrNotExists): - rw.WriteHeader(http.StatusNotFound) + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) case len(response.ValidationErrors) > 0: + rw.Header().Add("Content-Type", "application/json") rw.WriteHeader(http.StatusBadRequest) json.NewEncoder(rw).Encode(response) default: diff --git a/backend/presentation/http/api/comment/create_test.go b/backend/presentation/http/api/comment/create_test.go new file mode 100644 index 00000000..aaf94f2f --- /dev/null +++ b/backend/presentation/http/api/comment/create_test.go @@ -0,0 +1,139 @@ +package comment + +import ( + "bytes" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "os" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/khanzadimahdi/testproject/application/auth" + "github.com/khanzadimahdi/testproject/application/comment/createComment" + "github.com/khanzadimahdi/testproject/domain/author" + "github.com/khanzadimahdi/testproject/domain/comment" + "github.com/khanzadimahdi/testproject/domain/user" + "github.com/khanzadimahdi/testproject/infrastructure/repository/mocks/comments" +) + +func TestCreateHandler(t *testing.T) { + t.Run("creates a comment", func(t *testing.T) { + var ( + commentRepository comments.MockCommentsRepository + ) + + u := user.User{UUID: "auth-user-uuid"} + c := comment.Comment{ + Body: "this is a test body", + ParentUUID: "parent-uuid-1", + ObjectUUID: "object-uuid-test", + ObjectType: "article", + Author: author.Author{ + UUID: u.UUID, + }, + } + + commentRepository.On("Save", &c).Once().Return(c.UUID, nil) + defer commentRepository.AssertExpectations(t) + + handler := NewCreateHandler(createComment.NewUseCase(&commentRepository)) + + body := createComment.Request{ + Body: c.Body, + ParentUUID: c.ParentUUID, + ObjectUUID: c.ObjectUUID, + ObjectType: c.ObjectType, + } + + var payload bytes.Buffer + err := json.NewEncoder(&payload).Encode(body) + assert.NoError(t, err) + + request := httptest.NewRequest(http.MethodPost, "/", &payload) + request = request.WithContext(auth.ToContext(request.Context(), &u)) + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + assert.Len(t, response.Body.Bytes(), 0) + assert.Equal(t, http.StatusCreated, response.Code) + }) + + t.Run("validation failed", func(t *testing.T) { + var ( + commentRepository comments.MockCommentsRepository + ) + + u := user.User{UUID: "auth-user-uuid"} + + handler := NewCreateHandler(createComment.NewUseCase(&commentRepository)) + + body := createComment.Request{ + ObjectType: "some test type", + } + + var payload bytes.Buffer + err := json.NewEncoder(&payload).Encode(body) + assert.NoError(t, err) + + request := httptest.NewRequest(http.MethodPost, "/", &payload) + request = request.WithContext(auth.ToContext(request.Context(), &u)) + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + commentRepository.AssertNotCalled(t, "Save") + + expected, err := os.ReadFile("testdata/create-comment-validation-errors.json") + assert.NoError(t, err) + + assert.Equal(t, "application/json", response.Header().Get("content-type")) + assert.JSONEq(t, string(expected), response.Body.String()) + assert.Equal(t, http.StatusBadRequest, response.Code) + }) + + t.Run("error", func(t *testing.T) { + var ( + commentRepository comments.MockCommentsRepository + ) + + u := user.User{UUID: "auth-user-uuid"} + c := comment.Comment{ + Body: "this is a test body", + ParentUUID: "parent-uuid-1", + ObjectUUID: "object-uuid-test", + ObjectType: "article", + Author: author.Author{ + UUID: u.UUID, + }, + } + + commentRepository.On("Save", &c).Once().Return(c.UUID, errors.New("some unwanted error")) + defer commentRepository.AssertExpectations(t) + + handler := NewCreateHandler(createComment.NewUseCase(&commentRepository)) + + body := createComment.Request{ + Body: c.Body, + ParentUUID: c.ParentUUID, + ObjectUUID: c.ObjectUUID, + ObjectType: c.ObjectType, + } + + var payload bytes.Buffer + err := json.NewEncoder(&payload).Encode(body) + assert.NoError(t, err) + + request := httptest.NewRequest(http.MethodPost, "/", &payload) + request = request.WithContext(auth.ToContext(request.Context(), &u)) + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + assert.Len(t, response.Body.Bytes(), 0) + assert.Equal(t, http.StatusInternalServerError, response.Code) + }) +} diff --git a/backend/presentation/http/api/comment/index.go b/backend/presentation/http/api/comment/index.go index 2f469761..084bd661 100644 --- a/backend/presentation/http/api/comment/index.go +++ b/backend/presentation/http/api/comment/index.go @@ -45,7 +45,7 @@ func (h *indexHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.getCommentsUseCase.Execute(request) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/comment/index_test.go b/backend/presentation/http/api/comment/index_test.go new file mode 100644 index 00000000..fade5d58 --- /dev/null +++ b/backend/presentation/http/api/comment/index_test.go @@ -0,0 +1,180 @@ +package comment + +import ( + "errors" + "fmt" + "net/http" + "net/http/httptest" + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" + + "github.com/khanzadimahdi/testproject/application/comment/getComments" + "github.com/khanzadimahdi/testproject/domain/author" + "github.com/khanzadimahdi/testproject/domain/comment" + "github.com/khanzadimahdi/testproject/domain/user" + "github.com/khanzadimahdi/testproject/infrastructure/repository/mocks/comments" + "github.com/khanzadimahdi/testproject/infrastructure/repository/mocks/users" +) + +func TestIndexHandler(t *testing.T) { + t.Run("show comments", func(t *testing.T) { + var ( + commentsRepository comments.MockCommentsRepository + usersRepository users.MockUsersRepository + ) + + data := getComments.Request{ + Page: 1, + ObjectUUID: "test-uuid", + ObjectType: "test-type", + } + + u := []user.User{ + { + UUID: "user-uuid-1", + Name: "user-name-1", + }, + { + UUID: "user-uuid-2", + Name: "user-name-2", + Avatar: "user-avatar-2", + }, + } + + now, err := time.Parse(time.RFC3339, "2024-10-02T20:19:16Z") + assert.NoError(t, err) + + c := []comment.Comment{ + { + UUID: "comment-uuid-1", + Body: "comment-body-1", + Author: author.Author{ + UUID: u[0].UUID, + Name: u[0].Name, + Avatar: u[0].Avatar, + }, + ObjectUUID: data.ObjectUUID, + ObjectType: data.ObjectType, + ApprovedAt: now, + CreatedAt: now, + }, + { + UUID: "comment-uuid-2", + Body: "comment-body-2", + Author: author.Author{ + UUID: u[1].UUID, + Name: u[1].Name, + Avatar: u[1].Avatar, + }, + ObjectUUID: data.ObjectUUID, + ObjectType: data.ObjectType, + ApprovedAt: now, + CreatedAt: now, + }, + { + UUID: "comment-uuid-3", + Body: "comment-body-3", + Author: author.Author{ + UUID: u[1].UUID, + Name: u[1].Name, + Avatar: u[1].Avatar, + }, + ParentUUID: "comment-uuid-1", + ObjectUUID: data.ObjectUUID, + ObjectType: data.ObjectType, + ApprovedAt: now, + CreatedAt: now, + }, + } + + commentsRepository.On("CountApprovedByObjectUUID", data.ObjectType, data.ObjectUUID).Once().Return(uint(len(c)), nil) + commentsRepository.On("GetApprovedByObjectUUID", data.ObjectType, data.ObjectUUID, uint(0), uint(10)).Once().Return(c, nil) + defer commentsRepository.AssertExpectations(t) + + usersRepository.On("GetByUUIDs", []string{u[0].UUID, u[1].UUID, u[1].UUID}).Once().Return(u, nil) + defer usersRepository.AssertExpectations(t) + + handler := NewIndexHandler(getComments.NewUseCase(&commentsRepository, &usersRepository)) + + url := fmt.Sprintf("/?object_uuid=%s&object_type=%s&page=%d", data.ObjectUUID, data.ObjectType, data.Page) + request := httptest.NewRequest(http.MethodGet, url, nil) + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + expectedBody, err := os.ReadFile("testdata/show-comments.json") + assert.NoError(t, err) + + assert.Equal(t, "application/json", response.Header().Get("content-type")) + assert.JSONEq(t, string(expectedBody), response.Body.String()) + assert.Equal(t, http.StatusOK, response.Code) + }) + + t.Run("no data", func(t *testing.T) { + var ( + commentsRepository comments.MockCommentsRepository + usersRepository users.MockUsersRepository + ) + + data := getComments.Request{ + Page: 1, + ObjectUUID: "test-uuid", + ObjectType: "test-type", + } + + commentsRepository.On("CountApprovedByObjectUUID", data.ObjectType, data.ObjectUUID).Once().Return(uint(0), nil) + commentsRepository.On("GetApprovedByObjectUUID", data.ObjectType, data.ObjectUUID, uint(0), uint(10)).Once().Return(nil, nil) + defer commentsRepository.AssertExpectations(t) + + usersRepository.On("GetByUUIDs", []string{}).Once().Return(nil, nil) + defer usersRepository.AssertExpectations(t) + + handler := NewIndexHandler(getComments.NewUseCase(&commentsRepository, &usersRepository)) + + url := fmt.Sprintf("/?object_uuid=%s&object_type=%s&page=%d", data.ObjectUUID, data.ObjectType, data.Page) + request := httptest.NewRequest(http.MethodGet, url, nil) + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + expectedBody, err := os.ReadFile("testdata/show-comments-no-data.json") + assert.NoError(t, err) + + assert.Equal(t, "application/json", response.Header().Get("content-type")) + assert.JSONEq(t, string(expectedBody), response.Body.String()) + assert.Equal(t, http.StatusOK, response.Code) + }) + + t.Run("error", func(t *testing.T) { + var ( + commentsRepository comments.MockCommentsRepository + usersRepository users.MockUsersRepository + ) + + data := getComments.Request{ + Page: 1, + ObjectUUID: "test-uuid", + ObjectType: "test-type", + } + + commentsRepository.On("CountApprovedByObjectUUID", data.ObjectType, data.ObjectUUID).Once().Return(uint(0), errors.New("something doesn't work")) + defer commentsRepository.AssertExpectations(t) + + handler := NewIndexHandler(getComments.NewUseCase(&commentsRepository, &usersRepository)) + + url := fmt.Sprintf("/?object_uuid=%s&object_type=%s&page=%d", data.ObjectUUID, data.ObjectType, data.Page) + request := httptest.NewRequest(http.MethodGet, url, nil) + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + commentsRepository.AssertNotCalled(t, "GetApprovedByObjectUUID") + usersRepository.AssertNotCalled(t, "GetByUUIDs") + + assert.Len(t, response.Body.Bytes(), 0) + assert.Equal(t, http.StatusInternalServerError, response.Code) + }) +} diff --git a/backend/presentation/http/api/comment/testdata/create-comment-validation-errors.json b/backend/presentation/http/api/comment/testdata/create-comment-validation-errors.json new file mode 100644 index 00000000..2c55a9ac --- /dev/null +++ b/backend/presentation/http/api/comment/testdata/create-comment-validation-errors.json @@ -0,0 +1,7 @@ +{ + "errors": { + "body": "body is required", + "object_type": "object type is not supported", + "object_uuid": "object_uuid is required" + } +} \ No newline at end of file diff --git a/backend/presentation/http/api/comment/testdata/show-comments-no-data.json b/backend/presentation/http/api/comment/testdata/show-comments-no-data.json new file mode 100644 index 00000000..43936acd --- /dev/null +++ b/backend/presentation/http/api/comment/testdata/show-comments-no-data.json @@ -0,0 +1,7 @@ +{ + "items": [], + "pagination": { + "total_pages": 0, + "current_page": 1 + } +} \ No newline at end of file diff --git a/backend/presentation/http/api/comment/testdata/show-comments.json b/backend/presentation/http/api/comment/testdata/show-comments.json new file mode 100644 index 00000000..bec75e15 --- /dev/null +++ b/backend/presentation/http/api/comment/testdata/show-comments.json @@ -0,0 +1,39 @@ +{ + "items": [ + { + "uuid": "comment-uuid-1", + "body": "comment-body-1", + "author": { + "uuid": "user-uuid-1", + "name": "user-name-1", + "avatar": "" + }, + "created_at": "2024-10-02T20:19:16Z" + }, + { + "uuid": "comment-uuid-2", + "body": "comment-body-2", + "author": { + "uuid": "user-uuid-2", + "name": "user-name-2", + "avatar": "user-avatar-2" + }, + "created_at": "2024-10-02T20:19:16Z" + }, + { + "uuid": "comment-uuid-3", + "body": "comment-body-3", + "author": { + "uuid": "user-uuid-2", + "name": "user-name-2", + "avatar": "user-avatar-2" + }, + "parent_uuid": "comment-uuid-1", + "created_at": "2024-10-02T20:19:16Z" + } + ], + "pagination": { + "total_pages": 1, + "current_page": 1 + } +} \ No newline at end of file diff --git a/backend/presentation/http/api/dashboard/article/create.go b/backend/presentation/http/api/dashboard/article/create.go index 4c8ae766..865d2eb8 100644 --- a/backend/presentation/http/api/dashboard/article/create.go +++ b/backend/presentation/http/api/dashboard/article/create.go @@ -42,7 +42,7 @@ func (h *createHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.createArticleUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/article/create_test.go b/backend/presentation/http/api/dashboard/article/create_test.go new file mode 100644 index 00000000..d0cb435c --- /dev/null +++ b/backend/presentation/http/api/dashboard/article/create_test.go @@ -0,0 +1,7 @@ +package article + +import "testing" + +func TestCreateHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/article/delete.go b/backend/presentation/http/api/dashboard/article/delete.go index 4e465f44..c73a7003 100644 --- a/backend/presentation/http/api/dashboard/article/delete.go +++ b/backend/presentation/http/api/dashboard/article/delete.go @@ -38,7 +38,7 @@ func (h *deleteHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } err := h.deleteArticleUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/article/delete_test.go b/backend/presentation/http/api/dashboard/article/delete_test.go new file mode 100644 index 00000000..83dcafa9 --- /dev/null +++ b/backend/presentation/http/api/dashboard/article/delete_test.go @@ -0,0 +1,7 @@ +package article + +import "testing" + +func TestDeleteHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/article/index.go b/backend/presentation/http/api/dashboard/article/index.go index c782ed44..493a9ade 100644 --- a/backend/presentation/http/api/dashboard/article/index.go +++ b/backend/presentation/http/api/dashboard/article/index.go @@ -47,7 +47,7 @@ func (h *indexHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.getArticlesUseCase.Execute(request) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/dashboard/article/index_test.go b/backend/presentation/http/api/dashboard/article/index_test.go new file mode 100644 index 00000000..ac40a625 --- /dev/null +++ b/backend/presentation/http/api/dashboard/article/index_test.go @@ -0,0 +1,7 @@ +package article + +import "testing" + +func TestIndexHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/article/show.go b/backend/presentation/http/api/dashboard/article/show.go index aef0dd9b..418fd070 100644 --- a/backend/presentation/http/api/dashboard/article/show.go +++ b/backend/presentation/http/api/dashboard/article/show.go @@ -37,7 +37,7 @@ func (h *showHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.getArticleUseCase.Execute(UUID) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/article/show_test.go b/backend/presentation/http/api/dashboard/article/show_test.go new file mode 100644 index 00000000..ae4e2564 --- /dev/null +++ b/backend/presentation/http/api/dashboard/article/show_test.go @@ -0,0 +1,7 @@ +package article + +import "testing" + +func TestShowHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/article/update.go b/backend/presentation/http/api/dashboard/article/update.go index faf002ba..c8f7c699 100644 --- a/backend/presentation/http/api/dashboard/article/update.go +++ b/backend/presentation/http/api/dashboard/article/update.go @@ -41,7 +41,7 @@ func (h *updateHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { request.AuthorUUID = userUUID response, err := h.updateArticleUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/article/update_test.go b/backend/presentation/http/api/dashboard/article/update_test.go new file mode 100644 index 00000000..e8004ba2 --- /dev/null +++ b/backend/presentation/http/api/dashboard/article/update_test.go @@ -0,0 +1,7 @@ +package article + +import "testing" + +func TestUpdateHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/bookmark/deleteUser.go b/backend/presentation/http/api/dashboard/bookmark/deleteUser.go index 95f7a7cb..0d137576 100644 --- a/backend/presentation/http/api/dashboard/bookmark/deleteUser.go +++ b/backend/presentation/http/api/dashboard/bookmark/deleteUser.go @@ -1,4 +1,4 @@ -package comment +package bookmark import ( "encoding/json" @@ -41,7 +41,7 @@ func (h *deleteUserHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { request.OwnerUUID = auth.FromContext(r.Context()).UUID err := h.useCase.Execute(&request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/bookmark/deleteUser_test.go b/backend/presentation/http/api/dashboard/bookmark/deleteUser_test.go new file mode 100644 index 00000000..57a9ba8e --- /dev/null +++ b/backend/presentation/http/api/dashboard/bookmark/deleteUser_test.go @@ -0,0 +1,7 @@ +package bookmark + +import "testing" + +func TestDeleteUserHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/bookmark/indexUser.go b/backend/presentation/http/api/dashboard/bookmark/indexUser.go index 14241734..aa98afc7 100644 --- a/backend/presentation/http/api/dashboard/bookmark/indexUser.go +++ b/backend/presentation/http/api/dashboard/bookmark/indexUser.go @@ -1,4 +1,4 @@ -package comment +package bookmark import ( "encoding/json" @@ -48,7 +48,7 @@ func (h *indexUserHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.useCase.Execute(request) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/dashboard/bookmark/indexUser_test.go b/backend/presentation/http/api/dashboard/bookmark/indexUser_test.go new file mode 100644 index 00000000..659d4ea9 --- /dev/null +++ b/backend/presentation/http/api/dashboard/bookmark/indexUser_test.go @@ -0,0 +1,7 @@ +package bookmark + +import "testing" + +func TestIndexUserHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/comment/create.go b/backend/presentation/http/api/dashboard/comment/create.go index 08f4a46a..22ad39d1 100644 --- a/backend/presentation/http/api/dashboard/comment/create.go +++ b/backend/presentation/http/api/dashboard/comment/create.go @@ -42,7 +42,7 @@ func (h *createHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.createCommentUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/comment/create_test.go b/backend/presentation/http/api/dashboard/comment/create_test.go new file mode 100644 index 00000000..bfae7635 --- /dev/null +++ b/backend/presentation/http/api/dashboard/comment/create_test.go @@ -0,0 +1,7 @@ +package comment + +import "testing" + +func TestCreateHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/comment/delete.go b/backend/presentation/http/api/dashboard/comment/delete.go index 4285136c..b8cb83d5 100644 --- a/backend/presentation/http/api/dashboard/comment/delete.go +++ b/backend/presentation/http/api/dashboard/comment/delete.go @@ -39,7 +39,7 @@ func (h *deleteHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } err := h.deleteCommentUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/comment/deleteUser.go b/backend/presentation/http/api/dashboard/comment/deleteUser.go index a143e462..3c64f867 100644 --- a/backend/presentation/http/api/dashboard/comment/deleteUser.go +++ b/backend/presentation/http/api/dashboard/comment/deleteUser.go @@ -40,7 +40,7 @@ func (h *deleteUserHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } err := h.useCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/comment/deleteUser_test.go b/backend/presentation/http/api/dashboard/comment/deleteUser_test.go new file mode 100644 index 00000000..f2596c8f --- /dev/null +++ b/backend/presentation/http/api/dashboard/comment/deleteUser_test.go @@ -0,0 +1,7 @@ +package comment + +import "testing" + +func TestDeleteUserHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/comment/delete_test.go b/backend/presentation/http/api/dashboard/comment/delete_test.go new file mode 100644 index 00000000..afd4b467 --- /dev/null +++ b/backend/presentation/http/api/dashboard/comment/delete_test.go @@ -0,0 +1,7 @@ +package comment + +import "testing" + +func TestDeleteHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/comment/index.go b/backend/presentation/http/api/dashboard/comment/index.go index 149fe1c1..8b1f0f23 100644 --- a/backend/presentation/http/api/dashboard/comment/index.go +++ b/backend/presentation/http/api/dashboard/comment/index.go @@ -47,7 +47,7 @@ func (h *indexHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.getCommentsUseCase.Execute(request) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/dashboard/comment/indexUser.go b/backend/presentation/http/api/dashboard/comment/indexUser.go index 2121e215..e9cc2ad3 100644 --- a/backend/presentation/http/api/dashboard/comment/indexUser.go +++ b/backend/presentation/http/api/dashboard/comment/indexUser.go @@ -48,7 +48,7 @@ func (h *indexUserHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.useCase.Execute(request) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/dashboard/comment/indexUser_test.go b/backend/presentation/http/api/dashboard/comment/indexUser_test.go new file mode 100644 index 00000000..c4de3c52 --- /dev/null +++ b/backend/presentation/http/api/dashboard/comment/indexUser_test.go @@ -0,0 +1,7 @@ +package comment + +import "testing" + +func TestIndexUserHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/comment/show.go b/backend/presentation/http/api/dashboard/comment/show.go index d2218c6b..98f1c8cf 100644 --- a/backend/presentation/http/api/dashboard/comment/show.go +++ b/backend/presentation/http/api/dashboard/comment/show.go @@ -37,7 +37,7 @@ func (h *showHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.getCommentUseCase.Execute(UUID) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/comment/showUser.go b/backend/presentation/http/api/dashboard/comment/showUser.go index b80f9db3..d21c7ea7 100644 --- a/backend/presentation/http/api/dashboard/comment/showUser.go +++ b/backend/presentation/http/api/dashboard/comment/showUser.go @@ -37,7 +37,7 @@ func (h *showUserHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.useCase.Execute(UUID, userUUID) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/comment/showUser_test.go b/backend/presentation/http/api/dashboard/comment/showUser_test.go new file mode 100644 index 00000000..b84733fd --- /dev/null +++ b/backend/presentation/http/api/dashboard/comment/showUser_test.go @@ -0,0 +1,7 @@ +package comment + +import "testing" + +func TestShowUserHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/comment/update.go b/backend/presentation/http/api/dashboard/comment/update.go index 76f9cbb3..154271c6 100644 --- a/backend/presentation/http/api/dashboard/comment/update.go +++ b/backend/presentation/http/api/dashboard/comment/update.go @@ -41,7 +41,7 @@ func (h *updateHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { request.AuthorUUID = userUUID response, err := h.updateCommentUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/comment/updateUser.go b/backend/presentation/http/api/dashboard/comment/updateUser.go index 8e8cc9df..c6f3add5 100644 --- a/backend/presentation/http/api/dashboard/comment/updateUser.go +++ b/backend/presentation/http/api/dashboard/comment/updateUser.go @@ -41,7 +41,7 @@ func (h *updateUserHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { request.UserUUID = userUUID response, err := h.useCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/comment/updateUser_test.go b/backend/presentation/http/api/dashboard/comment/updateUser_test.go new file mode 100644 index 00000000..a37ca87d --- /dev/null +++ b/backend/presentation/http/api/dashboard/comment/updateUser_test.go @@ -0,0 +1,7 @@ +package comment + +import "testing" + +func TestUpdateUserHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/comment/update_test.go b/backend/presentation/http/api/dashboard/comment/update_test.go new file mode 100644 index 00000000..2acafff9 --- /dev/null +++ b/backend/presentation/http/api/dashboard/comment/update_test.go @@ -0,0 +1,7 @@ +package comment + +import "testing" + +func TestUpdateHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/config/show.go b/backend/presentation/http/api/dashboard/config/show.go index aefe52c5..850482fa 100644 --- a/backend/presentation/http/api/dashboard/config/show.go +++ b/backend/presentation/http/api/dashboard/config/show.go @@ -34,7 +34,7 @@ func (h *showHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.useCase.Execute() - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/dashboard/config/show_test.go b/backend/presentation/http/api/dashboard/config/show_test.go new file mode 100644 index 00000000..b0a06aff --- /dev/null +++ b/backend/presentation/http/api/dashboard/config/show_test.go @@ -0,0 +1,7 @@ +package config + +import "testing" + +func TestShowHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/config/update.go b/backend/presentation/http/api/dashboard/config/update.go index 9114a90e..9b5db8f0 100644 --- a/backend/presentation/http/api/dashboard/config/update.go +++ b/backend/presentation/http/api/dashboard/config/update.go @@ -39,7 +39,7 @@ func (h *updateHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.useCase.Execute(&request) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) case len(response.ValidationErrors) > 0: diff --git a/backend/presentation/http/api/dashboard/config/update_test.go b/backend/presentation/http/api/dashboard/config/update_test.go new file mode 100644 index 00000000..1e72238a --- /dev/null +++ b/backend/presentation/http/api/dashboard/config/update_test.go @@ -0,0 +1,7 @@ +package config + +import "testing" + +func TestUpdateHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/element/create.go b/backend/presentation/http/api/dashboard/element/create.go index 533a612d..546f36f2 100644 --- a/backend/presentation/http/api/dashboard/element/create.go +++ b/backend/presentation/http/api/dashboard/element/create.go @@ -41,7 +41,7 @@ func (h *createHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.createElementUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/element/create_test.go b/backend/presentation/http/api/dashboard/element/create_test.go new file mode 100644 index 00000000..268d3e0d --- /dev/null +++ b/backend/presentation/http/api/dashboard/element/create_test.go @@ -0,0 +1,7 @@ +package element + +import "testing" + +func TestCreateHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/element/delete.go b/backend/presentation/http/api/dashboard/element/delete.go index 5594fb3f..7ba1c374 100644 --- a/backend/presentation/http/api/dashboard/element/delete.go +++ b/backend/presentation/http/api/dashboard/element/delete.go @@ -38,7 +38,7 @@ func (h *deleteHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } err := h.deleteElementUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/element/delete_test.go b/backend/presentation/http/api/dashboard/element/delete_test.go new file mode 100644 index 00000000..5b5a472e --- /dev/null +++ b/backend/presentation/http/api/dashboard/element/delete_test.go @@ -0,0 +1,7 @@ +package element + +import "testing" + +func TestDeleteHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/element/index.go b/backend/presentation/http/api/dashboard/element/index.go index 72142b13..30833637 100644 --- a/backend/presentation/http/api/dashboard/element/index.go +++ b/backend/presentation/http/api/dashboard/element/index.go @@ -47,7 +47,7 @@ func (h *indexHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.getElementsUseCase.Execute(request) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/dashboard/element/index_test.go b/backend/presentation/http/api/dashboard/element/index_test.go new file mode 100644 index 00000000..dcb2bd46 --- /dev/null +++ b/backend/presentation/http/api/dashboard/element/index_test.go @@ -0,0 +1,7 @@ +package element + +import "testing" + +func TestIndexHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/element/show.go b/backend/presentation/http/api/dashboard/element/show.go index c7939d38..5daffc31 100644 --- a/backend/presentation/http/api/dashboard/element/show.go +++ b/backend/presentation/http/api/dashboard/element/show.go @@ -37,7 +37,7 @@ func (h *showHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.getElementUseCase.Execute(UUID) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/element/show_test.go b/backend/presentation/http/api/dashboard/element/show_test.go new file mode 100644 index 00000000..a2193473 --- /dev/null +++ b/backend/presentation/http/api/dashboard/element/show_test.go @@ -0,0 +1,7 @@ +package element + +import "testing" + +func TestShowHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/element/update.go b/backend/presentation/http/api/dashboard/element/update.go index 2b5da46d..06fc48c4 100644 --- a/backend/presentation/http/api/dashboard/element/update.go +++ b/backend/presentation/http/api/dashboard/element/update.go @@ -40,7 +40,7 @@ func (h *updateHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.updateElementUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/element/update_test.go b/backend/presentation/http/api/dashboard/element/update_test.go new file mode 100644 index 00000000..80395904 --- /dev/null +++ b/backend/presentation/http/api/dashboard/element/update_test.go @@ -0,0 +1,7 @@ +package element + +import "testing" + +func TestUpdateHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/file/delete.go b/backend/presentation/http/api/dashboard/file/delete.go index d775362c..f919e754 100644 --- a/backend/presentation/http/api/dashboard/file/delete.go +++ b/backend/presentation/http/api/dashboard/file/delete.go @@ -38,7 +38,7 @@ func (h *deleteHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { FileUUID: UUID, }) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/file/delete_test.go b/backend/presentation/http/api/dashboard/file/delete_test.go new file mode 100644 index 00000000..70a20c39 --- /dev/null +++ b/backend/presentation/http/api/dashboard/file/delete_test.go @@ -0,0 +1,7 @@ +package file + +import "testing" + +func TestDeleteHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/file/index.go b/backend/presentation/http/api/dashboard/file/index.go index 2cf2c6e6..ddd96bf8 100644 --- a/backend/presentation/http/api/dashboard/file/index.go +++ b/backend/presentation/http/api/dashboard/file/index.go @@ -47,7 +47,7 @@ func (h *indexHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.getFilesUseCase.Execute(request) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/dashboard/file/index_test.go b/backend/presentation/http/api/dashboard/file/index_test.go new file mode 100644 index 00000000..daf443e1 --- /dev/null +++ b/backend/presentation/http/api/dashboard/file/index_test.go @@ -0,0 +1,7 @@ +package file + +import "testing" + +func TestIndexHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/file/show.go b/backend/presentation/http/api/dashboard/file/show.go index ef3b309d..02e696c0 100644 --- a/backend/presentation/http/api/dashboard/file/show.go +++ b/backend/presentation/http/api/dashboard/file/show.go @@ -36,7 +36,7 @@ func (h *showHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { err := h.showFileUseCase.Execute(UUID, rw) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/file/show_test.go b/backend/presentation/http/api/dashboard/file/show_test.go new file mode 100644 index 00000000..0dfb952f --- /dev/null +++ b/backend/presentation/http/api/dashboard/file/show_test.go @@ -0,0 +1,7 @@ +package file + +import "testing" + +func TestShowHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/file/upload.go b/backend/presentation/http/api/dashboard/file/upload.go index ead57e1a..fc4d3a6b 100644 --- a/backend/presentation/http/api/dashboard/file/upload.go +++ b/backend/presentation/http/api/dashboard/file/upload.go @@ -50,7 +50,7 @@ func (h *createHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { FileReader: file, }) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) case len(response.ValidationErrors) > 0: diff --git a/backend/presentation/http/api/dashboard/file/upload_test.go b/backend/presentation/http/api/dashboard/file/upload_test.go new file mode 100644 index 00000000..20d65e78 --- /dev/null +++ b/backend/presentation/http/api/dashboard/file/upload_test.go @@ -0,0 +1,7 @@ +package file + +import "testing" + +func TestUploadHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/permission/index.go b/backend/presentation/http/api/dashboard/permission/index.go index 145258b6..fe9ed492 100644 --- a/backend/presentation/http/api/dashboard/permission/index.go +++ b/backend/presentation/http/api/dashboard/permission/index.go @@ -2,10 +2,11 @@ package permission import ( "encoding/json" + "net/http" + "github.com/khanzadimahdi/testproject/application/auth" "github.com/khanzadimahdi/testproject/domain" "github.com/khanzadimahdi/testproject/domain/permission" - "net/http" getpermissions "github.com/khanzadimahdi/testproject/application/dashboard/permission/getPermissions" ) @@ -33,7 +34,7 @@ func (h *indexHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.getPermissionsUseCase.Execute() - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/dashboard/permission/index_test.go b/backend/presentation/http/api/dashboard/permission/index_test.go new file mode 100644 index 00000000..012d4dca --- /dev/null +++ b/backend/presentation/http/api/dashboard/permission/index_test.go @@ -0,0 +1,7 @@ +package permission + +import "testing" + +func TestIndexHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/profile/changepassword.go b/backend/presentation/http/api/dashboard/profile/changepassword.go index 82c9193e..89de47e7 100644 --- a/backend/presentation/http/api/dashboard/profile/changepassword.go +++ b/backend/presentation/http/api/dashboard/profile/changepassword.go @@ -30,7 +30,7 @@ func (h *changePasswordHandler) ServeHTTP(rw http.ResponseWriter, r *http.Reques response, err := h.userCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/profile/changepassword_test.go b/backend/presentation/http/api/dashboard/profile/changepassword_test.go new file mode 100644 index 00000000..d301f145 --- /dev/null +++ b/backend/presentation/http/api/dashboard/profile/changepassword_test.go @@ -0,0 +1,7 @@ +package profile + +import "testing" + +func TestChangePasswordHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/profile/getprofile.go b/backend/presentation/http/api/dashboard/profile/getprofile.go index bd3a40ca..83b5098c 100644 --- a/backend/presentation/http/api/dashboard/profile/getprofile.go +++ b/backend/presentation/http/api/dashboard/profile/getprofile.go @@ -25,7 +25,7 @@ func (h *getProfileHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.useCase.Execute(userUUID) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/profile/getprofile_test.go b/backend/presentation/http/api/dashboard/profile/getprofile_test.go new file mode 100644 index 00000000..ab648f24 --- /dev/null +++ b/backend/presentation/http/api/dashboard/profile/getprofile_test.go @@ -0,0 +1,7 @@ +package profile + +import "testing" + +func TestGetProfileHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/profile/getroles.go b/backend/presentation/http/api/dashboard/profile/getroles.go index 7e7a2807..85c215e8 100644 --- a/backend/presentation/http/api/dashboard/profile/getroles.go +++ b/backend/presentation/http/api/dashboard/profile/getroles.go @@ -22,7 +22,7 @@ func (h *getRolesHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { userUUID := auth.FromContext(r.Context()).UUID response, err := h.getRolesUseCase.Execute(userUUID) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/dashboard/profile/getroles_test.go b/backend/presentation/http/api/dashboard/profile/getroles_test.go new file mode 100644 index 00000000..af76a0a2 --- /dev/null +++ b/backend/presentation/http/api/dashboard/profile/getroles_test.go @@ -0,0 +1,7 @@ +package profile + +import "testing" + +func TestGetRolesHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/profile/updateprofile.go b/backend/presentation/http/api/dashboard/profile/updateprofile.go index f8235992..859eea4a 100644 --- a/backend/presentation/http/api/dashboard/profile/updateprofile.go +++ b/backend/presentation/http/api/dashboard/profile/updateprofile.go @@ -30,7 +30,7 @@ func (h *updateProfileHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request response, err := h.useCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/profile/updateprofile_test.go b/backend/presentation/http/api/dashboard/profile/updateprofile_test.go new file mode 100644 index 00000000..189c5ade --- /dev/null +++ b/backend/presentation/http/api/dashboard/profile/updateprofile_test.go @@ -0,0 +1,7 @@ +package profile + +import "testing" + +func TestUpdateProfileHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/role/create.go b/backend/presentation/http/api/dashboard/role/create.go index 7532399f..ccb4c553 100644 --- a/backend/presentation/http/api/dashboard/role/create.go +++ b/backend/presentation/http/api/dashboard/role/create.go @@ -41,7 +41,7 @@ func (h *createHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.createRoleUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/role/create_test.go b/backend/presentation/http/api/dashboard/role/create_test.go new file mode 100644 index 00000000..c045d915 --- /dev/null +++ b/backend/presentation/http/api/dashboard/role/create_test.go @@ -0,0 +1,7 @@ +package role + +import "testing" + +func TestCreateHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/role/delete.go b/backend/presentation/http/api/dashboard/role/delete.go index 2173ccf7..defc2e58 100644 --- a/backend/presentation/http/api/dashboard/role/delete.go +++ b/backend/presentation/http/api/dashboard/role/delete.go @@ -39,7 +39,7 @@ func (h *deleteHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } err := h.deleteRoleUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/role/delete_test.go b/backend/presentation/http/api/dashboard/role/delete_test.go new file mode 100644 index 00000000..ecda39e8 --- /dev/null +++ b/backend/presentation/http/api/dashboard/role/delete_test.go @@ -0,0 +1,7 @@ +package role + +import "testing" + +func TestDeleteHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/role/index.go b/backend/presentation/http/api/dashboard/role/index.go index 4ac2c512..539b6f6c 100644 --- a/backend/presentation/http/api/dashboard/role/index.go +++ b/backend/presentation/http/api/dashboard/role/index.go @@ -47,7 +47,7 @@ func (h *indexHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.getRolesUseCase.Execute(request) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/dashboard/role/index_test.go b/backend/presentation/http/api/dashboard/role/index_test.go new file mode 100644 index 00000000..89c7b394 --- /dev/null +++ b/backend/presentation/http/api/dashboard/role/index_test.go @@ -0,0 +1,7 @@ +package role + +import "testing" + +func TestIndexHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/role/show.go b/backend/presentation/http/api/dashboard/role/show.go index 2fa0c062..92d9da1e 100644 --- a/backend/presentation/http/api/dashboard/role/show.go +++ b/backend/presentation/http/api/dashboard/role/show.go @@ -37,7 +37,7 @@ func (h *showHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.getRoleUseCase.Execute(UUID) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/role/show_test.go b/backend/presentation/http/api/dashboard/role/show_test.go new file mode 100644 index 00000000..0fe6e280 --- /dev/null +++ b/backend/presentation/http/api/dashboard/role/show_test.go @@ -0,0 +1,7 @@ +package role + +import "testing" + +func TestShowHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/role/update.go b/backend/presentation/http/api/dashboard/role/update.go index bf4b21fd..aa951d0a 100644 --- a/backend/presentation/http/api/dashboard/role/update.go +++ b/backend/presentation/http/api/dashboard/role/update.go @@ -40,7 +40,7 @@ func (h *updateHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.updateRoleUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/role/update_test.go b/backend/presentation/http/api/dashboard/role/update_test.go new file mode 100644 index 00000000..d434df95 --- /dev/null +++ b/backend/presentation/http/api/dashboard/role/update_test.go @@ -0,0 +1,7 @@ +package role + +import "testing" + +func TestUpdateHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/user/changepassword.go b/backend/presentation/http/api/dashboard/user/changepassword.go index c65a4171..758539d3 100644 --- a/backend/presentation/http/api/dashboard/user/changepassword.go +++ b/backend/presentation/http/api/dashboard/user/changepassword.go @@ -42,7 +42,7 @@ func (h *changePasswordHandler) ServeHTTP(rw http.ResponseWriter, r *http.Reques response, err := h.userCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/user/changepassword_test.go b/backend/presentation/http/api/dashboard/user/changepassword_test.go new file mode 100644 index 00000000..d62fdc4f --- /dev/null +++ b/backend/presentation/http/api/dashboard/user/changepassword_test.go @@ -0,0 +1,7 @@ +package user + +import "testing" + +func TestChangePasswordHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/user/create.go b/backend/presentation/http/api/dashboard/user/create.go index 429ab0c9..48401d66 100644 --- a/backend/presentation/http/api/dashboard/user/create.go +++ b/backend/presentation/http/api/dashboard/user/create.go @@ -41,7 +41,7 @@ func (h *createHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.createArticleUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/user/create_test.go b/backend/presentation/http/api/dashboard/user/create_test.go new file mode 100644 index 00000000..2483c71b --- /dev/null +++ b/backend/presentation/http/api/dashboard/user/create_test.go @@ -0,0 +1,7 @@ +package user + +import "testing" + +func TestCreateHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/user/delete.go b/backend/presentation/http/api/dashboard/user/delete.go index b33f0c4f..c9a89f63 100644 --- a/backend/presentation/http/api/dashboard/user/delete.go +++ b/backend/presentation/http/api/dashboard/user/delete.go @@ -39,7 +39,7 @@ func (h *deleteHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } err := h.deleteArticleUseCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/user/delete_test.go b/backend/presentation/http/api/dashboard/user/delete_test.go new file mode 100644 index 00000000..31d3216d --- /dev/null +++ b/backend/presentation/http/api/dashboard/user/delete_test.go @@ -0,0 +1,7 @@ +package user + +import "testing" + +func TestDeleteHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/user/index.go b/backend/presentation/http/api/dashboard/user/index.go index 3d96c029..85b6cc3f 100644 --- a/backend/presentation/http/api/dashboard/user/index.go +++ b/backend/presentation/http/api/dashboard/user/index.go @@ -47,7 +47,7 @@ func (h *indexHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } response, err := h.useCase.Execute(request) - switch true { + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/dashboard/user/index_test.go b/backend/presentation/http/api/dashboard/user/index_test.go new file mode 100644 index 00000000..a9a812fe --- /dev/null +++ b/backend/presentation/http/api/dashboard/user/index_test.go @@ -0,0 +1,7 @@ +package user + +import "testing" + +func TestIndexHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/user/show.go b/backend/presentation/http/api/dashboard/user/show.go index fd34c3e5..5ebc3d27 100644 --- a/backend/presentation/http/api/dashboard/user/show.go +++ b/backend/presentation/http/api/dashboard/user/show.go @@ -36,7 +36,7 @@ func (h *showHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { UUID := r.PathValue("uuid") response, err := h.useCase.Execute(UUID) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/user/show_test.go b/backend/presentation/http/api/dashboard/user/show_test.go new file mode 100644 index 00000000..7d948c74 --- /dev/null +++ b/backend/presentation/http/api/dashboard/user/show_test.go @@ -0,0 +1,7 @@ +package user + +import "testing" + +func TestShowHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/dashboard/user/update.go b/backend/presentation/http/api/dashboard/user/update.go index 960999bb..f025a176 100644 --- a/backend/presentation/http/api/dashboard/user/update.go +++ b/backend/presentation/http/api/dashboard/user/update.go @@ -41,7 +41,7 @@ func (h *updateHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { response, err := h.useCase.Execute(request) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/dashboard/user/update_test.go b/backend/presentation/http/api/dashboard/user/update_test.go new file mode 100644 index 00000000..653dd9e9 --- /dev/null +++ b/backend/presentation/http/api/dashboard/user/update_test.go @@ -0,0 +1,7 @@ +package user + +import "testing" + +func TestUpdateHandler(t *testing.T) { + +} diff --git a/backend/presentation/http/api/file/show.go b/backend/presentation/http/api/file/show.go index 5732f991..772d6e2a 100644 --- a/backend/presentation/http/api/file/show.go +++ b/backend/presentation/http/api/file/show.go @@ -25,7 +25,7 @@ func (h *showHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { var buffer bytes.Buffer err := h.showFileUseCase.Execute(UUID, &buffer) - switch true { + switch { case errors.Is(err, domain.ErrNotExists): rw.WriteHeader(http.StatusNotFound) case err != nil: diff --git a/backend/presentation/http/api/file/show_test.go b/backend/presentation/http/api/file/show_test.go new file mode 100644 index 00000000..c094a351 --- /dev/null +++ b/backend/presentation/http/api/file/show_test.go @@ -0,0 +1,118 @@ +package file + +import ( + "context" + "errors" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + + getfile "github.com/khanzadimahdi/testproject/application/file/getFile" + "github.com/khanzadimahdi/testproject/domain" + "github.com/khanzadimahdi/testproject/domain/file" + "github.com/khanzadimahdi/testproject/infrastructure/repository/mocks/files" + "github.com/khanzadimahdi/testproject/infrastructure/storage/mock" +) + +func TestShowHandler(t *testing.T) { + t.Run("show file", func(t *testing.T) { + var ( + filesRepository files.MockFilesRepository + storage mock.MockStorage + ) + + f := file.File{ + UUID: "file-test-uuid", + Name: "file-test-name", + } + + fileData := "this is the file payload" + reader := io.NopCloser(strings.NewReader(fileData)) + + filesRepository.On("GetOne", f.UUID).Once().Return(f, nil) + defer filesRepository.AssertExpectations(t) + + storage.On("Read", context.Background(), f.Name).Once().Return(reader, nil) + defer storage.AssertExpectations(t) + + useCase := getfile.NewUseCase(&filesRepository, &storage) + handler := NewShowHandler(useCase) + + request := httptest.NewRequest(http.MethodGet, "/", nil) + request.SetPathValue("uuid", f.UUID) + + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + assert.Equal(t, fileData, response.Body.String()) + assert.Equal(t, http.StatusOK, response.Code) + }) + + t.Run("file not found", func(t *testing.T) { + var ( + filesRepository files.MockFilesRepository + storage mock.MockStorage + ) + + f := file.File{ + UUID: "file-test-uuid", + } + + filesRepository.On("GetOne", f.UUID).Once().Return(file.File{}, domain.ErrNotExists) + defer filesRepository.AssertExpectations(t) + + useCase := getfile.NewUseCase(&filesRepository, &storage) + handler := NewShowHandler(useCase) + + request := httptest.NewRequest(http.MethodGet, "/", nil) + request.SetPathValue("uuid", f.UUID) + + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + storage.AssertNotCalled(t, "Read") + + assert.Equal(t, 0, response.Body.Len()) + assert.Equal(t, http.StatusNotFound, response.Code) + }) + + t.Run("error on reading file/writing to output", func(t *testing.T) { + var ( + filesRepository files.MockFilesRepository + storage mock.MockStorage + ) + + file := file.File{ + UUID: "file-test-uuid", + Name: "file-test-name", + } + + fileData := "this is the file payload" + reader := io.NopCloser(strings.NewReader(fileData)) + + filesRepository.On("GetOne", file.UUID).Once().Return(file, nil) + defer filesRepository.AssertExpectations(t) + + storage.On("Read", context.Background(), file.Name).Once().Return(reader, errors.New("some error")) + defer storage.AssertExpectations(t) + + useCase := getfile.NewUseCase(&filesRepository, &storage) + handler := NewShowHandler(useCase) + + request := httptest.NewRequest(http.MethodGet, "/", nil) + request.SetPathValue("uuid", file.UUID) + + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + assert.Equal(t, 0, response.Body.Len()) + assert.Equal(t, http.StatusInternalServerError, response.Code) + }) +} diff --git a/backend/presentation/http/api/hashtag/show.go b/backend/presentation/http/api/hashtag/show.go index 05c98c8c..6ca192e7 100644 --- a/backend/presentation/http/api/hashtag/show.go +++ b/backend/presentation/http/api/hashtag/show.go @@ -10,12 +10,12 @@ import ( ) type showHandler struct { - getArticlesByHashtagUseCase *getArticlesByHashtag.UseCase + useCase *getArticlesByHashtag.UseCase } -func NewShowHandler(getArticlesByHashtagUseCase *getArticlesByHashtag.UseCase) *showHandler { +func NewShowHandler(useCase *getArticlesByHashtag.UseCase) *showHandler { return &showHandler{ - getArticlesByHashtagUseCase: getArticlesByHashtagUseCase, + useCase: useCase, } } @@ -35,8 +35,9 @@ func (h *showHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { Hashtag: hashtag, } - response, err := h.getArticlesByHashtagUseCase.Execute(request) - switch true { + response, err := h.useCase.Execute(request) + + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/hashtag/show_test.go b/backend/presentation/http/api/hashtag/show_test.go new file mode 100644 index 00000000..dc7bd91e --- /dev/null +++ b/backend/presentation/http/api/hashtag/show_test.go @@ -0,0 +1,145 @@ +package hashtag + +import ( + "errors" + "net/http" + "net/http/httptest" + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" + + getArticlesByHashtag "github.com/khanzadimahdi/testproject/application/article/getArticlesByHashtag" + "github.com/khanzadimahdi/testproject/domain/article" + "github.com/khanzadimahdi/testproject/domain/author" + "github.com/khanzadimahdi/testproject/infrastructure/repository/mocks/articles" +) + +func TestShowHandler(t *testing.T) { + t.Run("show home data", func(t *testing.T) { + var ( + articlesRepository articles.MockArticlesRepository + ) + + publishedAt, err := time.Parse(time.RFC3339, "2024-09-29T15:56:25Z") + assert.NoError(t, err) + + articles := []article.Article{ + { + UUID: "article-uuid-1", + Cover: "article-cover-1", + Video: "article-video-1", + Title: "article-title-1", + Excerpt: "article-excerpt-1", + Body: "article-body-1", + PublishedAt: publishedAt, + Author: author.Author{ + UUID: "author-uuid-1", + Name: "author-name", + Avatar: "author-avatar", + }, + Tags: []string{"tag-1", "tag-2", "tag-3"}, + ViewCount: 123, + }, + { + UUID: "article-uuid-2", + Cover: "article-cover-2", + Title: "article-title-2", + Excerpt: "article-excerpt-2", + Body: "article-body-2", + Author: author.Author{ + UUID: "author-uuid-1", + Name: "author-name", + Avatar: "author-avatar", + }, + }, + { + UUID: "article-uuid-3", + Cover: "article-cover-3", + Title: "article-title-3", + Excerpt: "article-excerpt-3", + Body: "article-body-3", + Author: author.Author{ + UUID: "author-uuid-2", + Name: "author-name", + Avatar: "author-avatar", + }, + }, + } + + hashtag := "a-test-hashtag" + + articlesRepository.On("GetByHashtag", []string{hashtag}, uint(0), uint(10)).Once().Return(articles, nil) + defer articlesRepository.AssertExpectations(t) + + useCase := getArticlesByHashtag.NewUseCase(&articlesRepository) + handler := NewShowHandler(useCase) + + request := httptest.NewRequest(http.MethodGet, "/", nil) + request.SetPathValue("hashtag", hashtag) + + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + expected, err := os.ReadFile("testdata/response-01.txt") + assert.NoError(t, err) + + assert.Equal(t, "application/json", response.Header().Get("content-type")) + assert.JSONEq(t, string(expected), response.Body.String()) + assert.Equal(t, http.StatusOK, response.Code) + }) + + t.Run("no data", func(t *testing.T) { + var ( + articlesRepository articles.MockArticlesRepository + ) + + hashtag := "a-test-hashtag" + + articlesRepository.On("GetByHashtag", []string{hashtag}, uint(0), uint(10)).Once().Return(nil, nil) + defer articlesRepository.AssertExpectations(t) + + useCase := getArticlesByHashtag.NewUseCase(&articlesRepository) + handler := NewShowHandler(useCase) + + request := httptest.NewRequest(http.MethodGet, "/", nil) + request.SetPathValue("hashtag", hashtag) + + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + expected, err := os.ReadFile("testdata/response-02.txt") + assert.NoError(t, err) + + assert.Equal(t, "application/json", response.Header().Get("content-type")) + assert.JSONEq(t, string(expected), response.Body.String()) + assert.Equal(t, http.StatusOK, response.Code) + }) + + t.Run("error", func(t *testing.T) { + var ( + articlesRepository articles.MockArticlesRepository + ) + + hashtag := "a-test-hashtag" + + articlesRepository.On("GetByHashtag", []string{hashtag}, uint(0), uint(10)).Once().Return(nil, errors.New("some error happened")) + defer articlesRepository.AssertExpectations(t) + + useCase := getArticlesByHashtag.NewUseCase(&articlesRepository) + handler := NewShowHandler(useCase) + + request := httptest.NewRequest(http.MethodGet, "/", nil) + request.SetPathValue("hashtag", hashtag) + + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + assert.Len(t, response.Body.Bytes(), 0) + assert.Equal(t, http.StatusInternalServerError, response.Code) + }) +} diff --git a/backend/presentation/http/api/hashtag/testdata/response-01.txt b/backend/presentation/http/api/hashtag/testdata/response-01.txt new file mode 100644 index 00000000..0b4e0347 --- /dev/null +++ b/backend/presentation/http/api/hashtag/testdata/response-01.txt @@ -0,0 +1,43 @@ +{ + "items": [ + { + "uuid": "article-uuid-1", + "cover": "article-cover-1", + "video": "article-video-1", + "title": "article-title-1", + "excerpt": "article-excerpt-1", + "published_at": "2024-09-29T15:56:25Z", + "authorResponse": { + "name": "author-name", + "avatar": "author-avatar" + } + }, + { + "uuid": "article-uuid-2", + "cover": "article-cover-2", + "video": "", + "title": "article-title-2", + "excerpt": "article-excerpt-2", + "published_at": "0001-01-01T00:00:00Z", + "authorResponse": { + "name": "author-name", + "avatar": "author-avatar" + } + }, + { + "uuid": "article-uuid-3", + "cover": "article-cover-3", + "video": "", + "title": "article-title-3", + "excerpt": "article-excerpt-3", + "published_at": "0001-01-01T00:00:00Z", + "authorResponse": { + "name": "author-name", + "avatar": "author-avatar" + } + } + ], + "paginationResponse": { + "current_page": 1 + } +} \ No newline at end of file diff --git a/backend/presentation/http/api/hashtag/testdata/response-02.txt b/backend/presentation/http/api/hashtag/testdata/response-02.txt new file mode 100644 index 00000000..4f5e6586 --- /dev/null +++ b/backend/presentation/http/api/hashtag/testdata/response-02.txt @@ -0,0 +1,6 @@ +{ + "items": [], + "paginationResponse": { + "current_page": 1 + } +} \ No newline at end of file diff --git a/backend/presentation/http/api/home/home.go b/backend/presentation/http/api/home/home.go index 884d44a1..4ffa8e89 100644 --- a/backend/presentation/http/api/home/home.go +++ b/backend/presentation/http/api/home/home.go @@ -1,4 +1,4 @@ -package article +package home import ( "encoding/json" @@ -8,18 +8,19 @@ import ( ) type homeHandler struct { - homeUseCase *home.UseCase + useCase *home.UseCase } -func NewHomeHandler(homeUseCase *home.UseCase) *homeHandler { +func NewHomeHandler(useCase *home.UseCase) *homeHandler { return &homeHandler{ - homeUseCase: homeUseCase, + useCase: useCase, } } func (h *homeHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { - response, err := h.homeUseCase.Execute() - switch true { + response, err := h.useCase.Execute() + + switch { case err != nil: rw.WriteHeader(http.StatusInternalServerError) default: diff --git a/backend/presentation/http/api/home/home_test.go b/backend/presentation/http/api/home/home_test.go new file mode 100644 index 00000000..fd400bd2 --- /dev/null +++ b/backend/presentation/http/api/home/home_test.go @@ -0,0 +1,151 @@ +package home + +import ( + "errors" + "net/http" + "net/http/httptest" + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" + + "github.com/khanzadimahdi/testproject/application/home" + "github.com/khanzadimahdi/testproject/domain/article" + "github.com/khanzadimahdi/testproject/domain/author" + "github.com/khanzadimahdi/testproject/infrastructure/repository/mocks/articles" + "github.com/khanzadimahdi/testproject/infrastructure/repository/mocks/elements" +) + +func TestHomeHandler(t *testing.T) { + t.Run("show home data", func(t *testing.T) { + var ( + articlesRepository articles.MockArticlesRepository + elementsRepository elements.MockElementsRepository + ) + + publishedAt, err := time.Parse(time.RFC3339, "2024-09-29T15:56:25Z") + assert.NoError(t, err) + + articles := []article.Article{ + { + UUID: "article-uuid-1", + Cover: "article-cover-1", + Video: "article-video-1", + Title: "article-title-1", + Excerpt: "article-excerpt-1", + Body: "article-body-1", + PublishedAt: publishedAt, + Author: author.Author{ + UUID: "author-uuid-1", + Name: "author-name", + Avatar: "author-avatar", + }, + Tags: []string{"tag-1", "tag-2", "tag-3"}, + ViewCount: 123, + }, + { + UUID: "article-uuid-2", + Cover: "article-cover-2", + Title: "article-title-2", + Excerpt: "article-excerpt-2", + Body: "article-body-2", + Author: author.Author{ + UUID: "author-uuid-1", + Name: "author-name", + Avatar: "author-avatar", + }, + }, + { + UUID: "article-uuid-3", + Cover: "article-cover-3", + Title: "article-title-3", + Excerpt: "article-excerpt-3", + Body: "article-body-3", + Author: author.Author{ + UUID: "author-uuid-2", + Name: "author-name", + Avatar: "author-avatar", + }, + }, + } + + articlesRepository.On("GetMostViewed", uint(4)).Once().Return(articles, nil) + articlesRepository.On("GetAllPublished", uint(0), uint(3)).Once().Return(articles, nil) + articlesRepository.On("GetByUUIDs", []string{}).Once().Return(nil, nil) + defer articlesRepository.AssertExpectations(t) + + elementsRepository.On("GetByVenues", []string{"home"}).Once().Return(nil, nil) + defer elementsRepository.AssertExpectations(t) + + useCase := home.NewUseCase(&articlesRepository, &elementsRepository) + handler := NewHomeHandler(useCase) + + request := httptest.NewRequest(http.MethodGet, "/", nil) + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + expected, err := os.ReadFile("testdata/response-01.txt") + assert.NoError(t, err) + + assert.Equal(t, "application/json", response.Header().Get("content-type")) + assert.JSONEq(t, string(expected), response.Body.String()) + assert.Equal(t, http.StatusOK, response.Code) + }) + + t.Run("no data", func(t *testing.T) { + var ( + articlesRepository articles.MockArticlesRepository + elementsRepository elements.MockElementsRepository + ) + + articlesRepository.On("GetMostViewed", uint(4)).Once().Return([]article.Article{}, nil) + articlesRepository.On("GetAllPublished", uint(0), uint(3)).Once().Return([]article.Article{}, nil) + articlesRepository.On("GetByUUIDs", []string{}).Once().Return([]article.Article{}, nil) + defer articlesRepository.AssertExpectations(t) + + elementsRepository.On("GetByVenues", []string{"home"}).Once().Return(nil, nil) + defer elementsRepository.AssertExpectations(t) + + useCase := home.NewUseCase(&articlesRepository, &elementsRepository) + handler := NewHomeHandler(useCase) + + request := httptest.NewRequest(http.MethodGet, "/", nil) + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + expected, err := os.ReadFile("testdata/response-02.txt") + assert.NoError(t, err) + + assert.Equal(t, "application/json", response.Header().Get("content-type")) + assert.JSONEq(t, string(expected), response.Body.String()) + assert.Equal(t, http.StatusOK, response.Code) + }) + + t.Run("error", func(t *testing.T) { + var ( + articlesRepository articles.MockArticlesRepository + elementsRepository elements.MockElementsRepository + ) + + articlesRepository.On("GetMostViewed", uint(4)).Once().Return(nil, errors.New("an error has happened")) + defer articlesRepository.AssertExpectations(t) + + useCase := home.NewUseCase(&articlesRepository, &elementsRepository) + handler := NewHomeHandler(useCase) + + request := httptest.NewRequest(http.MethodGet, "/", nil) + response := httptest.NewRecorder() + + handler.ServeHTTP(response, request) + + articlesRepository.AssertNotCalled(t, "GetAllPublished") + articlesRepository.AssertNotCalled(t, "GetByUUIDs") + elementsRepository.AssertNotCalled(t, "GetByVenues") + + assert.Len(t, response.Body.Bytes(), 0) + assert.Equal(t, http.StatusInternalServerError, response.Code) + }) +} diff --git a/backend/presentation/http/api/home/testdata/response-01.txt b/backend/presentation/http/api/home/testdata/response-01.txt new file mode 100644 index 00000000..78e77595 --- /dev/null +++ b/backend/presentation/http/api/home/testdata/response-01.txt @@ -0,0 +1,87 @@ +{ + "all": [ + { + "uuid": "article-uuid-1", + "cover": "article-cover-1", + "title": "article-title-1", + "author": { + "name": "author-name", + "avatar": "author-avatar" + }, + "published_at": "2024-09-29T15:56:25Z", + "excerpt": "article-excerpt-1", + "tags": [ + "tag-1", + "tag-2", + "tag-3" + ] + }, + { + "uuid": "article-uuid-2", + "cover": "article-cover-2", + "title": "article-title-2", + "author": { + "name": "author-name", + "avatar": "author-avatar" + }, + "published_at": "0001-01-01T00:00:00Z", + "excerpt": "article-excerpt-2", + "tags": null + }, + { + "uuid": "article-uuid-3", + "cover": "article-cover-3", + "title": "article-title-3", + "author": { + "name": "author-name", + "avatar": "author-avatar" + }, + "published_at": "0001-01-01T00:00:00Z", + "excerpt": "article-excerpt-3", + "tags": null + } + ], + "popular": [ + { + "uuid": "article-uuid-1", + "cover": "article-cover-1", + "title": "article-title-1", + "author": { + "name": "author-name", + "avatar": "author-avatar" + }, + "published_at": "2024-09-29T15:56:25Z", + "excerpt": "article-excerpt-1", + "tags": [ + "tag-1", + "tag-2", + "tag-3" + ] + }, + { + "uuid": "article-uuid-2", + "cover": "article-cover-2", + "title": "article-title-2", + "author": { + "name": "author-name", + "avatar": "author-avatar" + }, + "published_at": "0001-01-01T00:00:00Z", + "excerpt": "article-excerpt-2", + "tags": null + }, + { + "uuid": "article-uuid-3", + "cover": "article-cover-3", + "title": "article-title-3", + "author": { + "name": "author-name", + "avatar": "author-avatar" + }, + "published_at": "0001-01-01T00:00:00Z", + "excerpt": "article-excerpt-3", + "tags": null + } + ], + "elements": [] +} \ No newline at end of file diff --git a/backend/presentation/http/api/home/testdata/response-02.txt b/backend/presentation/http/api/home/testdata/response-02.txt new file mode 100644 index 00000000..6d2891bf --- /dev/null +++ b/backend/presentation/http/api/home/testdata/response-02.txt @@ -0,0 +1,5 @@ +{ + "all": [], + "popular": [], + "elements": [] +} \ No newline at end of file diff --git a/backend/presentation/http/api/tag/show.go b/backend/presentation/http/api/tag/show.go deleted file mode 100644 index c72c1d89..00000000 --- a/backend/presentation/http/api/tag/show.go +++ /dev/null @@ -1,30 +0,0 @@ -package tag - -import ( - "encoding/json" - "net/http" - - "github.com/khanzadimahdi/testproject/application/home" -) - -type tagHandler struct { - tagUseCase *home.UseCase -} - -func NewTagHandler(tagUseCase *home.UseCase) *tagHandler { - return &tagHandler{ - tagUseCase: tagUseCase, - } -} - -func (h *tagHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { - response, err := h.tagUseCase.Execute() - switch true { - case err != nil: - rw.WriteHeader(http.StatusInternalServerError) - default: - rw.Header().Add("Content-Type", "application/json") - rw.WriteHeader(http.StatusOK) - json.NewEncoder(rw).Encode(response) - } -} diff --git a/backend/presentation/http/middleware/authorise_test.go b/backend/presentation/http/middleware/authorise_test.go new file mode 100644 index 00000000..a3975e5b --- /dev/null +++ b/backend/presentation/http/middleware/authorise_test.go @@ -0,0 +1,7 @@ +package middleware + +import "testing" + +func TestAuthorizeMiddleware(t *testing.T) { + +} diff --git a/backend/presentation/http/middleware/cors_test.go b/backend/presentation/http/middleware/cors_test.go new file mode 100644 index 00000000..8c806ac0 --- /dev/null +++ b/backend/presentation/http/middleware/cors_test.go @@ -0,0 +1,7 @@ +package middleware + +import "testing" + +func TestCorsMiddleware(t *testing.T) { + +} diff --git a/backend/presentation/http/middleware/ratelimit_test.go b/backend/presentation/http/middleware/ratelimit_test.go new file mode 100644 index 00000000..cee4279a --- /dev/null +++ b/backend/presentation/http/middleware/ratelimit_test.go @@ -0,0 +1,7 @@ +package middleware + +import "testing" + +func TestRateLimitMiddleware(t *testing.T) { + +}