]> git.giacomolli.uk - onechat/blobdiff - telegram_fake_helper_test.go
Accept owner photos and image files in Telegram
[onechat] / telegram_fake_helper_test.go
index 3056aa133372b3f79fb51632238609359033bc65..6a89054a8b0e10702b8b2d0ba05de3768fda7ffa 100644 (file)
@@ -45,6 +45,7 @@ type TelegramFake struct {
        stall   func(call TelegramFakeCall) bool
        added   chan struct{}
        stopped chan struct{}
        stall   func(call TelegramFakeCall) bool
        added   chan struct{}
        stopped chan struct{}
+       images  map[string][]byte
 }
 
 // Add makes update available to getUpdates.
 }
 
 // Add makes update available to getUpdates.
@@ -86,7 +87,36 @@ func (f *TelegramFake) Sent() []string {
        return texts
 }
 
        return texts
 }
 
+func (f *TelegramFake) Image(id string, data []byte) {
+       f.mutex.Lock()
+       defer f.mutex.Unlock()
+
+       if f.images == nil {
+               f.images = make(map[string][]byte)
+       }
+
+       f.images[id] = data
+}
+
 func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) {
 func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) {
+       id, file := strings.CutPrefix(r.URL.Path, "/file/bot"+telegramTestToken+"/")
+       if file {
+               f.mutex.Lock()
+
+               data, exists := f.images[id]
+               f.mutex.Unlock()
+
+               if !exists {
+                       http.NotFound(w, r)
+
+                       return
+               }
+
+               _, _ = w.Write(data)
+
+               return
+       }
+
        method, found := strings.CutPrefix(r.URL.Path, "/bot"+telegramTestToken+"/")
        if !found {
                http.Error(w, `{"ok":false,"error_code":404,"description":"Not Found"}`, http.StatusNotFound)
        method, found := strings.CutPrefix(r.URL.Path, "/bot"+telegramTestToken+"/")
        if !found {
                http.Error(w, `{"ok":false,"error_code":404,"description":"Not Found"}`, http.StatusNotFound)
@@ -146,6 +176,15 @@ func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) {
        call.ok = true
        f.record(call)
 
        call.ok = true
        f.record(call)
 
+       if method == "getFile" {
+               _ = json.NewEncoder(w).Encode(map[string]any{
+                       "ok":     true,
+                       "result": map[string]any{"file_path": params["file_id"]},
+               })
+
+               return
+       }
+
        if method != "getUpdates" {
                _, _ = io.WriteString(w, `{"ok":true,"result":true}`)
                return
        if method != "getUpdates" {
                _, _ = io.WriteString(w, `{"ok":true,"result":true}`)
                return