Skip to content

Commit

Permalink
Kruto
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed Dec 23, 2023
1 parent 99b8826 commit fbe2c9e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 8 deletions.
10 changes: 5 additions & 5 deletions internal/repository/follow/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Pgx struct {

const tableName = "follows"

func (c *Pgx) GetByID(ctx context.Context, id uuid.UUID) (domain.Follow, error) {
func (c *Pgx) GetByID(ctx context.Context, id uuid.UUID) (*domain.Follow, error) {
follow := Follow{}

query, args, err := repository.Sq.
Expand All @@ -41,7 +41,7 @@ func (c *Pgx) GetByID(ctx context.Context, id uuid.UUID) (domain.Follow, error)
id,
).ToSql()
if err != nil {
return domain.Follow{}, repository.ErrBadQuery
return nil, repository.ErrBadQuery
}

err = c.pg.QueryRow(ctx, query, args...).Scan(
Expand All @@ -52,13 +52,13 @@ func (c *Pgx) GetByID(ctx context.Context, id uuid.UUID) (domain.Follow, error)
)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return domain.Follow{}, ErrNotFound
return nil, ErrNotFound
}

return domain.Follow{}, err
return nil, err
}

return domain.Follow{
return &domain.Follow{
ID: follow.ID,
ChatID: follow.ChatID,
ChannelID: follow.ChannelID,
Expand Down
11 changes: 9 additions & 2 deletions internal/thumbnailchecker/temporal/impl_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type Activity struct {
client *http.Client
}

var ErrInvalidThumbnail = errors.New("invalid thumbnail")

func (c *Activity) ThumbnailCheckerTemporalActivity(
ctx context.Context,
thumbnailUrl string,
Expand All @@ -42,9 +44,14 @@ func (c *Activity) ThumbnailCheckerTemporalActivity(
return err
}

if res.StatusCode >= 200 && res.StatusCode < 300 {
contentType := res.Header.Get("Content-Type")
isImage := contentType == "image/png" || contentType == "image/jpeg"

isNotRedirect := res.StatusCode >= 200 && res.StatusCode < 300

if isImage && isNotRedirect {
return nil
}

return errors.New("invalid thumbnail")
return ErrInvalidThumbnail
}
73 changes: 73 additions & 0 deletions internal/thumbnailchecker/temporal/impl_activity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package temporal

import (
"context"
"net/http"
"net/http/httptest"
"testing"

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

func TestActivity_ThumbnailCheckerTemporalActivityCorrect(t *testing.T) {
t.Parallel()

ts := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
},
),
)
defer ts.Close()

activity := NewActivity()
err := activity.ThumbnailCheckerTemporalActivity(
context.TODO(),
ts.URL,
)

assert.NoError(t, err)
}

func TestActivity_ThumbnailCheckerTemporalActivityRedirect(t *testing.T) {
t.Parallel()

ts := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://google.com", http.StatusFound)
},
),
)
defer ts.Close()

activity := NewActivity()
err := activity.ThumbnailCheckerTemporalActivity(
context.TODO(),
ts.URL,
)

assert.ErrorIs(t, err, ErrInvalidThumbnail)
}

func TestActivity_ThumbnailCheckerTemporalActivityNotImage(t *testing.T) {
t.Parallel()

ts := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
},
),
)
defer ts.Close()

activity := NewActivity()
err := activity.ThumbnailCheckerTemporalActivity(
context.TODO(),
ts.URL,
)

assert.ErrorIs(t, err, ErrInvalidThumbnail)
}
4 changes: 3 additions & 1 deletion internal/thumbnailchecker/temporal/impl_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ type Workflow struct {
activity *Activity
}

const activityMaximumAttempts = 50

func (c *Workflow) Workflow(ctx workflow.Context, thumbNailUrl string) error {
ao := workflow.ActivityOptions{
TaskQueue: queueName,
StartToCloseTimeout: 10 * time.Second,
RetryPolicy: &temporal.RetryPolicy{
MaximumInterval: 15 * time.Second,
MaximumAttempts: 50,
MaximumAttempts: activityMaximumAttempts,
NonRetryableErrorTypes: nil,
},
}
Expand Down
29 changes: 29 additions & 0 deletions internal/thumbnailchecker/temporal/impl_workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
)

func Test_Workflow(t *testing.T) {
t.Parallel()

activity := &Activity{}
workflow := &Workflow{
activity: activity,
Expand All @@ -29,3 +31,30 @@ func Test_Workflow(t *testing.T) {
require.True(t, env.IsWorkflowCompleted())
require.NoError(t, env.GetWorkflowError())
}

func Test_WorkflowError(t *testing.T) {
t.Parallel()

activity := &Activity{}
workflow := &Workflow{
activity: activity,
}

testSuite := &testsuite.WorkflowTestSuite{}
env := testSuite.NewTestWorkflowEnvironment()

// Mock activity implementation
env.
OnActivity(
activity.ThumbnailCheckerTemporalActivity,
mock.Anything,
"https://twitch.tv/thumbNail",
).
Times(activityMaximumAttempts).
Return(ErrInvalidThumbnail)

env.ExecuteWorkflow(workflow.Workflow, "https://twitch.tv/thumbNail")

require.True(t, env.IsWorkflowCompleted())
require.Error(t, env.GetWorkflowError())
}

0 comments on commit fbe2c9e

Please sign in to comment.