Skip to content

Commit

Permalink
fix/qr code no usable (#477)
Browse files Browse the repository at this point in the history
* test: Validate if the code sent is used to generate the code image

* fix: use the code sent to generate the code image

use key to identify code type only when accessing cache
  • Loading branch information
Fernando-hub527 authored Sep 23, 2024
1 parent b7ed197 commit 1a3022c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 17 deletions.
12 changes: 12 additions & 0 deletions internal/fixture/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ func RectProp() props.Rect {
return prop
}

// ConfigEntity is responsible to give a valid entity.Config.
func ConfigEntity() entity.Config {
return entity.Config{
Margins: &entity.Margins{
Left: 10,
Top: 10,
Right: 10,
Bottom: 10,
},
}
}

// CellEntity is responsible to give a valid entity.Cell.
func CellEntity() entity.Cell {
return entity.Cell{
Expand Down
14 changes: 7 additions & 7 deletions internal/providers/gofpdf/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (g *provider) AddLine(cell *entity.Cell, prop *props.Line) {
}

func (g *provider) AddMatrixCode(code string, cell *entity.Cell, prop *props.Rect) {
img, err := g.loadCode(fmt.Sprintf("matrix-code-%s", code), g.code.GenDataMatrix)
img, err := g.loadCode(code, "matrix-code-", g.code.GenDataMatrix)
if err != nil {
g.text.Add("could not generate matrixcode", cell, merror.DefaultErrorText)
return
Expand All @@ -77,7 +77,7 @@ func (g *provider) AddMatrixCode(code string, cell *entity.Cell, prop *props.Rec
}

func (g *provider) AddQrCode(code string, cell *entity.Cell, prop *props.Rect) {
img, err := g.loadCode(fmt.Sprintf("qr-code-%s", code), g.code.GenQr)
img, err := g.loadCode(code, "qr-code-", g.code.GenQr)
if err != nil {
g.text.Add("could not generate qrcode", cell, merror.DefaultErrorText)
return
Expand Down Expand Up @@ -225,7 +225,7 @@ func (g *provider) GetDimensionsByImageByte(bytes []byte, extension extension.Ty
// GetDimensionsByMatrixCode is responsible for obtaining the dimensions of an MatrixCode
// If the image cannot be loaded, an error is returned
func (g *provider) GetDimensionsByMatrixCode(code string) (*entity.Dimensions, error) {
img, err := g.loadCode(code, g.code.GenDataMatrix)
img, err := g.loadCode(code, "matrix-code-", g.code.GenDataMatrix)
if err != nil {
return nil, err
}
Expand All @@ -241,7 +241,7 @@ func (g *provider) GetDimensionsByMatrixCode(code string) (*entity.Dimensions, e
// GetDimensionsByQrCode is responsible for obtaining the dimensions of an QrCode
// If the image cannot be loaded, an error is returned
func (g *provider) GetDimensionsByQrCode(code string) (*entity.Dimensions, error) {
img, err := g.loadCode(code, g.code.GenQr)
img, err := g.loadCode(code, "qr-code-", g.code.GenQr)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -277,8 +277,8 @@ func (g *provider) getBarcodeImageName(code string, prop *props.Barcode) string
}

// loadImage is responsible for loading an codes
func (g *provider) loadCode(code string, generate func(code string) (*entity.Image, error)) (*entity.Image, error) {
image, err := g.cache.GetImage(code, extension.Jpg)
func (g *provider) loadCode(code, codeType string, generate func(code string) (*entity.Image, error)) (*entity.Image, error) {
image, err := g.cache.GetImage(codeType+code, extension.Jpg)
if err != nil {
image, err = generate(code)
} else {
Expand All @@ -287,7 +287,7 @@ func (g *provider) loadCode(code string, generate func(code string) (*entity.Ima
if err != nil {
return nil, err
}
g.cache.AddImage(code, image)
g.cache.AddImage(codeType+code, image)

return image, nil
}
Expand Down
90 changes: 80 additions & 10 deletions internal/providers/gofpdf/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestProvider_AddMatrixCode(t *testing.T) {
cache.EXPECT().GetImage("matrix-code-code", extension.Jpg).Return(nil, errors.New("anyError1"))

code := mocks.NewCode(t)
code.EXPECT().GenDataMatrix("matrix-code-code").Return(nil, errors.New("anyError2"))
code.EXPECT().GenDataMatrix(codeContent).Return(nil, errors.New("anyError2"))

text := mocks.NewText(t)
text.EXPECT().Add("could not generate matrixcode", cell, merror.DefaultErrorText)
Expand Down Expand Up @@ -223,6 +223,40 @@ func TestProvider_AddMatrixCode(t *testing.T) {
cache.AssertNumberOfCalls(t, "AddImage", 0)
image.AssertNumberOfCalls(t, "Add", 1)
})

t.Run("when matrx code is generated with the code sent, it should generate matrix code with the same code", func(t *testing.T) {
// Arrange
cell := &entity.Cell{}
cfg := fixture.ConfigEntity()
prop := fixture.RectProp()
img := &entity.Image{Bytes: []byte{1, 2, 3}}

cache := mocks.NewCache(t)
cache.EXPECT().GetImage("matrix-code-code", extension.Jpg).Return(nil, errors.New("anyError1"))
cache.EXPECT().AddImage("matrix-code-code", img).Return()

code := mocks.NewCode(t)
code.EXPECT().GenDataMatrix(codeContent).Return(img, nil)

image := mocks.NewImage(t)
image.EXPECT().Add(img, cell, cfg.Margins, &prop, extension.Jpg, false).Return(nil)

dep := &gofpdf.Dependencies{
Cache: cache,
Code: code,
Cfg: &cfg,
Image: image,
}

// Act
gofpdf.New(dep).AddMatrixCode(codeContent, cell, &prop)

// Assert
cache.AssertNumberOfCalls(t, "GetImage", 1)
code.AssertNumberOfCalls(t, "GenDataMatrix", 1)
cache.AssertNumberOfCalls(t, "AddImage", 1)
image.AssertNumberOfCalls(t, "Add", 1)
})
}

// nolint: dupl
Expand All @@ -236,7 +270,7 @@ func TestProvider_AddQrCode(t *testing.T) {
cache.EXPECT().GetImage("qr-code-code", extension.Jpg).Return(nil, errors.New("anyError1"))

code := mocks.NewCode(t)
code.EXPECT().GenQr("qr-code-code").Return(nil, errors.New("anyError2"))
code.EXPECT().GenQr(codeContent).Return(nil, errors.New("anyError2"))

text := mocks.NewText(t)
text.EXPECT().Add("could not generate qrcode", cell, merror.DefaultErrorText)
Expand Down Expand Up @@ -350,6 +384,42 @@ func TestProvider_AddQrCode(t *testing.T) {
cache.AssertNumberOfCalls(t, "AddImage", 0)
image.AssertNumberOfCalls(t, "Add", 1)
})

t.Run("when qrcode is generated with the code sent, it should generate qr code with the same code", func(t *testing.T) {
// Arrange
cell := &entity.Cell{}
prop := fixture.RectProp()
img := &entity.Image{Bytes: []byte{1, 2, 3}}
cfg := fixture.ConfigEntity()

cache := mocks.NewCache(t)
cache.EXPECT().GetImage("qr-code-code", extension.Jpg).Return(nil, errors.New("anyError1"))
cache.EXPECT().AddImage("qr-code-code", img).Return()

code := mocks.NewCode(t)
code.EXPECT().GenQr(codeContent).Return(img, nil)

image := mocks.NewImage(t)
image.EXPECT().Add(img, cell, cfg.Margins, &prop, extension.Jpg, false).Return(nil)

dep := &gofpdf.Dependencies{
Cache: cache,
Code: code,
Cfg: &cfg,
Image: image,
}

sut := gofpdf.New(dep)

// Act
sut.AddQrCode(codeContent, cell, &prop)

// Assert
cache.AssertNumberOfCalls(t, "GetImage", 1)
code.AssertNumberOfCalls(t, "GenQr", 1)
cache.AssertNumberOfCalls(t, "AddImage", 1)
image.AssertNumberOfCalls(t, "Add", 1)
})
}

// nolint: dupl
Expand Down Expand Up @@ -904,7 +974,7 @@ func TestProvider_GetDimensionsByMatrixCode(t *testing.T) {
// Arrange

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(nil, errors.New("anyError1"))
cache.EXPECT().GetImage("matrix-code-code", extension.Jpg).Return(nil, errors.New("anyError1"))

code := mocks.NewCode(t)
code.EXPECT().GenDataMatrix(codeContent).Return(nil, errors.New("anyError2"))
Expand All @@ -931,8 +1001,8 @@ func TestProvider_GetDimensionsByMatrixCode(t *testing.T) {
img := &entity.Image{Bytes: []byte{1, 2, 3}}

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(nil, errors.New("anyError1"))
cache.EXPECT().AddImage(codeContent, img)
cache.EXPECT().GetImage("matrix-code-code", extension.Jpg).Return(nil, errors.New("anyError1"))
cache.EXPECT().AddImage("matrix-code-code", img)

code := mocks.NewCode(t)
code.EXPECT().GenDataMatrix(codeContent).Return(img, nil)
Expand Down Expand Up @@ -972,7 +1042,7 @@ func TestProvider_GetDimensionsByMatrixCode(t *testing.T) {
img := &entity.Image{Bytes: []byte{1, 2, 3}}

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(img, nil)
cache.EXPECT().GetImage("matrix-code-code", extension.Jpg).Return(img, nil)

code := mocks.NewCode(t)

Expand Down Expand Up @@ -1007,7 +1077,7 @@ func TestProvider_GetDimensionsByQrCode(t *testing.T) {
// Arrange

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(nil, errors.New("anyError1"))
cache.EXPECT().GetImage("qr-code-code", extension.Jpg).Return(nil, errors.New("anyError1"))

code := mocks.NewCode(t)
code.EXPECT().GenQr(codeContent).Return(nil, errors.New("anyError2"))
Expand All @@ -1034,8 +1104,8 @@ func TestProvider_GetDimensionsByQrCode(t *testing.T) {
img := &entity.Image{Bytes: []byte{1, 2, 3}}

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(nil, errors.New("anyError1"))
cache.EXPECT().AddImage(codeContent, img)
cache.EXPECT().GetImage("qr-code-code", extension.Jpg).Return(nil, errors.New("anyError1"))
cache.EXPECT().AddImage("qr-code-code", img)

code := mocks.NewCode(t)
code.EXPECT().GenQr(codeContent).Return(img, nil)
Expand Down Expand Up @@ -1075,7 +1145,7 @@ func TestProvider_GetDimensionsByQrCode(t *testing.T) {
img := &entity.Image{Bytes: []byte{1, 2, 3}}

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(img, nil)
cache.EXPECT().GetImage("qr-code-code", extension.Jpg).Return(img, nil)

code := mocks.NewCode(t)

Expand Down

0 comments on commit 1a3022c

Please sign in to comment.