From 1a3022c3c9c37688171c5c8cf8aee84a75dedda4 Mon Sep 17 00:00:00 2001 From: Fernando Coelho Saraiva <66834579+Fernando-hub527@users.noreply.github.com> Date: Mon, 23 Sep 2024 09:39:16 -0300 Subject: [PATCH] fix/qr code no usable (#477) * 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 --- internal/fixture/fixture.go | 12 +++ internal/providers/gofpdf/provider.go | 14 ++-- internal/providers/gofpdf/provider_test.go | 90 +++++++++++++++++++--- 3 files changed, 99 insertions(+), 17 deletions(-) diff --git a/internal/fixture/fixture.go b/internal/fixture/fixture.go index 6e1c7145..07762285 100644 --- a/internal/fixture/fixture.go +++ b/internal/fixture/fixture.go @@ -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{ diff --git a/internal/providers/gofpdf/provider.go b/internal/providers/gofpdf/provider.go index 1075b2af..b6d4631e 100644 --- a/internal/providers/gofpdf/provider.go +++ b/internal/providers/gofpdf/provider.go @@ -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 @@ -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 @@ -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 } @@ -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 } @@ -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 { @@ -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 } diff --git a/internal/providers/gofpdf/provider_test.go b/internal/providers/gofpdf/provider_test.go index 7fddd0a1..b488c033 100644 --- a/internal/providers/gofpdf/provider_test.go +++ b/internal/providers/gofpdf/provider_test.go @@ -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) @@ -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 @@ -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) @@ -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 @@ -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")) @@ -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) @@ -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) @@ -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")) @@ -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) @@ -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)