Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix retrieve media files for D3C #613

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions handlers/dialog360/dialog360.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"strings"
"time"

"github.com/buger/jsonparser"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/courier/utils"
Expand Down Expand Up @@ -375,16 +376,35 @@
return "", nil
}

token := channel.StringConfigForKey(courier.ConfigAuthToken, "")
if token == "" {
return "", fmt.Errorf("missing token for D3C channel")
}

Check warning on line 382 in handlers/dialog360/dialog360.go

View check run for this annotation

Codecov / codecov/patch

handlers/dialog360/dialog360.go#L381-L382

Added lines #L381 - L382 were not covered by tests

urlStr := channel.StringConfigForKey(courier.ConfigBaseURL, "")
url, err := url.Parse(urlStr)
if err != nil {
return "", fmt.Errorf("invalid base url set for D3C channel: %s", err)
}

mediaPath, _ := url.Parse("/whatsapp_business/attachments/")
mediaEndpoint := url.ResolveReference(mediaPath).String()
mediaPath, _ := url.Parse(mediaID)
mediaURL := url.ResolveReference(mediaPath).String()

req, _ := http.NewRequest(http.MethodGet, mediaURL, nil)
req.Header.Set("User-Agent", utils.HTTPUserAgent)
req.Header.Set(d3AuthorizationKey, token)

resp, respBody, err := handlers.RequestHTTP(req, clog)
if err != nil || resp.StatusCode/100 != 2 {
return "", fmt.Errorf("failed to request media URL for D3C channel: %s", err)
}

Check warning on line 400 in handlers/dialog360/dialog360.go

View check run for this annotation

Codecov / codecov/patch

handlers/dialog360/dialog360.go#L399-L400

Added lines #L399 - L400 were not covered by tests
Comment on lines +393 to +400
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


fbFileURL, err := jsonparser.GetString(respBody, "url")
if err != nil {
return "", fmt.Errorf("missing url field in response for D3C media: %s", err)
}

Check warning on line 405 in handlers/dialog360/dialog360.go

View check run for this annotation

Codecov / codecov/patch

handlers/dialog360/dialog360.go#L404-L405

Added lines #L404 - L405 were not covered by tests

fileURL := fmt.Sprintf("%s?mid=%s", mediaEndpoint, mediaID)
fileURL := strings.ReplaceAll(fbFileURL, "https://lookaside.fbsbx.com", urlStr)

return fileURL, nil
}
Expand Down
46 changes: 46 additions & 0 deletions handlers/dialog360/dialog360_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package dialog360
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -252,11 +255,54 @@ var testCasesD3C = []ChannelHandleTestCase{
},
}

func buildMockD3MediaService(testChannels []courier.Channel, testCases []ChannelHandleTestCase) *httptest.Server {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fileURL := ""

if strings.HasSuffix(r.URL.Path, "id_voice") {
fileURL = "https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=id_voice"
}
if strings.HasSuffix(r.URL.Path, "id_document") {
fileURL = "https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=id_document"
}
if strings.HasSuffix(r.URL.Path, "id_image") {
fileURL = "https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=id_image"
}
if strings.HasSuffix(r.URL.Path, "id_video") {
fileURL = "https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=id_video"
}
if strings.HasSuffix(r.URL.Path, "id_audio") {
fileURL = "https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=id_audio"
}

w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf(`{ "url": "%s" }`, fileURL)))
}))
testChannels[0].(*test.MockChannel).SetConfig("base_url", server.URL)

// update our tests media urls
for _, tc := range testCases {
for i := range tc.ExpectedAttachments {
if !strings.HasPrefix(tc.ExpectedAttachments[i], "geo:") {
tc.ExpectedAttachments[i] = strings.ReplaceAll(tc.ExpectedAttachments[i], "https://waba-v2.360dialog.io", server.URL)
}
}
}

return server
}

func TestHandler(t *testing.T) {

d3MediaService := buildMockD3MediaService(testChannels, testCasesD3C)
defer d3MediaService.Close()

RunChannelTestCases(t, testChannels, newWAHandler(courier.ChannelType("D3C"), "360Dialog"), testCasesD3C)
}

func BenchmarkHandler(b *testing.B) {
d3MediaService := buildMockD3MediaService(testChannels, testCasesD3C)
defer d3MediaService.Close()
RunChannelBenchmarks(b, testChannels, newWAHandler(courier.ChannelType("D3C"), "360Dialog"), testCasesD3C)
}

Expand Down
Loading