MCPcopy Index your code
hub / github.com/NdoleStudio/httpsms / TestSendSMS_Encrypted

Function TestSendSMS_Encrypted

tests/integration_test.go:20–86  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

18)
19
20func TestSendSMS_Encrypted(t *testing.T) {
21 ctx := context.Background()
22 phone := setupPhone(ctx, t, 60)
23
24 encryptionKey := randomEncryptionKey()
25 signingKey, webhookPath := setupWebhook(ctx, t, phone.PhoneNumber, []string{
26 "message.phone.sent",
27 "message.phone.delivered",
28 })
29
30 client := newAPIClient()
31 plaintext := "Hello encrypted world " + randomEncryptionKey()
32 ciphertext, err := client.Cipher.Encrypt(encryptionKey, plaintext)
33 require.NoError(t, err)
34 require.NotEqual(t, plaintext, ciphertext)
35
36 contactNumber := randomPhoneNumber()
37 sendResp, resp, err := client.Messages.Send(ctx, &httpsms.MessageSendParams{
38 From: phone.PhoneNumber,
39 To: contactNumber,
40 Content: ciphertext,
41 Encrypted: true,
42 })
43 require.NoError(t, err)
44 require.Equal(t, http.StatusOK, resp.HTTPResponse.StatusCode)
45
46 messageID := sendResp.Data.ID.String()
47 require.NotEmpty(t, messageID)
48 t.Logf("sent encrypted message: %s", messageID)
49
50 fcmRequests := waitForFCMPush(t, messageID, 30*time.Second)
51 require.Len(t, fcmRequests, 1)
52
53 outstanding := fetchOutstandingMessage(ctx, t, phone.PhoneAPIKey, messageID)
54 assert.Equal(t, true, outstanding["encrypted"])
55 assert.Equal(t, ciphertext, outstanding["content"])
56 assert.NotEqual(t, plaintext, outstanding["content"])
57
58 fireEvent(ctx, t, phone.PhoneAPIKey, messageID, "SENT")
59 time.Sleep(200 * time.Millisecond)
60 fireEvent(ctx, t, phone.PhoneAPIKey, messageID, "DELIVERED")
61
62 msg := pollMessageStatus(ctx, t, messageID, "delivered", 30*time.Second)
63 assert.Equal(t, "delivered", msg.Status)
64 assert.True(t, msg.Encrypted)
65 assert.Equal(t, ciphertext, msg.Content)
66
67 decrypted, err := client.Cipher.Decrypt(encryptionKey, msg.Content)
68 require.NoError(t, err)
69 assert.Equal(t, plaintext, decrypted)
70
71 webhookReqs := waitForWebhookEvents(t, webhookPath, 2, 30*time.Second)
72 for _, req := range webhookReqs {
73 assertWebhookJWT(t, req.Request, signingKey)
74 }
75
76 var eventTypes []string
77 for _, req := range webhookReqs {

Callers

nothing calls this directly

Calls 13

setupPhoneFunction · 0.85
randomEncryptionKeyFunction · 0.85
setupWebhookFunction · 0.85
newAPIClientFunction · 0.85
randomPhoneNumberFunction · 0.85
waitForFCMPushFunction · 0.85
fetchOutstandingMessageFunction · 0.85
fireEventFunction · 0.85
pollMessageStatusFunction · 0.85
waitForWebhookEventsFunction · 0.85
assertWebhookJWTFunction · 0.85
SendMethod · 0.65

Tested by

no test coverage detected