X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/b1be0ee8530df5db8973bfe355f2f6d3149383a0..fc0503be49438775c5d08499046852df59bd7377:/whatsapp_send_test.go?ds=sidebyside diff --git a/whatsapp_send_test.go b/whatsapp_send_test.go index 65c2fc6..977d06b 100644 --- a/whatsapp_send_test.go +++ b/whatsapp_send_test.go @@ -88,6 +88,37 @@ func TestWhatsAppSendThatFails(t *testing.T) { require.Len(t, fake.Sent(), 1) } +func TestWhatsAppSendLogsEachSendWithoutTextOrNumbers(t *testing.T) { + dir := t.TempDir() + whatsappTestSession(t, dir, true) + + whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock()) + daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) + whatsappTestConnect(t, fake) + + name, err := whatsapp.inbox.images.Save(imageTestPNG(t, 4, 3)) + require.NoError(t, err) + + _, err = whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "The keys are under the mat.", "") + require.NoError(t, err) + + _, err = whatsapp.Send(t.Context(), "plan", whatsappTestAna.String(), "The plan.", name) + require.NoError(t, err) + + fake.sendErr = errors.New("server error for " + whatsappTestNumber) + _, err = whatsapp.Send(t.Context(), "late", whatsappTestAna.String(), "Late.", "") + require.NoError(t, err) + + chat := whatsappChatHash(whatsappTestAna) + require.Contains(t, logs.String(), "<6>message sent chat="+chat+" image=false\n") + require.Contains(t, logs.String(), "<6>message sent chat="+chat+" image=true\n") + require.Contains(t, logs.String(), "<4>message send failed chat="+chat+" image=false: server error for OWN\n") + + for _, secret := range []string{"keys", "plan", "Late", whatsappTestNumber, whatsappTestAna.User} { + require.NotContains(t, logs.String(), secret) + } +} + func TestWhatsAppSendCutByAStop(t *testing.T) { whatsapp, fake := whatsappStartTestLinked(t) daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) @@ -116,35 +147,35 @@ func TestWhatsAppSendRefuses(t *testing.T) { err string }{ { - name: "no id", + name: "refuses no id", id: " ", chat: whatsappTestAna.String(), text: "hi", err: "the id must have 1 to 100 bytes", }, { - name: "no text", + name: "refuses no text", id: "a", chat: whatsappTestAna.String(), text: "", err: "the text must have 1 to 65536 bytes", }, { - name: "a name for a chat", + name: "refuses a name for a chat", id: "a", chat: "Ana Souza", text: "hi", err: `"Ana Souza" is not the ID of a WhatsApp chat: find the ID with whatsapp_find`, }, { - name: "a status broadcast", + name: "refuses a status broadcast", id: "a", chat: types.StatusBroadcastJID.String(), text: "hi", err: `"status@broadcast" is not a private chat or a group`, }, { - name: "the chat of the user", + name: "refuses the chat of the user", id: "a", chat: whatsappTestOwn.String(), text: "hi", @@ -159,7 +190,7 @@ func TestWhatsAppSendRefuses(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { _, err := whatsapp.Send(t.Context(), tc.id, tc.chat, tc.text, "") - require.EqualError(t, err, tc.err) + require.EqualError(t, err, "check WhatsApp send: "+tc.err) }) } @@ -254,7 +285,7 @@ func TestWhatsAppImageSendUploadsOnceAndKeepsCaption(t *testing.T) { require.Equal(t, name, record.Image) require.Equal(t, whatsappSent, record.State) - again, err := whatsappSend(t.Context(), whatsapp.link(), whatsapp.groups, state, whatsapp.inbox.images, whatsapp.clock, "photo", whatsappTestAna.String(), "The plan.", name) + again, err := whatsappSend(t.Context(), whatsapp.link(), whatsapp.groups, state, whatsapp.inbox.images, discardLog(), whatsapp.clock, "photo", whatsappTestAna.String(), "The plan.", name) require.NoError(t, err) require.Contains(t, again, "nothing was sent again") require.Len(t, fake.Uploads(), 1) @@ -304,6 +335,7 @@ func TestWhatsAppImageFailureKeepsNoSecretsAndDoesNotRetry(t *testing.T) { }, {name: "send failure stores a safe cause"}, } + for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { whatsapp, fake := whatsappStartTestLinked(t) @@ -401,6 +433,7 @@ func TestTelegramReceivedPhotoCanBeShownAndSentToWhatsApp(t *testing.T) { bot: bot, }, } + require.NotContains(t, box.run(t.Context(), "image_show", `{"image":"`+names[0]+`","caption":"The plan."}`), "error:") require.Contains(t, box.run(t.Context(), "whatsapp_send", `{"id":"forward","chat":"15550002222@s.whatsapp.net","text":"The plan.","image":"`+names[0]+`"}`), "Sent to") require.Equal(t, [][]byte{picture}, fake.Uploads()) @@ -409,7 +442,7 @@ func TestTelegramReceivedPhotoCanBeShownAndSentToWhatsApp(t *testing.T) { calls := telegram.Calls() require.Equal(t, "sendPhoto", calls[len(calls)-1].method) require.Equal(t, picture, calls[len(calls)-1].params["photo"]) - require.Empty(t, logs.String()) + require.Equal(t, "<6>image sent\n", logs.String()) } func TestWhatsAppImageUploadEndsWithItsCallerOrTimeout(t *testing.T) {