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

Function TestReceiveSMS_Encrypted

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

Source from the content-addressed store, hash-verified

86}
87
88func TestReceiveSMS_Encrypted(t *testing.T) {
89 ctx := context.Background()
90 phone := setupPhone(ctx, t, 60)
91
92 encryptionKey := randomEncryptionKey()
93 signingKey, webhookPath := setupWebhook(ctx, t, phone.PhoneNumber, []string{
94 "message.phone.received",
95 })
96
97 client := newAPIClient()
98 plaintext := "Incoming secret message " + randomEncryptionKey()
99 ciphertext, err := client.Cipher.Encrypt(encryptionKey, plaintext)
100 require.NoError(t, err)
101
102 contactNumber := randomPhoneNumber()
103 receivePayload := map[string]interface{}{
104 "from": contactNumber,
105 "to": phone.PhoneNumber,
106 "content": ciphertext,
107 "encrypted": true,
108 "sim": "SIM1",
109 "timestamp": time.Now().UTC().Format(time.RFC3339),
110 }
111 body, err := json.Marshal(receivePayload)
112 require.NoError(t, err)
113
114 url := apiBaseURL + "/v1/messages/receive"
115 req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
116 require.NoError(t, err)
117 req.Header.Set("Content-Type", "application/json")
118 req.Header.Set("x-api-key", phone.PhoneAPIKey)
119
120 httpResp, err := http.DefaultClient.Do(req)
121 require.NoError(t, err)
122 defer httpResp.Body.Close()
123
124 respBody, err := io.ReadAll(httpResp.Body)
125 require.NoError(t, err)
126 require.Equal(t, http.StatusOK, httpResp.StatusCode, "receive response: %s", string(respBody))
127
128 var receiveResult httpsms.MessageResponse
129 require.NoError(t, json.Unmarshal(respBody, &receiveResult))
130 messageID := receiveResult.Data.ID.String()
131 require.NotEmpty(t, messageID)
132 t.Logf("received encrypted message: %s", messageID)
133
134 msg := pollMessageStatus(ctx, t, messageID, "received", 15*time.Second)
135 assert.Equal(t, "received", msg.Status)
136 assert.True(t, msg.Encrypted)
137 assert.Equal(t, ciphertext, msg.Content)
138 assert.NotEqual(t, plaintext, msg.Content)
139
140 decrypted, err := client.Cipher.Decrypt(encryptionKey, msg.Content)
141 require.NoError(t, err)
142 assert.Equal(t, plaintext, decrypted)
143
144 webhookReqs := waitForWebhookEvents(t, webhookPath, 1, 30*time.Second)
145 require.GreaterOrEqual(t, len(webhookReqs), 1)

Callers

nothing calls this directly

Calls 10

setupPhoneFunction · 0.85
randomEncryptionKeyFunction · 0.85
setupWebhookFunction · 0.85
newAPIClientFunction · 0.85
randomPhoneNumberFunction · 0.85
pollMessageStatusFunction · 0.85
waitForWebhookEventsFunction · 0.85
assertWebhookJWTFunction · 0.85
SetMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected