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 10, 2024
1 parent 43e2e96 commit ab9faf6
Show file tree
Hide file tree
Showing 184 changed files with 3,888 additions and 803 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) not exists",
},
}, 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) not exists",
},
}
)

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
9 changes: 8 additions & 1 deletion backend/application/auth/login/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/khanzadimahdi/testproject/application/auth"
"github.com/khanzadimahdi/testproject/domain"
"github.com/khanzadimahdi/testproject/domain/password"
"github.com/khanzadimahdi/testproject/domain/user"
"github.com/khanzadimahdi/testproject/infrastructure/jwt"
Expand Down Expand Up @@ -31,7 +32,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
2 changes: 1 addition & 1 deletion backend/application/auth/refresh/response.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package refresh

type RefreshResponse struct {
type Response struct {
ValidationErrors validationErrors `json:"errors,omitempty"`
AccessToken string `json:"access_token,omitempty"`
RefreshToken string `json:"refresh_token,omitempty"`
Expand Down
21 changes: 14 additions & 7 deletions backend/application/auth/refresh/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/khanzadimahdi/testproject/application/auth"
"github.com/khanzadimahdi/testproject/domain"
"github.com/khanzadimahdi/testproject/domain/user"
"github.com/khanzadimahdi/testproject/infrastructure/jwt"
)
Expand All @@ -20,24 +21,24 @@ func NewUseCase(userRepository user.Repository, JWT *jwt.JWT) *UseCase {
}
}

func (uc *UseCase) Execute(request Request) (*RefreshResponse, error) {
func (uc *UseCase) Execute(request Request) (*Response, error) {
if ok, validation := request.Validate(); !ok {
return &RefreshResponse{
return &Response{
ValidationErrors: validation,
}, nil
}

claims, err := uc.JWT.Verify(request.Token)
if err != nil {
return &RefreshResponse{
return &Response{
ValidationErrors: validationErrors{
"token": err.Error(),
},
}, nil
}

if audiences, err := claims.GetAudience(); err != nil || len(audiences) == 0 || audiences[0] != auth.RefreshToken {
return &RefreshResponse{
return &Response{
ValidationErrors: validationErrors{
"token": "refresh token is not valid",
},
Expand All @@ -46,15 +47,21 @@ func (uc *UseCase) Execute(request Request) (*RefreshResponse, error) {

userUUID, err := claims.GetSubject()
if err != nil {
return &RefreshResponse{
return &Response{
ValidationErrors: validationErrors{
"token": err.Error(),
},
}, nil
}

u, err := uc.userRepository.GetOne(userUUID)
if err != nil {
if err == domain.ErrNotExists {
return &Response{
ValidationErrors: validationErrors{
"identity": "identity (email/username) not exists",
},
}, nil
} else if err != nil {
return nil, err
}

Expand All @@ -68,7 +75,7 @@ func (uc *UseCase) Execute(request Request) (*RefreshResponse, error) {
return nil, err
}

return &RefreshResponse{
return &Response{
AccessToken: accessToken,
RefreshToken: refreshToken,
}, nil
Expand Down
8 changes: 3 additions & 5 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 Expand Up @@ -60,7 +58,7 @@ func TestUseCase_Execute(t *testing.T) {
var (
userRepository users.MockUsersRepository
r = Request{}
expectedResponse = RefreshResponse{
expectedResponse = Response{
ValidationErrors: validationErrors{
"token": "token is required",
},
Expand All @@ -84,7 +82,7 @@ func TestUseCase_Execute(t *testing.T) {
r = Request{
Token: generateRefreshToken(t, j, u, time.Now().Add(-10*time.Second), auth.RefreshToken),
}
expectedResponse = RefreshResponse{
expectedResponse = Response{
ValidationErrors: validationErrors{
"token": "token has invalid claims: token is expired",
},
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
12 changes: 11 additions & 1 deletion backend/application/auth/resetpassword/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,15 @@ type Request struct {
}

func (r *Request) Validate() (bool, validationErrors) {
return true, nil
errors := make(validationErrors)

if len(r.Token) == 0 {
errors["token"] = "token is required"
}

if len(r.Password) == 0 {
errors["password"] = "password is required"
}

return len(errors) == 0, errors
}
9 changes: 8 additions & 1 deletion backend/application/auth/resetpassword/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"

"github.com/khanzadimahdi/testproject/application/auth"
"github.com/khanzadimahdi/testproject/domain"
"github.com/khanzadimahdi/testproject/domain/password"
"github.com/khanzadimahdi/testproject/domain/user"
"github.com/khanzadimahdi/testproject/infrastructure/jwt"
Expand Down Expand Up @@ -53,7 +54,13 @@ func (uc *UseCase) Execute(request Request) (*Response, error) {
}

u, err := uc.userRepository.GetOne(userUUID)
if err != nil {
if err == domain.ErrNotExists {
return &Response{
ValidationErrors: validationErrors{
"identity": "identity (email/username) not exists",
},
}, nil
} else if err != nil {
return nil, err
}

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
Loading

0 comments on commit ab9faf6

Please sign in to comment.