|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/gotd/td/tg" |
| 8 | +) |
| 9 | + |
| 10 | +func TestBusinessContextConnectionID(t *testing.T) { |
| 11 | + bc := newMockBot(newMockInvoker()).Business("bc9") |
| 12 | + |
| 13 | + if bc.ConnectionID() != "bc9" { |
| 14 | + t.Fatalf("connection id = %q", bc.ConnectionID()) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func TestBusinessContextSetNameScopesConnection(t *testing.T) { |
| 19 | + inv := newMockInvoker() |
| 20 | + inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, &tg.User{ID: 10}) |
| 21 | + |
| 22 | + if err := newMockBot(inv).Business("bc9").SetName(context.Background(), "First", "Last"); err != nil { |
| 23 | + t.Fatalf("SetName: %v", err) |
| 24 | + } |
| 25 | + |
| 26 | + wrapper := tg.InvokeWithBusinessConnectionRequest{Query: &tg.AccountUpdateProfileRequest{}} |
| 27 | + |
| 28 | + inv.decode(t, tg.InvokeWithBusinessConnectionRequestTypeID, &wrapper) |
| 29 | + |
| 30 | + if wrapper.ConnectionID != "bc9" { |
| 31 | + t.Fatalf("connection id = %q", wrapper.ConnectionID) |
| 32 | + } |
| 33 | + |
| 34 | + profile, ok := wrapper.Query.(*tg.AccountUpdateProfileRequest) |
| 35 | + if !ok { |
| 36 | + t.Fatalf("query = %#v", wrapper.Query) |
| 37 | + } |
| 38 | + |
| 39 | + if first, _ := profile.GetFirstName(); first != "First" { |
| 40 | + t.Fatalf("first name = %q", first) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestBusinessContextStarBalance(t *testing.T) { |
| 45 | + inv := newMockInvoker() |
| 46 | + inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, &tg.PaymentsStarsStatus{ |
| 47 | + Balance: &tg.StarsAmount{Amount: 7}, |
| 48 | + }) |
| 49 | + |
| 50 | + got, err := newMockBot(inv).Business("bc9").StarBalance(context.Background()) |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("StarBalance: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + if got.Amount != 7 { |
| 56 | + t.Fatalf("balance = %#v", got) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestBusinessContextDeleteMessages(t *testing.T) { |
| 61 | + inv := newMockInvoker() |
| 62 | + inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, &tg.MessagesAffectedMessages{}) |
| 63 | + |
| 64 | + if err := newMockBot(inv).Business("bc9").DeleteMessages(context.Background(), []int{1, 2}); err != nil { |
| 65 | + t.Fatalf("DeleteMessages: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + wrapper := tg.InvokeWithBusinessConnectionRequest{Query: &tg.MessagesDeleteMessagesRequest{}} |
| 69 | + |
| 70 | + inv.decode(t, tg.InvokeWithBusinessConnectionRequestTypeID, &wrapper) |
| 71 | + |
| 72 | + del, ok := wrapper.Query.(*tg.MessagesDeleteMessagesRequest) |
| 73 | + if !ok || len(del.ID) != 2 { |
| 74 | + t.Fatalf("query = %#v", wrapper.Query) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestBusinessContextConnection(t *testing.T) { |
| 79 | + inv := newMockInvoker() |
| 80 | + inv.reply(tg.AccountGetBotBusinessConnectionRequestTypeID, &tg.Updates{ |
| 81 | + Updates: []tg.UpdateClass{ |
| 82 | + &tg.UpdateBotBusinessConnect{Connection: tg.BotBusinessConnection{ConnectionID: "bc9", UserID: 10}}, |
| 83 | + }, |
| 84 | + Users: []tg.UserClass{&tg.User{ID: 10, FirstName: "Biz"}}, |
| 85 | + }) |
| 86 | + |
| 87 | + conn, err := newMockBot(inv).Business("bc9").Connection(context.Background()) |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("Connection: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + if conn.ID != "bc9" { |
| 93 | + t.Fatalf("conn = %#v", conn) |
| 94 | + } |
| 95 | + |
| 96 | + var req tg.AccountGetBotBusinessConnectionRequest |
| 97 | + |
| 98 | + inv.decode(t, tg.AccountGetBotBusinessConnectionRequestTypeID, &req) |
| 99 | + |
| 100 | + if req.ConnectionID != "bc9" { |
| 101 | + t.Fatalf("requested connection id = %q", req.ConnectionID) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestBusinessContextRemoveProfilePhoto(t *testing.T) { |
| 106 | + inv := newMockInvoker() |
| 107 | + inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, &tg.PhotosPhoto{Photo: &tg.PhotoEmpty{}}) |
| 108 | + |
| 109 | + if err := newMockBot(inv).Business("bc9").RemoveProfilePhoto(context.Background(), false); err != nil { |
| 110 | + t.Fatalf("RemoveProfilePhoto: %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + wrapper := tg.InvokeWithBusinessConnectionRequest{Query: &tg.PhotosUpdateProfilePhotoRequest{}} |
| 114 | + |
| 115 | + inv.decode(t, tg.InvokeWithBusinessConnectionRequestTypeID, &wrapper) |
| 116 | + |
| 117 | + if wrapper.ConnectionID != "bc9" { |
| 118 | + t.Fatalf("connection id = %q", wrapper.ConnectionID) |
| 119 | + } |
| 120 | +} |
0 commit comments