|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/gotd/td/tg" |
| 8 | +) |
| 9 | + |
| 10 | +func TestSetBusinessAccountProfilePhotoStatic(t *testing.T) { |
| 11 | + inv := newMockInvoker() |
| 12 | + inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, &tg.PhotosPhoto{Photo: &tg.PhotoEmpty{}}) |
| 13 | + |
| 14 | + photo := InputProfilePhotoStatic{Photo: &InputFileUpload{Name: "p.jpg", Bytes: []byte("img")}} |
| 15 | + if err := newMockBot(inv).SetBusinessAccountProfilePhoto(context.Background(), "bc1", photo, true); err != nil { |
| 16 | + t.Fatalf("SetBusinessAccountProfilePhoto: %v", err) |
| 17 | + } |
| 18 | + |
| 19 | + wrapper := tg.InvokeWithBusinessConnectionRequest{Query: &tg.PhotosUploadProfilePhotoRequest{}} |
| 20 | + |
| 21 | + inv.decode(t, tg.InvokeWithBusinessConnectionRequestTypeID, &wrapper) |
| 22 | + |
| 23 | + if wrapper.ConnectionID != "bc1" { |
| 24 | + t.Fatalf("connection id = %q", wrapper.ConnectionID) |
| 25 | + } |
| 26 | + |
| 27 | + req, ok := wrapper.Query.(*tg.PhotosUploadProfilePhotoRequest) |
| 28 | + if !ok { |
| 29 | + t.Fatalf("query = %#v", wrapper.Query) |
| 30 | + } |
| 31 | + |
| 32 | + if _, ok := req.GetFile(); !ok { |
| 33 | + t.Fatal("static photo should set File") |
| 34 | + } |
| 35 | + |
| 36 | + if !req.GetFallback() { |
| 37 | + t.Fatal("is_public should set Fallback") |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestSetBusinessAccountProfilePhotoAnimated(t *testing.T) { |
| 42 | + inv := newMockInvoker() |
| 43 | + inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, &tg.PhotosPhoto{Photo: &tg.PhotoEmpty{}}) |
| 44 | + |
| 45 | + photo := InputProfilePhotoAnimated{ |
| 46 | + Animation: &InputFileUpload{Name: "a.mp4", Bytes: []byte("vid")}, |
| 47 | + MainFrameTimestamp: 1.5, |
| 48 | + } |
| 49 | + if err := newMockBot(inv).SetBusinessAccountProfilePhoto(context.Background(), "bc1", photo, false); err != nil { |
| 50 | + t.Fatalf("SetBusinessAccountProfilePhoto: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + wrapper := tg.InvokeWithBusinessConnectionRequest{Query: &tg.PhotosUploadProfilePhotoRequest{}} |
| 54 | + |
| 55 | + inv.decode(t, tg.InvokeWithBusinessConnectionRequestTypeID, &wrapper) |
| 56 | + |
| 57 | + req, ok := wrapper.Query.(*tg.PhotosUploadProfilePhotoRequest) |
| 58 | + if !ok { |
| 59 | + t.Fatalf("query = %#v", wrapper.Query) |
| 60 | + } |
| 61 | + |
| 62 | + if _, ok := req.GetVideo(); !ok { |
| 63 | + t.Fatal("animated photo should set Video") |
| 64 | + } |
| 65 | + |
| 66 | + if ts, ok := req.GetVideoStartTs(); !ok || ts != 1.5 { |
| 67 | + t.Fatalf("video start ts = %v, ok=%v", ts, ok) |
| 68 | + } |
| 69 | + |
| 70 | + if req.GetFallback() { |
| 71 | + t.Fatal("Fallback should be unset when is_public is false") |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestSetBusinessAccountProfilePhotoRejectsFileID(t *testing.T) { |
| 76 | + inv := newMockInvoker() |
| 77 | + |
| 78 | + photo := InputProfilePhotoStatic{Photo: FileID("abc")} |
| 79 | + |
| 80 | + err := newMockBot(inv).SetBusinessAccountProfilePhoto(context.Background(), "bc1", photo, false) |
| 81 | + if err == nil { |
| 82 | + t.Fatal("expected error for non-upload profile photo") |
| 83 | + } |
| 84 | + |
| 85 | + if inv.count() != 0 { |
| 86 | + t.Fatal("should not make an RPC for an invalid file") |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestRemoveBusinessAccountProfilePhoto(t *testing.T) { |
| 91 | + inv := newMockInvoker() |
| 92 | + inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, &tg.PhotosPhoto{Photo: &tg.PhotoEmpty{}}) |
| 93 | + |
| 94 | + if err := newMockBot(inv).RemoveBusinessAccountProfilePhoto(context.Background(), "bc1", true); err != nil { |
| 95 | + t.Fatalf("RemoveBusinessAccountProfilePhoto: %v", err) |
| 96 | + } |
| 97 | + |
| 98 | + wrapper := tg.InvokeWithBusinessConnectionRequest{Query: &tg.PhotosUpdateProfilePhotoRequest{}} |
| 99 | + |
| 100 | + inv.decode(t, tg.InvokeWithBusinessConnectionRequestTypeID, &wrapper) |
| 101 | + |
| 102 | + req, ok := wrapper.Query.(*tg.PhotosUpdateProfilePhotoRequest) |
| 103 | + if !ok { |
| 104 | + t.Fatalf("query = %#v", wrapper.Query) |
| 105 | + } |
| 106 | + |
| 107 | + if _, ok := req.ID.(*tg.InputPhotoEmpty); !ok { |
| 108 | + t.Fatalf("id = %#v, want empty", req.ID) |
| 109 | + } |
| 110 | + |
| 111 | + if !req.GetFallback() { |
| 112 | + t.Fatal("is_public should set Fallback") |
| 113 | + } |
| 114 | +} |
0 commit comments