|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/rs/zerolog" |
| 8 | + "maunium.net/go/mautrix/bridgev2" |
| 9 | + "maunium.net/go/mautrix/event" |
| 10 | + |
| 11 | + "github.com/highesttt/matrix-line-messenger/pkg/line" |
| 12 | +) |
| 13 | + |
| 14 | +func TestOversizedMediaNotice(t *testing.T) { |
| 15 | + h := &Handler{Log: zerolog.Nop()} |
| 16 | + relatesTo := &event.RelatesTo{} |
| 17 | + converted := h.oversizedMediaNotice(BeeperMaxFileSize+1, "downloaded", relatesTo) |
| 18 | + if converted == nil || len(converted.Parts) != 1 { |
| 19 | + t.Fatalf("converted = %#v, want one notice part", converted) |
| 20 | + } |
| 21 | + |
| 22 | + part := converted.Parts[0] |
| 23 | + if part.Type != event.EventMessage { |
| 24 | + t.Fatalf("event type = %v, want %v", part.Type, event.EventMessage) |
| 25 | + } |
| 26 | + if part.Content == nil { |
| 27 | + t.Fatal("notice content is nil") |
| 28 | + } |
| 29 | + if part.Content.MsgType != event.MsgNotice { |
| 30 | + t.Fatalf("message type = %v, want %v", part.Content.MsgType, event.MsgNotice) |
| 31 | + } |
| 32 | + const expectedBody = "This file exceeds Beeper's 100MB file size limit. Open LINE to view it." |
| 33 | + if part.Content.Body != expectedBody { |
| 34 | + t.Fatalf("body = %q, want %q", part.Content.Body, expectedBody) |
| 35 | + } |
| 36 | + if part.Content.RelatesTo != relatesTo { |
| 37 | + t.Fatalf("relates_to = %#v, want original pointer %#v", part.Content.RelatesTo, relatesTo) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestOversizedMediaNoticeAllowsLimitAndBelow(t *testing.T) { |
| 42 | + h := &Handler{Log: zerolog.Nop()} |
| 43 | + for _, size := range []int64{0, BeeperMaxFileSize - 1, BeeperMaxFileSize} { |
| 44 | + if converted := h.oversizedMediaNotice(size, "downloaded", nil); converted != nil { |
| 45 | + t.Fatalf("size %d converted = %#v, want nil", size, converted) |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestOversizedMediaMetadataShortCircuitsAllMediaHandlers(t *testing.T) { |
| 51 | + h := &Handler{ |
| 52 | + Log: zerolog.Nop(), |
| 53 | + NewClient: func() *line.Client { |
| 54 | + t.Fatal("NewClient was called for media declared over the size limit") |
| 55 | + return nil |
| 56 | + }, |
| 57 | + } |
| 58 | + message := line.Message{ |
| 59 | + ID: "message-id", |
| 60 | + ContentMetadata: map[string]string{ |
| 61 | + "FILE_SIZE": "104857633", |
| 62 | + }, |
| 63 | + } |
| 64 | + relatesTo := &event.RelatesTo{} |
| 65 | + |
| 66 | + tests := map[string]func() (*bridgev2.ConvertedMessage, error){ |
| 67 | + "image": func() (*bridgev2.ConvertedMessage, error) { |
| 68 | + return h.ConvertImage(context.Background(), nil, nil, message, "", relatesTo) |
| 69 | + }, |
| 70 | + "video": func() (*bridgev2.ConvertedMessage, error) { |
| 71 | + return h.ConvertVideo(context.Background(), nil, nil, message, "", relatesTo) |
| 72 | + }, |
| 73 | + "audio and voice": func() (*bridgev2.ConvertedMessage, error) { |
| 74 | + return h.ConvertAudio(context.Background(), nil, nil, message, "", relatesTo) |
| 75 | + }, |
| 76 | + "file": func() (*bridgev2.ConvertedMessage, error) { |
| 77 | + return h.ConvertFile(context.Background(), nil, nil, message, "", relatesTo) |
| 78 | + }, |
| 79 | + } |
| 80 | + for name, convert := range tests { |
| 81 | + t.Run(name, func(t *testing.T) { |
| 82 | + converted, err := convert() |
| 83 | + if err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + if converted == nil || len(converted.Parts) != 1 || converted.Parts[0].Content.Body != oversizedFileBody { |
| 87 | + t.Fatalf("converted = %#v, want oversized notice", converted) |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestOversizedMediaMetadataFallsBackForMissingInvalidAndAllowedSizes(t *testing.T) { |
| 94 | + h := &Handler{Log: zerolog.Nop()} |
| 95 | + for name, metadata := range map[string]map[string]string{ |
| 96 | + "missing": nil, |
| 97 | + "invalid": {"FILE_SIZE": "not-a-number"}, |
| 98 | + "negative": {"FILE_SIZE": "-1"}, |
| 99 | + "at limit": {"FILE_SIZE": "104857600"}, |
| 100 | + "possible encrypted overhead": {"FILE_SIZE": "104857632"}, |
| 101 | + } { |
| 102 | + t.Run(name, func(t *testing.T) { |
| 103 | + if converted := h.oversizedMediaNoticeFromMetadata(metadata, nil); converted != nil { |
| 104 | + t.Fatalf("converted = %#v, want normal download fallback", converted) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
0 commit comments