|
| 1 | +package pdfengines |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "reflect" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/gotenberg/gotenberg/v8/pkg/gotenberg" |
| 10 | + "github.com/gotenberg/gotenberg/v8/pkg/modules/api" |
| 11 | +) |
| 12 | + |
| 13 | +func TestFormDataPdfStamps(t *testing.T) { |
| 14 | + for _, tc := range []struct { |
| 15 | + scenario string |
| 16 | + values map[string][]string |
| 17 | + expect []gotenberg.Stamp |
| 18 | + expectErr bool |
| 19 | + expectCode int |
| 20 | + }{ |
| 21 | + { |
| 22 | + scenario: "single text stamp (backward compatible)", |
| 23 | + values: map[string][]string{ |
| 24 | + "stampSource": {"text"}, |
| 25 | + "stampExpression": {"CONFIDENTIAL"}, |
| 26 | + "stampOptions": {`{"rot":"45"}`}, |
| 27 | + }, |
| 28 | + expect: []gotenberg.Stamp{ |
| 29 | + {Source: "text", Expression: "CONFIDENTIAL", Options: map[string]string{"rot": "45"}}, |
| 30 | + }, |
| 31 | + }, |
| 32 | + { |
| 33 | + scenario: "multiple stamps aligned by position", |
| 34 | + values: map[string][]string{ |
| 35 | + "stampSource": {"text", "image"}, |
| 36 | + "stampExpression": {"ONE"}, |
| 37 | + "stampPages": {"1-2", "3"}, |
| 38 | + "stampOptions": {`{"pos":"tl"}`, `{"pos":"br"}`}, |
| 39 | + }, |
| 40 | + expect: []gotenberg.Stamp{ |
| 41 | + {Source: "text", Expression: "ONE", Pages: "1-2", Options: map[string]string{"pos": "tl"}}, |
| 42 | + {Source: "image", Expression: "", Pages: "3", Options: map[string]string{"pos": "br"}}, |
| 43 | + }, |
| 44 | + }, |
| 45 | + { |
| 46 | + scenario: "no stamp fields", |
| 47 | + values: map[string][]string{}, |
| 48 | + expect: []gotenberg.Stamp{}, |
| 49 | + }, |
| 50 | + { |
| 51 | + scenario: "invalid source", |
| 52 | + values: map[string][]string{"stampSource": {"text", "foo"}}, |
| 53 | + expectErr: true, |
| 54 | + expectCode: http.StatusBadRequest, |
| 55 | + }, |
| 56 | + { |
| 57 | + scenario: "invalid options JSON", |
| 58 | + values: map[string][]string{ |
| 59 | + "stampSource": {"text"}, |
| 60 | + "stampOptions": {"{"}, |
| 61 | + }, |
| 62 | + expectErr: true, |
| 63 | + expectCode: http.StatusBadRequest, |
| 64 | + }, |
| 65 | + } { |
| 66 | + t.Run(tc.scenario, func(t *testing.T) { |
| 67 | + ctx := &api.ContextMock{Context: &api.Context{}} |
| 68 | + ctx.SetValues(tc.values) |
| 69 | + form := ctx.FormData() |
| 70 | + |
| 71 | + got, err := FormDataPdfStamps(form) |
| 72 | + |
| 73 | + if tc.expectErr { |
| 74 | + if err == nil { |
| 75 | + t.Fatal("expected an error, got nil") |
| 76 | + } |
| 77 | + var httpErr api.HttpError |
| 78 | + if !errors.As(err, &httpErr) { |
| 79 | + t.Fatalf("expected an api.HttpError, got %T", err) |
| 80 | + } |
| 81 | + if status, _ := httpErr.HttpError(); status != tc.expectCode { |
| 82 | + t.Fatalf("status = %d, want %d", status, tc.expectCode) |
| 83 | + } |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + if err != nil { |
| 88 | + t.Fatalf("expected no error, got %v", err) |
| 89 | + } |
| 90 | + if !reflect.DeepEqual(got, tc.expect) { |
| 91 | + t.Fatalf("stamps = %#v, want %#v", got, tc.expect) |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestBindStampFiles(t *testing.T) { |
| 98 | + for _, tc := range []struct { |
| 99 | + scenario string |
| 100 | + stamps []gotenberg.Stamp |
| 101 | + files []string |
| 102 | + expect []gotenberg.Stamp |
| 103 | + expectErr bool |
| 104 | + expectCode int |
| 105 | + }{ |
| 106 | + { |
| 107 | + scenario: "text stamps consume no files", |
| 108 | + stamps: []gotenberg.Stamp{{Source: "text", Expression: "FOO"}}, |
| 109 | + expect: []gotenberg.Stamp{{Source: "text", Expression: "FOO"}}, |
| 110 | + }, |
| 111 | + { |
| 112 | + scenario: "image and pdf stamps consume files in order, overwriting expression", |
| 113 | + stamps: []gotenberg.Stamp{ |
| 114 | + {Source: "image", Expression: "ignored"}, |
| 115 | + {Source: "text", Expression: "MIDDLE"}, |
| 116 | + {Source: "pdf"}, |
| 117 | + }, |
| 118 | + files: []string{"/a.png", "/b.pdf"}, |
| 119 | + expect: []gotenberg.Stamp{ |
| 120 | + {Source: "image", Expression: "/a.png"}, |
| 121 | + {Source: "text", Expression: "MIDDLE"}, |
| 122 | + {Source: "pdf", Expression: "/b.pdf"}, |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + scenario: "not enough files for the image or pdf stamps", |
| 127 | + stamps: []gotenberg.Stamp{{Source: "image"}, {Source: "image"}}, |
| 128 | + files: []string{"/a.png"}, |
| 129 | + expectErr: true, |
| 130 | + expectCode: http.StatusBadRequest, |
| 131 | + }, |
| 132 | + } { |
| 133 | + t.Run(tc.scenario, func(t *testing.T) { |
| 134 | + err := BindStampFiles(tc.stamps, tc.files) |
| 135 | + |
| 136 | + if tc.expectErr { |
| 137 | + if err == nil { |
| 138 | + t.Fatal("expected an error, got nil") |
| 139 | + } |
| 140 | + var httpErr api.HttpError |
| 141 | + if !errors.As(err, &httpErr) { |
| 142 | + t.Fatalf("expected an api.HttpError, got %T", err) |
| 143 | + } |
| 144 | + if status, _ := httpErr.HttpError(); status != tc.expectCode { |
| 145 | + t.Fatalf("status = %d, want %d", status, tc.expectCode) |
| 146 | + } |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + if err != nil { |
| 151 | + t.Fatalf("expected no error, got %v", err) |
| 152 | + } |
| 153 | + if !reflect.DeepEqual(tc.stamps, tc.expect) { |
| 154 | + t.Fatalf("stamps = %#v, want %#v", tc.stamps, tc.expect) |
| 155 | + } |
| 156 | + }) |
| 157 | + } |
| 158 | +} |
0 commit comments