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 2, 2024
1 parent 43e2e96 commit 7235a5f
Show file tree
Hide file tree
Showing 145 changed files with 1,520 additions and 162 deletions.
4 changes: 2 additions & 2 deletions backend/application/article/getArticle/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -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
5 changes: 2 additions & 3 deletions backend/application/auth/forgetpassword/usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := "[email protected]"
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: 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
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
16 changes: 8 additions & 8 deletions backend/application/home/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
11 changes: 4 additions & 7 deletions backend/infrastructure/commandbus/commandbus_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package commandbus

import (
"bytes"
"crypto/rand"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBus(t *testing.T) {
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
},
}

Expand Down Expand Up @@ -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{},
},
}

Expand Down Expand Up @@ -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{},
},
}

Expand Down Expand Up @@ -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{},
},
}

Expand Down Expand Up @@ -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{},
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
},
},
},
Expand Down Expand Up @@ -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{},
},
},
},
Expand Down
28 changes: 28 additions & 0 deletions backend/infrastructure/template/renderer_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
6 changes: 6 additions & 0 deletions backend/infrastructure/template/testdata/base.layout.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!doctype html>

<html>
<head>{{block "head" .}}{{end}}</head>
<body>{{block "body" .}}{{end}}</body>
</html>
3 changes: 3 additions & 0 deletions backend/infrastructure/template/testdata/page.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{template "base.layout.tmpl" .}}
{{define "head"}}<title>{{.head}}</title>{{end}}
{{define "body"}}<div>{{.body}}</div>{{end}}
7 changes: 7 additions & 0 deletions backend/infrastructure/template/testdata/page.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!doctype html>

<html>
<head><title>test head</title></head>
<body><div>test body</div></body>
</html>

33 changes: 28 additions & 5 deletions backend/presentation/commands/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})
}
2 changes: 1 addition & 1 deletion backend/presentation/http/api/article/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading

0 comments on commit 7235a5f

Please sign in to comment.