Skip to content

Commit

Permalink
integration test WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
khanzadimahdi committed Oct 8, 2024
1 parent 43e2e96 commit 9f1c478
Show file tree
Hide file tree
Showing 171 changed files with 2,957 additions and 768 deletions.
3 changes: 2 additions & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ WORKDIR /dist
COPY . .
ENV GOARCH=amd64 CGO_ENABLED=0
RUN go mod download
RUN go build -v -o app ./main.go && chmod +x app
RUN go build -v -o app . && chmod +x app

FROM alpine:latest as production
COPY --from=build /dist /usr/bin
ENV GODEBUG=gctrace=1
EXPOSE 80
CMD ["app", "serve", "-port=80"]
8 changes: 4 additions & 4 deletions backend/application/article/getArticle/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Response struct {
Title string `json:"title"`
Excerpt string `json:"excerpt"`
Body string `json:"body"`
PublishedAt time.Time `json:"published_at"`
PublishedAt string `json:"published_at"`
Author authorResponse `json:"avatar"`
Tags []string `json:"tags"`
ViewCount uint `json:"view_count"`
Expand All @@ -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"`
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func NewResponse(a article.Article, e []element.Element, elementsContent []artic
Title: a.Title,
Excerpt: a.Excerpt,
Body: a.Body,
PublishedAt: a.PublishedAt,
PublishedAt: a.PublishedAt.Format(time.RFC3339),
Author: authorResponse{
Name: a.Author.Name,
Avatar: a.Author.Avatar,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions backend/application/article/getArticles/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions backend/application/article/getArticlesByHashtag/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion backend/application/auth/forgetpassword/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ func (uc *UseCase) Execute(request Request) (*Response, error) {
}

u, err := uc.userRepository.GetOneByIdentity(request.Identity)
if err != nil {
if err == domain.ErrNotExists {
return &Response{
ValidationErrors: validationErrors{
"identity": "identity (email/username) or password is wrong",
},
}, nil
} else if err != nil {
return nil, err
}

Expand Down
35 changes: 32 additions & 3 deletions backend/application/auth/forgetpassword/usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/khanzadimahdi/testproject/domain"
"github.com/khanzadimahdi/testproject/domain/user"
"github.com/khanzadimahdi/testproject/infrastructure/crypto/ecdsa"
"github.com/khanzadimahdi/testproject/infrastructure/email"
Expand All @@ -17,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 := "[email protected]"
Expand Down Expand Up @@ -79,6 +79,35 @@ func TestUseCase_Execute(t *testing.T) {
assert.Equal(t, &expectedResponse, response)
})

t.Run("user not found", func(t *testing.T) {
var (
userRepository users.MockUsersRepository
mailer email.MockMailer
renderer template.MockRenderer

request = Request{
Identity: "[email protected]",
}
expectedResponse = Response{
ValidationErrors: validationErrors{
"identity": "identity (email/username) or password is wrong",
},
}
)

userRepository.On("GetOneByIdentity", request.Identity).Once().Return(user.User{}, domain.ErrNotExists)
defer userRepository.AssertExpectations(t)

response, err := NewUseCase(&userRepository, j, &mailer, mailFrom, &renderer).Execute(request)

renderer.AssertNotCalled(t, "Render")
mailer.AssertNotCalled(t, "SendMail")

assert.NoError(t, err)
assert.NotNil(t, response)
assert.Equal(t, &expectedResponse, response)
})

t.Run("error on finding user", func(t *testing.T) {
var (
userRepository users.MockUsersRepository
Expand Down
4 changes: 1 addition & 3 deletions backend/application/auth/refresh/usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
5 changes: 2 additions & 3 deletions backend/application/auth/register/usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := "[email protected]"
Expand Down
4 changes: 1 addition & 3 deletions backend/application/auth/resetpassword/usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
4 changes: 1 addition & 3 deletions backend/application/auth/verify/usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
4 changes: 4 additions & 0 deletions backend/application/bookmark/updateBookmark/usecase.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package updateBookmark

import (
"log"

"github.com/khanzadimahdi/testproject/domain/bookmark"
)

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions backend/application/comment/getComments/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
22 changes: 11 additions & 11 deletions backend/application/dashboard/article/getArticle/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
)

type Response struct {
UUID string `json:"uuid"`
Cover string `json:"cover"`
Video string `json:"video"`
Title string `json:"title"`
Excerpt string `json:"excerpt"`
Body string `json:"body"`
PublishedAt time.Time `json:"published_at"`
Author author `json:"avatar"`
Tags []string `json:"tags"`
ViewCount uint `json:"view_count"`
UUID string `json:"uuid"`
Cover string `json:"cover"`
Video string `json:"video"`
Title string `json:"title"`
Excerpt string `json:"excerpt"`
Body string `json:"body"`
PublishedAt string `json:"published_at"`
Author author `json:"avatar"`
Tags []string `json:"tags"`
ViewCount uint `json:"view_count"`
}

type author struct {
Expand All @@ -35,7 +35,7 @@ func NewResponse(a article.Article) *Response {
Title: a.Title,
Excerpt: a.Excerpt,
Body: a.Body,
PublishedAt: a.PublishedAt,
PublishedAt: a.PublishedAt.Format(time.RFC3339),
Author: author{
Name: a.Author.Name,
Avatar: a.Author.Avatar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package getarticle
import (
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"

Expand All @@ -20,8 +21,9 @@ func TestUseCase_Execute(t *testing.T) {
UUID: articleUUID,
}
expectedResponse = Response{
UUID: articleUUID,
Tags: []string{},
UUID: articleUUID,
Tags: []string{},
PublishedAt: a.PublishedAt.Format(time.RFC3339),
}
)

Expand Down
14 changes: 7 additions & 7 deletions backend/application/dashboard/article/getArticles/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type Response struct {
}

type bookmarkResponse struct {
Title string `json:"title"`
ObjectUUID string `json:"object_uuid"`
ObjectType string `json:"object_type"`
CreatedAt time.Time `json:"created_at"`
Title string `json:"title"`
ObjectUUID string `json:"object_uuid"`
ObjectType string `json:"object_type"`
CreatedAt string `json:"created_at"`
}

type pagination struct {
Expand All @@ -30,7 +30,7 @@ func NewResponse(b []bookmark.Bookmark, totalPages, currentPage uint) *Response
items[i].Title = b[i].Title
items[i].ObjectUUID = b[i].ObjectUUID
items[i].ObjectType = b[i].ObjectType
items[i].CreatedAt = b[i].CreatedAt
items[i].CreatedAt = b[i].CreatedAt.Format(time.RFC3339)
}

return &Response{
Expand Down
20 changes: 10 additions & 10 deletions backend/application/dashboard/comment/getComment/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
)

type Response struct {
UUID string `json:"uuid"`
Body string `json:"body"`
Author author `json:"author"`
ParentUUID string `json:"parent_uuid,omitempty"`
ObjectType string `json:"object_type"`
ObjectUUID string `json:"object_uuid"`
CreatedAt time.Time `json:"created_at"`
ApprovedAt time.Time `json:"approved_at"`
UUID string `json:"uuid"`
Body string `json:"body"`
Author author `json:"author"`
ParentUUID string `json:"parent_uuid,omitempty"`
ObjectType string `json:"object_type"`
ObjectUUID string `json:"object_uuid"`
CreatedAt string `json:"created_at"`
ApprovedAt string `json:"approved_at"`
}

type author struct {
Expand All @@ -35,7 +35,7 @@ func NewResponse(c comment.Comment) *Response {
ParentUUID: c.ParentUUID,
ObjectType: c.ObjectType,
ObjectUUID: c.ObjectUUID,
CreatedAt: c.CreatedAt,
ApprovedAt: c.ApprovedAt,
CreatedAt: c.CreatedAt.Format(time.RFC3339),
ApprovedAt: c.ApprovedAt.Format(time.RFC3339),
}
}
Loading

0 comments on commit 9f1c478

Please sign in to comment.